|
1
|
import { useParams, useSearchParams } from "react-router-dom" |
|
2
|
import { RouteFocusModal } from "../../../components/modals" |
|
3
|
import { usePriceList } from "../../../hooks/api/price-lists" |
|
4
|
import { useProducts } from "../../../hooks/api/products" |
|
5
|
import { usePriceListCurrencyData } from "../common/hooks/use-price-list-currency-data" |
|
6
|
import { PriceListPricesEditForm } from "./components/price-list-prices-edit-form" |
|
7
|
|
|
8
|
export const PriceListPricesEdit = () => { |
|
9
|
const { id } = useParams() |
|
10
|
const [searchParams] = useSearchParams() |
|
11
|
const ids = searchParams.get("ids[]") |
|
12
|
|
|
13
|
const { price_list, isLoading, isError, error } = usePriceList(id!, { |
|
14
|
fields: |
|
15
|
"*prices,prices.price_set.variant.id,prices.price_rules.attribute,prices.price_rules.value", |
|
16
|
}) |
|
17
|
const productIds = ids?.split(",") |
|
18
|
|
|
19
|
const { |
|
20
|
products, |
|
21
|
isLoading: isProductsLoading, |
|
22
|
isError: isProductsError, |
|
23
|
error: productError, |
|
24
|
} = useProducts({ |
|
25
|
id: productIds, |
|
26
|
limit: productIds?.length || 9999, // Temporary until we support lazy loading in the DataGrid |
|
27
|
price_list_id: [id!], |
|
28
|
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config |
|
29
|
fields: |
|
30
|
"title,thumbnail,*variants,-type,-collection,-options,-tags,-images,-sales_channels", |
|
31
|
}) |
|
32
|
|
|
33
|
const { isReady, regions, currencies, pricePreferences } = |
|
34
|
usePriceListCurrencyData() |
|
35
|
|
|
36
|
const ready = |
|
37
|
!isLoading && !!price_list && !isProductsLoading && !!products && isReady |
|
38
|
|
|
39
|
if (isError) { |
|
40
|
throw error |
|
41
|
} |
|
42
|
|
|
43
|
if (isProductsError) { |
|
44
|
throw productError |
|
45
|
} |
|
46
|
|
|
47
|
return ( |
|
48
|
<RouteFocusModal> |
|
49
|
<RouteFocusModal.Title asChild> |
|
50
|
<span className="sr-only">Edit Prices for {price_list?.title}</span> |
|
51
|
</RouteFocusModal.Title> |
|
52
|
<RouteFocusModal.Description className="sr-only"> |
|
53
|
Update prices for products in the price list |
|
54
|
</RouteFocusModal.Description> |
|
55
|
{ready && ( |
|
56
|
<PriceListPricesEditForm |
|
57
|
priceList={price_list} |
|
58
|
products={products} |
|
59
|
regions={regions} |
|
60
|
currencies={currencies} |
|
61
|
pricePreferences={pricePreferences} |
|
62
|
/> |
|
63
|
)} |
|
64
|
</RouteFocusModal> |
|
65
|
) |
|
66
|
} |