Skip to content

Commit

Permalink
Only persist unsaved changes in session in All view
Browse files Browse the repository at this point in the history
  • Loading branch information
chloerice committed Sep 11, 2024
1 parent 3168958 commit 23a3380
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions polaris-react/playground/OrdersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,13 @@ function OrdersIndexTableWithFilters(
},
) {
const sortOptions: IndexFiltersProps['sortOptions'] = [
{label: 'Order', value: 'order asc', directionLabel: 'Ascending'},
{label: 'Order', value: 'order desc', directionLabel: 'Ascending'},
{label: 'Order', value: 'order desc', directionLabel: 'Descending'},
{label: 'Customer', value: 'customer asc', directionLabel: 'A-Z'},
{label: 'Customer', value: 'customer desc', directionLabel: 'A-Z'},
{label: 'Customer', value: 'customer desc', directionLabel: 'Z-A'},
{label: 'Date', value: 'date asc', directionLabel: 'A-Z'},
{label: 'Date', value: 'date desc', directionLabel: 'A-Z'},
{label: 'Date', value: 'date desc', directionLabel: 'Z-A'},
{label: 'Total', value: 'total asc', directionLabel: 'Ascending'},
{label: 'Total', value: 'total desc', directionLabel: 'Ascending'},
{label: 'Total', value: 'total desc', directionLabel: 'Descending'},
];

Expand All @@ -406,7 +406,7 @@ function OrdersIndexTableWithFilters(
'Archived',
]);
const [selectedView, setSelectedView] = useState(0);
const [sortSelected, setSortSelected] = useState(['order asc']);
const [sortSelected, setSortSelected] = useState(['order desc']);
const [queryValue, setQueryValue] = useState('');
const [status, setStatus] = useState<string[]>([]);
const [paymentStatus, setPaymentStatus] = useState<string[]>([]);
Expand Down Expand Up @@ -458,11 +458,11 @@ function OrdersIndexTableWithFilters(
);

const [savedSortSelected, setSavedSortSelected] = useState([
'order asc',
'order asc',
'order asc',
'order asc',
'order asc',
'order desc',
'order desc',
'order desc',
'order desc',
'order desc',
]);

const {mode, setMode} = useSetIndexFiltersMode(IndexFiltersMode.Default);
Expand Down Expand Up @@ -870,13 +870,19 @@ function OrdersIndexTableWithFilters(
}
};

const hasUnsavedChanges =
sortSelected[0] !== savedSortSelected[selectedView] ||
const hasUnsavedSortChange =
(selectedView === 0 && sortSelected[0] !== 'order desc') ||
sortSelected[0] !== savedSortSelected[selectedView];

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

const hasUnsavedChanges = hasUnsavedSortChange || hasUnsavedFilterChange;

// ---- View event handlers
const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand All @@ -897,27 +903,25 @@ function OrdersIndexTableWithFilters(

const handleSelectView = async (view: number) => {
const previousView = selectedView;
const previousSortSelected = sortSelected[0];
let nextFilters;
if (previousView === 0 && (queryValue || hasUnsavedChanges)) {

if (previousView === 0 && hasUnsavedChanges) {
nextFilters = getFiltersToSave();
} else if (queryValue) {
nextFilters = [
...savedViewFilters[previousView],
{
key: 'queryValue',
value: queryValue,
label: handlers.queryValue.label,
},
];
}

setQueryValue('');
setMode(IndexFiltersMode.Default);
setSelectedView(view);
setLoading(true);
handleResetToSavedFilters(view);
if (nextFilters) await handleSaveViewFilters(previousView, nextFilters);
else await sleep(250);
if (nextFilters) {
await handleSaveViewFilters(
previousView,
nextFilters,
previousSortSelected,
);
} else await sleep(250);
setLoading(false);
};

Expand Down Expand Up @@ -1014,7 +1018,7 @@ function OrdersIndexTableWithFilters(
setSavedViewFilters((filters) => [...filters, []]);
setSavedSortSelected((currentSavedSortSelected) => [
...currentSavedSortSelected,
'order asc',
'order desc',
]);
}

Expand Down Expand Up @@ -1059,6 +1063,7 @@ function OrdersIndexTableWithFilters(
console.log('cancelled -- resetting to saved');
}
};

const tabs: TabProps[] = viewNames.map((name, index) => {
return {
index,
Expand Down Expand Up @@ -1095,7 +1100,6 @@ function OrdersIndexTableWithFilters(

const cancelAction: IndexFiltersProps['cancelAction'] = {
onAction: handleCancel,
disabled: !hasUnsavedChanges,
};

const queryPlaceholder = `Search ${viewNames[selectedView]?.toLowerCase()}`;
Expand Down Expand Up @@ -1124,7 +1128,7 @@ function OrdersIndexTableWithFilters(
onClearAll={handleClearFilters}
mode={mode}
setMode={setMode}
sortUnsaved={sortSelected[0] !== savedSortSelected[selectedView]}
sortUnsaved={hasUnsavedSortChange}
/>
<Table orders={filteredOrders} />
</Card>
Expand Down

0 comments on commit 23a3380

Please sign in to comment.