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

Introduce new filters to draft orders #5380

Merged
merged 20 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
5 changes: 5 additions & 0 deletions .changeset/silver-items-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

List of draft orders now use new filters
8 changes: 0 additions & 8 deletions locale/defaultMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7154,10 +7154,6 @@
"context": "button",
"string": "Apply"
},
"iEeIhY": {
"context": "draft order",
"string": "Customer"
},
"iFM716": {
"context": "grant refund, refund card subtitle",
"string": "How much money do you want to return to the customer for the order?"
Expand Down Expand Up @@ -9188,10 +9184,6 @@
"vuKrlW": {
"string": "Stock"
},
"vwMO04": {
"context": "draft order",
"string": "Created"
},
"vz3yxp": {
"string": "Channels permissions"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { FilterAPIProvider } from "@dashboard/components/ConditionalFilter/API/FilterAPIProvider";

export const useDraftOrderFilterAPIProvider = (): FilterAPIProvider => {
const fetchRightOptions = async () => {
return [];
};
const fetchLeftOptions = async () => {
return [];
};

return {
fetchRightOptions,
fetchLeftOptions,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export class InitialStateResponse implements InitialState {
}

if (!token.isLoadable()) {
if (Array.isArray(token.value)) {
return token.value;
}

return [token.value] as string[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { prepareStructure } from "./utils";

export const useUrlValueProvider = (
locationSearch: string,
type: "product" | "order" | "discount",
type: "product" | "draft-order" | "order" | "discount",
initialState?: InitialAPIState | InitialOrderAPIState,
): FilterValueProvider => {
const router = useRouter();
Expand Down
16 changes: 16 additions & 0 deletions src/components/ConditionalFilter/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,26 @@ export const STATIC_ORDER_OPTIONS: LeftOperand[] = [
},
];

export const STATIC_DRAFT_ORDER_OPTIONS: LeftOperand[] = [
{
value: "customer",
label: "Customer",
type: "customer",
slug: "customer",
},
{
value: "created",
label: "Created",
type: "created",
slug: "created",
},
];

export const STATIC_OPTIONS = [
...STATIC_PRODUCT_OPTIONS,
...STATIC_DISCOUNT_OPTIONS,
...STATIC_ORDER_OPTIONS,
...STATIC_DRAFT_ORDER_OPTIONS,
];

export const ATTRIBUTE_INPUT_TYPE_CONDITIONS = {
Expand Down
27 changes: 27 additions & 0 deletions src/components/ConditionalFilter/context/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useDraftOrderFilterAPIProvider } from "@dashboard/components/ConditionalFilter/API/DraftOrderFilterAPIProvider";
import React, { FC } from "react";

import { useDiscountFilterAPIProvider } from "../API/DiscountFiltersAPIProvider";
Expand All @@ -7,6 +8,7 @@ import { useOrderFilterAPIProvider } from "../API/OrderFilterAPIProvider";
import { useProductFilterAPIProvider } from "../API/ProductFilterAPIProvider";
import {
STATIC_DISCOUNT_OPTIONS,
STATIC_DRAFT_ORDER_OPTIONS,
STATIC_ORDER_OPTIONS,
STATIC_PRODUCT_OPTIONS,
} from "../constants";
Expand Down Expand Up @@ -90,3 +92,28 @@ export const ConditionalOrderFilterProvider: FC<{
</ConditionalFilterContext.Provider>
);
};

export const ConditionalDraftOrderFilterProvider: FC<{
locationSearch: string;
}> = ({ children, locationSearch }) => {
const apiProvider = useDraftOrderFilterAPIProvider();

const valueProvider = useUrlValueProvider(locationSearch, "draft-order");
const leftOperandsProvider = useFilterLeftOperandsProvider(STATIC_DRAFT_ORDER_OPTIONS);
const containerState = useContainerState(valueProvider);
const filterWindow = useFilterWindow();

return (
<ConditionalFilterContext.Provider
value={{
apiProvider,
valueProvider,
leftOperandsProvider,
containerState,
filterWindow,
}}
>
{children}
</ConditionalFilterContext.Provider>
);
};
43 changes: 42 additions & 1 deletion src/components/ConditionalFilter/queryVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DateTimeRangeInput,
DecimalFilterInput,
GlobalIdFilterInput,
OrderDraftFilterInput,
ProductWhereInput,
PromotionWhereInput,
} from "@dashboard/graphql";
Expand All @@ -19,7 +20,12 @@ const createStaticQueryPart = (selected: ConditionSelected): StaticQueryPart =>
if (!selected.conditionValue) return "";

const { label } = selected.conditionValue;
const { value } = selected;
const { value: selectedValue } = selected;

const value =
Array.isArray(selectedValue) && !isTuple(selectedValue) && !isItemOptionArray(selectedValue)
? selectedValue[0]
: selectedValue;

if (label === "lower") {
return { range: { lte: value } };
Expand Down Expand Up @@ -57,6 +63,27 @@ const createStaticQueryPart = (selected: ConditionSelected): StaticQueryPart =>

return value;
};

const mapStaticQueryPartToLegacyVariables = (queryPart: StaticQueryPart) => {
if (typeof queryPart !== "object") {
return queryPart;
}

if ("range" in queryPart) {
return queryPart.range;
}

if ("eq" in queryPart) {
return queryPart.eq;
}

if ("oneOf" in queryPart) {
return queryPart.oneOf;
}

return queryPart;
};

const getRangeQueryPartByType = (value: [string, string], type: string) => {
const [gte, lte] = value;

Expand All @@ -70,6 +97,7 @@ const getRangeQueryPartByType = (value: [string, string], type: string) => {
return { valuesRange: { lte: parseFloat(lte), gte: parseFloat(gte) } };
}
};

const getQueryPartByType = (value: string, type: string, what: "lte" | "gte") => {
switch (type) {
case "datetime":
Expand All @@ -80,6 +108,7 @@ const getQueryPartByType = (value: string, type: string, what: "lte" | "gte") =>
return { valuesRange: { [what]: parseFloat(value) } };
}
};

const createAttributeQueryPart = (
attributeSlug: string,
selected: ConditionSelected,
Expand Down Expand Up @@ -199,3 +228,15 @@ export const createOrderQueryVariables = (value: FilterContainer) => {
return p;
}, {} as OrderQueryVars);
};

export const creatDraftOrderQueryVariables = (value: FilterContainer): OrderDraftFilterInput => {
return value.reduce((p, c) => {
if (typeof c === "string" || Array.isArray(c)) return p;

p[c.value.value as keyof OrderDraftFilterInput] = mapStaticQueryPartToLegacyVariables(
createStaticQueryPart(c.condition.selected),
);

return p;
}, {} as OrderDraftFilterInput);
};
22 changes: 10 additions & 12 deletions src/orders/components/OrderDraftListPage/OrderDraftListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { BulkDeleteButton } from "@dashboard/components/BulkDeleteButton";
import { DashboardCard } from "@dashboard/components/Card";
import { OrderDraftListQuery, RefreshLimitsQuery } from "@dashboard/graphql";
import { OrderDraftListUrlSortField } from "@dashboard/orders/urls";
import { FilterPagePropsWithPresets, PageListProps, RelayToFlat, SortPage } from "@dashboard/types";
import {
FilterPresetsProps,
PageListProps,
RelayToFlat,
SearchPageProps,
SortPage,
} from "@dashboard/types";
import { isLimitReached } from "@dashboard/utils/limits";
import { Box } from "@saleor/macaw-ui-next";
import React, { useState } from "react";
Expand All @@ -13,11 +19,11 @@ import { useIntl } from "react-intl";
import { OrderDraftListDatagrid } from "../OrderDraftListDatagrid";
import { OrderDraftListHeader } from "../OrderDraftListHeader/OrderDraftListHeader";
import OrderLimitReached from "../OrderLimitReached";
import { createFilterStructure, OrderDraftFilterKeys, OrderDraftListFilterOpts } from "./filters";

export interface OrderDraftListPageProps
extends PageListProps,
FilterPagePropsWithPresets<OrderDraftFilterKeys, OrderDraftListFilterOpts>,
SearchPageProps,
FilterPresetsProps,
SortPage<OrderDraftListUrlSortField> {
limits: RefreshLimitsQuery["shop"]["limits"];
orders: RelayToFlat<OrderDraftListQuery["draftOrders"]>;
Expand All @@ -31,12 +37,10 @@ export interface OrderDraftListPageProps
const OrderDraftListPage: React.FC<OrderDraftListPageProps> = ({
selectedFilterPreset,
disabled,
filterOpts,
initialSearch,
limits,
onAdd,
onFilterPresetsAll,
onFilterChange,
onSearchChange,
onFilterPresetChange,
onFilterPresetDelete,
Expand All @@ -45,14 +49,11 @@ const OrderDraftListPage: React.FC<OrderDraftListPageProps> = ({
filterPresets,
hasPresetsChanged,
onDraftOrdersDelete,
onFilterAttributeFocus,
currencySymbol,
selectedOrderDraftIds,
...listProps
}) => {
const intl = useIntl();
const [isFilterPresetOpen, setFilterPresetOpen] = useState(false);
const filterStructure = createFilterStructure(intl, filterOpts);
const limitsReached = isLimitReached(limits, "orders");

return (
Expand Down Expand Up @@ -84,12 +85,9 @@ const OrderDraftListPage: React.FC<OrderDraftListPageProps> = ({
justifyContent="space-between"
>
<ListFilters
currencySymbol={currencySymbol}
type="expression-filter"
initialSearch={initialSearch}
onFilterChange={onFilterChange}
onFilterAttributeFocus={onFilterAttributeFocus}
onSearchChange={onSearchChange}
filterStructure={filterStructure}
searchPlaceholder={intl.formatMessage({
id: "IzECoP",
defaultMessage: "Search draft orders...",
Expand Down
51 changes: 0 additions & 51 deletions src/orders/components/OrderDraftListPage/filters.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/orders/components/OrderDraftListPage/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./filters";
export { default } from "./OrderDraftListPage";
export * from "./OrderDraftListPage";
11 changes: 9 additions & 2 deletions src/orders/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ConditionalOrderFilterProvider } from "@dashboard/components/ConditionalFilter";
import {
ConditionalDraftOrderFilterProvider,
ConditionalOrderFilterProvider,
} from "@dashboard/components/ConditionalFilter";
import { Route } from "@dashboard/components/Router";
import { sectionNames } from "@dashboard/intl";
import { asSortParams } from "@dashboard/utils/sort";
Expand Down Expand Up @@ -71,7 +74,11 @@ const OrderDraftList: React.FC<RouteComponentProps<any>> = ({ location }) => {
false,
);

return <OrderDraftListComponent params={params} />;
return (
<ConditionalDraftOrderFilterProvider locationSearch={location.search}>
<OrderDraftListComponent params={params} />
</ConditionalDraftOrderFilterProvider>
);
};
const OrderDetails: React.FC<RouteComponentProps<any>> = ({ location, match }) => {
const qs = parseQs(location.search.substr(1)) as any;
Expand Down
Loading
Loading