Skip to content

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

Make ProductListFilter as generic as possible

dominik-zeglen998ca561ccec
diff --git a/saleor/static/dashboard-next/orders/views/OrderList/OrderList.tsx b/saleor/static/dashboard-next/orders/views/OrderList/OrderList.tsx
new file mode 100644
index 0000000000..236976d49a
--- /dev/null
+++ b/saleor/static/dashboard-next/orders/views/OrderList/OrderList.tsx
@@ -0,0 +1,200 @@
+import Button from "@material-ui/core/Button";
+import * as React from "react";
+
+import useBulkActions from "../../../hooks/useBulkActions";
+import useNavigator from "../../../hooks/useNavigator";
+import useNotifier from "../../../hooks/useNotifier";
+import usePaginator, {
+ createPaginationState
+} from "../../../hooks/usePaginator";
+import i18n from "../../../i18n";
+import { getMutationState, maybe } from "../../../misc";
+import { OrderStatus, OrderStatusFilter } from "../../../types/globalTypes";
+import OrderBulkCancelDialog from "../../components/OrderBulkCancelDialog";
+import OrderListPage from "../../components/OrderListPage/OrderListPage";
+import { getTabName } from "../../misc";
+import {
+ TypedOrderBulkCancelMutation,
+ TypedOrderDraftCreateMutation
+} from "../../mutations";
+import { TypedOrderListQuery } from "../../queries";
+import { OrderBulkCancel } from "../../types/OrderBulkCancel";
+import { OrderDraftCreate } from "../../types/OrderDraftCreate";
+import {
+ orderListUrl,
+ OrderListUrlFilters,
+ OrderListUrlQueryParams,
+ orderUrl
+} from "../../urls";
+
+interface OrderListProps {
+ params: OrderListUrlQueryParams;
+}
+
+const PAGINATE_BY = 20;
+
+export const OrderList: React.StatelessComponent<OrderListProps> = ({
+ params
+}) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
+ const paginate = usePaginator();
+ const { isSelected, listElements, reset, toggle, toggleAll } = useBulkActions(
+ params.ids
+ );
+
+ const closeModal = () =>
+ navigate(
+ orderListUrl({
+ ...params,
+ action: undefined,
+ ids: undefined
+ }),
+ true
+ );
+
+ const handleCreateOrderCreateSuccess = (data: OrderDraftCreate) => {
+ notify({
+ text: i18n.t("Order draft succesfully created")
+ });
+ navigate(orderUrl(data.draftOrderCreate.order.id));
+ };
+
+ const changeFilters = (filters: OrderListUrlFilters) => {
+ reset();
+ navigate(
+ orderListUrl({
+ ...params,
+ ...filters,
+ after: undefined,
+ before: undefined
+ })
+ );
+ };
+
+ const paginationState = createPaginationState(PAGINATE_BY, params);
+ const currentTab = getTabName(params);
+
+ return (
+ <TypedOrderDraftCreateMutation onCompleted={handleCreateOrderCreateSuccess}>
+ {createOrder => (
+ <TypedOrderListQuery
+ displayLoader
+ variables={{
+ ...paginationState,
+ filter: { customer: params.email },
+ status: OrderStatus[params.status]
+ }}
+ >
+ {({ data, loading, refetch }) => {
+ const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
+ maybe(() => data.orders.pageInfo),
+ paginationState,
+ params
+ );
+
+ const handleOrderBulkCancel = (data: OrderBulkCancel) => {
+ if (data.orderBulkCancel.errors.length === 0) {
+ notify({
+ text: i18n.t("Orders cancelled", {
+ context: "notification"
+ })
+ });
+ reset();
+ refetch();
+ closeModal();
+ }
+ };
+
+ return (
+ <TypedOrderBulkCancelMutation onCompleted={handleOrderBulkCancel}>
+ {(orderBulkCancel, orderBulkCancelOpts) => {
+ const orderBulkCancelTransitionState = getMutationState(
+ orderBulkCancelOpts.called,
+ orderBulkCancelOpts.loading,
+ maybe(() => orderBulkCancelOpts.data.orderBulkCancel.errors)
+ );
+ const onOrderBulkCancel = (restock: boolean) =>
+ orderBulkCancel({
+ variables: {
+ ids: params.ids,
+ restock
+ }
+ });
+
+ return (
+ <>
+ <OrderListPage
+ filtersList={[]}
+ currentTab={currentTab}
+ disabled={loading}
+ orders={maybe(() =>
+ data.orders.edges.map(edge => edge.node)
+ )}
+ pageInfo={pageInfo}
+ onAdd={createOrder}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () => navigate(orderUrl(id))}
+ onAllProducts={() =>
+ changeFilters({
+ status: undefined
+ })
+ }
+ onToFulfill={() =>
+ changeFilters({
+ status: OrderStatusFilter.READY_TO_FULFILL
+ })
+ }
+ onToCapture={() =>
+ changeFilters({
+ status: OrderStatusFilter.READY_TO_CAPTURE
+ })
+ }
+ onCustomFilter={() => undefined}
+ isChecked={isSelected}
+ selected={listElements.length}
+ toggle={toggle}
+ toggleAll={toggleAll}
+ toolbar={
+ <Button
+ color="primary"
+ onClick={() =>
+ navigate(
+ orderListUrl({
+ ...params,
+ action: "cancel",
+ ids: listElements
+ })
+ )
+ }
+ >
+ {i18n.t("Cancel", {
+ context: "cancel orders"
+ })}
+ </Button>
+ }
+ />
+ <OrderBulkCancelDialog
+ confirmButtonState={orderBulkCancelTransitionState}
+ numberOfOrders={maybe(
+ () => params.ids.length.toString(),
+ "..."
+ )}
+ onClose={closeModal}
+ onConfirm={onOrderBulkCancel}
+ open={params.action === "cancel"}
+ />
+ </>
+ );
+ }}
+ </TypedOrderBulkCancelMutation>
+ );
+ }}
+ </TypedOrderListQuery>
+ )}
+ </TypedOrderDraftCreateMutation>
+ );
+};
+
+export default OrderList;
diff --git a/saleor/static/dashboard-next/orders/views/OrderList/index.ts b/saleor/static/dashboard-next/orders/views/OrderList/index.ts
new file mode 100644
index 0000000000..6f012db870
--- /dev/null
+++ b/saleor/static/dashboard-next/orders/views/OrderList/index.ts
@@ -0,0 +1,2 @@
+export * from "./OrderList";
+export { default } from "./OrderList";
diff --git a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
index 24c801e2a2..0b9f9a35d2 100644
--- a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
@@ -10,21 +10,16 @@ import ProductList from "@saleor/components/ProductList";
import { Filter } from "@saleor/components/TableFilter";
import { CategoryDetails_category_products_edges_node } from "../../../categories/types/CategoryDetails";
import i18n from "../../../i18n";
-import { ListActions, PageListProps } from "../../../types";
+import { FilterViewProps, ListActions, PageListProps } from "../../../types";
+import { ProductListUrlFilters } from "../../urls";
import ProductListFilter from "../ProductListFilter";
-export interface ProductListCardProps extends PageListProps, ListActions {
+export interface ProductListCardProps
+ extends PageListProps,
+ ListActions,
+ FilterViewProps<FilterContentSubmitData, ProductListUrlFilters> {
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;
}
export const ProductListCard: React.StatelessComponent<
@@ -32,26 +27,17 @@ export const ProductListCard: React.StatelessComponent<
> = ({
currencySymbol,
currentTab,
- disabled,
filtersList,
+ filterTabs,
initialSearch,
- pageInfo,
- products,
onAdd,
- onAllProducts,
- onNextPage,
- onPreviousPage,
- onRowClick,
- isChecked,
- selected,
- toggle,
- toggleAll,
- toolbar,
+ onAll,
onSearchChange,
onFilterAdd,
onFilterSave,
onTabChange,
- onFilterDelete
+ onFilterDelete,
+ ...listProps
}) => (
<Container>
<PageHeader title={i18n.t("Products")}>
@@ -61,30 +47,22 @@ export const ProductListCard: React.StatelessComponent<
</PageHeader>
<Card>
<ProductListFilter
+ allTabLabel={i18n.t("All Products")}
currencySymbol={currencySymbol}
currentTab={currentTab}
+ filterLabel={i18n.t("Select all products where:")}
+ filterTabs={filterTabs}
filtersList={filtersList}
initialSearch={initialSearch}
- onAllProducts={onAllProducts}
+ searchPlaceholder={i18n.t("Search Products...")}
+ onAll={onAll}
onSearchChange={onSearchChange}
onFilterAdd={onFilterAdd}
onFilterSave={onFilterSave}
onTabChange={onTabChange}
onFilterDelete={onFilterDelete}
/>
- <ProductList
- products={products}
- disabled={disabled}
- pageInfo={pageInfo}
- toolbar={toolbar}
- selected={selected}
- isChecked={isChecked}
- toggle={toggle}
- toggleAll={toggleAll}
- onNextPage={onNextPage}
- onPreviousPage={onPreviousPage}
- onRowClick={onRowClick}
- />
+ <ProductList {...listProps} />
</Card>
</Container>
);
diff --git a/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx b/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
index 6878781aba..3d80f96ab4 100644
--- a/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
@@ -14,9 +14,10 @@ import {
import i18n from "../../../i18n";
import { FilterProps } from "../../../types";
import { StockAvailability } from "../../../types/globalTypes";
-import { getFilterTabs } from "../../views/ProductList/filters";
+import { ProductListUrlFilters } from "../../urls";
-interface ProductListFilterProps extends FilterProps<FilterContentSubmitData> {
+interface ProductListFilterProps
+ extends FilterProps<FilterContentSubmitData, ProductListUrlFilters> {
currencySymbol: string;
}
@@ -99,12 +100,16 @@ const filterMenu: IFilter = [
}
];
-const ProductListFilter: React.StatelessComponent<ProductListFilterProps> = ({
+const ProductListFilter: React.FC<ProductListFilterProps> = ({
+ allTabLabel,
currencySymbol,
+ filterLabel,
filtersList,
+ filterTabs,
currentTab,
initialSearch,
- onAllProducts,
+ searchPlaceholder,
+ onAll,
onSearchChange,
onFilterAdd,
onFilterSave,
@@ -114,14 +119,12 @@ const ProductListFilter: React.StatelessComponent<ProductListFilterProps> = ({
const [search, setSearch] = React.useState(initialSearch);
React.useEffect(() => setSearch(initialSearch), [currentTab, initialSearch]);
- const filterTabs = getFilterTabs();
-
const isCustom = currentTab === filterTabs.length + 1;
return (
<>
<FilterTabs currentTab={currentTab}>
- <FilterTab label={i18n.t("All Products")} onClick={onAllProducts} />
+ <FilterTab label={allTabLabel} onClick={onAll} />
{filterTabs.map((tab, tabIndex) => (
<FilterTab
onClick={() => onTabChange(tabIndex + 1)}
@@ -148,8 +151,8 @@ const ProductListFilter: React.StatelessComponent<ProductListFilterProps> = ({
currencySymbol={currencySymbol}
menu={filterMenu}
filtersList={filtersList}
- filterLabel={i18n.t("Select all products where:")}
- placeholder={i18n.t("Search Products...")}
+ filterLabel={filterLabel}
+ placeholder={searchPlaceholder}
search={search}
onSearchChange={handleSearchChange}
onFilterAdd={onFilterAdd}
diff --git a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
index e30d408995..43b39ca6db 100644
--- a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
@@ -214,7 +214,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
onPreviousPage={loadPreviousPage}
pageInfo={pageInfo}
onRowClick={id => () => navigate(productUrl(id))}
- onAllProducts={() =>
+ onAll={() =>
changeFilters({
status: undefined
})
@@ -255,6 +255,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
onFilterDelete={() => openModal("delete-search")}
onTabChange={handleTabChange}
initialSearch={params.query || ""}
+ filterTabs={getFilterTabs()}
/>
<ActionDialog
open={params.action === "delete"}
diff --git a/saleor/static/dashboard-next/types.ts b/saleor/static/dashboard-next/types.ts
index 5be4f6b031..0dfc837f28 100644
--- a/saleor/static/dashboard-next/types.ts
+++ b/saleor/static/dashboard-next/types.ts
@@ -1,6 +1,7 @@
import { MutationResult } from "react-apollo";
import { Filter } from "./components/TableFilter";
+import { GetFilterTabsOutput } from "./utils/filters";
export interface UserError {
field: string;
@@ -33,17 +34,24 @@ export interface ListActions extends ListActionsWithoutToolbar {
export interface PageListProps extends ListProps {
onAdd: () => void;
}
-export interface FilterProps<TFilterContentSubmitData> {
+export interface FilterViewProps<TFilterContentSubmitData, TUrlFilters> {
currentTab: number;
+ filterTabs: GetFilterTabsOutput<TUrlFilters>;
filtersList: Filter[];
initialSearch: string;
- onAllProducts: () => void;
+ onAll: () => void;
onSearchChange: (value: string) => void;
onFilterAdd: (filter: TFilterContentSubmitData) => void;
onFilterDelete: () => void;
onFilterSave: () => void;
onTabChange: (tab: number) => void;
}
+export interface FilterProps<TFilterContentSubmitData, TUrlFilters>
+ extends FilterViewProps<TFilterContentSubmitData, TUrlFilters> {
+ allTabLabel: string;
+ filterLabel: string;
+ searchPlaceholder: string;
+}
export interface PartialMutationProviderOutput<
TData extends {} = {},
diff --git a/saleor/static/dashboard-next/utils/filters/storage.ts b/saleor/static/dashboard-next/utils/filters/storage.ts
index fc4d7481a8..533e3f182f 100644
--- a/saleor/static/dashboard-next/utils/filters/storage.ts
+++ b/saleor/static/dashboard-next/utils/filters/storage.ts
@@ -3,9 +3,11 @@ interface UserFilter<TUrlFilters> {
data: TUrlFilters;
}
+export type GetFilterTabsOutput<TUrlFilters> = Array<UserFilter<TUrlFilters>>;
+
function getFilterTabs<TUrlFilters>(
key: string
-): Array<UserFilter<TUrlFilters>> {
+): GetFilterTabsOutput<TUrlFilters> {
return JSON.parse(localStorage.getItem(key)) || [];
}