Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IFS proto: Quick search above with separate includes filter #12725

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 109 additions & 47 deletions polaris-react/playground/OrdersPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-nocheck

Check failure on line 1 in polaris-react/playground/OrdersPage.tsx

View workflow job for this annotation

GitHub Actions / Lint & type check

Do not use "@ts-nocheck" because it alters compilation errors
import React, {useRef, useState, useCallback} from 'react';
import {
ChartVerticalIcon,
Expand Down Expand Up @@ -45,6 +45,7 @@
IndexFiltersMode,
Badge,
Text,
TextField,
} from '../src';

import {orders} from './orders';
Expand Down Expand Up @@ -408,11 +409,20 @@
const [selectedView, setSelectedView] = useState(0);
const [sortSelected, setSortSelected] = useState(['order desc']);
const [queryValue, setQueryValue] = useState('');
const [contains, setContains] = useState<string>('');
const [status, setStatus] = useState<string[]>([]);
const [paymentStatus, setPaymentStatus] = useState<string[]>([]);
const [fulfillmentStatus, setFulfillmentStatus] = useState<string[]>([]);
const [loading, setLoading] = useState<boolean>(false);
const [filteredOrders, setFilteredOrders] = useState(orders);
const [tempPersistedSearch, setTempPersistedSearch] = useState<string[]>([
'',
'',
'',
'',
'',
]);

const [savedViewFilters, setSavedViewFilters] = useState<SavedViewFilter[][]>(
[
[],
Expand Down Expand Up @@ -465,7 +475,7 @@
'order desc',
]);

const {mode, setMode} = useSetIndexFiltersMode(IndexFiltersMode.Default);
const {mode, setMode} = useSetIndexFiltersMode();

const preProcessText = (input: string) => {
// Insert a space between numbers and letters if they are adjacent
Expand Down Expand Up @@ -512,6 +522,7 @@
};

const handleFilterOrders = (nextFilters: {
contains?: string;
queryValue?: string;
paymentStatus?: string[];
fulfillmentStatus?: string[];
Expand All @@ -523,6 +534,8 @@
? nextFilters.queryValue
: queryValue;

const nextContains =
nextFilters.contains !== undefined ? nextFilters.contains : contains;
const nextStatus =
nextFilters.status !== undefined ? nextFilters.status : status;
const nextPaymentStatus =
Expand All @@ -534,11 +547,16 @@
? nextFilters.fulfillmentStatus
: fulfillmentStatus;

const containsSet = new Set(nextContains);
const statusSet = new Set(nextStatus);
const paymentStatusSet = new Set(nextPaymentStatus);
const fulfillmentStatusSet = new Set(nextFulfillmentStatus);
const result = orders.filter((order) => {
const matchesQueryValue = hasTextValueMatches(nextQueryValue, order);
const matchesContains = hasTextValueMatches(
preProcessText(nextContains),
order,
);

const matchesStatus = hasArrayValueMatches(
new Set([order.status]),
Expand All @@ -555,27 +573,10 @@
fulfillmentStatusSet,
);

// if (
// matchesQueryValue &&
// matchesPaymentStatus &&
// matchesFulfillmentStatus &&
// matchesStatus
// ) {
// console.log(
// `
// nextFilters: `,
// nextFilters,
// `
// matchesQueryValue: ${matchesQueryValue},
// matchesPaymentStatus: ${matchesPaymentStatus},
// matchesFulfillmentStatus: ${matchesFulfillmentStatus},
// matchesStatus: ${matchesStatus}
// `,
// );
// }
setLoading(false);
return (
matchesQueryValue &&
matchesContains &&
matchesPaymentStatus &&
matchesFulfillmentStatus &&
matchesStatus
Expand All @@ -595,7 +596,22 @@

const handleQueryValueRemove = () => {
setQueryValue('');
const nextSavedViewFilters = [...savedViewFilters];
nextSavedViewFilters[selectedView] = nextSavedViewFilters[
selectedView
].filter(({key}) => key !== 'queryValue');
handleFilterOrders({queryValue: ''});
setSavedViewFilters(nextSavedViewFilters);
};

const handleContainsChange = (value: string) => {
setContains(value);
handleFilterOrders({contains: value});
};

const handleContainsRemove = (value: string[]) => {
setContains('');
handleFilterOrders({contains: ''});
};

const handlePaymentStatusChange = (value: string[]) => {
Expand Down Expand Up @@ -634,6 +650,13 @@
change: handleQueryValueChange,
remove: handleQueryValueRemove,
emptyValue: '',
label: 'Search',
},
contains: {
set: setContains,
change: handleContainsChange,
remove: handleContainsRemove,
emptyValue: '',
label: 'Include',
},
status: {
Expand Down Expand Up @@ -686,6 +709,7 @@
};

const handleChangeFilters = (nextFilterValues: {
contains?: string;
queryValue?: string;
paymentStatus?: string[];
fulfillmentStatus?: string[];
Expand All @@ -702,11 +726,13 @@

const handleResetToSavedFilters = (view: number) => {
const nextFilters: {
contains: string;
queryValue: string;
paymentStatus: string[];
fulfillmentStatus: string[];
status: string[];
} = {
contains: '',
queryValue: '',
paymentStatus: [],
fulfillmentStatus: [],
Expand All @@ -731,6 +757,7 @@

const handleClearFilters = () => {
handleChangeFilters({
contains: '',
queryValue: '',
paymentStatus: [],
fulfillmentStatus: [],
Expand Down Expand Up @@ -765,10 +792,19 @@

const filters: FilterInterface[] = [
{
key: 'queryValue',
label: handlers.queryValue.label,
hidden: true,
filter: null,
key: 'contains',
label: handlers.contains.label,
filter: (
<TextField
autoFocus
labelHidden
type="text"
autoComplete="off"
label={handlers.contains.label}
value={contains}
onChange={handleContainsChange}
/>
),
},
{
key: 'paymentStatus',
Expand Down Expand Up @@ -833,12 +869,14 @@
const appliedFilters: AppliedFilterInterface[] = [];

Object.entries({
queryValue,
contains,
status,
paymentStatus,
fulfillmentStatus,
}).forEach(([key, value]) => {
if (isEmpty(value)) return;
if (isEmpty(value)) {
return;
}

const savedValue = savedViewFilters[selectedView]?.find(
(filter) => filter.key === key,
Expand All @@ -849,40 +887,55 @@
value,
label: getHumanReadableValue(handlers[key].label, value),
unsavedChanges: selectedView === 0 ? true : isUnsaved(value, savedValue),
onRemove: key === 'queryValue' ? undefined : handlers[key].remove,
onRemove: handlers[key].remove,
});
});

const savedViewFiltersWithoutQuery = savedViewFilters[selectedView]?.filter(
({key}) => key !== 'queryValue',
);

const appliedFilterMatchesSavedFilter = (
appliedFilter: AppliedFilterInterface,
) => {
const savedFilter = savedViewFilters[selectedView].find(
const savedFilter = savedViewFiltersWithoutQuery?.find(
(savedFilter) => savedFilter.key === appliedFilter.key,
);

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

return hasSameArrayValue;
} else {
return true;
}
};

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

const isAllViewAndFiltersAreApplied =
selectedView === 0 && appliedFilters.length > 0;

const appliedFilterCountDoesNotEqualSavedFilterCount =
appliedFilters.length !== savedViewFiltersWithoutQuery?.length;

const appliedFiltersDoNotMatchSavedFilters = !appliedFilters.every(
appliedFilterMatchesSavedFilter,
);

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

const hasUnsavedChanges = hasUnsavedSortChange || hasUnsavedFilterChange;

Expand All @@ -894,11 +947,12 @@
const getFiltersToSave = () => {
return Object.entries({
queryValue,
fulfillmentStatus,
paymentStatus,
contains,
status,
paymentStatus,
fulfillmentStatus,
})
.filter(([, value]) => value !== undefined)
.filter(([, value]) => !isEmpty(value))
.map(([key, value]) => {
return {key, value, label: handlers[key].label};
});
Expand All @@ -909,12 +963,11 @@
const previousSortSelected = sortSelected[0];
let nextFilters;

if (previousView === 0 && hasUnsavedChanges) {
if ((previousView === 0 && hasUnsavedChanges) || queryValue) {
nextFilters = getFiltersToSave();
}

setQueryValue('');
setMode(IndexFiltersMode.Default);
setSelectedView(view);
setLoading(true);
handleResetToSavedFilters(view);
Expand All @@ -930,8 +983,10 @@

const handleDeleteView = (index: number) => async () => {
const nextViewNames = [...viewNames];
const nextTempPersistedSearch = [...tempPersistedSearch];
const nextSavedViewFilters = [...savedViewFilters];
nextViewNames.splice(index, 1);
nextTempPersistedSearch.splice(index, 1);
nextSavedViewFilters.splice(index, 1);
setSavedViewFilters(nextSavedViewFilters);
setViewNames(nextViewNames);
Expand All @@ -955,6 +1010,7 @@
setSavedViewFilters((filters) => [...filters, duplicateViewFilters]);
const nextAppliedFilters = {
queryValue: '',
contains: '',
status: [],
paymentStatus: [],
fulfillmentStatus: [],
Expand All @@ -963,24 +1019,29 @@
duplicateViewFilters.forEach(({key, value}) => {
nextAppliedFilters[key] = value;
});

setViewNames((names) => [...names, name]);
setSavedSortSelected((currentSavedSortSelected) => [
...currentSavedSortSelected,
'order desc',
]);
await sleep(250);
setSelectedView(duplicateViewIndex);
setLoading(false);
return true;
};

const handleSaveViewFilters = async (
index: number,
view: number,
nextFilters?: SavedViewFilter[],
nextSortSelected?: string,
) => {
const nextSavedFilters = [...savedViewFilters];
const nextSavedSortSelected = [...savedSortSelected];
nextSavedSortSelected[index] = nextSortSelected
nextSavedSortSelected[view] = nextSortSelected
? nextSortSelected
: sortSelected[0];
nextSavedFilters[index] = nextFilters
nextSavedFilters[view] = nextFilters
? nextFilters
: appliedFilters.map(({key, value, label}) => ({
key,
Expand All @@ -1006,16 +1067,17 @@
sortSelected[0] !== savedSortSelected[0]);

const nextFilters = shouldSaveAppliedFiltersAsNew
? {queryValue, status, paymentStatus, fulfillmentStatus}
? getFiltersToSave()
: {
contains: '',
queryValue: '',
status: [],
paymentStatus: [],
fulfillmentStatus: [],
};

if (shouldSaveAppliedFiltersAsNew) {
handleSaveViewFilters(newViewIndex);
handleSaveViewFilters(newViewIndex, nextFilters);
} else {
handleClearFilters();
setSavedViewFilters((filters) => [...filters, []]);
Expand All @@ -1032,10 +1094,9 @@
};

const handleSaveViewAs = async (index: number, name: string) => {
setMode(IndexFiltersMode.Default);
setViewNames((names) => [...names, name]);
setSelectedView(index);
const nextFilters = getFiltersToSave();
const nextFilters = getFiltersToSave(true);
const nextSortSelected = sortSelected[0];
await handleSaveViewFilters(0, []);
return handleSaveViewFilters(index, nextFilters, nextSortSelected);
Expand Down Expand Up @@ -1108,6 +1169,7 @@

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

const queryPlaceholder = `Search ${viewNames[selectedView]?.toLowerCase()}`;
Expand Down
Loading
Loading