Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Reset query after tab change but not after save

dominik-zeglen1db34b383e73
diff --git a/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx b/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
index e0f09e52f1..2886f29716 100644
--- a/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
+++ b/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
@@ -107,8 +107,10 @@ interface FilterChipProps {
filterLabel: string;
placeholder: string;
search: string;
+ isCustomSearch: boolean;
onSearchChange: (event: React.ChangeEvent<any>) => void;
onFilterAdd: (filter: FilterContentSubmitData) => void;
+ onFilterDelete: () => void;
onFilterSave: () => void;
}
@@ -121,7 +123,9 @@ export const FilterChips: React.FC<FilterChipProps> = ({
onSearchChange,
search,
onFilterAdd,
- onFilterSave
+ onFilterSave,
+ onFilterDelete,
+ isCustomSearch
}) => {
const theme = useTheme();
const classes = useStyles({ theme });
@@ -159,7 +163,11 @@ export const FilterChips: React.FC<FilterChipProps> = ({
</div>
))}
</div>
- <Link onClick={onFilterSave}>{i18n.t("Save Custom Search")}</Link>
+ {isCustomSearch ? (
+ <Link onClick={onFilterSave}>{i18n.t("Save Custom Search")}</Link>
+ ) : (
+ <Link onClick={onFilterDelete}>{i18n.t("Delete Search")}</Link>
+ )}
</div>
) : (
<Hr />
diff --git a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
index 1b5d80578e..eb62c41fac 100644
--- a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
@@ -17,10 +17,12 @@ export interface ProductListCardProps extends PageListProps, ListActions {
currencySymbol: string;
currentTab: number;
filtersList: Filter[];
+ initialSearch: string;
products: CategoryDetails_category_products_edges_node[];
onAllProducts: () => void;
onSearchChange: (value: string) => void;
onFilterAdd: (filter: FilterContentSubmitData) => void;
+ onFilterDelete: () => void;
onFilterSave: () => void;
onTabChange: (tab: number) => void;
}
@@ -32,6 +34,7 @@ export const ProductListCard: React.StatelessComponent<
currentTab,
disabled,
filtersList,
+ initialSearch,
pageInfo,
products,
onAdd,
@@ -47,7 +50,8 @@ export const ProductListCard: React.StatelessComponent<
onSearchChange,
onFilterAdd,
onFilterSave,
- onTabChange
+ onTabChange,
+ onFilterDelete
}) => (
<Container>
<PageHeader title={i18n.t("Products")}>
@@ -60,11 +64,13 @@ export const ProductListCard: React.StatelessComponent<
currencySymbol={currencySymbol}
currentTab={currentTab}
filtersList={filtersList}
+ initialSearch={initialSearch}
onAllProducts={onAllProducts}
onSearchChange={onSearchChange}
onFilterAdd={onFilterAdd}
onFilterSave={onFilterSave}
onTabChange={onTabChange}
+ onFilterDelete={onFilterDelete}
/>
<ProductList
products={products}
diff --git a/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx b/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
index 5876299d1b..189be0edd7 100644
--- a/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
@@ -17,9 +17,11 @@ interface ProductListFilterProps {
currencySymbol: string;
currentTab: number;
filtersList: Filter[];
+ initialSearch: string;
onAllProducts: () => void;
onSearchChange: (value: string) => void;
onFilterAdd: (filter: FilterContentSubmitData) => void;
+ onFilterDelete: () => void;
onFilterSave: () => void;
onTabChange: (tab: number) => void;
}
@@ -104,15 +106,24 @@ const ProductListFilter: React.StatelessComponent<ProductListFilterProps> = ({
currencySymbol,
filtersList,
currentTab,
+ initialSearch,
onAllProducts,
onSearchChange,
onFilterAdd,
onFilterSave,
- onTabChange
+ onTabChange,
+ onFilterDelete
}) => {
- const [search, setSearch] = React.useState("");
+ const [search, setSearch] = React.useState(initialSearch);
+ React.useEffect(
+ () => console.log(initialSearch) || setSearch(initialSearch),
+ [currentTab, initialSearch]
+ );
+
const filterTabs = getFilterTabs();
+ const isCustom = currentTab === filterTabs.length + 1;
+
return (
<>
<FilterTabs currentTab={currentTab}>
@@ -123,7 +134,7 @@ const ProductListFilter: React.StatelessComponent<ProductListFilterProps> = ({
label={tab.name}
/>
))}
- {currentTab === filterTabs.length + 1 && (
+ {isCustom && (
<FilterTab
onClick={() => undefined}
label={i18n.t("Custom Filter")}
@@ -149,6 +160,8 @@ const ProductListFilter: React.StatelessComponent<ProductListFilterProps> = ({
onSearchChange={handleSearchChange}
onFilterAdd={onFilterAdd}
onFilterSave={onFilterSave}
+ isCustomSearch={isCustom}
+ onFilterDelete={onFilterDelete}
/>
);
}}
diff --git a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
index db65910016..28ef788c98 100644
--- a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
@@ -5,7 +5,9 @@ import DeleteIcon from "@material-ui/icons/Delete";
import * as React from "react";
import ActionDialog from "../../../components/ActionDialog";
-import SaveFilterTabDialog from "../../../components/SaveFilterTabDialog";
+import SaveFilterTabDialog, {
+ SaveFilterTabDialogFormData
+} from "../../../components/SaveFilterTabDialog";
import useBulkActions from "../../../hooks/useBulkActions";
import useLocale from "../../../hooks/useLocale";
import useNavigator from "../../../hooks/useNavigator";
@@ -36,6 +38,7 @@ import {
areFiltersApplied,
createFilter,
createFilterChips,
+ deleteFilterTab,
getActiveFilters,
getFilterTabs,
getFilterVariables,
@@ -60,6 +63,13 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
params.ids
);
+ const currentTab =
+ params.activeTab === undefined
+ ? areFiltersApplied(params)
+ ? getFilterTabs().length + 1
+ : 0
+ : parseInt(params.activeTab, 0);
+
const closeModal = () =>
navigate(
productListUrl({
@@ -76,11 +86,18 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
};
const changeFilterField = (filter: ProductListUrlFilters) => {
+ const hasOnlyQueryChanged =
+ filter.query !== undefined &&
+ Object.keys(filter)
+ .filter((key: keyof ProductListUrlFilters) => key !== "query")
+ .every(key => !filter[key]);
+
reset();
navigate(
productListUrl({
...getActiveFilters(params),
- ...filter
+ ...filter,
+ activeTab: hasOnlyQueryChanged ? params.activeTab : undefined
})
);
};
@@ -99,11 +116,23 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
navigate(
productListUrl({
activeTab: tab.toString(),
- ...tabs[tab - 1].data
+ ...tabs[tab - 1].data,
+ query: params.query
})
);
};
+ const handleFilterTabDelete = () => {
+ deleteFilterTab(currentTab);
+ reset();
+ navigate(productListUrl());
+ };
+
+ const handleFilterTabSave = (data: SaveFilterTabDialogFormData) => {
+ saveFilterTab(data.name, getActiveFilters(params));
+ handleTabChange(getFilterTabs().length);
+ };
+
const paginationState = createPaginationState(PAGINATE_BY, params);
const currencySymbol = maybe(() => shop.defaultCurrency, "USD");
@@ -116,12 +145,6 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
}}
>
{({ data, loading, refetch }) => {
- const currentTab =
- params.activeTab === undefined
- ? areFiltersApplied(params)
- ? getFilterTabs().length + 1
- : 0
- : parseInt(params.activeTab, 0);
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
maybe(() => data.products.pageInfo),
paginationState,
@@ -240,7 +263,9 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
changeFilterField(createFilter(filter))
}
onFilterSave={() => openModal("save-search")}
+ onFilterDelete={handleFilterTabDelete}
onTabChange={handleTabChange}
+ initialSearch={params.query || ""}
/>
<ActionDialog
open={params.action === "delete"}
@@ -326,10 +351,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
open={params.action === "save-search"}
confirmButtonState="default"
onClose={closeModal}
- onSubmit={data => {
- saveFilterTab(data.name, getActiveFilters(params));
- handleTabChange(getFilterTabs().length);
- }}
+ onSubmit={handleFilterTabSave}
/>
</>
);
diff --git a/saleor/static/dashboard-next/products/views/ProductList/filters.ts b/saleor/static/dashboard-next/products/views/ProductList/filters.ts
index 9dc498d6b7..ee7bdf219a 100644
--- a/saleor/static/dashboard-next/products/views/ProductList/filters.ts
+++ b/saleor/static/dashboard-next/products/views/ProductList/filters.ts
@@ -46,7 +46,9 @@ export function getActiveFilters(
}
export function areFiltersApplied(params: ProductListUrlQueryParams): boolean {
- return Object.keys(getActiveFilters(params)).some(key => !!params[key]);
+ return Object.keys(getActiveFilters(params))
+ .filter((key: keyof ProductListUrlFilters) => key !== "query")
+ .some(key => !!params[key]);
}
export function createFilter(
@@ -210,9 +212,21 @@ export function saveFilterTab(name: string, data: ProductListUrlFilters) {
JSON.stringify([
...userFilters,
{
- data,
+ data: {
+ ...data,
+ query: undefined
+ },
name
}
])
);
}
+
+export function deleteFilterTab(id: number) {
+ const userFilters = getFilterTabs();
+
+ localStorage.setItem(
+ PRODUCT_FILTERS_KEY,
+ JSON.stringify([...userFilters.slice(0, id - 1), ...userFilters.slice(id)])
+ );
+}