Skip to content

Commit 2dd3d2d

Browse files
LPD-9465 - Feat. Handle Default view and prevent object mutation (mainly filters)
1 parent 20a8e0f commit 2dd3d2d

File tree

2 files changed

+53
-50
lines changed

2 files changed

+53
-50
lines changed

modules/apps/frontend-data-set/frontend-data-set-web/src/main/resources/META-INF/resources/FrontendDataSet.tsx

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,13 @@ const FrontendDataSetContent = ({
500500
};
501501

502502
const getInitialViewsState = () => {
503+
const defaultSnapshot: any = {
504+
modifiedFields: {},
505+
paginationDelta:
506+
showPagination &&
507+
(pagination?.initialDelta || DEFAULT_PAGINATION_DELTA),
508+
};
509+
503510
const customInternalViews =
504511
customRenderers?.views?.map((customRenderer: TRenderer) => ({
505512

@@ -522,6 +529,11 @@ const FrontendDataSetContent = ({
522529
views[0] ||
523530
(customInternalViews?.length && customInternalViews[0]);
524531

532+
defaultSnapshot.activeView = {
533+
component: getViewComponent(initialActiveView as IView),
534+
...initialActiveView,
535+
};
536+
525537
let initialVisibleFieldNames = {};
526538

527539
if (!Liferay.FeatureFlags['LPD-10683'] && activeViewSettings) {
@@ -545,6 +557,8 @@ const FrontendDataSetContent = ({
545557

546558
const visibleFieldNames = getVisibleFields();
547559

560+
defaultSnapshot.visibleFieldNames = initialVisibleFieldNames;
561+
548562
let saveVisibleFieldNames = false;
549563

550564
if (visibleFieldNames) {
@@ -576,29 +590,30 @@ const FrontendDataSetContent = ({
576590
...initialActiveView,
577591
};
578592

593+
defaultSnapshot.filters = initialFilters
594+
? initialFilters.map((filter) => {
595+
const preloadedData = filter.preloadedData;
596+
597+
if (preloadedData) {
598+
return activateFilter({
599+
filter,
600+
selectedData: preloadedData,
601+
});
602+
}
603+
604+
return {...filter};
605+
})
606+
: [];
607+
579608
const filters = initialFilters
580609
? updateFilterActivation({
581610
newFilters: getFilters(),
582-
oldFilters: initialFilters.map((filter) => {
583-
const preloadedData = filter.preloadedData;
584-
585-
if (preloadedData) {
586-
filter = activateFilter({
587-
filter,
588-
selectedData: preloadedData,
589-
});
590-
}
591-
592-
return filter;
593-
}),
611+
oldFilters: defaultSnapshot.filters,
594612
})
595613
: [];
596614

597615
const paginationDelta =
598-
showPagination &&
599-
(getDelta() ||
600-
pagination?.initialDelta ||
601-
DEFAULT_PAGINATION_DELTA);
616+
showPagination && (getDelta() || defaultSnapshot.paginationDelta);
602617

603618
const pageNumber =
604619
getPageNumber() ||
@@ -607,6 +622,8 @@ const FrontendDataSetContent = ({
607622

608623
const searchParam = getSearchParam();
609624

625+
defaultSnapshot.sorts = sortsProp;
626+
610627
const sorts = updateSortsActivation({
611628
newSorts: getActiveSorts(),
612629
oldSorts: sortsProp,
@@ -646,14 +663,7 @@ const FrontendDataSetContent = ({
646663

647664
return {
648665
activeView,
649-
defaultSnapshot: {
650-
activeView,
651-
filters: deepClone(filters),
652-
modifiedFields: {},
653-
paginationDelta,
654-
sorts,
655-
visibleFieldNames: initialVisibleFieldNames,
656-
},
666+
defaultSnapshot,
657667
filters,
658668
modifiedFields: {},
659669
pageNumber,
@@ -668,10 +678,7 @@ const FrontendDataSetContent = ({
668678
};
669679

670680
const [viewsState, viewsDispatch] = useThunk(
671-
useReducer(
672-
viewsReducer,
673-
getInitialViewsState(),
674-
)
681+
useReducer(viewsReducer, getInitialViewsState())
675682
);
676683

677684
const {
@@ -1591,22 +1598,10 @@ const FrontendDataSetContent = ({
15911598
}
15921599

15931600
const handleSnapshotChange = ({defaultSnapshot, snapshots, value}: any) => {
1594-
const snapshot = deepClone(
1595-
snapshots.find((view: ISnapshot) => view.erc === value)
1596-
);
1597-
1598-
if (!snapshot || value === 'DEFAULT_VIEW') {
1601+
if (value === 'DEFAULT_VIEW') {
15991602
updateConfig({
1600-
[EConfigInURLKeys.ACTIVE_FILTERS]: updateFilterActivation({
1601-
newFilters: defaultSnapshot.filters.filter(
1602-
(filter: any) => filter.active
1603-
),
1604-
oldFilters: deepClone(filters),
1605-
}),
1606-
[EConfigInURLKeys.ACTIVE_SORTS]: updateSortsActivation({
1607-
newSorts: defaultSnapshot.sorts,
1608-
oldSorts: sorts,
1609-
}),
1603+
[EConfigInURLKeys.ACTIVE_FILTERS]: defaultSnapshot.filters,
1604+
[EConfigInURLKeys.ACTIVE_SORTS]: defaultSnapshot.sorts,
16101605
[EConfigInURLKeys.DELTA]: {...defaultSnapshot.paginationDelta},
16111606
[EConfigInURLKeys.VIEW_NAME]: {
16121607
...defaultSnapshot.activeView.name,
@@ -1621,6 +1616,10 @@ const FrontendDataSetContent = ({
16211616
});
16221617
}
16231618
else {
1619+
const snapshot = deepClone(
1620+
snapshots.find((view: ISnapshot) => view.erc === value)
1621+
);
1622+
16241623
updateConfig({
16251624
[EConfigInURLKeys.ACTIVE_FILTERS]: updateFilterActivation({
16261625
newFilters: snapshot.configuration.filters.filter(

modules/apps/frontend-data-set/frontend-data-set-web/src/main/resources/META-INF/resources/utils/filters/activateFilter.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import {FILTER_IMPLEMENTATIONS} from '../../management_bar/controls/filters/Filter';
7+
import deepClone from '../deepClone';
78

89
export function activateFilter({
910
filter,
@@ -12,19 +13,22 @@ export function activateFilter({
1213
filter: any;
1314
selectedData?: any;
1415
}) {
15-
filter.active = true;
16+
const updatedFilter = deepClone(filter);
17+
18+
updatedFilter.active = true;
1619

1720
if (selectedData) {
18-
filter.selectedData = selectedData;
21+
updatedFilter.selectedData = selectedData;
1922
}
2023

21-
const filterType: keyof typeof FILTER_IMPLEMENTATIONS = filter.type;
24+
const filterType: keyof typeof FILTER_IMPLEMENTATIONS = updatedFilter.type;
2225

2326
const filterImplementation = FILTER_IMPLEMENTATIONS[filterType];
2427

25-
filter.odataFilterString = filterImplementation.getOdataString(filter);
26-
filter.selectedItemsLabel =
27-
filterImplementation.getSelectedItemsLabel(filter);
28+
updatedFilter.odataFilterString =
29+
filterImplementation.getOdataString(updatedFilter);
30+
updatedFilter.selectedItemsLabel =
31+
filterImplementation.getSelectedItemsLabel(updatedFilter);
2832

29-
return filter;
33+
return updatedFilter;
3034
}

0 commit comments

Comments
 (0)