@medusajs/draft-order 2.10.2-snapshot-20250901132949 → 2.10.2-snapshot-20250902104934

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