@medusajs/draft-order 2.12.0-preview-20251103180145 → 2.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -352,10 +352,14 @@ const DataTableAction = ({
352
352
  }
353
353
  return /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { ...buttonProps, onClick: props.onClick, children: label });
354
354
  };
355
+ const backendUrl = __BACKEND_URL__ ?? "/";
356
+ const authType = __AUTH_TYPE__ ?? "session";
357
+ const jwtTokenStorageKey = __JWT_TOKEN_STORAGE_KEY__ || void 0;
355
358
  const sdk = new Medusa__default.default({
356
- baseUrl: __BACKEND_URL__ || "/",
359
+ baseUrl: backendUrl,
357
360
  auth: {
358
- type: "session"
361
+ type: authType,
362
+ jwtTokenStorageKey
359
363
  }
360
364
  });
361
365
  const CUSTOMER_QUERY_KEY = "customers";
@@ -4654,9 +4658,6 @@ ZodReadonly.create = (type, params) => {
4654
4658
  ...processCreateParams(params)
4655
4659
  });
4656
4660
  };
4657
- ({
4658
- object: ZodObject.lazycreate
4659
- });
4660
4661
  var ZodFirstPartyTypeKind;
4661
4662
  (function(ZodFirstPartyTypeKind2) {
4662
4663
  ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
@@ -4703,7 +4704,6 @@ const anyType = ZodAny.create;
4703
4704
  ZodNever.create;
4704
4705
  const arrayType = ZodArray.create;
4705
4706
  const objectType = ZodObject.create;
4706
- ZodObject.strictCreate;
4707
4707
  const unionType = ZodUnion.create;
4708
4708
  ZodIntersection.create;
4709
4709
  ZodTuple.create;
@@ -8729,7 +8729,7 @@ const Contact = ({ order }) => {
8729
8729
  children: phone
8730
8730
  }
8731
8731
  ),
8732
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Copy, { content: email, className: "text-ui-fg-muted" }) })
8732
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Copy, { content: phone, className: "text-ui-fg-muted" }) })
8733
8733
  ] })
8734
8734
  ] })
8735
8735
  ] });
@@ -10825,632 +10825,632 @@ const customItemSchema = objectType({
10825
10825
  quantity: numberType(),
10826
10826
  unit_price: unionType([numberType(), stringType()])
10827
10827
  });
10828
- const InlineTip = React.forwardRef(
10829
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10830
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10831
- return /* @__PURE__ */ jsxRuntime.jsxs(
10832
- "div",
10833
- {
10834
- ref,
10835
- className: ui.clx(
10836
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10837
- className
10838
- ),
10839
- ...props,
10840
- children: [
10841
- /* @__PURE__ */ jsxRuntime.jsx(
10842
- "div",
10843
- {
10844
- role: "presentation",
10845
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10846
- "bg-ui-tag-orange-icon": variant === "warning"
10847
- })
10848
- }
10849
- ),
10850
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10851
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10852
- labelValue,
10853
- ":"
10854
- ] }),
10855
- " ",
10856
- children
10857
- ] })
10858
- ]
10859
- }
10860
- );
10861
- }
10862
- );
10863
- InlineTip.displayName = "InlineTip";
10864
- const MetadataFieldSchema = objectType({
10865
- key: stringType(),
10866
- disabled: booleanType().optional(),
10867
- value: anyType()
10868
- });
10869
- const MetadataSchema = objectType({
10870
- metadata: arrayType(MetadataFieldSchema)
10871
- });
10872
- const Metadata = () => {
10873
- const { id } = reactRouterDom.useParams();
10874
- const { order, isPending, isError, error } = useOrder(id, {
10875
- fields: "metadata"
10828
+ const PROMOTION_QUERY_KEY = "promotions";
10829
+ const promotionsQueryKeys = {
10830
+ list: (query2) => [
10831
+ PROMOTION_QUERY_KEY,
10832
+ query2 ? query2 : void 0
10833
+ ],
10834
+ detail: (id, query2) => [
10835
+ PROMOTION_QUERY_KEY,
10836
+ id,
10837
+ query2 ? query2 : void 0
10838
+ ]
10839
+ };
10840
+ const usePromotions = (query2, options) => {
10841
+ const { data, ...rest } = reactQuery.useQuery({
10842
+ queryKey: promotionsQueryKeys.list(query2),
10843
+ queryFn: async () => sdk.admin.promotion.list(query2),
10844
+ ...options
10876
10845
  });
10877
- if (isError) {
10878
- throw error;
10846
+ return { ...data, ...rest };
10847
+ };
10848
+ const Promotions = () => {
10849
+ const { id } = reactRouterDom.useParams();
10850
+ const {
10851
+ order: preview,
10852
+ isError: isPreviewError,
10853
+ error: previewError
10854
+ } = useOrderPreview(id, void 0);
10855
+ useInitiateOrderEdit({ preview });
10856
+ const { onCancel } = useCancelOrderEdit({ preview });
10857
+ if (isPreviewError) {
10858
+ throw previewError;
10879
10859
  }
10880
- const isReady = !isPending && !!order;
10881
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10882
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10883
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10884
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10885
- ] }),
10886
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10860
+ const isReady = !!preview;
10861
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
10862
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
10863
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
10887
10864
  ] });
10888
10865
  };
10889
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10890
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10891
- const MetadataForm = ({ orderId, metadata }) => {
10866
+ const PromotionForm = ({ preview }) => {
10867
+ const { items, shipping_methods } = preview;
10868
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
10869
+ const [comboboxValue, setComboboxValue] = React.useState("");
10892
10870
  const { handleSuccess } = useRouteModal();
10893
- const hasUneditableRows = getHasUneditableRows(metadata);
10894
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10895
- const form = reactHookForm.useForm({
10896
- defaultValues: {
10897
- metadata: getDefaultValues(metadata)
10871
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10872
+ const promoIds = getPromotionIds(items, shipping_methods);
10873
+ const { promotions, isPending, isError, error } = usePromotions(
10874
+ {
10875
+ id: promoIds
10898
10876
  },
10899
- resolver: zod.zodResolver(MetadataSchema)
10877
+ {
10878
+ enabled: !!promoIds.length
10879
+ }
10880
+ );
10881
+ const comboboxData = useComboboxData({
10882
+ queryKey: ["promotions", "combobox", promoIds],
10883
+ queryFn: async (params) => {
10884
+ return await sdk.admin.promotion.list({
10885
+ ...params,
10886
+ id: {
10887
+ $nin: promoIds
10888
+ }
10889
+ });
10890
+ },
10891
+ getOptions: (data) => {
10892
+ return data.promotions.map((promotion) => ({
10893
+ label: promotion.code,
10894
+ value: promotion.code
10895
+ }));
10896
+ }
10900
10897
  });
10901
- const handleSubmit = form.handleSubmit(async (data) => {
10902
- const parsedData = parseValues(data);
10903
- await mutateAsync(
10898
+ const add = async (value) => {
10899
+ if (!value) {
10900
+ return;
10901
+ }
10902
+ addPromotions(
10904
10903
  {
10905
- metadata: parsedData
10904
+ promo_codes: [value]
10906
10905
  },
10907
10906
  {
10908
- onSuccess: () => {
10909
- ui.toast.success("Metadata updated");
10910
- handleSuccess();
10907
+ onError: (e) => {
10908
+ ui.toast.error(e.message);
10909
+ comboboxData.onSearchValueChange("");
10910
+ setComboboxValue("");
10911
10911
  },
10912
- onError: (error) => {
10913
- ui.toast.error(error.message);
10912
+ onSuccess: () => {
10913
+ comboboxData.onSearchValueChange("");
10914
+ setComboboxValue("");
10914
10915
  }
10915
10916
  }
10916
10917
  );
10917
- });
10918
- const { fields, insert, remove } = reactHookForm.useFieldArray({
10919
- control: form.control,
10920
- name: "metadata"
10921
- });
10922
- function deleteRow(index) {
10923
- remove(index);
10924
- if (fields.length === 1) {
10925
- insert(0, {
10926
- key: "",
10927
- value: "",
10928
- disabled: false
10929
- });
10918
+ };
10919
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10920
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10921
+ const onSubmit = async () => {
10922
+ setIsSubmitting(true);
10923
+ let requestSucceeded = false;
10924
+ await requestOrderEdit(void 0, {
10925
+ onError: (e) => {
10926
+ ui.toast.error(e.message);
10927
+ },
10928
+ onSuccess: () => {
10929
+ requestSucceeded = true;
10930
+ }
10931
+ });
10932
+ if (!requestSucceeded) {
10933
+ setIsSubmitting(false);
10934
+ return;
10930
10935
  }
10931
- }
10932
- function insertRow(index, position) {
10933
- insert(index + (position === "above" ? 0 : 1), {
10934
- key: "",
10935
- value: "",
10936
- disabled: false
10936
+ await confirmOrderEdit(void 0, {
10937
+ onError: (e) => {
10938
+ ui.toast.error(e.message);
10939
+ },
10940
+ onSuccess: () => {
10941
+ handleSuccess();
10942
+ },
10943
+ onSettled: () => {
10944
+ setIsSubmitting(false);
10945
+ }
10937
10946
  });
10947
+ };
10948
+ if (isError) {
10949
+ throw error;
10938
10950
  }
10939
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10940
- KeyboundForm,
10941
- {
10942
- onSubmit: handleSubmit,
10943
- className: "flex flex-1 flex-col overflow-hidden",
10944
- children: [
10945
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10946
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10947
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10948
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
10949
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
10950
- ] }),
10951
- fields.map((field, index) => {
10952
- const isDisabled = field.disabled || false;
10953
- let placeholder = "-";
10954
- if (typeof field.value === "object") {
10955
- placeholder = "{ ... }";
10956
- }
10957
- if (Array.isArray(field.value)) {
10958
- placeholder = "[ ... ]";
10959
- }
10960
- return /* @__PURE__ */ jsxRuntime.jsx(
10961
- ConditionalTooltip,
10962
- {
10963
- showTooltip: isDisabled,
10964
- content: "This row is disabled because it contains non-primitive data.",
10965
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
10966
- /* @__PURE__ */ jsxRuntime.jsxs(
10967
- "div",
10968
- {
10969
- className: ui.clx("grid grid-cols-2 divide-x", {
10970
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10971
- }),
10972
- children: [
10973
- /* @__PURE__ */ jsxRuntime.jsx(
10974
- Form$2.Field,
10975
- {
10976
- control: form.control,
10977
- name: `metadata.${index}.key`,
10978
- render: ({ field: field2 }) => {
10979
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10980
- GridInput,
10981
- {
10982
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10983
- ...field2,
10984
- disabled: isDisabled,
10985
- placeholder: "Key"
10986
- }
10987
- ) }) });
10988
- }
10989
- }
10990
- ),
10991
- /* @__PURE__ */ jsxRuntime.jsx(
10992
- Form$2.Field,
10993
- {
10994
- control: form.control,
10995
- name: `metadata.${index}.value`,
10996
- render: ({ field: { value, ...field2 } }) => {
10997
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10998
- GridInput,
10999
- {
11000
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
11001
- ...field2,
11002
- value: isDisabled ? placeholder : value,
11003
- disabled: isDisabled,
11004
- placeholder: "Value"
11005
- }
11006
- ) }) });
11007
- }
11008
- }
11009
- )
11010
- ]
11011
- }
11012
- ),
11013
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
11014
- /* @__PURE__ */ jsxRuntime.jsx(
11015
- ui.DropdownMenu.Trigger,
11016
- {
11017
- className: ui.clx(
11018
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11019
- {
11020
- hidden: isDisabled
11021
- }
11022
- ),
11023
- disabled: isDisabled,
11024
- asChild: true,
11025
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
11026
- }
11027
- ),
11028
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11029
- /* @__PURE__ */ jsxRuntime.jsxs(
11030
- ui.DropdownMenu.Item,
11031
- {
11032
- className: "gap-x-2",
11033
- onClick: () => insertRow(index, "above"),
11034
- children: [
11035
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11036
- "Insert row above"
11037
- ]
11038
- }
11039
- ),
11040
- /* @__PURE__ */ jsxRuntime.jsxs(
11041
- ui.DropdownMenu.Item,
11042
- {
11043
- className: "gap-x-2",
11044
- onClick: () => insertRow(index, "below"),
11045
- children: [
11046
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11047
- "Insert row below"
11048
- ]
11049
- }
11050
- ),
11051
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11052
- /* @__PURE__ */ jsxRuntime.jsxs(
11053
- ui.DropdownMenu.Item,
11054
- {
11055
- className: "gap-x-2",
11056
- onClick: () => deleteRow(index),
11057
- children: [
11058
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11059
- "Delete row"
11060
- ]
11061
- }
11062
- )
11063
- ] })
11064
- ] })
11065
- ] })
11066
- },
11067
- field.id
11068
- );
11069
- })
11070
- ] }),
11071
- hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
10951
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10952
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
10953
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
10954
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10955
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10956
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11072
10957
  ] }),
11073
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11074
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11075
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11076
- ] }) })
11077
- ]
11078
- }
11079
- ) });
11080
- };
11081
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11082
- return /* @__PURE__ */ jsxRuntime.jsx(
11083
- "input",
11084
- {
11085
- ref,
11086
- ...props,
11087
- autoComplete: "off",
11088
- className: ui.clx(
11089
- "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
11090
- className
10958
+ /* @__PURE__ */ jsxRuntime.jsx(
10959
+ Combobox,
10960
+ {
10961
+ id: "promotion-combobox",
10962
+ "aria-describedby": "promotion-combobox-hint",
10963
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10964
+ fetchNextPage: comboboxData.fetchNextPage,
10965
+ options: comboboxData.options,
10966
+ onSearchValueChange: comboboxData.onSearchValueChange,
10967
+ searchValue: comboboxData.searchValue,
10968
+ disabled: comboboxData.disabled || isAddingPromotions,
10969
+ onChange: add,
10970
+ value: comboboxValue
10971
+ }
10972
+ )
10973
+ ] }),
10974
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10975
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
10976
+ PromotionItem,
10977
+ {
10978
+ promotion,
10979
+ orderId: preview.id,
10980
+ isLoading: isPending
10981
+ },
10982
+ promotion.id
10983
+ )) })
10984
+ ] }) }),
10985
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10986
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10987
+ /* @__PURE__ */ jsxRuntime.jsx(
10988
+ ui.Button,
10989
+ {
10990
+ size: "small",
10991
+ type: "submit",
10992
+ isLoading: isSubmitting || isAddingPromotions,
10993
+ children: "Save"
10994
+ }
11091
10995
  )
11092
- }
11093
- );
11094
- });
11095
- GridInput.displayName = "MetadataForm.GridInput";
11096
- const PlaceholderInner = () => {
11097
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11098
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11099
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11100
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11101
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11102
10996
  ] }) })
11103
10997
  ] });
11104
10998
  };
11105
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11106
- function getDefaultValues(metadata) {
11107
- if (!metadata || !Object.keys(metadata).length) {
11108
- return [
10999
+ const PromotionItem = ({
11000
+ promotion,
11001
+ orderId,
11002
+ isLoading
11003
+ }) => {
11004
+ var _a;
11005
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11006
+ const onRemove = async () => {
11007
+ removePromotions(
11109
11008
  {
11110
- key: "",
11111
- value: "",
11112
- disabled: false
11009
+ promo_codes: [promotion.code]
11010
+ },
11011
+ {
11012
+ onError: (e) => {
11013
+ ui.toast.error(e.message);
11014
+ }
11113
11015
  }
11114
- ];
11016
+ );
11017
+ };
11018
+ const displayValue = getDisplayValue(promotion);
11019
+ return /* @__PURE__ */ jsxRuntime.jsxs(
11020
+ "div",
11021
+ {
11022
+ className: ui.clx(
11023
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11024
+ {
11025
+ "animate-pulse": isLoading
11026
+ }
11027
+ ),
11028
+ children: [
11029
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11030
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11031
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11032
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11033
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11034
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11035
+ ] }),
11036
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11037
+ ] })
11038
+ ] }),
11039
+ /* @__PURE__ */ jsxRuntime.jsx(
11040
+ ui.IconButton,
11041
+ {
11042
+ size: "small",
11043
+ type: "button",
11044
+ variant: "transparent",
11045
+ onClick: onRemove,
11046
+ isLoading: isPending || isLoading,
11047
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11048
+ }
11049
+ )
11050
+ ]
11051
+ },
11052
+ promotion.id
11053
+ );
11054
+ };
11055
+ function getDisplayValue(promotion) {
11056
+ var _a, _b, _c, _d;
11057
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11058
+ if (!value) {
11059
+ return null;
11115
11060
  }
11116
- return Object.entries(metadata).map(([key, value]) => {
11117
- if (!EDITABLE_TYPES.includes(typeof value)) {
11118
- return {
11119
- key,
11120
- value,
11121
- disabled: true
11122
- };
11123
- }
11124
- let stringValue = value;
11125
- if (typeof value !== "string") {
11126
- stringValue = JSON.stringify(value);
11061
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11062
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11063
+ if (!currency) {
11064
+ return null;
11127
11065
  }
11128
- return {
11129
- key,
11130
- value: stringValue,
11131
- original_key: key
11132
- };
11133
- });
11066
+ return getLocaleAmount(value, currency);
11067
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11068
+ return formatPercentage(value);
11069
+ }
11070
+ return null;
11134
11071
  }
11135
- function parseValues(values) {
11136
- const metadata = values.metadata;
11137
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11138
- if (isEmpty) {
11139
- return null;
11072
+ const formatter = new Intl.NumberFormat([], {
11073
+ style: "percent",
11074
+ minimumFractionDigits: 2
11075
+ });
11076
+ const formatPercentage = (value, isPercentageValue = false) => {
11077
+ let val = value || 0;
11078
+ if (!isPercentageValue) {
11079
+ val = val / 100;
11140
11080
  }
11141
- const update = {};
11142
- metadata.forEach((field) => {
11143
- let key = field.key;
11144
- let value = field.value;
11145
- const disabled = field.disabled;
11146
- if (!key || !value) {
11147
- return;
11148
- }
11149
- if (disabled) {
11150
- update[key] = value;
11151
- return;
11081
+ return formatter.format(val);
11082
+ };
11083
+ function getPromotionIds(items, shippingMethods) {
11084
+ const promotionIds = /* @__PURE__ */ new Set();
11085
+ for (const item of items) {
11086
+ if (item.adjustments) {
11087
+ for (const adjustment of item.adjustments) {
11088
+ if (adjustment.promotion_id) {
11089
+ promotionIds.add(adjustment.promotion_id);
11090
+ }
11091
+ }
11152
11092
  }
11153
- key = key.trim();
11154
- value = value.trim();
11155
- if (value === "true") {
11156
- update[key] = true;
11157
- } else if (value === "false") {
11158
- update[key] = false;
11159
- } else {
11160
- const parsedNumber = parseFloat(value);
11161
- if (!isNaN(parsedNumber)) {
11162
- update[key] = parsedNumber;
11163
- } else {
11164
- update[key] = value;
11093
+ }
11094
+ for (const shippingMethod of shippingMethods) {
11095
+ if (shippingMethod.adjustments) {
11096
+ for (const adjustment of shippingMethod.adjustments) {
11097
+ if (adjustment.promotion_id) {
11098
+ promotionIds.add(adjustment.promotion_id);
11099
+ }
11165
11100
  }
11166
11101
  }
11167
- });
11168
- return update;
11169
- }
11170
- function getHasUneditableRows(metadata) {
11171
- if (!metadata) {
11172
- return false;
11173
11102
  }
11174
- return Object.values(metadata).some(
11175
- (value) => !EDITABLE_TYPES.includes(typeof value)
11176
- );
11103
+ return Array.from(promotionIds);
11177
11104
  }
11178
- const PROMOTION_QUERY_KEY = "promotions";
11179
- const promotionsQueryKeys = {
11180
- list: (query2) => [
11181
- PROMOTION_QUERY_KEY,
11182
- query2 ? query2 : void 0
11183
- ],
11184
- detail: (id, query2) => [
11185
- PROMOTION_QUERY_KEY,
11186
- id,
11187
- query2 ? query2 : void 0
11188
- ]
11189
- };
11190
- const usePromotions = (query2, options) => {
11191
- const { data, ...rest } = reactQuery.useQuery({
11192
- queryKey: promotionsQueryKeys.list(query2),
11193
- queryFn: async () => sdk.admin.promotion.list(query2),
11194
- ...options
11195
- });
11196
- return { ...data, ...rest };
11197
- };
11198
- const Promotions = () => {
11105
+ const InlineTip = React.forwardRef(
11106
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
11107
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
11108
+ return /* @__PURE__ */ jsxRuntime.jsxs(
11109
+ "div",
11110
+ {
11111
+ ref,
11112
+ className: ui.clx(
11113
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
11114
+ className
11115
+ ),
11116
+ ...props,
11117
+ children: [
11118
+ /* @__PURE__ */ jsxRuntime.jsx(
11119
+ "div",
11120
+ {
11121
+ role: "presentation",
11122
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
11123
+ "bg-ui-tag-orange-icon": variant === "warning"
11124
+ })
11125
+ }
11126
+ ),
11127
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
11128
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
11129
+ labelValue,
11130
+ ":"
11131
+ ] }),
11132
+ " ",
11133
+ children
11134
+ ] })
11135
+ ]
11136
+ }
11137
+ );
11138
+ }
11139
+ );
11140
+ InlineTip.displayName = "InlineTip";
11141
+ const MetadataFieldSchema = objectType({
11142
+ key: stringType(),
11143
+ disabled: booleanType().optional(),
11144
+ value: anyType()
11145
+ });
11146
+ const MetadataSchema = objectType({
11147
+ metadata: arrayType(MetadataFieldSchema)
11148
+ });
11149
+ const Metadata = () => {
11199
11150
  const { id } = reactRouterDom.useParams();
11200
- const {
11201
- order: preview,
11202
- isError: isPreviewError,
11203
- error: previewError
11204
- } = useOrderPreview(id, void 0);
11205
- useInitiateOrderEdit({ preview });
11206
- const { onCancel } = useCancelOrderEdit({ preview });
11207
- if (isPreviewError) {
11208
- throw previewError;
11151
+ const { order, isPending, isError, error } = useOrder(id, {
11152
+ fields: "metadata"
11153
+ });
11154
+ if (isError) {
11155
+ throw error;
11209
11156
  }
11210
- const isReady = !!preview;
11211
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11212
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11213
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11157
+ const isReady = !isPending && !!order;
11158
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11159
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11160
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
11161
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
11162
+ ] }),
11163
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
11214
11164
  ] });
11215
11165
  };
11216
- const PromotionForm = ({ preview }) => {
11217
- const { items, shipping_methods } = preview;
11218
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11219
- const [comboboxValue, setComboboxValue] = React.useState("");
11166
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
11167
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
11168
+ const MetadataForm = ({ orderId, metadata }) => {
11220
11169
  const { handleSuccess } = useRouteModal();
11221
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11222
- const promoIds = getPromotionIds(items, shipping_methods);
11223
- const { promotions, isPending, isError, error } = usePromotions(
11224
- {
11225
- id: promoIds
11226
- },
11227
- {
11228
- enabled: !!promoIds.length
11229
- }
11230
- );
11231
- const comboboxData = useComboboxData({
11232
- queryKey: ["promotions", "combobox", promoIds],
11233
- queryFn: async (params) => {
11234
- return await sdk.admin.promotion.list({
11235
- ...params,
11236
- id: {
11237
- $nin: promoIds
11238
- }
11239
- });
11170
+ const hasUneditableRows = getHasUneditableRows(metadata);
11171
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
11172
+ const form = reactHookForm.useForm({
11173
+ defaultValues: {
11174
+ metadata: getDefaultValues(metadata)
11240
11175
  },
11241
- getOptions: (data) => {
11242
- return data.promotions.map((promotion) => ({
11243
- label: promotion.code,
11244
- value: promotion.code
11245
- }));
11246
- }
11176
+ resolver: zod.zodResolver(MetadataSchema)
11247
11177
  });
11248
- const add = async (value) => {
11249
- if (!value) {
11250
- return;
11251
- }
11252
- addPromotions(
11178
+ const handleSubmit = form.handleSubmit(async (data) => {
11179
+ const parsedData = parseValues(data);
11180
+ await mutateAsync(
11253
11181
  {
11254
- promo_codes: [value]
11182
+ metadata: parsedData
11255
11183
  },
11256
11184
  {
11257
- onError: (e) => {
11258
- ui.toast.error(e.message);
11259
- comboboxData.onSearchValueChange("");
11260
- setComboboxValue("");
11261
- },
11262
11185
  onSuccess: () => {
11263
- comboboxData.onSearchValueChange("");
11264
- setComboboxValue("");
11186
+ ui.toast.success("Metadata updated");
11187
+ handleSuccess();
11188
+ },
11189
+ onError: (error) => {
11190
+ ui.toast.error(error.message);
11265
11191
  }
11266
11192
  }
11267
11193
  );
11268
- };
11269
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11270
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11271
- const onSubmit = async () => {
11272
- setIsSubmitting(true);
11273
- let requestSucceeded = false;
11274
- await requestOrderEdit(void 0, {
11275
- onError: (e) => {
11276
- ui.toast.error(e.message);
11277
- },
11278
- onSuccess: () => {
11279
- requestSucceeded = true;
11280
- }
11281
- });
11282
- if (!requestSucceeded) {
11283
- setIsSubmitting(false);
11284
- return;
11194
+ });
11195
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
11196
+ control: form.control,
11197
+ name: "metadata"
11198
+ });
11199
+ function deleteRow(index) {
11200
+ remove(index);
11201
+ if (fields.length === 1) {
11202
+ insert(0, {
11203
+ key: "",
11204
+ value: "",
11205
+ disabled: false
11206
+ });
11285
11207
  }
11286
- await confirmOrderEdit(void 0, {
11287
- onError: (e) => {
11288
- ui.toast.error(e.message);
11289
- },
11290
- onSuccess: () => {
11291
- handleSuccess();
11292
- },
11293
- onSettled: () => {
11294
- setIsSubmitting(false);
11295
- }
11208
+ }
11209
+ function insertRow(index, position) {
11210
+ insert(index + (position === "above" ? 0 : 1), {
11211
+ key: "",
11212
+ value: "",
11213
+ disabled: false
11296
11214
  });
11297
- };
11298
- if (isError) {
11299
- throw error;
11300
11215
  }
11301
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11302
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11303
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11304
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11305
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11306
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11307
- ] }),
11308
- /* @__PURE__ */ jsxRuntime.jsx(
11309
- Combobox,
11310
- {
11311
- id: "promotion-combobox",
11312
- "aria-describedby": "promotion-combobox-hint",
11313
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11314
- fetchNextPage: comboboxData.fetchNextPage,
11315
- options: comboboxData.options,
11316
- onSearchValueChange: comboboxData.onSearchValueChange,
11317
- searchValue: comboboxData.searchValue,
11318
- disabled: comboboxData.disabled || isAddingPromotions,
11319
- onChange: add,
11320
- value: comboboxValue
11321
- }
11322
- )
11323
- ] }),
11324
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11325
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11326
- PromotionItem,
11327
- {
11328
- promotion,
11329
- orderId: preview.id,
11330
- isLoading: isPending
11331
- },
11332
- promotion.id
11333
- )) })
11334
- ] }) }),
11335
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11336
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11337
- /* @__PURE__ */ jsxRuntime.jsx(
11338
- ui.Button,
11339
- {
11340
- size: "small",
11341
- type: "submit",
11342
- isLoading: isSubmitting || isAddingPromotions,
11343
- children: "Save"
11344
- }
11345
- )
11346
- ] }) })
11347
- ] });
11348
- };
11349
- const PromotionItem = ({
11350
- promotion,
11351
- orderId,
11352
- isLoading
11353
- }) => {
11354
- var _a;
11355
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11356
- const onRemove = async () => {
11357
- removePromotions(
11358
- {
11359
- promo_codes: [promotion.code]
11360
- },
11361
- {
11362
- onError: (e) => {
11363
- ui.toast.error(e.message);
11364
- }
11365
- }
11366
- );
11367
- };
11368
- const displayValue = getDisplayValue(promotion);
11369
- return /* @__PURE__ */ jsxRuntime.jsxs(
11370
- "div",
11216
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11217
+ KeyboundForm,
11371
11218
  {
11372
- className: ui.clx(
11373
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11374
- {
11375
- "animate-pulse": isLoading
11376
- }
11377
- ),
11219
+ onSubmit: handleSubmit,
11220
+ className: "flex flex-1 flex-col overflow-hidden",
11378
11221
  children: [
11379
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11380
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11381
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11382
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11383
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11384
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11222
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
11223
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
11224
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
11225
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
11226
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
11385
11227
  ] }),
11386
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11387
- ] })
11228
+ fields.map((field, index) => {
11229
+ const isDisabled = field.disabled || false;
11230
+ let placeholder = "-";
11231
+ if (typeof field.value === "object") {
11232
+ placeholder = "{ ... }";
11233
+ }
11234
+ if (Array.isArray(field.value)) {
11235
+ placeholder = "[ ... ]";
11236
+ }
11237
+ return /* @__PURE__ */ jsxRuntime.jsx(
11238
+ ConditionalTooltip,
11239
+ {
11240
+ showTooltip: isDisabled,
11241
+ content: "This row is disabled because it contains non-primitive data.",
11242
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
11243
+ /* @__PURE__ */ jsxRuntime.jsxs(
11244
+ "div",
11245
+ {
11246
+ className: ui.clx("grid grid-cols-2 divide-x", {
11247
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
11248
+ }),
11249
+ children: [
11250
+ /* @__PURE__ */ jsxRuntime.jsx(
11251
+ Form$2.Field,
11252
+ {
11253
+ control: form.control,
11254
+ name: `metadata.${index}.key`,
11255
+ render: ({ field: field2 }) => {
11256
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11257
+ GridInput,
11258
+ {
11259
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
11260
+ ...field2,
11261
+ disabled: isDisabled,
11262
+ placeholder: "Key"
11263
+ }
11264
+ ) }) });
11265
+ }
11266
+ }
11267
+ ),
11268
+ /* @__PURE__ */ jsxRuntime.jsx(
11269
+ Form$2.Field,
11270
+ {
11271
+ control: form.control,
11272
+ name: `metadata.${index}.value`,
11273
+ render: ({ field: { value, ...field2 } }) => {
11274
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11275
+ GridInput,
11276
+ {
11277
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
11278
+ ...field2,
11279
+ value: isDisabled ? placeholder : value,
11280
+ disabled: isDisabled,
11281
+ placeholder: "Value"
11282
+ }
11283
+ ) }) });
11284
+ }
11285
+ }
11286
+ )
11287
+ ]
11288
+ }
11289
+ ),
11290
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
11291
+ /* @__PURE__ */ jsxRuntime.jsx(
11292
+ ui.DropdownMenu.Trigger,
11293
+ {
11294
+ className: ui.clx(
11295
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11296
+ {
11297
+ hidden: isDisabled
11298
+ }
11299
+ ),
11300
+ disabled: isDisabled,
11301
+ asChild: true,
11302
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
11303
+ }
11304
+ ),
11305
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11306
+ /* @__PURE__ */ jsxRuntime.jsxs(
11307
+ ui.DropdownMenu.Item,
11308
+ {
11309
+ className: "gap-x-2",
11310
+ onClick: () => insertRow(index, "above"),
11311
+ children: [
11312
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11313
+ "Insert row above"
11314
+ ]
11315
+ }
11316
+ ),
11317
+ /* @__PURE__ */ jsxRuntime.jsxs(
11318
+ ui.DropdownMenu.Item,
11319
+ {
11320
+ className: "gap-x-2",
11321
+ onClick: () => insertRow(index, "below"),
11322
+ children: [
11323
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11324
+ "Insert row below"
11325
+ ]
11326
+ }
11327
+ ),
11328
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11329
+ /* @__PURE__ */ jsxRuntime.jsxs(
11330
+ ui.DropdownMenu.Item,
11331
+ {
11332
+ className: "gap-x-2",
11333
+ onClick: () => deleteRow(index),
11334
+ children: [
11335
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11336
+ "Delete row"
11337
+ ]
11338
+ }
11339
+ )
11340
+ ] })
11341
+ ] })
11342
+ ] })
11343
+ },
11344
+ field.id
11345
+ );
11346
+ })
11347
+ ] }),
11348
+ hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
11388
11349
  ] }),
11389
- /* @__PURE__ */ jsxRuntime.jsx(
11390
- ui.IconButton,
11391
- {
11392
- size: "small",
11393
- type: "button",
11394
- variant: "transparent",
11395
- onClick: onRemove,
11396
- isLoading: isPending || isLoading,
11397
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11398
- }
11399
- )
11350
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11351
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11352
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11353
+ ] }) })
11400
11354
  ]
11401
- },
11402
- promotion.id
11403
- );
11404
- };
11405
- function getDisplayValue(promotion) {
11406
- var _a, _b, _c, _d;
11407
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11408
- if (!value) {
11409
- return null;
11410
- }
11411
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11412
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11413
- if (!currency) {
11414
- return null;
11415
11355
  }
11416
- return getLocaleAmount(value, currency);
11417
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11418
- return formatPercentage(value);
11419
- }
11420
- return null;
11421
- }
11422
- const formatter = new Intl.NumberFormat([], {
11423
- style: "percent",
11424
- minimumFractionDigits: 2
11425
- });
11426
- const formatPercentage = (value, isPercentageValue = false) => {
11427
- let val = value || 0;
11428
- if (!isPercentageValue) {
11429
- val = val / 100;
11430
- }
11431
- return formatter.format(val);
11356
+ ) });
11432
11357
  };
11433
- function getPromotionIds(items, shippingMethods) {
11434
- const promotionIds = /* @__PURE__ */ new Set();
11435
- for (const item of items) {
11436
- if (item.adjustments) {
11437
- for (const adjustment of item.adjustments) {
11438
- if (adjustment.promotion_id) {
11439
- promotionIds.add(adjustment.promotion_id);
11440
- }
11358
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11359
+ return /* @__PURE__ */ jsxRuntime.jsx(
11360
+ "input",
11361
+ {
11362
+ ref,
11363
+ ...props,
11364
+ autoComplete: "off",
11365
+ className: ui.clx(
11366
+ "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
11367
+ className
11368
+ )
11369
+ }
11370
+ );
11371
+ });
11372
+ GridInput.displayName = "MetadataForm.GridInput";
11373
+ const PlaceholderInner = () => {
11374
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11375
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11376
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11377
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11378
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11379
+ ] }) })
11380
+ ] });
11381
+ };
11382
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
11383
+ function getDefaultValues(metadata) {
11384
+ if (!metadata || !Object.keys(metadata).length) {
11385
+ return [
11386
+ {
11387
+ key: "",
11388
+ value: "",
11389
+ disabled: false
11441
11390
  }
11391
+ ];
11392
+ }
11393
+ return Object.entries(metadata).map(([key, value]) => {
11394
+ if (!EDITABLE_TYPES.includes(typeof value)) {
11395
+ return {
11396
+ key,
11397
+ value,
11398
+ disabled: true
11399
+ };
11442
11400
  }
11401
+ let stringValue = value;
11402
+ if (typeof value !== "string") {
11403
+ stringValue = JSON.stringify(value);
11404
+ }
11405
+ return {
11406
+ key,
11407
+ value: stringValue,
11408
+ original_key: key
11409
+ };
11410
+ });
11411
+ }
11412
+ function parseValues(values) {
11413
+ const metadata = values.metadata;
11414
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11415
+ if (isEmpty) {
11416
+ return null;
11443
11417
  }
11444
- for (const shippingMethod of shippingMethods) {
11445
- if (shippingMethod.adjustments) {
11446
- for (const adjustment of shippingMethod.adjustments) {
11447
- if (adjustment.promotion_id) {
11448
- promotionIds.add(adjustment.promotion_id);
11449
- }
11418
+ const update = {};
11419
+ metadata.forEach((field) => {
11420
+ let key = field.key;
11421
+ let value = field.value;
11422
+ const disabled = field.disabled;
11423
+ if (!key || !value) {
11424
+ return;
11425
+ }
11426
+ if (disabled) {
11427
+ update[key] = value;
11428
+ return;
11429
+ }
11430
+ key = key.trim();
11431
+ value = value.trim();
11432
+ if (value === "true") {
11433
+ update[key] = true;
11434
+ } else if (value === "false") {
11435
+ update[key] = false;
11436
+ } else {
11437
+ const parsedNumber = parseFloat(value);
11438
+ if (!isNaN(parsedNumber)) {
11439
+ update[key] = parsedNumber;
11440
+ } else {
11441
+ update[key] = value;
11450
11442
  }
11451
11443
  }
11444
+ });
11445
+ return update;
11446
+ }
11447
+ function getHasUneditableRows(metadata) {
11448
+ if (!metadata) {
11449
+ return false;
11452
11450
  }
11453
- return Array.from(promotionIds);
11451
+ return Object.values(metadata).some(
11452
+ (value) => !EDITABLE_TYPES.includes(typeof value)
11453
+ );
11454
11454
  }
11455
11455
  const SalesChannel = () => {
11456
11456
  const { id } = reactRouterDom.useParams();
@@ -11558,1151 +11558,141 @@ const SalesChannelField = ({ control, order }) => {
11558
11558
  const schema$2 = objectType({
11559
11559
  sales_channel_id: stringType().min(1)
11560
11560
  });
11561
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11562
- const Shipping = () => {
11563
- var _a;
11561
+ const TransferOwnership = () => {
11564
11562
  const { id } = reactRouterDom.useParams();
11565
- const { order, isPending, isError, error } = useOrder(id, {
11566
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11563
+ const { draft_order, isPending, isError, error } = useDraftOrder(id, {
11564
+ fields: "id,customer_id,customer.*"
11567
11565
  });
11568
- const {
11569
- order: preview,
11570
- isPending: isPreviewPending,
11571
- isError: isPreviewError,
11572
- error: previewError
11573
- } = useOrderPreview(id);
11574
- useInitiateOrderEdit({ preview });
11575
- const { onCancel } = useCancelOrderEdit({ preview });
11576
11566
  if (isError) {
11577
11567
  throw error;
11578
11568
  }
11579
- if (isPreviewError) {
11580
- throw previewError;
11581
- }
11582
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11583
- const isReady = preview && !isPreviewPending && order && !isPending;
11584
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11585
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11586
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
11587
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11588
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11589
- ] }) }) }),
11590
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11591
- ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11592
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11593
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11594
- ] }) });
11569
+ const isReady = !isPending && !!draft_order;
11570
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11571
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11572
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Transfer Ownership" }) }),
11573
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
11574
+ ] }),
11575
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(TransferOwnershipForm, { order: draft_order })
11576
+ ] });
11595
11577
  };
11596
- const ShippingForm = ({ preview, order }) => {
11597
- var _a;
11598
- const { setIsOpen } = useStackedModal();
11599
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11600
- const [data, setData] = React.useState(null);
11601
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11602
- const { shipping_options } = useShippingOptions(
11603
- {
11604
- id: appliedShippingOptionIds,
11605
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11578
+ const TransferOwnershipForm = ({ order }) => {
11579
+ var _a, _b;
11580
+ const form = reactHookForm.useForm({
11581
+ defaultValues: {
11582
+ customer_id: order.customer_id || ""
11606
11583
  },
11607
- {
11608
- enabled: appliedShippingOptionIds.length > 0
11609
- }
11610
- );
11611
- const uniqueShippingProfiles = React.useMemo(() => {
11612
- const profiles = /* @__PURE__ */ new Map();
11613
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11614
- profiles.set(profile.id, profile);
11615
- });
11616
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11617
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11618
- });
11619
- return Array.from(profiles.values());
11620
- }, [order.items, shipping_options]);
11584
+ resolver: zod.zodResolver(schema$1)
11585
+ });
11586
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11621
11587
  const { handleSuccess } = useRouteModal();
11622
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11623
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11624
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11625
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11626
- const onSubmit = async () => {
11627
- setIsSubmitting(true);
11628
- let requestSucceeded = false;
11629
- await requestOrderEdit(void 0, {
11630
- onError: (e) => {
11631
- ui.toast.error(`Failed to request order edit: ${e.message}`);
11632
- },
11633
- onSuccess: () => {
11634
- requestSucceeded = true;
11635
- }
11636
- });
11637
- if (!requestSucceeded) {
11638
- setIsSubmitting(false);
11639
- return;
11640
- }
11641
- await confirmOrderEdit(void 0, {
11642
- onError: (e) => {
11643
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
11644
- },
11645
- onSuccess: () => {
11646
- handleSuccess();
11647
- },
11648
- onSettled: () => {
11649
- setIsSubmitting(false);
11650
- }
11651
- });
11652
- };
11653
- const onKeydown = React.useCallback(
11654
- (e) => {
11655
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11656
- if (data || isSubmitting) {
11657
- return;
11588
+ const name = [(_a = order.customer) == null ? void 0 : _a.first_name, (_b = order.customer) == null ? void 0 : _b.last_name].filter(Boolean).join(" ");
11589
+ const currentCustomer = order.customer ? {
11590
+ label: name ? `${name} (${order.customer.email})` : order.customer.email,
11591
+ value: order.customer.id
11592
+ } : null;
11593
+ const onSubmit = form.handleSubmit(async (data) => {
11594
+ await mutateAsync(
11595
+ { customer_id: data.customer_id },
11596
+ {
11597
+ onSuccess: () => {
11598
+ ui.toast.success("Customer updated");
11599
+ handleSuccess();
11600
+ },
11601
+ onError: (error) => {
11602
+ ui.toast.error(error.message);
11658
11603
  }
11659
- onSubmit();
11660
11604
  }
11661
- },
11662
- [data, isSubmitting, onSubmit]
11663
- );
11664
- React.useEffect(() => {
11665
- document.addEventListener("keydown", onKeydown);
11666
- return () => {
11667
- document.removeEventListener("keydown", onKeydown);
11668
- };
11669
- }, [onKeydown]);
11670
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11671
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11672
- /* @__PURE__ */ jsxRuntime.jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11673
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
11674
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11675
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11676
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
11677
- ] }),
11678
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11679
- /* @__PURE__ */ jsxRuntime.jsx(radixUi.Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
11680
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between px-4 py-2", children: [
11681
- /* @__PURE__ */ jsxRuntime.jsx(
11682
- ui.Text,
11683
- {
11684
- size: "xsmall",
11685
- weight: "plus",
11686
- className: "text-ui-fg-muted",
11687
- children: "Shipping profile"
11688
- }
11689
- ),
11690
- /* @__PURE__ */ jsxRuntime.jsx(
11691
- ui.Text,
11692
- {
11693
- size: "xsmall",
11694
- weight: "plus",
11695
- className: "text-ui-fg-muted",
11696
- children: "Action"
11697
- }
11698
- )
11605
+ );
11606
+ });
11607
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11608
+ KeyboundForm,
11609
+ {
11610
+ className: "flex flex-1 flex-col overflow-hidden",
11611
+ onSubmit,
11612
+ children: [
11613
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: [
11614
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center bg-ui-bg-component rounded-md border", children: /* @__PURE__ */ jsxRuntime.jsx(Illustration, {}) }),
11615
+ currentCustomer && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col space-y-3", children: [
11616
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11617
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "current-customer", children: "Current owner" }),
11618
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { children: "The customer that is currently associated with this draft order." })
11619
+ ] }),
11620
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Select, { disabled: true, value: currentCustomer.value, children: [
11621
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { id: "current-customer", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, {}) }),
11622
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: currentCustomer.value, children: currentCustomer.label }) })
11623
+ ] })
11699
11624
  ] }),
11700
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
11701
- var _a2, _b, _c, _d, _e, _f, _g;
11702
- const items = getItemsWithShippingProfile(
11703
- profile.id,
11704
- order.items
11705
- );
11706
- const hasItems = items.length > 0;
11707
- const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
11708
- (option) => option.shipping_profile_id === profile.id
11709
- );
11710
- const shippingMethod = preview.shipping_methods.find(
11711
- (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
11712
- );
11713
- const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
11714
- (action) => action.action === "SHIPPING_ADD"
11715
- );
11716
- return /* @__PURE__ */ jsxRuntime.jsxs(
11717
- radixUi.Accordion.Item,
11718
- {
11719
- value: profile.id,
11720
- className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
11721
- children: [
11722
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between gap-3 px-3 py-2", children: [
11723
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3 overflow-hidden", children: [
11724
- /* @__PURE__ */ jsxRuntime.jsx(radixUi.Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(
11725
- ui.IconButton,
11726
- {
11727
- size: "2xsmall",
11728
- variant: "transparent",
11729
- className: "group/trigger",
11730
- disabled: !hasItems,
11731
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.TriangleRightMini, { className: "transition-transform group-data-[state=open]/trigger:rotate-90" })
11732
- }
11733
- ) }),
11734
- !shippingOption ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
11735
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "shadow-borders-base flex size-7 items-center justify-center rounded-md", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "bg-ui-bg-component-hover flex size-6 items-center justify-center rounded", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Shopping, { className: "text-ui-fg-subtle" }) }) }),
11736
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col", children: [
11737
- /* @__PURE__ */ jsxRuntime.jsx(
11738
- ui.Text,
11739
- {
11740
- size: "small",
11741
- weight: "plus",
11742
- leading: "compact",
11743
- children: profile.name
11744
- }
11745
- ),
11746
- /* @__PURE__ */ jsxRuntime.jsxs(
11747
- ui.Text,
11748
- {
11749
- size: "small",
11750
- leading: "compact",
11751
- className: "text-ui-fg-subtle",
11752
- children: [
11753
- items.length,
11754
- " ",
11755
- pluralize(items.length, "items", "item")
11756
- ]
11757
- }
11758
- )
11759
- ] })
11760
- ] }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full flex-1 items-center gap-[5px] overflow-hidden max-sm:flex-col max-sm:items-start", children: [
11761
- /* @__PURE__ */ jsxRuntime.jsx(
11762
- ui.Tooltip,
11763
- {
11764
- content: /* @__PURE__ */ jsxRuntime.jsx("ul", { children: items.map((item) => {
11765
- var _a3, _b2, _c2;
11766
- return /* @__PURE__ */ jsxRuntime.jsx(
11767
- "li",
11768
- {
11769
- children: `${item.quantity}x ${(_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title} (${(_c2 = item.variant) == null ? void 0 : _c2.title})`
11770
- },
11771
- item.id
11772
- );
11773
- }) }),
11774
- children: /* @__PURE__ */ jsxRuntime.jsxs(
11775
- ui.Badge,
11776
- {
11777
- className: "flex cursor-default items-center gap-x-[3px] overflow-hidden",
11778
- size: "xsmall",
11779
- children: [
11780
- /* @__PURE__ */ jsxRuntime.jsx(icons.Shopping, { className: "shrink-0" }),
11781
- /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "truncate", children: [
11782
- items.reduce(
11783
- (acc, item) => acc + item.quantity,
11784
- 0
11785
- ),
11786
- "x",
11787
- " ",
11788
- pluralize(items.length, "items", "item")
11789
- ] })
11790
- ]
11791
- }
11792
- )
11793
- }
11794
- ),
11795
- /* @__PURE__ */ jsxRuntime.jsx(
11796
- ui.Tooltip,
11797
- {
11798
- content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
11799
- children: /* @__PURE__ */ jsxRuntime.jsxs(
11800
- ui.Badge,
11801
- {
11802
- className: "flex cursor-default items-center gap-x-[3px] overflow-hidden",
11803
- size: "xsmall",
11804
- children: [
11805
- /* @__PURE__ */ jsxRuntime.jsx(icons.Buildings, { className: "shrink-0" }),
11806
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "truncate", children: (_g = (_f = (_e = shippingOption.service_zone) == null ? void 0 : _e.fulfillment_set) == null ? void 0 : _f.location) == null ? void 0 : _g.name })
11807
- ]
11808
- }
11809
- )
11810
- }
11811
- ),
11812
- /* @__PURE__ */ jsxRuntime.jsx(ui.Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxRuntime.jsxs(
11813
- ui.Badge,
11814
- {
11815
- className: "flex cursor-default items-center gap-x-[3px] overflow-hidden",
11816
- size: "xsmall",
11817
- children: [
11818
- /* @__PURE__ */ jsxRuntime.jsx(icons.TruckFast, { className: "shrink-0" }),
11819
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "truncate", children: shippingOption.name })
11820
- ]
11821
- }
11822
- ) })
11823
- ] })
11824
- ] }),
11825
- shippingOption ? /* @__PURE__ */ jsxRuntime.jsx(
11826
- ActionMenu,
11827
- {
11828
- groups: [
11829
- {
11830
- actions: [
11831
- hasItems ? {
11832
- label: "Edit shipping option",
11833
- icon: /* @__PURE__ */ jsxRuntime.jsx(icons.Channels, {}),
11834
- onClick: () => {
11835
- setIsOpen(
11836
- STACKED_FOCUS_MODAL_ID,
11837
- true
11838
- );
11839
- setData({
11840
- shippingProfileId: profile.id,
11841
- shippingOption,
11842
- shippingMethod
11843
- });
11844
- }
11845
- } : void 0,
11846
- {
11847
- label: "Remove shipping option",
11848
- icon: /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, {}),
11849
- onClick: () => {
11850
- if (shippingMethod) {
11851
- if (addShippingMethodAction) {
11852
- removeActionShippingMethod(
11853
- addShippingMethodAction.id
11854
- );
11855
- } else {
11856
- removeShippingMethod(
11857
- shippingMethod.id
11858
- );
11859
- }
11860
- }
11861
- }
11862
- }
11863
- ].filter(Boolean)
11864
- }
11865
- ]
11866
- }
11867
- ) : /* @__PURE__ */ jsxRuntime.jsx(
11868
- StackedModalTrigger,
11869
- {
11870
- shippingProfileId: profile.id,
11871
- shippingOption,
11872
- shippingMethod,
11873
- setData,
11874
- children: "Add shipping option"
11875
- }
11876
- )
11877
- ] }),
11878
- /* @__PURE__ */ jsxRuntime.jsxs(radixUi.Accordion.Content, { children: [
11879
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11880
- items.map((item, idx) => {
11881
- var _a3, _b2, _c2, _d2, _e2;
11882
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11883
- /* @__PURE__ */ jsxRuntime.jsxs(
11884
- "div",
11885
- {
11886
- className: "flex items-center gap-x-3 px-3",
11887
- children: [
11888
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex h-[56px] w-5 flex-col items-center justify-center", children: /* @__PURE__ */ jsxRuntime.jsx(
11889
- ui.Divider,
11890
- {
11891
- variant: "dashed",
11892
- orientation: "vertical"
11893
- }
11894
- ) }),
11895
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3 py-2", children: [
11896
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex size-7 items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxRuntime.jsxs(
11897
- ui.Text,
11898
- {
11899
- size: "small",
11900
- leading: "compact",
11901
- className: "text-ui-fg-subtle",
11902
- children: [
11903
- item.quantity,
11904
- "x"
11905
- ]
11906
- }
11907
- ) }),
11908
- /* @__PURE__ */ jsxRuntime.jsx(Thumbnail, { thumbnail: item.thumbnail }),
11909
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11910
- /* @__PURE__ */ jsxRuntime.jsxs(
11911
- ui.Text,
11912
- {
11913
- size: "small",
11914
- leading: "compact",
11915
- weight: "plus",
11916
- children: [
11917
- (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
11918
- " (",
11919
- (_c2 = item.variant) == null ? void 0 : _c2.title,
11920
- ")"
11921
- ]
11922
- }
11923
- ),
11924
- /* @__PURE__ */ jsxRuntime.jsx(
11925
- ui.Text,
11926
- {
11927
- size: "small",
11928
- leading: "compact",
11929
- className: "text-ui-fg-subtle",
11930
- children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
11931
- }
11932
- )
11933
- ] })
11934
- ] })
11935
- ]
11936
- },
11937
- item.id
11938
- ),
11939
- idx !== items.length - 1 && /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" })
11940
- ] }, item.id);
11941
- })
11942
- ] })
11943
- ]
11944
- },
11945
- profile.id
11946
- );
11947
- }) })
11948
- ] }) })
11949
- ] }) }),
11950
- /* @__PURE__ */ jsxRuntime.jsx(
11951
- StackedFocusModal,
11952
- {
11953
- id: STACKED_FOCUS_MODAL_ID,
11954
- onOpenChangeCallback: (open) => {
11955
- if (!open) {
11956
- setData(null);
11957
- }
11958
- return open;
11959
- },
11960
- children: data && /* @__PURE__ */ jsxRuntime.jsx(ShippingProfileForm, { data, order, preview })
11961
- }
11962
- )
11963
- ] }),
11964
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-x-2", children: [
11965
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11966
- /* @__PURE__ */ jsxRuntime.jsx(
11967
- ui.Button,
11968
- {
11969
- size: "small",
11970
- type: "button",
11971
- isLoading: isSubmitting,
11972
- onClick: onSubmit,
11973
- children: "Save"
11974
- }
11975
- )
11976
- ] }) })
11977
- ] });
11978
- };
11979
- const StackedModalTrigger = ({
11980
- shippingProfileId,
11981
- shippingOption,
11982
- shippingMethod,
11983
- setData,
11984
- children
11985
- }) => {
11986
- const { setIsOpen, getIsOpen } = useStackedModal();
11987
- const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
11988
- const onToggle = () => {
11989
- if (isOpen) {
11990
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11991
- setData(null);
11992
- } else {
11993
- setIsOpen(STACKED_FOCUS_MODAL_ID, true);
11994
- setData({
11995
- shippingProfileId,
11996
- shippingOption,
11997
- shippingMethod
11998
- });
11999
- }
12000
- };
12001
- return /* @__PURE__ */ jsxRuntime.jsx(
12002
- ui.Button,
12003
- {
12004
- size: "small",
12005
- variant: "secondary",
12006
- onClick: onToggle,
12007
- className: "text-ui-fg-primary shrink-0",
12008
- children
12009
- }
12010
- );
12011
- };
12012
- const ShippingProfileForm = ({
12013
- data,
12014
- order,
12015
- preview
12016
- }) => {
12017
- var _a, _b, _c, _d, _e, _f;
12018
- const { setIsOpen } = useStackedModal();
12019
- const form = reactHookForm.useForm({
12020
- resolver: zod.zodResolver(shippingMethodSchema),
12021
- defaultValues: {
12022
- location_id: (_d = (_c = (_b = (_a = data.shippingOption) == null ? void 0 : _a.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.id,
12023
- shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
12024
- custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
12025
- }
12026
- });
12027
- const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
12028
- const {
12029
- mutateAsync: updateShippingMethod,
12030
- isPending: isUpdatingShippingMethod
12031
- } = useDraftOrderUpdateShippingMethod(order.id);
12032
- const onSubmit = form.handleSubmit(async (values) => {
12033
- if (isEqual__default.default(values, form.formState.defaultValues)) {
12034
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12035
- return;
12036
- }
12037
- if (data.shippingMethod) {
12038
- await updateShippingMethod(
12039
- {
12040
- method_id: data.shippingMethod.id,
12041
- shipping_option_id: values.shipping_option_id,
12042
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12043
- },
12044
- {
12045
- onError: (e) => {
12046
- ui.toast.error(e.message);
12047
- },
12048
- onSuccess: () => {
12049
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12050
- }
12051
- }
12052
- );
12053
- return;
12054
- }
12055
- await addShippingMethod(
12056
- {
12057
- shipping_option_id: values.shipping_option_id,
12058
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12059
- },
12060
- {
12061
- onError: (e) => {
12062
- ui.toast.error(e.message);
12063
- },
12064
- onSuccess: () => {
12065
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12066
- }
12067
- }
12068
- );
12069
- });
12070
- return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12071
- KeyboundForm,
12072
- {
12073
- className: "flex h-full flex-col overflow-hidden",
12074
- onSubmit,
12075
- children: [
12076
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
12077
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
12078
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12079
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
12080
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a shipping method for the selected shipping profile. You can see the items that will be shipped using this method in the preview below." }) })
12081
- ] }),
12082
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12083
- /* @__PURE__ */ jsxRuntime.jsx(
12084
- LocationField,
12085
- {
12086
- control: form.control,
12087
- setValue: form.setValue
12088
- }
12089
- ),
12090
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12091
- /* @__PURE__ */ jsxRuntime.jsx(
12092
- ShippingOptionField,
12093
- {
12094
- shippingProfileId: data.shippingProfileId,
12095
- preview,
12096
- control: form.control
12097
- }
12098
- ),
12099
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12100
11625
  /* @__PURE__ */ jsxRuntime.jsx(
12101
- CustomAmountField,
11626
+ CustomerField,
12102
11627
  {
12103
11628
  control: form.control,
12104
- currencyCode: order.currency_code
12105
- }
12106
- ),
12107
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12108
- /* @__PURE__ */ jsxRuntime.jsx(
12109
- ItemsPreview,
12110
- {
12111
- order,
12112
- shippingProfileId: data.shippingProfileId
12113
- }
12114
- )
12115
- ] }) }) }),
12116
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-x-2", children: [
12117
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12118
- /* @__PURE__ */ jsxRuntime.jsx(
12119
- ui.Button,
12120
- {
12121
- size: "small",
12122
- type: "submit",
12123
- isLoading: isPending || isUpdatingShippingMethod,
12124
- children: data.shippingMethod ? "Update" : "Add"
11629
+ currentCustomerId: order.customer_id
12125
11630
  }
12126
11631
  )
11632
+ ] }),
11633
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11634
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "secondary", size: "small", children: "Cancel" }) }),
11635
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12127
11636
  ] }) })
12128
11637
  ]
12129
11638
  }
12130
- ) }) });
11639
+ ) });
12131
11640
  };
12132
- const shippingMethodSchema = objectType({
12133
- location_id: stringType(),
12134
- shipping_option_id: stringType(),
12135
- custom_amount: unionType([numberType(), stringType()]).optional()
12136
- });
12137
- const ItemsPreview = ({ order, shippingProfileId }) => {
12138
- const matches = order.items.filter(
12139
- (item) => {
12140
- var _a, _b, _c;
12141
- return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
12142
- }
12143
- );
12144
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
12145
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12146
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12147
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12148
- ] }) }),
12149
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12150
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-muted grid grid-cols-2 gap-3 px-4 py-2", children: [
12151
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
12152
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) })
12153
- ] }),
12154
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsxs(
12155
- "div",
12156
- {
12157
- className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-2 items-center gap-3 rounded-lg px-4 py-2",
12158
- children: [
12159
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
12160
- /* @__PURE__ */ jsxRuntime.jsx(
12161
- Thumbnail,
12162
- {
12163
- thumbnail: item.thumbnail,
12164
- alt: item.product_title ?? void 0
12165
- }
12166
- ),
12167
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12168
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
12169
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12170
- /* @__PURE__ */ jsxRuntime.jsxs(
12171
- ui.Text,
12172
- {
12173
- size: "small",
12174
- leading: "compact",
12175
- className: "text-ui-fg-subtle",
12176
- children: [
12177
- "(",
12178
- item.variant_title,
12179
- ")"
12180
- ]
12181
- }
12182
- )
12183
- ] }),
12184
- /* @__PURE__ */ jsxRuntime.jsx(
12185
- ui.Text,
12186
- {
12187
- size: "small",
12188
- leading: "compact",
12189
- className: "text-ui-fg-subtle",
12190
- children: item.variant_sku
12191
- }
12192
- )
12193
- ] })
12194
- ] }),
12195
- /* @__PURE__ */ jsxRuntime.jsxs(
12196
- ui.Text,
12197
- {
12198
- size: "small",
12199
- leading: "compact",
12200
- className: "text-ui-fg-subtle",
12201
- children: [
12202
- item.quantity,
12203
- "x"
12204
- ]
12205
- }
12206
- )
12207
- ]
12208
- },
12209
- item.id
12210
- )) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12211
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12212
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
12213
- 'No items found for "',
12214
- query,
12215
- '".'
12216
- ] })
12217
- ] }) })
12218
- ] })
12219
- ] });
12220
- };
12221
- const LocationField = ({ control, setValue }) => {
12222
- const locations = useComboboxData({
12223
- queryKey: ["locations"],
12224
- queryFn: async (params) => {
12225
- return await sdk.admin.stockLocation.list(params);
12226
- },
12227
- getOptions: (data) => {
12228
- return data.stock_locations.map((location) => ({
12229
- label: location.name,
12230
- value: location.id
12231
- }));
12232
- }
12233
- });
12234
- return /* @__PURE__ */ jsxRuntime.jsx(
12235
- Form$2.Field,
12236
- {
12237
- control,
12238
- name: "location_id",
12239
- render: ({ field: { onChange, ...field } }) => {
12240
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12241
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12242
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Location" }),
12243
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12244
- ] }),
12245
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12246
- Combobox,
12247
- {
12248
- options: locations.options,
12249
- fetchNextPage: locations.fetchNextPage,
12250
- isFetchingNextPage: locations.isFetchingNextPage,
12251
- searchValue: locations.searchValue,
12252
- onSearchValueChange: locations.onSearchValueChange,
12253
- placeholder: "Select location",
12254
- onChange: (value) => {
12255
- setValue("shipping_option_id", "", {
12256
- shouldDirty: true,
12257
- shouldTouch: true
12258
- });
12259
- onChange(value);
12260
- },
12261
- ...field
12262
- }
12263
- ) })
12264
- ] }) });
12265
- }
12266
- }
12267
- );
12268
- };
12269
- const ShippingOptionField = ({
12270
- shippingProfileId,
12271
- preview,
12272
- control
12273
- }) => {
12274
- var _a;
12275
- const locationId = reactHookForm.useWatch({ control, name: "location_id" });
12276
- const shippingOptions = useComboboxData({
12277
- queryKey: ["shipping_options", locationId, shippingProfileId],
12278
- queryFn: async (params) => {
12279
- return await sdk.admin.shippingOption.list({
12280
- ...params,
12281
- stock_location_id: locationId,
12282
- shipping_profile_id: shippingProfileId
12283
- });
12284
- },
12285
- getOptions: (data) => {
12286
- return data.shipping_options.map((option) => {
12287
- var _a2;
12288
- if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12289
- (r) => r.attribute === "is_return" && r.value === "true"
12290
- )) {
12291
- return void 0;
12292
- }
12293
- return {
12294
- label: option.name,
12295
- value: option.id
12296
- };
12297
- }).filter(Boolean);
12298
- },
12299
- enabled: !!locationId && !!shippingProfileId,
12300
- defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12301
- });
12302
- const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12303
- return /* @__PURE__ */ jsxRuntime.jsx(
12304
- Form$2.Field,
12305
- {
12306
- control,
12307
- name: "shipping_option_id",
12308
- render: ({ field }) => {
12309
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12310
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12311
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Shipping option" }),
12312
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12313
- ] }),
12314
- /* @__PURE__ */ jsxRuntime.jsx(
12315
- ConditionalTooltip,
12316
- {
12317
- content: tooltipContent,
12318
- showTooltip: !locationId || !shippingProfileId,
12319
- children: /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12320
- Combobox,
12321
- {
12322
- options: shippingOptions.options,
12323
- fetchNextPage: shippingOptions.fetchNextPage,
12324
- isFetchingNextPage: shippingOptions.isFetchingNextPage,
12325
- searchValue: shippingOptions.searchValue,
12326
- onSearchValueChange: shippingOptions.onSearchValueChange,
12327
- placeholder: "Select shipping option",
12328
- ...field,
12329
- disabled: !locationId || !shippingProfileId
12330
- }
12331
- ) }) })
12332
- }
12333
- )
12334
- ] }) });
12335
- }
11641
+ const CustomerField = ({ control, currentCustomerId }) => {
11642
+ const customers = useComboboxData({
11643
+ queryFn: async (params) => {
11644
+ return await sdk.admin.customer.list({
11645
+ ...params,
11646
+ id: currentCustomerId ? { $nin: [currentCustomerId] } : void 0
11647
+ });
11648
+ },
11649
+ queryKey: ["customers"],
11650
+ getOptions: (data) => {
11651
+ return data.customers.map((customer) => {
11652
+ const name = [customer.first_name, customer.last_name].filter(Boolean).join(" ");
11653
+ return {
11654
+ label: name ? `${name} (${customer.email})` : customer.email,
11655
+ value: customer.id
11656
+ };
11657
+ });
11658
+ }
11659
+ });
11660
+ return /* @__PURE__ */ jsxRuntime.jsx(
11661
+ Form$2.Field,
11662
+ {
11663
+ name: "customer_id",
11664
+ control,
11665
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { className: "space-y-3", children: [
11666
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11667
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "New customer" }),
11668
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "The customer to transfer this draft order to." })
11669
+ ] }),
11670
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11671
+ Combobox,
11672
+ {
11673
+ options: customers.options,
11674
+ fetchNextPage: customers.fetchNextPage,
11675
+ isFetchingNextPage: customers.isFetchingNextPage,
11676
+ searchValue: customers.searchValue,
11677
+ onSearchValueChange: customers.onSearchValueChange,
11678
+ placeholder: "Select customer",
11679
+ ...field
11680
+ }
11681
+ ) }),
11682
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11683
+ ] })
12336
11684
  }
12337
11685
  );
12338
11686
  };
12339
- const CustomAmountField = ({
12340
- control,
12341
- currencyCode
12342
- }) => {
12343
- return /* @__PURE__ */ jsxRuntime.jsx(
12344
- Form$2.Field,
11687
+ const Illustration = () => {
11688
+ return /* @__PURE__ */ jsxRuntime.jsxs(
11689
+ "svg",
12345
11690
  {
12346
- control,
12347
- name: "custom_amount",
12348
- render: ({ field: { onChange, ...field } }) => {
12349
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12350
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12351
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12352
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12353
- ] }),
12354
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12355
- ui.CurrencyInput,
12356
- {
12357
- ...field,
12358
- onValueChange: (value) => onChange(value),
12359
- symbol: getNativeSymbol(currencyCode),
12360
- code: currencyCode
12361
- }
12362
- ) })
12363
- ] });
12364
- }
12365
- }
12366
- );
12367
- };
12368
- const ShippingAddress = () => {
12369
- const { id } = reactRouterDom.useParams();
12370
- const { order, isPending, isError, error } = useOrder(id, {
12371
- fields: "+shipping_address"
12372
- });
12373
- if (isError) {
12374
- throw error;
12375
- }
12376
- const isReady = !isPending && !!order;
12377
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12378
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12379
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Shipping Address" }) }),
12380
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12381
- ] }),
12382
- isReady && /* @__PURE__ */ jsxRuntime.jsx(ShippingAddressForm, { order })
12383
- ] });
12384
- };
12385
- const ShippingAddressForm = ({ order }) => {
12386
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12387
- const form = reactHookForm.useForm({
12388
- defaultValues: {
12389
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12390
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12391
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12392
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12393
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12394
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12395
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12396
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12397
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12398
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12399
- },
12400
- resolver: zod.zodResolver(schema$1)
12401
- });
12402
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12403
- const { handleSuccess } = useRouteModal();
12404
- const onSubmit = form.handleSubmit(async (data) => {
12405
- await mutateAsync(
12406
- {
12407
- shipping_address: {
12408
- first_name: data.first_name,
12409
- last_name: data.last_name,
12410
- company: data.company,
12411
- address_1: data.address_1,
12412
- address_2: data.address_2,
12413
- city: data.city,
12414
- province: data.province,
12415
- country_code: data.country_code,
12416
- postal_code: data.postal_code,
12417
- phone: data.phone
12418
- }
12419
- },
12420
- {
12421
- onSuccess: () => {
12422
- handleSuccess();
12423
- },
12424
- onError: (error) => {
12425
- ui.toast.error(error.message);
12426
- }
12427
- }
12428
- );
12429
- });
12430
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12431
- KeyboundForm,
12432
- {
12433
- className: "flex flex-1 flex-col overflow-hidden",
12434
- onSubmit,
12435
- children: [
12436
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
12437
- /* @__PURE__ */ jsxRuntime.jsx(
12438
- Form$2.Field,
12439
- {
12440
- control: form.control,
12441
- name: "country_code",
12442
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12443
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
12444
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
12445
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12446
- ] })
12447
- }
12448
- ),
12449
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12450
- /* @__PURE__ */ jsxRuntime.jsx(
12451
- Form$2.Field,
12452
- {
12453
- control: form.control,
12454
- name: "first_name",
12455
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12456
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
12457
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12458
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12459
- ] })
12460
- }
12461
- ),
12462
- /* @__PURE__ */ jsxRuntime.jsx(
12463
- Form$2.Field,
12464
- {
12465
- control: form.control,
12466
- name: "last_name",
12467
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12468
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
12469
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12470
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12471
- ] })
12472
- }
12473
- )
12474
- ] }),
12475
- /* @__PURE__ */ jsxRuntime.jsx(
12476
- Form$2.Field,
12477
- {
12478
- control: form.control,
12479
- name: "company",
12480
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12481
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
12482
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12483
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12484
- ] })
12485
- }
12486
- ),
12487
- /* @__PURE__ */ jsxRuntime.jsx(
12488
- Form$2.Field,
12489
- {
12490
- control: form.control,
12491
- name: "address_1",
12492
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12493
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
12494
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12495
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12496
- ] })
12497
- }
12498
- ),
12499
- /* @__PURE__ */ jsxRuntime.jsx(
12500
- Form$2.Field,
12501
- {
12502
- control: form.control,
12503
- name: "address_2",
12504
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12505
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12506
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12507
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12508
- ] })
12509
- }
12510
- ),
12511
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12512
- /* @__PURE__ */ jsxRuntime.jsx(
12513
- Form$2.Field,
12514
- {
12515
- control: form.control,
12516
- name: "postal_code",
12517
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12518
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
12519
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12520
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12521
- ] })
12522
- }
12523
- ),
12524
- /* @__PURE__ */ jsxRuntime.jsx(
12525
- Form$2.Field,
12526
- {
12527
- control: form.control,
12528
- name: "city",
12529
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12530
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
12531
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12532
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12533
- ] })
12534
- }
12535
- )
12536
- ] }),
12537
- /* @__PURE__ */ jsxRuntime.jsx(
12538
- Form$2.Field,
12539
- {
12540
- control: form.control,
12541
- name: "province",
12542
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12543
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12544
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12545
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12546
- ] })
12547
- }
12548
- ),
12549
- /* @__PURE__ */ jsxRuntime.jsx(
12550
- Form$2.Field,
12551
- {
12552
- control: form.control,
12553
- name: "phone",
12554
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12555
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
12556
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12557
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12558
- ] })
12559
- }
12560
- )
12561
- ] }) }),
12562
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12563
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12564
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12565
- ] }) })
12566
- ]
12567
- }
12568
- ) });
12569
- };
12570
- const schema$1 = addressSchema;
12571
- const TransferOwnership = () => {
12572
- const { id } = reactRouterDom.useParams();
12573
- const { draft_order, isPending, isError, error } = useDraftOrder(id, {
12574
- fields: "id,customer_id,customer.*"
12575
- });
12576
- if (isError) {
12577
- throw error;
12578
- }
12579
- const isReady = !isPending && !!draft_order;
12580
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12581
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12582
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Transfer Ownership" }) }),
12583
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
12584
- ] }),
12585
- isReady && /* @__PURE__ */ jsxRuntime.jsx(TransferOwnershipForm, { order: draft_order })
12586
- ] });
12587
- };
12588
- const TransferOwnershipForm = ({ order }) => {
12589
- var _a, _b;
12590
- const form = reactHookForm.useForm({
12591
- defaultValues: {
12592
- customer_id: order.customer_id || ""
12593
- },
12594
- resolver: zod.zodResolver(schema)
12595
- });
12596
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12597
- const { handleSuccess } = useRouteModal();
12598
- const name = [(_a = order.customer) == null ? void 0 : _a.first_name, (_b = order.customer) == null ? void 0 : _b.last_name].filter(Boolean).join(" ");
12599
- const currentCustomer = order.customer ? {
12600
- label: name ? `${name} (${order.customer.email})` : order.customer.email,
12601
- value: order.customer.id
12602
- } : null;
12603
- const onSubmit = form.handleSubmit(async (data) => {
12604
- await mutateAsync(
12605
- { customer_id: data.customer_id },
12606
- {
12607
- onSuccess: () => {
12608
- ui.toast.success("Customer updated");
12609
- handleSuccess();
12610
- },
12611
- onError: (error) => {
12612
- ui.toast.error(error.message);
12613
- }
12614
- }
12615
- );
12616
- });
12617
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12618
- KeyboundForm,
12619
- {
12620
- className: "flex flex-1 flex-col overflow-hidden",
12621
- onSubmit,
12622
- children: [
12623
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: [
12624
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center bg-ui-bg-component rounded-md border", children: /* @__PURE__ */ jsxRuntime.jsx(Illustration, {}) }),
12625
- currentCustomer && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col space-y-3", children: [
12626
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12627
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "current-customer", children: "Current owner" }),
12628
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { children: "The customer that is currently associated with this draft order." })
12629
- ] }),
12630
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Select, { disabled: true, value: currentCustomer.value, children: [
12631
- /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { id: "current-customer", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, {}) }),
12632
- /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: currentCustomer.value, children: currentCustomer.label }) })
12633
- ] })
12634
- ] }),
12635
- /* @__PURE__ */ jsxRuntime.jsx(
12636
- CustomerField,
12637
- {
12638
- control: form.control,
12639
- currentCustomerId: order.customer_id
12640
- }
12641
- )
12642
- ] }),
12643
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12644
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "secondary", size: "small", children: "Cancel" }) }),
12645
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12646
- ] }) })
12647
- ]
12648
- }
12649
- ) });
12650
- };
12651
- const CustomerField = ({ control, currentCustomerId }) => {
12652
- const customers = useComboboxData({
12653
- queryFn: async (params) => {
12654
- return await sdk.admin.customer.list({
12655
- ...params,
12656
- id: currentCustomerId ? { $nin: [currentCustomerId] } : void 0
12657
- });
12658
- },
12659
- queryKey: ["customers"],
12660
- getOptions: (data) => {
12661
- return data.customers.map((customer) => {
12662
- const name = [customer.first_name, customer.last_name].filter(Boolean).join(" ");
12663
- return {
12664
- label: name ? `${name} (${customer.email})` : customer.email,
12665
- value: customer.id
12666
- };
12667
- });
12668
- }
12669
- });
12670
- return /* @__PURE__ */ jsxRuntime.jsx(
12671
- Form$2.Field,
12672
- {
12673
- name: "customer_id",
12674
- control,
12675
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { className: "space-y-3", children: [
12676
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12677
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "New customer" }),
12678
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "The customer to transfer this draft order to." })
12679
- ] }),
12680
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12681
- Combobox,
12682
- {
12683
- options: customers.options,
12684
- fetchNextPage: customers.fetchNextPage,
12685
- isFetchingNextPage: customers.isFetchingNextPage,
12686
- searchValue: customers.searchValue,
12687
- onSearchValueChange: customers.onSearchValueChange,
12688
- placeholder: "Select customer",
12689
- ...field
12690
- }
12691
- ) }),
12692
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12693
- ] })
12694
- }
12695
- );
12696
- };
12697
- const Illustration = () => {
12698
- return /* @__PURE__ */ jsxRuntime.jsxs(
12699
- "svg",
12700
- {
12701
- width: "280",
12702
- height: "180",
12703
- viewBox: "0 0 280 180",
12704
- fill: "none",
12705
- xmlns: "http://www.w3.org/2000/svg",
11691
+ width: "280",
11692
+ height: "180",
11693
+ viewBox: "0 0 280 180",
11694
+ fill: "none",
11695
+ xmlns: "http://www.w3.org/2000/svg",
12706
11696
  children: [
12707
11697
  /* @__PURE__ */ jsxRuntime.jsx(
12708
11698
  "rect",
@@ -12985,65 +11975,1075 @@ const Illustration = () => {
12985
11975
  /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12986
11976
  "rect",
12987
11977
  {
12988
- width: "12",
12989
- height: "12",
12990
- fill: "white",
12991
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
11978
+ width: "12",
11979
+ height: "12",
11980
+ fill: "white",
11981
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
11982
+ }
11983
+ ) }),
11984
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11985
+ "rect",
11986
+ {
11987
+ width: "12",
11988
+ height: "12",
11989
+ fill: "white",
11990
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
11991
+ }
11992
+ ) }),
11993
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11994
+ "rect",
11995
+ {
11996
+ width: "12",
11997
+ height: "12",
11998
+ fill: "white",
11999
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12000
+ }
12001
+ ) }),
12002
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12003
+ "rect",
12004
+ {
12005
+ width: "12",
12006
+ height: "12",
12007
+ fill: "white",
12008
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12009
+ }
12010
+ ) }),
12011
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12012
+ "rect",
12013
+ {
12014
+ width: "12",
12015
+ height: "12",
12016
+ fill: "white",
12017
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12018
+ }
12019
+ ) }),
12020
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12021
+ "rect",
12022
+ {
12023
+ width: "12",
12024
+ height: "12",
12025
+ fill: "white",
12026
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12027
+ }
12028
+ ) })
12029
+ ] })
12030
+ ]
12031
+ }
12032
+ );
12033
+ };
12034
+ const schema$1 = objectType({
12035
+ customer_id: stringType().min(1)
12036
+ });
12037
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
12038
+ const Shipping = () => {
12039
+ var _a;
12040
+ const { id } = reactRouterDom.useParams();
12041
+ const { order, isPending, isError, error } = useOrder(id, {
12042
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
12043
+ });
12044
+ const {
12045
+ order: preview,
12046
+ isPending: isPreviewPending,
12047
+ isError: isPreviewError,
12048
+ error: previewError
12049
+ } = useOrderPreview(id);
12050
+ useInitiateOrderEdit({ preview });
12051
+ const { onCancel } = useCancelOrderEdit({ preview });
12052
+ if (isError) {
12053
+ throw error;
12054
+ }
12055
+ if (isPreviewError) {
12056
+ throw previewError;
12057
+ }
12058
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
12059
+ const isReady = preview && !isPreviewPending && order && !isPending;
12060
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
12061
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
12062
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
12063
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
12064
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
12065
+ ] }) }) }),
12066
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
12067
+ ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12068
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
12069
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
12070
+ ] }) });
12071
+ };
12072
+ const ShippingForm = ({ preview, order }) => {
12073
+ var _a;
12074
+ const { setIsOpen } = useStackedModal();
12075
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
12076
+ const [data, setData] = React.useState(null);
12077
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
12078
+ const { shipping_options } = useShippingOptions(
12079
+ {
12080
+ id: appliedShippingOptionIds,
12081
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
12082
+ },
12083
+ {
12084
+ enabled: appliedShippingOptionIds.length > 0
12085
+ }
12086
+ );
12087
+ const uniqueShippingProfiles = React.useMemo(() => {
12088
+ const profiles = /* @__PURE__ */ new Map();
12089
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
12090
+ profiles.set(profile.id, profile);
12091
+ });
12092
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
12093
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
12094
+ });
12095
+ return Array.from(profiles.values());
12096
+ }, [order.items, shipping_options]);
12097
+ const { handleSuccess } = useRouteModal();
12098
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12099
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
12100
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
12101
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
12102
+ const onSubmit = async () => {
12103
+ setIsSubmitting(true);
12104
+ let requestSucceeded = false;
12105
+ await requestOrderEdit(void 0, {
12106
+ onError: (e) => {
12107
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
12108
+ },
12109
+ onSuccess: () => {
12110
+ requestSucceeded = true;
12111
+ }
12112
+ });
12113
+ if (!requestSucceeded) {
12114
+ setIsSubmitting(false);
12115
+ return;
12116
+ }
12117
+ await confirmOrderEdit(void 0, {
12118
+ onError: (e) => {
12119
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
12120
+ },
12121
+ onSuccess: () => {
12122
+ handleSuccess();
12123
+ },
12124
+ onSettled: () => {
12125
+ setIsSubmitting(false);
12126
+ }
12127
+ });
12128
+ };
12129
+ const onKeydown = React.useCallback(
12130
+ (e) => {
12131
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
12132
+ if (data || isSubmitting) {
12133
+ return;
12134
+ }
12135
+ onSubmit();
12136
+ }
12137
+ },
12138
+ [data, isSubmitting, onSubmit]
12139
+ );
12140
+ React.useEffect(() => {
12141
+ document.addEventListener("keydown", onKeydown);
12142
+ return () => {
12143
+ document.removeEventListener("keydown", onKeydown);
12144
+ };
12145
+ }, [onKeydown]);
12146
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
12147
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
12148
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
12149
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
12150
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12151
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
12152
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
12153
+ ] }),
12154
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12155
+ /* @__PURE__ */ jsxRuntime.jsx(radixUi.Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12156
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between px-4 py-2", children: [
12157
+ /* @__PURE__ */ jsxRuntime.jsx(
12158
+ ui.Text,
12159
+ {
12160
+ size: "xsmall",
12161
+ weight: "plus",
12162
+ className: "text-ui-fg-muted",
12163
+ children: "Shipping profile"
12164
+ }
12165
+ ),
12166
+ /* @__PURE__ */ jsxRuntime.jsx(
12167
+ ui.Text,
12168
+ {
12169
+ size: "xsmall",
12170
+ weight: "plus",
12171
+ className: "text-ui-fg-muted",
12172
+ children: "Action"
12173
+ }
12174
+ )
12175
+ ] }),
12176
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
12177
+ var _a2, _b, _c, _d, _e, _f, _g;
12178
+ const items = getItemsWithShippingProfile(
12179
+ profile.id,
12180
+ order.items
12181
+ );
12182
+ const hasItems = items.length > 0;
12183
+ const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
12184
+ (option) => option.shipping_profile_id === profile.id
12185
+ );
12186
+ const shippingMethod = preview.shipping_methods.find(
12187
+ (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
12188
+ );
12189
+ const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
12190
+ (action) => action.action === "SHIPPING_ADD"
12191
+ );
12192
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12193
+ radixUi.Accordion.Item,
12194
+ {
12195
+ value: profile.id,
12196
+ className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
12197
+ children: [
12198
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between gap-3 px-3 py-2", children: [
12199
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3 overflow-hidden", children: [
12200
+ /* @__PURE__ */ jsxRuntime.jsx(radixUi.Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(
12201
+ ui.IconButton,
12202
+ {
12203
+ size: "2xsmall",
12204
+ variant: "transparent",
12205
+ className: "group/trigger",
12206
+ disabled: !hasItems,
12207
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.TriangleRightMini, { className: "transition-transform group-data-[state=open]/trigger:rotate-90" })
12208
+ }
12209
+ ) }),
12210
+ !shippingOption ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
12211
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "shadow-borders-base flex size-7 items-center justify-center rounded-md", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "bg-ui-bg-component-hover flex size-6 items-center justify-center rounded", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Shopping, { className: "text-ui-fg-subtle" }) }) }),
12212
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col", children: [
12213
+ /* @__PURE__ */ jsxRuntime.jsx(
12214
+ ui.Text,
12215
+ {
12216
+ size: "small",
12217
+ weight: "plus",
12218
+ leading: "compact",
12219
+ children: profile.name
12220
+ }
12221
+ ),
12222
+ /* @__PURE__ */ jsxRuntime.jsxs(
12223
+ ui.Text,
12224
+ {
12225
+ size: "small",
12226
+ leading: "compact",
12227
+ className: "text-ui-fg-subtle",
12228
+ children: [
12229
+ items.length,
12230
+ " ",
12231
+ pluralize(items.length, "items", "item")
12232
+ ]
12233
+ }
12234
+ )
12235
+ ] })
12236
+ ] }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full flex-1 items-center gap-[5px] overflow-hidden max-sm:flex-col max-sm:items-start", children: [
12237
+ /* @__PURE__ */ jsxRuntime.jsx(
12238
+ ui.Tooltip,
12239
+ {
12240
+ content: /* @__PURE__ */ jsxRuntime.jsx("ul", { children: items.map((item) => {
12241
+ var _a3, _b2, _c2;
12242
+ return /* @__PURE__ */ jsxRuntime.jsx(
12243
+ "li",
12244
+ {
12245
+ children: `${item.quantity}x ${(_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title} (${(_c2 = item.variant) == null ? void 0 : _c2.title})`
12246
+ },
12247
+ item.id
12248
+ );
12249
+ }) }),
12250
+ children: /* @__PURE__ */ jsxRuntime.jsxs(
12251
+ ui.Badge,
12252
+ {
12253
+ className: "flex cursor-default items-center gap-x-[3px] overflow-hidden",
12254
+ size: "xsmall",
12255
+ children: [
12256
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Shopping, { className: "shrink-0" }),
12257
+ /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "truncate", children: [
12258
+ items.reduce(
12259
+ (acc, item) => acc + item.quantity,
12260
+ 0
12261
+ ),
12262
+ "x",
12263
+ " ",
12264
+ pluralize(items.length, "items", "item")
12265
+ ] })
12266
+ ]
12267
+ }
12268
+ )
12269
+ }
12270
+ ),
12271
+ /* @__PURE__ */ jsxRuntime.jsx(
12272
+ ui.Tooltip,
12273
+ {
12274
+ content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
12275
+ children: /* @__PURE__ */ jsxRuntime.jsxs(
12276
+ ui.Badge,
12277
+ {
12278
+ className: "flex cursor-default items-center gap-x-[3px] overflow-hidden",
12279
+ size: "xsmall",
12280
+ children: [
12281
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Buildings, { className: "shrink-0" }),
12282
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "truncate", children: (_g = (_f = (_e = shippingOption.service_zone) == null ? void 0 : _e.fulfillment_set) == null ? void 0 : _f.location) == null ? void 0 : _g.name })
12283
+ ]
12284
+ }
12285
+ )
12286
+ }
12287
+ ),
12288
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxRuntime.jsxs(
12289
+ ui.Badge,
12290
+ {
12291
+ className: "flex cursor-default items-center gap-x-[3px] overflow-hidden",
12292
+ size: "xsmall",
12293
+ children: [
12294
+ /* @__PURE__ */ jsxRuntime.jsx(icons.TruckFast, { className: "shrink-0" }),
12295
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "truncate", children: shippingOption.name })
12296
+ ]
12297
+ }
12298
+ ) })
12299
+ ] })
12300
+ ] }),
12301
+ shippingOption ? /* @__PURE__ */ jsxRuntime.jsx(
12302
+ ActionMenu,
12303
+ {
12304
+ groups: [
12305
+ {
12306
+ actions: [
12307
+ hasItems ? {
12308
+ label: "Edit shipping option",
12309
+ icon: /* @__PURE__ */ jsxRuntime.jsx(icons.Channels, {}),
12310
+ onClick: () => {
12311
+ setIsOpen(
12312
+ STACKED_FOCUS_MODAL_ID,
12313
+ true
12314
+ );
12315
+ setData({
12316
+ shippingProfileId: profile.id,
12317
+ shippingOption,
12318
+ shippingMethod
12319
+ });
12320
+ }
12321
+ } : void 0,
12322
+ {
12323
+ label: "Remove shipping option",
12324
+ icon: /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, {}),
12325
+ onClick: () => {
12326
+ if (shippingMethod) {
12327
+ if (addShippingMethodAction) {
12328
+ removeActionShippingMethod(
12329
+ addShippingMethodAction.id
12330
+ );
12331
+ } else {
12332
+ removeShippingMethod(
12333
+ shippingMethod.id
12334
+ );
12335
+ }
12336
+ }
12337
+ }
12338
+ }
12339
+ ].filter(Boolean)
12340
+ }
12341
+ ]
12342
+ }
12343
+ ) : /* @__PURE__ */ jsxRuntime.jsx(
12344
+ StackedModalTrigger,
12345
+ {
12346
+ shippingProfileId: profile.id,
12347
+ shippingOption,
12348
+ shippingMethod,
12349
+ setData,
12350
+ children: "Add shipping option"
12351
+ }
12352
+ )
12353
+ ] }),
12354
+ /* @__PURE__ */ jsxRuntime.jsxs(radixUi.Accordion.Content, { children: [
12355
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12356
+ items.map((item, idx) => {
12357
+ var _a3, _b2, _c2, _d2, _e2;
12358
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12359
+ /* @__PURE__ */ jsxRuntime.jsxs(
12360
+ "div",
12361
+ {
12362
+ className: "flex items-center gap-x-3 px-3",
12363
+ children: [
12364
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex h-[56px] w-5 flex-col items-center justify-center", children: /* @__PURE__ */ jsxRuntime.jsx(
12365
+ ui.Divider,
12366
+ {
12367
+ variant: "dashed",
12368
+ orientation: "vertical"
12369
+ }
12370
+ ) }),
12371
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3 py-2", children: [
12372
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex size-7 items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxRuntime.jsxs(
12373
+ ui.Text,
12374
+ {
12375
+ size: "small",
12376
+ leading: "compact",
12377
+ className: "text-ui-fg-subtle",
12378
+ children: [
12379
+ item.quantity,
12380
+ "x"
12381
+ ]
12382
+ }
12383
+ ) }),
12384
+ /* @__PURE__ */ jsxRuntime.jsx(Thumbnail, { thumbnail: item.thumbnail }),
12385
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12386
+ /* @__PURE__ */ jsxRuntime.jsxs(
12387
+ ui.Text,
12388
+ {
12389
+ size: "small",
12390
+ leading: "compact",
12391
+ weight: "plus",
12392
+ children: [
12393
+ (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
12394
+ " (",
12395
+ (_c2 = item.variant) == null ? void 0 : _c2.title,
12396
+ ")"
12397
+ ]
12398
+ }
12399
+ ),
12400
+ /* @__PURE__ */ jsxRuntime.jsx(
12401
+ ui.Text,
12402
+ {
12403
+ size: "small",
12404
+ leading: "compact",
12405
+ className: "text-ui-fg-subtle",
12406
+ children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
12407
+ }
12408
+ )
12409
+ ] })
12410
+ ] })
12411
+ ]
12412
+ },
12413
+ item.id
12414
+ ),
12415
+ idx !== items.length - 1 && /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" })
12416
+ ] }, item.id);
12417
+ })
12418
+ ] })
12419
+ ]
12420
+ },
12421
+ profile.id
12422
+ );
12423
+ }) })
12424
+ ] }) })
12425
+ ] }) }),
12426
+ /* @__PURE__ */ jsxRuntime.jsx(
12427
+ StackedFocusModal,
12428
+ {
12429
+ id: STACKED_FOCUS_MODAL_ID,
12430
+ onOpenChangeCallback: (open) => {
12431
+ if (!open) {
12432
+ setData(null);
12433
+ }
12434
+ return open;
12435
+ },
12436
+ children: data && /* @__PURE__ */ jsxRuntime.jsx(ShippingProfileForm, { data, order, preview })
12437
+ }
12438
+ )
12439
+ ] }),
12440
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-x-2", children: [
12441
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12442
+ /* @__PURE__ */ jsxRuntime.jsx(
12443
+ ui.Button,
12444
+ {
12445
+ size: "small",
12446
+ type: "button",
12447
+ isLoading: isSubmitting,
12448
+ onClick: onSubmit,
12449
+ children: "Save"
12450
+ }
12451
+ )
12452
+ ] }) })
12453
+ ] });
12454
+ };
12455
+ const StackedModalTrigger = ({
12456
+ shippingProfileId,
12457
+ shippingOption,
12458
+ shippingMethod,
12459
+ setData,
12460
+ children
12461
+ }) => {
12462
+ const { setIsOpen, getIsOpen } = useStackedModal();
12463
+ const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
12464
+ const onToggle = () => {
12465
+ if (isOpen) {
12466
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12467
+ setData(null);
12468
+ } else {
12469
+ setIsOpen(STACKED_FOCUS_MODAL_ID, true);
12470
+ setData({
12471
+ shippingProfileId,
12472
+ shippingOption,
12473
+ shippingMethod
12474
+ });
12475
+ }
12476
+ };
12477
+ return /* @__PURE__ */ jsxRuntime.jsx(
12478
+ ui.Button,
12479
+ {
12480
+ size: "small",
12481
+ variant: "secondary",
12482
+ onClick: onToggle,
12483
+ className: "text-ui-fg-primary shrink-0",
12484
+ children
12485
+ }
12486
+ );
12487
+ };
12488
+ const ShippingProfileForm = ({
12489
+ data,
12490
+ order,
12491
+ preview
12492
+ }) => {
12493
+ var _a, _b, _c, _d, _e, _f;
12494
+ const { setIsOpen } = useStackedModal();
12495
+ const form = reactHookForm.useForm({
12496
+ resolver: zod.zodResolver(shippingMethodSchema),
12497
+ defaultValues: {
12498
+ location_id: (_d = (_c = (_b = (_a = data.shippingOption) == null ? void 0 : _a.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.id,
12499
+ shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
12500
+ custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
12501
+ }
12502
+ });
12503
+ const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
12504
+ const {
12505
+ mutateAsync: updateShippingMethod,
12506
+ isPending: isUpdatingShippingMethod
12507
+ } = useDraftOrderUpdateShippingMethod(order.id);
12508
+ const onSubmit = form.handleSubmit(async (values) => {
12509
+ if (isEqual__default.default(values, form.formState.defaultValues)) {
12510
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12511
+ return;
12512
+ }
12513
+ if (data.shippingMethod) {
12514
+ await updateShippingMethod(
12515
+ {
12516
+ method_id: data.shippingMethod.id,
12517
+ shipping_option_id: values.shipping_option_id,
12518
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12519
+ },
12520
+ {
12521
+ onError: (e) => {
12522
+ ui.toast.error(e.message);
12523
+ },
12524
+ onSuccess: () => {
12525
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12526
+ }
12527
+ }
12528
+ );
12529
+ return;
12530
+ }
12531
+ await addShippingMethod(
12532
+ {
12533
+ shipping_option_id: values.shipping_option_id,
12534
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12535
+ },
12536
+ {
12537
+ onError: (e) => {
12538
+ ui.toast.error(e.message);
12539
+ },
12540
+ onSuccess: () => {
12541
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12542
+ }
12543
+ }
12544
+ );
12545
+ });
12546
+ return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12547
+ KeyboundForm,
12548
+ {
12549
+ className: "flex h-full flex-col overflow-hidden",
12550
+ onSubmit,
12551
+ children: [
12552
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
12553
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
12554
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12555
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
12556
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a shipping method for the selected shipping profile. You can see the items that will be shipped using this method in the preview below." }) })
12557
+ ] }),
12558
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12559
+ /* @__PURE__ */ jsxRuntime.jsx(
12560
+ LocationField,
12561
+ {
12562
+ control: form.control,
12563
+ setValue: form.setValue
12564
+ }
12565
+ ),
12566
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12567
+ /* @__PURE__ */ jsxRuntime.jsx(
12568
+ ShippingOptionField,
12569
+ {
12570
+ shippingProfileId: data.shippingProfileId,
12571
+ preview,
12572
+ control: form.control
12573
+ }
12574
+ ),
12575
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12576
+ /* @__PURE__ */ jsxRuntime.jsx(
12577
+ CustomAmountField,
12578
+ {
12579
+ control: form.control,
12580
+ currencyCode: order.currency_code
12581
+ }
12582
+ ),
12583
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12584
+ /* @__PURE__ */ jsxRuntime.jsx(
12585
+ ItemsPreview,
12586
+ {
12587
+ order,
12588
+ shippingProfileId: data.shippingProfileId
12589
+ }
12590
+ )
12591
+ ] }) }) }),
12592
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-x-2", children: [
12593
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12594
+ /* @__PURE__ */ jsxRuntime.jsx(
12595
+ ui.Button,
12596
+ {
12597
+ size: "small",
12598
+ type: "submit",
12599
+ isLoading: isPending || isUpdatingShippingMethod,
12600
+ children: data.shippingMethod ? "Update" : "Add"
12601
+ }
12602
+ )
12603
+ ] }) })
12604
+ ]
12605
+ }
12606
+ ) }) });
12607
+ };
12608
+ const shippingMethodSchema = objectType({
12609
+ location_id: stringType(),
12610
+ shipping_option_id: stringType(),
12611
+ custom_amount: unionType([numberType(), stringType()]).optional()
12612
+ });
12613
+ const ItemsPreview = ({ order, shippingProfileId }) => {
12614
+ const matches = order.items.filter(
12615
+ (item) => {
12616
+ var _a, _b, _c;
12617
+ return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
12618
+ }
12619
+ );
12620
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
12621
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12622
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12623
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12624
+ ] }) }),
12625
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12626
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-muted grid grid-cols-2 gap-3 px-4 py-2", children: [
12627
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
12628
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) })
12629
+ ] }),
12630
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsxs(
12631
+ "div",
12632
+ {
12633
+ className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-2 items-center gap-3 rounded-lg px-4 py-2",
12634
+ children: [
12635
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
12636
+ /* @__PURE__ */ jsxRuntime.jsx(
12637
+ Thumbnail,
12638
+ {
12639
+ thumbnail: item.thumbnail,
12640
+ alt: item.product_title ?? void 0
12641
+ }
12642
+ ),
12643
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12644
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
12645
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12646
+ /* @__PURE__ */ jsxRuntime.jsxs(
12647
+ ui.Text,
12648
+ {
12649
+ size: "small",
12650
+ leading: "compact",
12651
+ className: "text-ui-fg-subtle",
12652
+ children: [
12653
+ "(",
12654
+ item.variant_title,
12655
+ ")"
12656
+ ]
12657
+ }
12658
+ )
12659
+ ] }),
12660
+ /* @__PURE__ */ jsxRuntime.jsx(
12661
+ ui.Text,
12662
+ {
12663
+ size: "small",
12664
+ leading: "compact",
12665
+ className: "text-ui-fg-subtle",
12666
+ children: item.variant_sku
12667
+ }
12668
+ )
12669
+ ] })
12670
+ ] }),
12671
+ /* @__PURE__ */ jsxRuntime.jsxs(
12672
+ ui.Text,
12673
+ {
12674
+ size: "small",
12675
+ leading: "compact",
12676
+ className: "text-ui-fg-subtle",
12677
+ children: [
12678
+ item.quantity,
12679
+ "x"
12680
+ ]
12681
+ }
12682
+ )
12683
+ ]
12684
+ },
12685
+ item.id
12686
+ )) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12687
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12688
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
12689
+ 'No items found for "',
12690
+ query,
12691
+ '".'
12692
+ ] })
12693
+ ] }) })
12694
+ ] })
12695
+ ] });
12696
+ };
12697
+ const LocationField = ({ control, setValue }) => {
12698
+ const locations = useComboboxData({
12699
+ queryKey: ["locations"],
12700
+ queryFn: async (params) => {
12701
+ return await sdk.admin.stockLocation.list(params);
12702
+ },
12703
+ getOptions: (data) => {
12704
+ return data.stock_locations.map((location) => ({
12705
+ label: location.name,
12706
+ value: location.id
12707
+ }));
12708
+ }
12709
+ });
12710
+ return /* @__PURE__ */ jsxRuntime.jsx(
12711
+ Form$2.Field,
12712
+ {
12713
+ control,
12714
+ name: "location_id",
12715
+ render: ({ field: { onChange, ...field } }) => {
12716
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12717
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12718
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Location" }),
12719
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12720
+ ] }),
12721
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12722
+ Combobox,
12723
+ {
12724
+ options: locations.options,
12725
+ fetchNextPage: locations.fetchNextPage,
12726
+ isFetchingNextPage: locations.isFetchingNextPage,
12727
+ searchValue: locations.searchValue,
12728
+ onSearchValueChange: locations.onSearchValueChange,
12729
+ placeholder: "Select location",
12730
+ onChange: (value) => {
12731
+ setValue("shipping_option_id", "", {
12732
+ shouldDirty: true,
12733
+ shouldTouch: true
12734
+ });
12735
+ onChange(value);
12736
+ },
12737
+ ...field
12738
+ }
12739
+ ) })
12740
+ ] }) });
12741
+ }
12742
+ }
12743
+ );
12744
+ };
12745
+ const ShippingOptionField = ({
12746
+ shippingProfileId,
12747
+ preview,
12748
+ control
12749
+ }) => {
12750
+ var _a;
12751
+ const locationId = reactHookForm.useWatch({ control, name: "location_id" });
12752
+ const shippingOptions = useComboboxData({
12753
+ queryKey: ["shipping_options", locationId, shippingProfileId],
12754
+ queryFn: async (params) => {
12755
+ return await sdk.admin.shippingOption.list({
12756
+ ...params,
12757
+ stock_location_id: locationId,
12758
+ shipping_profile_id: shippingProfileId
12759
+ });
12760
+ },
12761
+ getOptions: (data) => {
12762
+ return data.shipping_options.map((option) => {
12763
+ var _a2;
12764
+ if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12765
+ (r) => r.attribute === "is_return" && r.value === "true"
12766
+ )) {
12767
+ return void 0;
12768
+ }
12769
+ return {
12770
+ label: option.name,
12771
+ value: option.id
12772
+ };
12773
+ }).filter(Boolean);
12774
+ },
12775
+ enabled: !!locationId && !!shippingProfileId,
12776
+ defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12777
+ });
12778
+ const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12779
+ return /* @__PURE__ */ jsxRuntime.jsx(
12780
+ Form$2.Field,
12781
+ {
12782
+ control,
12783
+ name: "shipping_option_id",
12784
+ render: ({ field }) => {
12785
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12786
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12787
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Shipping option" }),
12788
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12789
+ ] }),
12790
+ /* @__PURE__ */ jsxRuntime.jsx(
12791
+ ConditionalTooltip,
12792
+ {
12793
+ content: tooltipContent,
12794
+ showTooltip: !locationId || !shippingProfileId,
12795
+ children: /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12796
+ Combobox,
12797
+ {
12798
+ options: shippingOptions.options,
12799
+ fetchNextPage: shippingOptions.fetchNextPage,
12800
+ isFetchingNextPage: shippingOptions.isFetchingNextPage,
12801
+ searchValue: shippingOptions.searchValue,
12802
+ onSearchValueChange: shippingOptions.onSearchValueChange,
12803
+ placeholder: "Select shipping option",
12804
+ ...field,
12805
+ disabled: !locationId || !shippingProfileId
12806
+ }
12807
+ ) }) })
12808
+ }
12809
+ )
12810
+ ] }) });
12811
+ }
12812
+ }
12813
+ );
12814
+ };
12815
+ const CustomAmountField = ({
12816
+ control,
12817
+ currencyCode
12818
+ }) => {
12819
+ return /* @__PURE__ */ jsxRuntime.jsx(
12820
+ Form$2.Field,
12821
+ {
12822
+ control,
12823
+ name: "custom_amount",
12824
+ render: ({ field: { onChange, ...field } }) => {
12825
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12826
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12827
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12828
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12829
+ ] }),
12830
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12831
+ ui.CurrencyInput,
12832
+ {
12833
+ ...field,
12834
+ onValueChange: (value) => onChange(value),
12835
+ symbol: getNativeSymbol(currencyCode),
12836
+ code: currencyCode
12837
+ }
12838
+ ) })
12839
+ ] });
12840
+ }
12841
+ }
12842
+ );
12843
+ };
12844
+ const ShippingAddress = () => {
12845
+ const { id } = reactRouterDom.useParams();
12846
+ const { order, isPending, isError, error } = useOrder(id, {
12847
+ fields: "+shipping_address"
12848
+ });
12849
+ if (isError) {
12850
+ throw error;
12851
+ }
12852
+ const isReady = !isPending && !!order;
12853
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12854
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12855
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Shipping Address" }) }),
12856
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12857
+ ] }),
12858
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(ShippingAddressForm, { order })
12859
+ ] });
12860
+ };
12861
+ const ShippingAddressForm = ({ order }) => {
12862
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12863
+ const form = reactHookForm.useForm({
12864
+ defaultValues: {
12865
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12866
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12867
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12868
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12869
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12870
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12871
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12872
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12873
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12874
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12875
+ },
12876
+ resolver: zod.zodResolver(schema)
12877
+ });
12878
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12879
+ const { handleSuccess } = useRouteModal();
12880
+ const onSubmit = form.handleSubmit(async (data) => {
12881
+ await mutateAsync(
12882
+ {
12883
+ shipping_address: {
12884
+ first_name: data.first_name,
12885
+ last_name: data.last_name,
12886
+ company: data.company,
12887
+ address_1: data.address_1,
12888
+ address_2: data.address_2,
12889
+ city: data.city,
12890
+ province: data.province,
12891
+ country_code: data.country_code,
12892
+ postal_code: data.postal_code,
12893
+ phone: data.phone
12894
+ }
12895
+ },
12896
+ {
12897
+ onSuccess: () => {
12898
+ handleSuccess();
12899
+ },
12900
+ onError: (error) => {
12901
+ ui.toast.error(error.message);
12902
+ }
12903
+ }
12904
+ );
12905
+ });
12906
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12907
+ KeyboundForm,
12908
+ {
12909
+ className: "flex flex-1 flex-col overflow-hidden",
12910
+ onSubmit,
12911
+ children: [
12912
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
12913
+ /* @__PURE__ */ jsxRuntime.jsx(
12914
+ Form$2.Field,
12915
+ {
12916
+ control: form.control,
12917
+ name: "country_code",
12918
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12919
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
12920
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
12921
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12922
+ ] })
12992
12923
  }
12993
- ) }),
12994
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12995
- "rect",
12924
+ ),
12925
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12926
+ /* @__PURE__ */ jsxRuntime.jsx(
12927
+ Form$2.Field,
12928
+ {
12929
+ control: form.control,
12930
+ name: "first_name",
12931
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12932
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
12933
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12934
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12935
+ ] })
12936
+ }
12937
+ ),
12938
+ /* @__PURE__ */ jsxRuntime.jsx(
12939
+ Form$2.Field,
12940
+ {
12941
+ control: form.control,
12942
+ name: "last_name",
12943
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12944
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
12945
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12946
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12947
+ ] })
12948
+ }
12949
+ )
12950
+ ] }),
12951
+ /* @__PURE__ */ jsxRuntime.jsx(
12952
+ Form$2.Field,
12996
12953
  {
12997
- width: "12",
12998
- height: "12",
12999
- fill: "white",
13000
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12954
+ control: form.control,
12955
+ name: "company",
12956
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12957
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
12958
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12959
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12960
+ ] })
13001
12961
  }
13002
- ) }),
13003
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13004
- "rect",
12962
+ ),
12963
+ /* @__PURE__ */ jsxRuntime.jsx(
12964
+ Form$2.Field,
13005
12965
  {
13006
- width: "12",
13007
- height: "12",
13008
- fill: "white",
13009
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12966
+ control: form.control,
12967
+ name: "address_1",
12968
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12969
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
12970
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12971
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12972
+ ] })
13010
12973
  }
13011
- ) }),
13012
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13013
- "rect",
12974
+ ),
12975
+ /* @__PURE__ */ jsxRuntime.jsx(
12976
+ Form$2.Field,
13014
12977
  {
13015
- width: "12",
13016
- height: "12",
13017
- fill: "white",
13018
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12978
+ control: form.control,
12979
+ name: "address_2",
12980
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12981
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12982
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12983
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12984
+ ] })
13019
12985
  }
13020
- ) }),
13021
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13022
- "rect",
12986
+ ),
12987
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12988
+ /* @__PURE__ */ jsxRuntime.jsx(
12989
+ Form$2.Field,
12990
+ {
12991
+ control: form.control,
12992
+ name: "postal_code",
12993
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12994
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
12995
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12996
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12997
+ ] })
12998
+ }
12999
+ ),
13000
+ /* @__PURE__ */ jsxRuntime.jsx(
13001
+ Form$2.Field,
13002
+ {
13003
+ control: form.control,
13004
+ name: "city",
13005
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13006
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
13007
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13008
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13009
+ ] })
13010
+ }
13011
+ )
13012
+ ] }),
13013
+ /* @__PURE__ */ jsxRuntime.jsx(
13014
+ Form$2.Field,
13023
13015
  {
13024
- width: "12",
13025
- height: "12",
13026
- fill: "white",
13027
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
13016
+ control: form.control,
13017
+ name: "province",
13018
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13019
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
13020
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13021
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13022
+ ] })
13028
13023
  }
13029
- ) }),
13030
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13031
- "rect",
13024
+ ),
13025
+ /* @__PURE__ */ jsxRuntime.jsx(
13026
+ Form$2.Field,
13032
13027
  {
13033
- width: "12",
13034
- height: "12",
13035
- fill: "white",
13036
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
13028
+ control: form.control,
13029
+ name: "phone",
13030
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13031
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
13032
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13033
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13034
+ ] })
13037
13035
  }
13038
- ) })
13039
- ] })
13036
+ )
13037
+ ] }) }),
13038
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
13039
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13040
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13041
+ ] }) })
13040
13042
  ]
13041
13043
  }
13042
- );
13044
+ ) });
13043
13045
  };
13044
- const schema = objectType({
13045
- customer_id: stringType().min(1)
13046
- });
13046
+ const schema = addressSchema;
13047
13047
  const widgetModule = { widgets: [] };
13048
13048
  const routeModule = {
13049
13049
  routes: [
@@ -13080,18 +13080,22 @@ const routeModule = {
13080
13080
  Component: Items,
13081
13081
  path: "/draft-orders/:id/items"
13082
13082
  },
13083
- {
13084
- Component: Metadata,
13085
- path: "/draft-orders/:id/metadata"
13086
- },
13087
13083
  {
13088
13084
  Component: Promotions,
13089
13085
  path: "/draft-orders/:id/promotions"
13090
13086
  },
13087
+ {
13088
+ Component: Metadata,
13089
+ path: "/draft-orders/:id/metadata"
13090
+ },
13091
13091
  {
13092
13092
  Component: SalesChannel,
13093
13093
  path: "/draft-orders/:id/sales-channel"
13094
13094
  },
13095
+ {
13096
+ Component: TransferOwnership,
13097
+ path: "/draft-orders/:id/transfer-ownership"
13098
+ },
13095
13099
  {
13096
13100
  Component: Shipping,
13097
13101
  path: "/draft-orders/:id/shipping"
@@ -13099,10 +13103,6 @@ const routeModule = {
13099
13103
  {
13100
13104
  Component: ShippingAddress,
13101
13105
  path: "/draft-orders/:id/shipping-address"
13102
- },
13103
- {
13104
- Component: TransferOwnership,
13105
- path: "/draft-orders/:id/transfer-ownership"
13106
13106
  }
13107
13107
  ]
13108
13108
  }
@@ -13114,7 +13114,9 @@ const menuItemModule = {
13114
13114
  label: config.label,
13115
13115
  icon: void 0,
13116
13116
  path: "/draft-orders",
13117
- nested: "/orders"
13117
+ nested: "/orders",
13118
+ rank: void 0,
13119
+ translationNs: void 0
13118
13120
  }
13119
13121
  ]
13120
13122
  };