1 import { Heading } from "@medusajs/ui"
2 import { useTranslation } from "react-i18next"
3 import { useParams } from "react-router-dom"
4 import { RouteDrawer } from "../../../components/modals"
5 import { useCustomerGroups } from "../../../hooks/api/customer-groups"
6 import { usePriceList } from "../../../hooks/api/price-lists"
7 import { PriceListConfigurationForm } from "./components/price-list-configuration-form"
8
9 export const PriceListConfiguration = () => {
10 const { t } = useTranslation()
11 const { id } = useParams()
12
13 const { price_list, isPending, isError, error } = usePriceList(id!)
14
15 const customerGroupIds = price_list?.rules?.["customer.groups.id"] as
16 | string[]
17 | undefined
18
19 const {
20 customer_groups,
21 isPending: isCustomerGroupsPending,
22 isError: isCustomerGroupsError,
23 error: customerGroupsError,
24 } = useCustomerGroups(
25 {
26 id: customerGroupIds,
27 },
28 { enabled: !!customerGroupIds?.length }
29 )
30
31 const initialCustomerGroups =
32 customer_groups?.map((group) => ({
33 id: group.id,
34 name: group.name!,
35 })) || []
36
37 const isCustomerGroupsReady = isPending
38 ? false
39 : !!customerGroupIds?.length && isCustomerGroupsPending
40 ? false
41 : true
42
43 const ready = !isPending && !!price_list && isCustomerGroupsReady
44
45 if (isError) {
46 throw error
47 }
48
49 if (isCustomerGroupsError) {
50 throw customerGroupsError
51 }
52
53 return (
54 <RouteDrawer>
55 <RouteDrawer.Header>
56 <RouteDrawer.Title asChild>
57 <Heading>{t("priceLists.configuration.edit.header")}</Heading>
58 </RouteDrawer.Title>
59 </RouteDrawer.Header>
60 {ready && (
61 <PriceListConfigurationForm
62 priceList={price_list}
63 customerGroups={initialCustomerGroups}
64 />
65 )}
66 </RouteDrawer>
67 )
68 }