|
1
|
import { useParams, useSearchParams } from "react-router-dom" |
|
2
|
|
|
3
|
import { RouteFocusModal } from "../../../components/modals" |
|
4
|
import { useStockLocation } from "../../../hooks/api/stock-locations" |
|
5
|
import { CreateShippingOptionsForm } from "./components/create-shipping-options-form" |
|
6
|
import { LOC_CREATE_SHIPPING_OPTION_FIELDS } from "./constants" |
|
7
|
import { FulfillmentSetType } from "../common/constants" |
|
8
|
import { jsonResponse } from "../../../lib/json-response" |
|
9
|
|
|
10
|
export function LocationServiceZoneShippingOptionCreate() { |
|
11
|
const { location_id, fset_id, zone_id } = useParams() |
|
12
|
const [searchParams] = useSearchParams() |
|
13
|
const isReturn = searchParams.has("is_return") |
|
14
|
|
|
15
|
const { stock_location, isPending, isFetching, isError, error } = |
|
16
|
useStockLocation(location_id!, { |
|
17
|
fields: LOC_CREATE_SHIPPING_OPTION_FIELDS, |
|
18
|
}) |
|
19
|
|
|
20
|
const fulfillmentSet = stock_location?.fulfillment_sets?.find( |
|
21
|
(f) => f.id === fset_id |
|
22
|
) |
|
23
|
|
|
24
|
if (!isPending && !isFetching && !fulfillmentSet) { |
|
25
|
throw jsonResponse( |
|
26
|
{ message: `Fulfillment set with ID ${fset_id} was not found` }, |
|
27
|
404 |
|
28
|
) |
|
29
|
} |
|
30
|
|
|
31
|
const zone = fulfillmentSet?.service_zones?.find((z) => z.id === zone_id) |
|
32
|
|
|
33
|
if (!isPending && !isFetching && !zone) { |
|
34
|
throw jsonResponse( |
|
35
|
{ message: `Service zone with ID ${zone_id} was not found` }, |
|
36
|
404 |
|
37
|
) |
|
38
|
} |
|
39
|
|
|
40
|
if (isError) { |
|
41
|
throw error |
|
42
|
} |
|
43
|
|
|
44
|
return ( |
|
45
|
<RouteFocusModal prev={`/settings/locations/${location_id}`}> |
|
46
|
{zone && ( |
|
47
|
<CreateShippingOptionsForm |
|
48
|
zone={zone} |
|
49
|
isReturn={isReturn} |
|
50
|
locationId={location_id!} |
|
51
|
type={fulfillmentSet!.type as FulfillmentSetType} |
|
52
|
/> |
|
53
|
)} |
|
54
|
</RouteFocusModal> |
|
55
|
) |
|
56
|
} |