1 import { Heading } from "@medusajs/ui"
2 import { useTranslation } from "react-i18next"
3 import { useParams } from "react-router-dom"
4
5 import { RouteDrawer } from "../../../components/modals"
6 import { useShippingOptions } from "../../../hooks/api/shipping-options"
7 import { EditShippingOptionForm } from "./components/edit-region-form"
8 import { FulfillmentSetType } from "../common/constants"
9 import { jsonResponse } from "../../../lib/json-response"
10
11 export const LocationServiceZoneShippingOptionEdit = () => {
12 const { t } = useTranslation()
13
14 const { location_id, so_id } = useParams()
15
16 const { shipping_options, isPending, isFetching, isError, error } =
17 useShippingOptions({
18 id: so_id,
19 fields: "+service_zone.fulfillment_set.type",
20 })
21
22 const shippingOption = shipping_options?.find((so) => so.id === so_id)
23
24 if (!isPending && !isFetching && !shippingOption) {
25 throw jsonResponse(
26 { message: `Shipping option with ID ${so_id} was not found` },
27 404
28 )
29 }
30
31 if (isError) {
32 throw error
33 }
34
35 const isPickup =
36 shippingOption?.service_zone.fulfillment_set.type ===
37 FulfillmentSetType.Pickup
38
39 return (
40 <RouteDrawer>
41 <RouteDrawer.Header>
42 <Heading>
43 {t(
44 `stockLocations.${
45 isPickup ? "pickupOptions" : "shippingOptions"
46 }.edit.header`
47 )}
48 </Heading>
49 </RouteDrawer.Header>
50 {shippingOption && (
51 <EditShippingOptionForm
52 shippingOption={shippingOption}
53 locationId={location_id!}
54 type={
55 shippingOption.service_zone.fulfillment_set
56 .type as FulfillmentSetType
57 }
58 />
59 )}
60 </RouteDrawer>
61 )
62 }