From 3fdba42db38d76699f13cde39a6dbe3e7f78b1b8 Mon Sep 17 00:00:00 2001 From: Gondragos Date: Thu, 12 Dec 2024 02:40:32 +0000 Subject: [PATCH] fix: LEAP-1409: Add preserving hidden filters during filter operations Ensure hidden filters are retained and correctly processed in filter-related operations. Introduced `_originalFilters` to store and manage filters not editable by the user, preventing their loss during requests. --- web/libs/datamanager/src/stores/Tabs/store.js | 10 ++++++- web/libs/datamanager/src/stores/Tabs/tab.js | 28 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/web/libs/datamanager/src/stores/Tabs/store.js b/web/libs/datamanager/src/stores/Tabs/store.js index 5f01c571d232..70266a5d15af 100644 --- a/web/libs/datamanager/src/stores/Tabs/store.js +++ b/web/libs/datamanager/src/stores/Tabs/store.js @@ -27,7 +27,10 @@ const dataCleanup = (tab, columnIds) => { if (!data) return { ...tab }; if (data.filters) { - data.filters.items = data.filters.items.filter(({ filter }) => { + data._originalFilters = []; + data.filters.items = data.filters.items.filter((item) => { + const { filter } = item; + data._originalFilters.push(item); return columnIds.includes(filter.replace(/^filter:/, "")); }); } @@ -305,11 +308,16 @@ export const TabStore = types return view; } const viewSnapshot = getSnapshot(view); + let originalFilters = []; + if (result.filters) { + originalFilters = result.filters.items; + } const newViewSnapshot = { ...viewSnapshot, ...result, saved: true, filters: viewSnapshot.filters, + _originalFilters: originalFilters, conjunction: viewSnapshot.conjunction, }; diff --git a/web/libs/datamanager/src/stores/Tabs/tab.js b/web/libs/datamanager/src/stores/Tabs/tab.js index a059f7b7492f..02b1151cfc40 100644 --- a/web/libs/datamanager/src/stores/Tabs/tab.js +++ b/web/libs/datamanager/src/stores/Tabs/tab.js @@ -26,6 +26,9 @@ export const Tab = types target: types.optional(types.enumeration(["tasks", "annotations"]), "tasks"), filters: types.array(types.late(() => TabFilter)), + // this field keeps information about hidden filters that we can't change so we do not have them in filters + // but these filters should appears on the next request affects selected filters as we do not want to loose them + _originalFilters: types.optional(types.frozen()), conjunction: types.optional(types.enumeration(["and", "or"]), "and"), hiddenColumns: types.maybeNull(types.optional(TabHiddenColumns, {})), ordering: types.optional(types.array(types.string), []), @@ -135,7 +138,8 @@ export const Tab = types }, get serializedFilters() { - return self.validFilters.map((el) => { + const originalFilters = self._originalFilters ? [...self._originalFilters] : []; + return self.validFilters.reduce((acc, el) => { const filterItem = { ...getSnapshot(el), type: el.filter.currentType, @@ -143,8 +147,18 @@ export const Tab = types filterItem.value = normalizeFilterValue(filterItem.type, filterItem.operator, filterItem.value); - return filterItem; - }); + // restoring unavailable to edit original (hidden) filters + while (originalFilters[0] && originalFilters[0].filter !== filterItem.filter) { + acc.push(originalFilters.shift()); + } + // skipping duplicates + if (originalFilters[0] && originalFilters[0].filter === filterItem.filter) { + originalFilters.shift(); + } + + acc.push(filterItem); + return acc; + }, []); }, get selectedCount() { @@ -387,6 +401,11 @@ export const Tab = types }), deleteFilter(filter) { + const originalIndex = self._originalFilters.findIndex((f) => f.filter === filter.filter.id); + if (originalIndex > -1) { + self._originalFilters = self._originalFilters.filter((_item, index) => originalIndex !== index); + } + const index = self.filters.findIndex((f) => f === filter); self.filters.splice(index, 1); @@ -460,4 +479,7 @@ export const Tab = types delete sn.selectedItems; return sn; + }) + .postProcessSnapshot(({ _originalFilters, ...snapshot }) => { + return snapshot; });