From 1c5593e50a43e6cc9be0d1151084fd9294bf8994 Mon Sep 17 00:00:00 2001 From: Lukasz Ostafin Date: Fri, 13 Dec 2024 14:39:51 +0100 Subject: [PATCH] IBX-9322: ezobjectrelationlist field allows selecting the same content multiple times --- .../js/scripts/fieldType/ezobjectrelationlist.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/bundle/Resources/public/js/scripts/fieldType/ezobjectrelationlist.js b/src/bundle/Resources/public/js/scripts/fieldType/ezobjectrelationlist.js index 82f94ec72d..3a844d6152 100644 --- a/src/bundle/Resources/public/js/scripts/fieldType/ezobjectrelationlist.js +++ b/src/bundle/Resources/public/js/scripts/fieldType/ezobjectrelationlist.js @@ -113,12 +113,12 @@ sourceInput.dispatchEvent(new CustomEvent(EVENT_CUSTOM)); }; const onConfirm = (items) => { - items = excludeDuplicatedItems(items); + const itemsWithoutDuplicate = excludeDuplicatedItems(items); - renderRows(items); + renderRows(itemsWithoutDuplicate); attachRowsEventHandlers(); - selectedItems = [...selectedItems, ...items.map((item) => item.ContentInfo.Content._id)]; + selectedItems = [...selectedItems, ...itemsWithoutDuplicate.map((item) => item.ContentInfo.Content._id)]; updateInputValue(selectedItems); closeUDW(); @@ -126,6 +126,7 @@ updateAddBtnState(); relationsTable.dispatchEvent(new CustomEvent('ibexa-refresh-main-table-checkbox')); }; + const openUDW = (event) => { event.preventDefault(); @@ -157,11 +158,7 @@ }), ); }; - const excludeDuplicatedItems = (items) => { - selectedItemsMap = items.reduce((total, item) => ({ ...total, [item.ContentInfo.Content._id]: item }), selectedItemsMap); - - return items.filter((item) => selectedItemsMap[item.ContentInfo.Content._id]); - }; + const excludeDuplicatedItems = (items) => items.filter((item) => !selectedItems.includes(item.ContentInfo.Content._id)); const renderRow = (item, index) => { const { escapeHTML } = ibexa.helpers.text; const { formatShortDateTime } = ibexa.helpers.timezone; @@ -304,7 +301,6 @@ updateInputValue(selectedItems); }; let selectedItems = [...fieldContainer.querySelectorAll(SELECTOR_ROW)].map((row) => parseInt(row.dataset.contentId, 10)); - let selectedItemsMap = selectedItems.reduce((total, item) => ({ ...total, [item]: item }), {}); updateAddBtnState(); attachRowsEventHandlers();