From f6256b345d636856d6182f3f429cd241405aad46 Mon Sep 17 00:00:00 2001 From: Mitul David Date: Mon, 7 Mar 2022 14:58:56 +0530 Subject: [PATCH 1/2] fix: Duplicate attachments in sidebar --- .../js/frappe/form/sidebar/attachments.js | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/frappe/public/js/frappe/form/sidebar/attachments.js b/frappe/public/js/frappe/form/sidebar/attachments.js index 538534e5cf..9b74f94849 100644 --- a/frappe/public/js/frappe/form/sidebar/attachments.js +++ b/frappe/public/js/frappe/form/sidebar/attachments.js @@ -44,8 +44,14 @@ frappe.ui.form.Attachments = class Attachments { // add attachment objects var attachments = this.get_attachments(); if(attachments.length) { - attachments.forEach(function(attachment) { - me.add_attachment(attachment) + let exists = {}; + let unique_attachments = attachments.filter(attachment => { + return exists.hasOwnProperty(attachment.file_name) + ? false + : (exists[attachment.file_name] = true); + }); + unique_attachments.forEach(attachment => { + me.add_attachment(attachment); }); } else { this.attachments_label.removeClass("has-attachments"); @@ -75,7 +81,19 @@ frappe.ui.form.Attachments = class Attachments { remove_action = function(target_id) { frappe.confirm(__("Are you sure you want to delete the attachment?"), function() { - me.remove_attachment(target_id); + let target_attachment = me + .get_attachments() + .find(attachment => attachment.name === target_id); + let to_be_removed = me + .get_attachments() + .filter( + attachment => + attachment.file_name === + target_attachment.file_name + ); + to_be_removed.forEach(attachment => + me.remove_attachment(attachment.name) + ); } ); return false; From 084e2639caa980a91706111b733cba761b257271 Mon Sep 17 00:00:00 2001 From: Mitul David Date: Mon, 7 Mar 2022 16:05:52 +0530 Subject: [PATCH 2/2] fix: Call hasOwnProperty method from Object.prototype --- frappe/public/js/frappe/form/sidebar/attachments.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/frappe/public/js/frappe/form/sidebar/attachments.js b/frappe/public/js/frappe/form/sidebar/attachments.js index 9b74f94849..0713d5dc43 100644 --- a/frappe/public/js/frappe/form/sidebar/attachments.js +++ b/frappe/public/js/frappe/form/sidebar/attachments.js @@ -46,7 +46,10 @@ frappe.ui.form.Attachments = class Attachments { if(attachments.length) { let exists = {}; let unique_attachments = attachments.filter(attachment => { - return exists.hasOwnProperty(attachment.file_name) + return Object.prototype.hasOwnProperty.call( + exists, + attachment.file_name + ) ? false : (exists[attachment.file_name] = true); });