1 import { useParams } from "react-router-dom"
2
3 import { RouteFocusModal } from "../../../components/modals"
4 import { useStockLocation } from "../../../hooks/api/stock-locations"
5 import { CreateServiceZoneForm } from "./components/create-service-zone-form"
6 import { FulfillmentSetType } from "../common/constants"
7 import { jsonResponse } from "../../../lib/json-response"
8
9 export function LocationCreateServiceZone() {
10 const { fset_id, location_id } = useParams()
11
12 const { stock_location, isPending, isFetching, isError, error } =
13 useStockLocation(location_id!, {
14 fields: "*fulfillment_sets",
15 })
16
17 const fulfillmentSet = stock_location?.fulfillment_sets?.find(
18 (f) => f.id === fset_id
19 )
20
21 const type: FulfillmentSetType =
22 fulfillmentSet?.type === FulfillmentSetType.Pickup
23 ? FulfillmentSetType.Pickup
24 : FulfillmentSetType.Shipping
25
26 if (!isPending && !isFetching && !fulfillmentSet) {
27 throw jsonResponse(
28 { message: `Fulfillment set with ID: ${fset_id} was not found.` },
29 404
30 )
31 }
32
33 if (isError) {
34 throw error
35 }
36
37 return (
38 <RouteFocusModal prev={`/settings/locations/${location_id}`}>
39 {fulfillmentSet && (
40 <CreateServiceZoneForm
41 fulfillmentSet={fulfillmentSet}
42 location={stock_location!}
43 type={type}
44 />
45 )}
46 </RouteFocusModal>
47 )
48 }