Skip to content

Commit

Permalink
unsaved changes not being recognized on filter removal
Browse files Browse the repository at this point in the history
  • Loading branch information
chloerice committed Aug 27, 2024
1 parent 13206bb commit 4f66531
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
32 changes: 27 additions & 5 deletions polaris-react/playground/OrdersPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import React, {useEffect, useRef, useState} from 'react';
import React, {useEffect, useRef, useState, useMemo} from 'react';
import {
ChartVerticalIcon,
AppsIcon,
Expand Down Expand Up @@ -683,7 +683,7 @@ function OrdersIndexTableWithFilters(
remove: handleStatusRemove,
label: 'Status',
emptyValue: [],
locked: selectedView === 3 || selectedView === 4,
locked: selectedView > 0 && selectedView < 5,
},
paymentStatus: {
set: setPaymentStatus,
Expand Down Expand Up @@ -863,9 +863,31 @@ function OrdersIndexTableWithFilters(
});
});

const hasUnsavedChanges = appliedFilters.some(
(filter) => filter.unsavedChanges,
);
const appliedFilterMatchesSavedFilter = (
appliedFilter: AppliedFilterInterface,
) => {
const savedFilter = savedViewFilters[selectedView].find(
(savedFilter) => savedFilter.key === appliedFilter.key,
);

if (!savedFilter) {
return false;
} else if (typeof appliedFilter.value === 'string') {
return appliedFilter.value === savedFilter.value;
} else {
const hasSameArrayValue =
new Set(savedFilter.value).difference(new Set(appliedFilter.value))
.size === 0;

return hasSameArrayValue;
}
};

const hasUnsavedChanges =
(!savedViewFilters[selectedView] && appliedFilters.length > 0) ||
(appliedFilters.length === 0 &&
savedViewFilters[selectedView].length > 0) ||
!appliedFilters.every(appliedFilterMatchesSavedFilter);

// ---- View event handlers
const sleep = (ms: number) => {
Expand Down
12 changes: 10 additions & 2 deletions polaris-react/src/components/IndexFilters/IndexFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export function IndexFilters({
onQueryChange(searchFilterValue ? searchFilterValue + value : value);
setSearchOnlyValue(value);
} else {
onQueryChange(searchOnlyValue ? value + searchOnlyValue : value);
onQueryChange(searchOnlyValue ? `${value},${searchOnlyValue}` : value);
setSearchFilterValue(value);
}
},
Expand All @@ -377,7 +377,13 @@ export function IndexFilters({
setSearchFilterValue(augmentedQuery);
setSearchOnlyValue('');
}
}, [mode, searchFilterValue, searchOnlyValue, handleShowFilters]);
}, [
mode,
searchFilterValue,
searchOnlyValue,
onQueryChange,
handleShowFilters,
]);

const handleQueryClear = useCallback(
(input: 'searchOnly' | 'searchFilter') => () => {
Expand Down Expand Up @@ -455,6 +461,7 @@ export function IndexFilters({
let searchFilter;

const supportsSavedFilters =
!hideQueryField &&
!hideFilters &&
primaryAction &&
(primaryAction.type === 'save' || primaryAction.type === 'save-as');
Expand All @@ -481,6 +488,7 @@ export function IndexFilters({
}, [
queryValue,
hideFilters,
hideQueryField,
primaryAction,
appliedFilters,
searchFilterValue,
Expand Down

0 comments on commit 4f66531

Please sign in to comment.