@medusajs/draft-order 2.10.4-preview-20250922210211 → 2.10.4-preview-20250923031201

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.
@@ -9573,27 +9573,6 @@ const ID = () => {
9573
9573
  /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Outlet, {})
9574
9574
  ] });
9575
9575
  };
9576
- const CustomItems = () => {
9577
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9578
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9579
- /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9580
- ] });
9581
- };
9582
- const CustomItemsForm = () => {
9583
- const form = reactHookForm.useForm({
9584
- resolver: zod.zodResolver(schema$5)
9585
- });
9586
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9587
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9588
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9589
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9590
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9591
- ] }) })
9592
- ] }) });
9593
- };
9594
- const schema$5 = objectType({
9595
- email: stringType().email()
9596
- });
9597
9576
  const BillingAddress = () => {
9598
9577
  const { id } = reactRouterDom.useParams();
9599
9578
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9626,7 +9605,7 @@ const BillingAddressForm = ({ order }) => {
9626
9605
  postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9627
9606
  phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9628
9607
  },
9629
- resolver: zod.zodResolver(schema$4)
9608
+ resolver: zod.zodResolver(schema$5)
9630
9609
  });
9631
9610
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9632
9611
  const { handleSuccess } = useRouteModal();
@@ -9783,11 +9762,76 @@ const BillingAddressForm = ({ order }) => {
9783
9762
  }
9784
9763
  ) });
9785
9764
  };
9786
- const schema$4 = addressSchema;
9787
- const Email = () => {
9765
+ const schema$5 = addressSchema;
9766
+ const CustomItems = () => {
9767
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9768
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9769
+ /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9770
+ ] });
9771
+ };
9772
+ const CustomItemsForm = () => {
9773
+ const form = reactHookForm.useForm({
9774
+ resolver: zod.zodResolver(schema$4)
9775
+ });
9776
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9777
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9778
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9779
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9780
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9781
+ ] }) })
9782
+ ] }) });
9783
+ };
9784
+ const schema$4 = objectType({
9785
+ email: stringType().email()
9786
+ });
9787
+ const InlineTip = React.forwardRef(
9788
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9789
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9790
+ return /* @__PURE__ */ jsxRuntime.jsxs(
9791
+ "div",
9792
+ {
9793
+ ref,
9794
+ className: ui.clx(
9795
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9796
+ className
9797
+ ),
9798
+ ...props,
9799
+ children: [
9800
+ /* @__PURE__ */ jsxRuntime.jsx(
9801
+ "div",
9802
+ {
9803
+ role: "presentation",
9804
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9805
+ "bg-ui-tag-orange-icon": variant === "warning"
9806
+ })
9807
+ }
9808
+ ),
9809
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
9810
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9811
+ labelValue,
9812
+ ":"
9813
+ ] }),
9814
+ " ",
9815
+ children
9816
+ ] })
9817
+ ]
9818
+ }
9819
+ );
9820
+ }
9821
+ );
9822
+ InlineTip.displayName = "InlineTip";
9823
+ const MetadataFieldSchema = objectType({
9824
+ key: stringType(),
9825
+ disabled: booleanType().optional(),
9826
+ value: anyType()
9827
+ });
9828
+ const MetadataSchema = objectType({
9829
+ metadata: arrayType(MetadataFieldSchema)
9830
+ });
9831
+ const Metadata = () => {
9788
9832
  const { id } = reactRouterDom.useParams();
9789
9833
  const { order, isPending, isError, error } = useOrder(id, {
9790
- fields: "+email"
9834
+ fields: "metadata"
9791
9835
  });
9792
9836
  if (isError) {
9793
9837
  throw error;
@@ -9795,26 +9839,33 @@ const Email = () => {
9795
9839
  const isReady = !isPending && !!order;
9796
9840
  return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9797
9841
  /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9798
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
9799
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9842
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
9843
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9800
9844
  ] }),
9801
- isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
9845
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9802
9846
  ] });
9803
9847
  };
9804
- const EmailForm = ({ order }) => {
9848
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9849
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9850
+ const MetadataForm = ({ orderId, metadata }) => {
9851
+ const { handleSuccess } = useRouteModal();
9852
+ const hasUneditableRows = getHasUneditableRows(metadata);
9853
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9805
9854
  const form = reactHookForm.useForm({
9806
9855
  defaultValues: {
9807
- email: order.email ?? ""
9856
+ metadata: getDefaultValues(metadata)
9808
9857
  },
9809
- resolver: zod.zodResolver(schema$3)
9858
+ resolver: zod.zodResolver(MetadataSchema)
9810
9859
  });
9811
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9812
- const { handleSuccess } = useRouteModal();
9813
- const onSubmit = form.handleSubmit(async (data) => {
9860
+ const handleSubmit = form.handleSubmit(async (data) => {
9861
+ const parsedData = parseValues(data);
9814
9862
  await mutateAsync(
9815
- { email: data.email },
9863
+ {
9864
+ metadata: parsedData
9865
+ },
9816
9866
  {
9817
9867
  onSuccess: () => {
9868
+ ui.toast.success("Metadata updated");
9818
9869
  handleSuccess();
9819
9870
  },
9820
9871
  onError: (error) => {
@@ -9823,156 +9874,282 @@ const EmailForm = ({ order }) => {
9823
9874
  }
9824
9875
  );
9825
9876
  });
9877
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
9878
+ control: form.control,
9879
+ name: "metadata"
9880
+ });
9881
+ function deleteRow(index) {
9882
+ remove(index);
9883
+ if (fields.length === 1) {
9884
+ insert(0, {
9885
+ key: "",
9886
+ value: "",
9887
+ disabled: false
9888
+ });
9889
+ }
9890
+ }
9891
+ function insertRow(index, position) {
9892
+ insert(index + (position === "above" ? 0 : 1), {
9893
+ key: "",
9894
+ value: "",
9895
+ disabled: false
9896
+ });
9897
+ }
9826
9898
  return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9827
9899
  KeyboundForm,
9828
9900
  {
9901
+ onSubmit: handleSubmit,
9829
9902
  className: "flex flex-1 flex-col overflow-hidden",
9830
- onSubmit,
9831
9903
  children: [
9832
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
9833
- Form$2.Field,
9834
- {
9835
- control: form.control,
9836
- name: "email",
9837
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9838
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
9839
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9840
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9841
- ] })
9842
- }
9843
- ) }),
9844
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9845
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9846
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9847
- ] }) })
9848
- ]
9849
- }
9850
- ) });
9851
- };
9852
- const schema$3 = objectType({
9853
- email: stringType().email()
9854
- });
9855
- const NumberInput = React.forwardRef(
9856
- ({
9857
- value,
9858
- onChange,
9859
- size = "base",
9860
- min = 0,
9861
- max = 100,
9862
- step = 1,
9863
- className,
9864
- disabled,
9865
- ...props
9866
- }, ref) => {
9867
- const handleChange = (event) => {
9868
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9869
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9870
- onChange(newValue);
9871
- }
9872
- };
9873
- const handleIncrement = () => {
9874
- const newValue = value + step;
9875
- if (max === void 0 || newValue <= max) {
9876
- onChange(newValue);
9877
- }
9878
- };
9879
- const handleDecrement = () => {
9880
- const newValue = value - step;
9881
- if (min === void 0 || newValue >= min) {
9882
- onChange(newValue);
9883
- }
9884
- };
9885
- return /* @__PURE__ */ jsxRuntime.jsxs(
9886
- "div",
9887
- {
9888
- className: ui.clx(
9889
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9890
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9891
- {
9892
- "h-7": size === "small",
9893
- "h-8": size === "base"
9894
- },
9895
- className
9896
- ),
9897
- children: [
9898
- /* @__PURE__ */ jsxRuntime.jsx(
9899
- "input",
9900
- {
9901
- ref,
9902
- type: "number",
9903
- value,
9904
- onChange: handleChange,
9905
- min,
9906
- max,
9907
- step,
9908
- className: ui.clx(
9909
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9910
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9911
- "placeholder:text-ui-fg-muted"
9912
- ),
9913
- ...props
9914
- }
9915
- ),
9916
- /* @__PURE__ */ jsxRuntime.jsxs(
9917
- "button",
9918
- {
9919
- className: ui.clx(
9920
- "flex items-center justify-center outline-none transition-fg",
9921
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9922
- "focus:bg-ui-bg-field-component-hover",
9923
- "hover:bg-ui-bg-field-component-hover",
9904
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9905
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9906
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9907
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
9908
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
9909
+ ] }),
9910
+ fields.map((field, index) => {
9911
+ const isDisabled = field.disabled || false;
9912
+ let placeholder = "-";
9913
+ if (typeof field.value === "object") {
9914
+ placeholder = "{ ... }";
9915
+ }
9916
+ if (Array.isArray(field.value)) {
9917
+ placeholder = "[ ... ]";
9918
+ }
9919
+ return /* @__PURE__ */ jsxRuntime.jsx(
9920
+ ConditionalTooltip,
9924
9921
  {
9925
- "size-7": size === "small",
9926
- "size-8": size === "base"
9927
- }
9928
- ),
9929
- type: "button",
9930
- onClick: handleDecrement,
9931
- disabled: min !== void 0 && value <= min || disabled,
9932
- children: [
9933
- /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
9934
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9935
- ]
9936
- }
9937
- ),
9938
- /* @__PURE__ */ jsxRuntime.jsxs(
9939
- "button",
9940
- {
9941
- className: ui.clx(
9942
- "flex items-center justify-center outline-none transition-fg",
9943
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9944
- "focus:bg-ui-bg-field-hover",
9945
- "hover:bg-ui-bg-field-hover",
9946
- {
9947
- "size-7": size === "small",
9948
- "size-8": size === "base"
9949
- }
9950
- ),
9951
- type: "button",
9952
- onClick: handleIncrement,
9953
- disabled: max !== void 0 && value >= max || disabled,
9954
- children: [
9955
- /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
9956
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9957
- ]
9958
- }
9959
- )
9960
- ]
9922
+ showTooltip: isDisabled,
9923
+ content: "This row is disabled because it contains non-primitive data.",
9924
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
9925
+ /* @__PURE__ */ jsxRuntime.jsxs(
9926
+ "div",
9927
+ {
9928
+ className: ui.clx("grid grid-cols-2 divide-x", {
9929
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9930
+ }),
9931
+ children: [
9932
+ /* @__PURE__ */ jsxRuntime.jsx(
9933
+ Form$2.Field,
9934
+ {
9935
+ control: form.control,
9936
+ name: `metadata.${index}.key`,
9937
+ render: ({ field: field2 }) => {
9938
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
9939
+ GridInput,
9940
+ {
9941
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
9942
+ ...field2,
9943
+ disabled: isDisabled,
9944
+ placeholder: "Key"
9945
+ }
9946
+ ) }) });
9947
+ }
9948
+ }
9949
+ ),
9950
+ /* @__PURE__ */ jsxRuntime.jsx(
9951
+ Form$2.Field,
9952
+ {
9953
+ control: form.control,
9954
+ name: `metadata.${index}.value`,
9955
+ render: ({ field: { value, ...field2 } }) => {
9956
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
9957
+ GridInput,
9958
+ {
9959
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
9960
+ ...field2,
9961
+ value: isDisabled ? placeholder : value,
9962
+ disabled: isDisabled,
9963
+ placeholder: "Value"
9964
+ }
9965
+ ) }) });
9966
+ }
9967
+ }
9968
+ )
9969
+ ]
9970
+ }
9971
+ ),
9972
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
9973
+ /* @__PURE__ */ jsxRuntime.jsx(
9974
+ ui.DropdownMenu.Trigger,
9975
+ {
9976
+ className: ui.clx(
9977
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9978
+ {
9979
+ hidden: isDisabled
9980
+ }
9981
+ ),
9982
+ disabled: isDisabled,
9983
+ asChild: true,
9984
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
9985
+ }
9986
+ ),
9987
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
9988
+ /* @__PURE__ */ jsxRuntime.jsxs(
9989
+ ui.DropdownMenu.Item,
9990
+ {
9991
+ className: "gap-x-2",
9992
+ onClick: () => insertRow(index, "above"),
9993
+ children: [
9994
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
9995
+ "Insert row above"
9996
+ ]
9997
+ }
9998
+ ),
9999
+ /* @__PURE__ */ jsxRuntime.jsxs(
10000
+ ui.DropdownMenu.Item,
10001
+ {
10002
+ className: "gap-x-2",
10003
+ onClick: () => insertRow(index, "below"),
10004
+ children: [
10005
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
10006
+ "Insert row below"
10007
+ ]
10008
+ }
10009
+ ),
10010
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
10011
+ /* @__PURE__ */ jsxRuntime.jsxs(
10012
+ ui.DropdownMenu.Item,
10013
+ {
10014
+ className: "gap-x-2",
10015
+ onClick: () => deleteRow(index),
10016
+ children: [
10017
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
10018
+ "Delete row"
10019
+ ]
10020
+ }
10021
+ )
10022
+ ] })
10023
+ ] })
10024
+ ] })
10025
+ },
10026
+ field.id
10027
+ );
10028
+ })
10029
+ ] }),
10030
+ hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
10031
+ ] }),
10032
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10033
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10034
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10035
+ ] }) })
10036
+ ]
10037
+ }
10038
+ ) });
10039
+ };
10040
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
10041
+ return /* @__PURE__ */ jsxRuntime.jsx(
10042
+ "input",
10043
+ {
10044
+ ref,
10045
+ ...props,
10046
+ autoComplete: "off",
10047
+ className: ui.clx(
10048
+ "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",
10049
+ className
10050
+ )
10051
+ }
10052
+ );
10053
+ });
10054
+ GridInput.displayName = "MetadataForm.GridInput";
10055
+ const PlaceholderInner = () => {
10056
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10057
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10058
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10059
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
10060
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
10061
+ ] }) })
10062
+ ] });
10063
+ };
10064
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
10065
+ function getDefaultValues(metadata) {
10066
+ if (!metadata || !Object.keys(metadata).length) {
10067
+ return [
10068
+ {
10069
+ key: "",
10070
+ value: "",
10071
+ disabled: false
9961
10072
  }
9962
- );
10073
+ ];
9963
10074
  }
9964
- );
9965
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9966
- const productVariantsQueryKeys = {
10075
+ return Object.entries(metadata).map(([key, value]) => {
10076
+ if (!EDITABLE_TYPES.includes(typeof value)) {
10077
+ return {
10078
+ key,
10079
+ value,
10080
+ disabled: true
10081
+ };
10082
+ }
10083
+ let stringValue = value;
10084
+ if (typeof value !== "string") {
10085
+ stringValue = JSON.stringify(value);
10086
+ }
10087
+ return {
10088
+ key,
10089
+ value: stringValue,
10090
+ original_key: key
10091
+ };
10092
+ });
10093
+ }
10094
+ function parseValues(values) {
10095
+ const metadata = values.metadata;
10096
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10097
+ if (isEmpty) {
10098
+ return null;
10099
+ }
10100
+ const update = {};
10101
+ metadata.forEach((field) => {
10102
+ let key = field.key;
10103
+ let value = field.value;
10104
+ const disabled = field.disabled;
10105
+ if (!key || !value) {
10106
+ return;
10107
+ }
10108
+ if (disabled) {
10109
+ update[key] = value;
10110
+ return;
10111
+ }
10112
+ key = key.trim();
10113
+ value = value.trim();
10114
+ if (value === "true") {
10115
+ update[key] = true;
10116
+ } else if (value === "false") {
10117
+ update[key] = false;
10118
+ } else {
10119
+ const parsedNumber = parseFloat(value);
10120
+ if (!isNaN(parsedNumber)) {
10121
+ update[key] = parsedNumber;
10122
+ } else {
10123
+ update[key] = value;
10124
+ }
10125
+ }
10126
+ });
10127
+ return update;
10128
+ }
10129
+ function getHasUneditableRows(metadata) {
10130
+ if (!metadata) {
10131
+ return false;
10132
+ }
10133
+ return Object.values(metadata).some(
10134
+ (value) => !EDITABLE_TYPES.includes(typeof value)
10135
+ );
10136
+ }
10137
+ const PROMOTION_QUERY_KEY = "promotions";
10138
+ const promotionsQueryKeys = {
9967
10139
  list: (query2) => [
9968
- PRODUCT_VARIANTS_QUERY_KEY,
10140
+ PROMOTION_QUERY_KEY,
10141
+ query2 ? query2 : void 0
10142
+ ],
10143
+ detail: (id, query2) => [
10144
+ PROMOTION_QUERY_KEY,
10145
+ id,
9969
10146
  query2 ? query2 : void 0
9970
10147
  ]
9971
10148
  };
9972
- const useProductVariants = (query2, options) => {
10149
+ const usePromotions = (query2, options) => {
9973
10150
  const { data, ...rest } = reactQuery.useQuery({
9974
- queryKey: productVariantsQueryKeys.list(query2),
9975
- queryFn: async () => await sdk.admin.productVariant.list(query2),
10151
+ queryKey: promotionsQueryKeys.list(query2),
10152
+ queryFn: async () => sdk.admin.promotion.list(query2),
9976
10153
  ...options
9977
10154
  });
9978
10155
  return { ...data, ...rest };
@@ -10023,65 +10200,85 @@ const useInitiateOrderEdit = ({
10023
10200
  run();
10024
10201
  }, [preview, navigate, mutateAsync]);
10025
10202
  };
10026
- function convertNumber(value) {
10027
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10028
- }
10029
- const STACKED_MODAL_ID = "items_stacked_modal";
10030
- const Items = () => {
10203
+ const Promotions = () => {
10031
10204
  const { id } = reactRouterDom.useParams();
10032
10205
  const {
10033
10206
  order: preview,
10034
- isPending: isPreviewPending,
10035
10207
  isError: isPreviewError,
10036
10208
  error: previewError
10037
- } = useOrderPreview(id, void 0, {
10038
- placeholderData: reactQuery.keepPreviousData
10039
- });
10209
+ } = useOrderPreview(id, void 0);
10040
10210
  useInitiateOrderEdit({ preview });
10041
- const { draft_order, isPending, isError, error } = useDraftOrder(
10042
- id,
10043
- {
10044
- fields: "currency_code"
10045
- },
10046
- {
10047
- enabled: !!id
10048
- }
10049
- );
10050
10211
  const { onCancel } = useCancelOrderEdit({ preview });
10051
- if (isError) {
10052
- throw error;
10053
- }
10054
10212
  if (isPreviewError) {
10055
10213
  throw previewError;
10056
10214
  }
10057
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10058
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10059
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10060
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10061
- ] }) });
10215
+ const isReady = !!preview;
10216
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
10217
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
10218
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
10219
+ ] });
10062
10220
  };
10063
- const ItemsForm = ({ preview, currencyCode }) => {
10064
- var _a;
10221
+ const PromotionForm = ({ preview }) => {
10222
+ const { items, shipping_methods } = preview;
10065
10223
  const [isSubmitting, setIsSubmitting] = React.useState(false);
10066
- const [modalContent, setModalContent] = React.useState(
10067
- null
10068
- );
10224
+ const [comboboxValue, setComboboxValue] = React.useState("");
10069
10225
  const { handleSuccess } = useRouteModal();
10070
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10226
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10227
+ const promoIds = getPromotionIds(items, shipping_methods);
10228
+ const { promotions, isPending, isError, error } = usePromotions(
10229
+ {
10230
+ id: promoIds
10231
+ },
10232
+ {
10233
+ enabled: !!promoIds.length
10234
+ }
10235
+ );
10236
+ const comboboxData = useComboboxData({
10237
+ queryKey: ["promotions", "combobox", promoIds],
10238
+ queryFn: async (params) => {
10239
+ return await sdk.admin.promotion.list({
10240
+ ...params,
10241
+ id: {
10242
+ $nin: promoIds
10243
+ }
10244
+ });
10245
+ },
10246
+ getOptions: (data) => {
10247
+ return data.promotions.map((promotion) => ({
10248
+ label: promotion.code,
10249
+ value: promotion.code
10250
+ }));
10251
+ }
10252
+ });
10253
+ const add = async (value) => {
10254
+ if (!value) {
10255
+ return;
10256
+ }
10257
+ addPromotions(
10258
+ {
10259
+ promo_codes: [value]
10260
+ },
10261
+ {
10262
+ onError: (e) => {
10263
+ ui.toast.error(e.message);
10264
+ comboboxData.onSearchValueChange("");
10265
+ setComboboxValue("");
10266
+ },
10267
+ onSuccess: () => {
10268
+ comboboxData.onSearchValueChange("");
10269
+ setComboboxValue("");
10270
+ }
10271
+ }
10272
+ );
10273
+ };
10071
10274
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10072
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10073
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10074
- const matches = React.useMemo(() => {
10075
- return matchSorter.matchSorter(preview.items, query2, {
10076
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10077
- });
10078
- }, [preview.items, query2]);
10275
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10079
10276
  const onSubmit = async () => {
10080
10277
  setIsSubmitting(true);
10081
10278
  let requestSucceeded = false;
10082
10279
  await requestOrderEdit(void 0, {
10083
10280
  onError: (e) => {
10084
- ui.toast.error(`Failed to request order edit: ${e.message}`);
10281
+ ui.toast.error(e.message);
10085
10282
  },
10086
10283
  onSuccess: () => {
10087
10284
  requestSucceeded = true;
@@ -10093,7 +10290,7 @@ const ItemsForm = ({ preview, currencyCode }) => {
10093
10290
  }
10094
10291
  await confirmOrderEdit(void 0, {
10095
10292
  onError: (e) => {
10096
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10293
+ ui.toast.error(e.message);
10097
10294
  },
10098
10295
  onSuccess: () => {
10099
10296
  handleSuccess();
@@ -10103,1792 +10300,399 @@ const ItemsForm = ({ preview, currencyCode }) => {
10103
10300
  }
10104
10301
  });
10105
10302
  };
10106
- const onKeyDown = React.useCallback(
10107
- (e) => {
10108
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10109
- if (modalContent || isSubmitting) {
10110
- return;
10303
+ if (isError) {
10304
+ throw error;
10305
+ }
10306
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10307
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
10308
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
10309
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10310
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10311
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10312
+ ] }),
10313
+ /* @__PURE__ */ jsxRuntime.jsx(
10314
+ Combobox,
10315
+ {
10316
+ id: "promotion-combobox",
10317
+ "aria-describedby": "promotion-combobox-hint",
10318
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10319
+ fetchNextPage: comboboxData.fetchNextPage,
10320
+ options: comboboxData.options,
10321
+ onSearchValueChange: comboboxData.onSearchValueChange,
10322
+ searchValue: comboboxData.searchValue,
10323
+ disabled: comboboxData.disabled || isAddingPromotions,
10324
+ onChange: add,
10325
+ value: comboboxValue
10326
+ }
10327
+ )
10328
+ ] }),
10329
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10330
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
10331
+ PromotionItem,
10332
+ {
10333
+ promotion,
10334
+ orderId: preview.id,
10335
+ isLoading: isPending
10336
+ },
10337
+ promotion.id
10338
+ )) })
10339
+ ] }) }),
10340
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10341
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10342
+ /* @__PURE__ */ jsxRuntime.jsx(
10343
+ ui.Button,
10344
+ {
10345
+ size: "small",
10346
+ type: "submit",
10347
+ isLoading: isSubmitting || isAddingPromotions,
10348
+ children: "Save"
10349
+ }
10350
+ )
10351
+ ] }) })
10352
+ ] });
10353
+ };
10354
+ const PromotionItem = ({
10355
+ promotion,
10356
+ orderId,
10357
+ isLoading
10358
+ }) => {
10359
+ var _a;
10360
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10361
+ const onRemove = async () => {
10362
+ removePromotions(
10363
+ {
10364
+ promo_codes: [promotion.code]
10365
+ },
10366
+ {
10367
+ onError: (e) => {
10368
+ ui.toast.error(e.message);
10111
10369
  }
10112
- onSubmit();
10113
10370
  }
10371
+ );
10372
+ };
10373
+ const displayValue = getDisplayValue(promotion);
10374
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10375
+ "div",
10376
+ {
10377
+ className: ui.clx(
10378
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
10379
+ {
10380
+ "animate-pulse": isLoading
10381
+ }
10382
+ ),
10383
+ children: [
10384
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10385
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
10386
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
10387
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
10388
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
10389
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
10390
+ ] }),
10391
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
10392
+ ] })
10393
+ ] }),
10394
+ /* @__PURE__ */ jsxRuntime.jsx(
10395
+ ui.IconButton,
10396
+ {
10397
+ size: "small",
10398
+ type: "button",
10399
+ variant: "transparent",
10400
+ onClick: onRemove,
10401
+ isLoading: isPending || isLoading,
10402
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
10403
+ }
10404
+ )
10405
+ ]
10114
10406
  },
10115
- [modalContent, isSubmitting, onSubmit]
10407
+ promotion.id
10116
10408
  );
10117
- React.useEffect(() => {
10118
- document.addEventListener("keydown", onKeyDown);
10119
- return () => {
10120
- document.removeEventListener("keydown", onKeyDown);
10121
- };
10122
- }, [onKeyDown]);
10123
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10124
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10125
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10126
- StackedFocusModal,
10127
- {
10128
- id: STACKED_MODAL_ID,
10129
- onOpenChangeCallback: (open) => {
10130
- if (!open) {
10131
- setModalContent(null);
10132
- }
10133
- },
10134
- children: [
10135
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
10136
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10137
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10138
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Edit the items in the draft order" }) })
10139
- ] }),
10140
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10141
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10142
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10143
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10144
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10145
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10146
- ] }),
10147
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10148
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10149
- ui.Input,
10150
- {
10151
- type: "search",
10152
- placeholder: "Search items",
10153
- value: searchValue,
10154
- onChange: (e) => onSearchValueChange(e.target.value)
10155
- }
10156
- ) }),
10157
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10158
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10159
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10160
- /* @__PURE__ */ jsxRuntime.jsx(
10161
- StackedModalTrigger$1,
10162
- {
10163
- type: "add-items",
10164
- setModalContent
10165
- }
10166
- ),
10167
- /* @__PURE__ */ jsxRuntime.jsx(
10168
- StackedModalTrigger$1,
10169
- {
10170
- type: "add-custom-item",
10171
- setModalContent
10172
- }
10173
- )
10174
- ] })
10175
- ] })
10176
- ] })
10177
- ] }),
10178
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10179
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-muted grid grid-cols-[2fr_1fr_2fr_28px] gap-3 px-4 py-2", children: [
10180
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10181
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10182
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10183
- /* @__PURE__ */ jsxRuntime.jsx("div", {})
10184
- ] }) }),
10185
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
10186
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10187
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10188
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10189
- Item,
10190
- {
10191
- item,
10192
- preview,
10193
- currencyCode
10194
- },
10195
- item.id
10196
- )) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
10197
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10198
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10199
- 'No items found for "',
10200
- query2,
10201
- '".'
10202
- ] })
10203
- ] }) })
10204
- ] })
10205
- ] }),
10206
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10207
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10208
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10209
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10210
- ui.Text,
10211
- {
10212
- size: "small",
10213
- leading: "compact",
10214
- className: "text-ui-fg-subtle",
10215
- children: [
10216
- itemCount,
10217
- " ",
10218
- itemCount === 1 ? "item" : "items"
10219
- ]
10220
- }
10221
- ) }),
10222
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
10223
- ] })
10224
- ] }) }),
10225
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10226
- CustomItemForm,
10227
- {
10228
- orderId: preview.id,
10229
- currencyCode
10230
- }
10231
- ) : null)
10232
- ]
10409
+ };
10410
+ function getDisplayValue(promotion) {
10411
+ var _a, _b, _c, _d;
10412
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
10413
+ if (!value) {
10414
+ return null;
10415
+ }
10416
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
10417
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
10418
+ if (!currency) {
10419
+ return null;
10420
+ }
10421
+ return getLocaleAmount(value, currency);
10422
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
10423
+ return formatPercentage(value);
10424
+ }
10425
+ return null;
10426
+ }
10427
+ const formatter = new Intl.NumberFormat([], {
10428
+ style: "percent",
10429
+ minimumFractionDigits: 2
10430
+ });
10431
+ const formatPercentage = (value, isPercentageValue = false) => {
10432
+ let val = value || 0;
10433
+ if (!isPercentageValue) {
10434
+ val = val / 100;
10435
+ }
10436
+ return formatter.format(val);
10437
+ };
10438
+ function getPromotionIds(items, shippingMethods) {
10439
+ const promotionIds = /* @__PURE__ */ new Set();
10440
+ for (const item of items) {
10441
+ if (item.adjustments) {
10442
+ for (const adjustment of item.adjustments) {
10443
+ if (adjustment.promotion_id) {
10444
+ promotionIds.add(adjustment.promotion_id);
10445
+ }
10233
10446
  }
10234
- ) }),
10235
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10236
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10237
- /* @__PURE__ */ jsxRuntime.jsx(
10238
- ui.Button,
10239
- {
10240
- size: "small",
10241
- type: "button",
10242
- onClick: onSubmit,
10243
- isLoading: isSubmitting,
10244
- children: "Save"
10447
+ }
10448
+ }
10449
+ for (const shippingMethod of shippingMethods) {
10450
+ if (shippingMethod.adjustments) {
10451
+ for (const adjustment of shippingMethod.adjustments) {
10452
+ if (adjustment.promotion_id) {
10453
+ promotionIds.add(adjustment.promotion_id);
10245
10454
  }
10246
- )
10247
- ] }) })
10248
- ] });
10249
- };
10250
- const Item = ({ item, preview, currencyCode }) => {
10251
- if (item.variant_id) {
10252
- return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10455
+ }
10456
+ }
10253
10457
  }
10254
- return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10458
+ return Array.from(promotionIds);
10459
+ }
10460
+ const SalesChannel = () => {
10461
+ const { id } = reactRouterDom.useParams();
10462
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10463
+ id,
10464
+ {
10465
+ fields: "+sales_channel_id"
10466
+ },
10467
+ {
10468
+ enabled: !!id
10469
+ }
10470
+ );
10471
+ if (isError) {
10472
+ throw error;
10473
+ }
10474
+ const ISrEADY = !!draft_order && !isPending;
10475
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10476
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10477
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
10478
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
10479
+ ] }),
10480
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
10481
+ ] });
10255
10482
  };
10256
- const VariantItem = ({ item, preview, currencyCode }) => {
10257
- const [editing, setEditing] = React.useState(false);
10483
+ const SalesChannelForm = ({ order }) => {
10258
10484
  const form = reactHookForm.useForm({
10259
10485
  defaultValues: {
10260
- quantity: item.quantity,
10261
- unit_price: item.unit_price
10486
+ sales_channel_id: order.sales_channel_id || ""
10262
10487
  },
10263
- resolver: zod.zodResolver(variantItemSchema)
10488
+ resolver: zod.zodResolver(schema$3)
10264
10489
  });
10265
- const actionId = React.useMemo(() => {
10266
- var _a, _b;
10267
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10268
- }, [item]);
10269
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10270
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10271
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10490
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10491
+ const { handleSuccess } = useRouteModal();
10272
10492
  const onSubmit = form.handleSubmit(async (data) => {
10273
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10274
- setEditing(false);
10275
- return;
10276
- }
10277
- if (!actionId) {
10278
- await updateOriginalItem(
10279
- {
10280
- item_id: item.id,
10281
- quantity: data.quantity,
10282
- unit_price: convertNumber(data.unit_price)
10283
- },
10284
- {
10285
- onSuccess: () => {
10286
- setEditing(false);
10287
- },
10288
- onError: (e) => {
10289
- ui.toast.error(e.message);
10290
- }
10291
- }
10292
- );
10293
- return;
10294
- }
10295
- await updateActionItem(
10493
+ await mutateAsync(
10296
10494
  {
10297
- action_id: actionId,
10298
- quantity: data.quantity,
10299
- unit_price: convertNumber(data.unit_price)
10495
+ sales_channel_id: data.sales_channel_id
10300
10496
  },
10301
10497
  {
10302
10498
  onSuccess: () => {
10303
- setEditing(false);
10499
+ ui.toast.success("Sales channel updated");
10500
+ handleSuccess();
10304
10501
  },
10305
- onError: (e) => {
10306
- ui.toast.error(e.message);
10502
+ onError: (error) => {
10503
+ ui.toast.error(error.message);
10307
10504
  }
10308
10505
  }
10309
10506
  );
10310
10507
  });
10311
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10312
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10313
- /* @__PURE__ */ jsxRuntime.jsx(
10314
- Thumbnail,
10315
- {
10316
- thumbnail: item.thumbnail,
10317
- alt: item.product_title ?? void 0
10318
- }
10319
- ),
10320
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10321
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10322
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10323
- /* @__PURE__ */ jsxRuntime.jsxs(
10324
- ui.Text,
10325
- {
10326
- size: "small",
10327
- leading: "compact",
10328
- className: "text-ui-fg-subtle",
10329
- children: [
10330
- "(",
10331
- item.variant_title,
10332
- ")"
10333
- ]
10334
- }
10335
- )
10336
- ] }),
10337
- /* @__PURE__ */ jsxRuntime.jsx(
10338
- ui.Text,
10339
- {
10340
- size: "small",
10341
- leading: "compact",
10342
- className: "text-ui-fg-subtle",
10343
- children: item.variant_sku
10344
- }
10345
- )
10346
- ] })
10347
- ] }),
10348
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10349
- Form$2.Field,
10350
- {
10351
- control: form.control,
10352
- name: "quantity",
10353
- render: ({ field }) => {
10354
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10355
- }
10356
- }
10357
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10358
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10359
- Form$2.Field,
10360
- {
10361
- control: form.control,
10362
- name: "unit_price",
10363
- render: ({ field: { onChange, ...field } }) => {
10364
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10365
- ui.CurrencyInput,
10508
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10509
+ KeyboundForm,
10510
+ {
10511
+ className: "flex flex-1 flex-col overflow-hidden",
10512
+ onSubmit,
10513
+ children: [
10514
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
10515
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10516
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10517
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10518
+ ] }) })
10519
+ ]
10520
+ }
10521
+ ) });
10522
+ };
10523
+ const SalesChannelField = ({ control, order }) => {
10524
+ const salesChannels = useComboboxData({
10525
+ queryFn: async (params) => {
10526
+ return await sdk.admin.salesChannel.list(params);
10527
+ },
10528
+ queryKey: ["sales-channels"],
10529
+ getOptions: (data) => {
10530
+ return data.sales_channels.map((salesChannel) => ({
10531
+ label: salesChannel.name,
10532
+ value: salesChannel.id
10533
+ }));
10534
+ },
10535
+ defaultValue: order.sales_channel_id || void 0
10536
+ });
10537
+ return /* @__PURE__ */ jsxRuntime.jsx(
10538
+ Form$2.Field,
10539
+ {
10540
+ control,
10541
+ name: "sales_channel_id",
10542
+ render: ({ field }) => {
10543
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10544
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
10545
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10546
+ Combobox,
10366
10547
  {
10367
- ...field,
10368
- symbol: getNativeSymbol(currencyCode),
10369
- code: currencyCode,
10370
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10548
+ options: salesChannels.options,
10549
+ fetchNextPage: salesChannels.fetchNextPage,
10550
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
10551
+ searchValue: salesChannels.searchValue,
10552
+ onSearchValueChange: salesChannels.onSearchValueChange,
10553
+ placeholder: "Select sales channel",
10554
+ ...field
10371
10555
  }
10372
- ) }) });
10373
- }
10374
- }
10375
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10376
- /* @__PURE__ */ jsxRuntime.jsx(
10377
- ui.IconButton,
10378
- {
10379
- type: "button",
10380
- size: "small",
10381
- onClick: editing ? onSubmit : () => {
10382
- setEditing(true);
10383
- },
10384
- disabled: isPending,
10385
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10556
+ ) }),
10557
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10558
+ ] });
10386
10559
  }
10387
- )
10388
- ] }) }) });
10560
+ }
10561
+ );
10389
10562
  };
10390
- const variantItemSchema = objectType({
10391
- quantity: numberType(),
10392
- unit_price: unionType([numberType(), stringType()])
10563
+ const schema$3 = objectType({
10564
+ sales_channel_id: stringType().min(1)
10393
10565
  });
10394
- const CustomItem = ({ item, preview, currencyCode }) => {
10395
- const [editing, setEditing] = React.useState(false);
10396
- const { quantity, unit_price, title } = item;
10397
- const form = reactHookForm.useForm({
10398
- defaultValues: {
10399
- title,
10400
- quantity,
10401
- unit_price
10402
- },
10403
- resolver: zod.zodResolver(customItemSchema)
10566
+ function convertNumber(value) {
10567
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10568
+ }
10569
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
10570
+ const Shipping = () => {
10571
+ var _a;
10572
+ const { id } = reactRouterDom.useParams();
10573
+ const { order, isPending, isError, error } = useOrder(id, {
10574
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
10404
10575
  });
10405
- React.useEffect(() => {
10406
- form.reset({
10407
- title,
10408
- quantity,
10409
- unit_price
10410
- });
10411
- }, [form, title, quantity, unit_price]);
10412
- const actionId = React.useMemo(() => {
10413
- var _a, _b;
10414
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10415
- }, [item]);
10416
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10417
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10418
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10419
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10420
- const onSubmit = form.handleSubmit(async (data) => {
10421
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10422
- setEditing(false);
10423
- return;
10424
- }
10425
- if (!actionId) {
10426
- await updateOriginalItem(
10427
- {
10428
- item_id: item.id,
10429
- quantity: data.quantity,
10430
- unit_price: convertNumber(data.unit_price)
10431
- },
10432
- {
10433
- onSuccess: () => {
10434
- setEditing(false);
10435
- },
10436
- onError: (e) => {
10437
- ui.toast.error(e.message);
10438
- }
10439
- }
10440
- );
10441
- return;
10576
+ const {
10577
+ order: preview,
10578
+ isPending: isPreviewPending,
10579
+ isError: isPreviewError,
10580
+ error: previewError
10581
+ } = useOrderPreview(id);
10582
+ useInitiateOrderEdit({ preview });
10583
+ const { onCancel } = useCancelOrderEdit({ preview });
10584
+ if (isError) {
10585
+ throw error;
10586
+ }
10587
+ if (isPreviewError) {
10588
+ throw previewError;
10589
+ }
10590
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
10591
+ const isReady = preview && !isPreviewPending && order && !isPending;
10592
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
10593
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10594
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
10595
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
10596
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
10597
+ ] }) }) }),
10598
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
10599
+ ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10600
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
10601
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10602
+ ] }) });
10603
+ };
10604
+ const ShippingForm = ({ preview, order }) => {
10605
+ var _a;
10606
+ const { setIsOpen } = useStackedModal();
10607
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
10608
+ const [data, setData] = React.useState(null);
10609
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
10610
+ const { shipping_options } = useShippingOptions(
10611
+ {
10612
+ id: appliedShippingOptionIds,
10613
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
10614
+ },
10615
+ {
10616
+ enabled: appliedShippingOptionIds.length > 0
10442
10617
  }
10443
- if (data.quantity === 0) {
10444
- await removeActionItem(actionId, {
10445
- onSuccess: () => {
10446
- setEditing(false);
10447
- },
10448
- onError: (e) => {
10449
- ui.toast.error(e.message);
10450
- }
10451
- });
10618
+ );
10619
+ const uniqueShippingProfiles = React.useMemo(() => {
10620
+ const profiles = /* @__PURE__ */ new Map();
10621
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
10622
+ profiles.set(profile.id, profile);
10623
+ });
10624
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
10625
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
10626
+ });
10627
+ return Array.from(profiles.values());
10628
+ }, [order.items, shipping_options]);
10629
+ const { handleSuccess } = useRouteModal();
10630
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10631
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10632
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
10633
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
10634
+ const onSubmit = async () => {
10635
+ setIsSubmitting(true);
10636
+ let requestSucceeded = false;
10637
+ await requestOrderEdit(void 0, {
10638
+ onError: (e) => {
10639
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
10640
+ },
10641
+ onSuccess: () => {
10642
+ requestSucceeded = true;
10643
+ }
10644
+ });
10645
+ if (!requestSucceeded) {
10646
+ setIsSubmitting(false);
10452
10647
  return;
10453
10648
  }
10454
- await updateActionItem(
10455
- {
10456
- action_id: actionId,
10457
- quantity: data.quantity,
10458
- unit_price: convertNumber(data.unit_price)
10649
+ await confirmOrderEdit(void 0, {
10650
+ onError: (e) => {
10651
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10459
10652
  },
10460
- {
10461
- onSuccess: () => {
10462
- setEditing(false);
10463
- },
10464
- onError: (e) => {
10465
- ui.toast.error(e.message);
10653
+ onSuccess: () => {
10654
+ handleSuccess();
10655
+ },
10656
+ onSettled: () => {
10657
+ setIsSubmitting(false);
10658
+ }
10659
+ });
10660
+ };
10661
+ const onKeydown = React.useCallback(
10662
+ (e) => {
10663
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10664
+ if (data || isSubmitting) {
10665
+ return;
10466
10666
  }
10667
+ onSubmit();
10467
10668
  }
10468
- );
10469
- });
10470
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10471
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10472
- /* @__PURE__ */ jsxRuntime.jsx(
10473
- Thumbnail,
10474
- {
10475
- thumbnail: item.thumbnail,
10476
- alt: item.title ?? void 0
10477
- }
10478
- ),
10479
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10480
- Form$2.Field,
10481
- {
10482
- control: form.control,
10483
- name: "title",
10484
- render: ({ field }) => {
10485
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
10486
- }
10487
- }
10488
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
10489
- ] }),
10490
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10491
- Form$2.Field,
10492
- {
10493
- control: form.control,
10494
- name: "quantity",
10495
- render: ({ field }) => {
10496
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10497
- }
10498
- }
10499
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
10500
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10501
- Form$2.Field,
10502
- {
10503
- control: form.control,
10504
- name: "unit_price",
10505
- render: ({ field: { onChange, ...field } }) => {
10506
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10507
- ui.CurrencyInput,
10508
- {
10509
- ...field,
10510
- symbol: getNativeSymbol(currencyCode),
10511
- code: currencyCode,
10512
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10513
- }
10514
- ) }) });
10515
- }
10516
- }
10517
- ) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10518
- /* @__PURE__ */ jsxRuntime.jsx(
10519
- ui.IconButton,
10520
- {
10521
- type: "button",
10522
- size: "small",
10523
- onClick: editing ? onSubmit : () => {
10524
- setEditing(true);
10525
- },
10526
- disabled: isPending,
10527
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10528
- }
10529
- )
10530
- ] }) }) });
10531
- };
10532
- const StackedModalTrigger$1 = ({
10533
- type,
10534
- setModalContent
10535
- }) => {
10536
- const { setIsOpen } = useStackedModal();
10537
- const onClick = React.useCallback(() => {
10538
- setModalContent(type);
10539
- setIsOpen(STACKED_MODAL_ID, true);
10540
- }, [setModalContent, setIsOpen, type]);
10541
- return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10542
- };
10543
- const VARIANT_PREFIX = "items";
10544
- const LIMIT = 50;
10545
- const ExistingItemsForm = ({ orderId, items }) => {
10546
- const { setIsOpen } = useStackedModal();
10547
- const [rowSelection, setRowSelection] = React.useState(
10548
- items.reduce((acc, item) => {
10549
- acc[item.variant_id] = true;
10550
- return acc;
10551
- }, {})
10552
- );
10553
- React.useEffect(() => {
10554
- setRowSelection(
10555
- items.reduce((acc, item) => {
10556
- if (item.variant_id) {
10557
- acc[item.variant_id] = true;
10558
- }
10559
- return acc;
10560
- }, {})
10561
- );
10562
- }, [items]);
10563
- const { q, order, offset } = useQueryParams(
10564
- ["q", "order", "offset"],
10565
- VARIANT_PREFIX
10566
- );
10567
- const { variants, count, isPending, isError, error } = useProductVariants(
10568
- {
10569
- q,
10570
- order,
10571
- offset: offset ? parseInt(offset) : void 0,
10572
- limit: LIMIT
10573
10669
  },
10574
- {
10575
- placeholderData: reactQuery.keepPreviousData
10576
- }
10670
+ [data, isSubmitting, onSubmit]
10577
10671
  );
10578
- const columns = useColumns();
10579
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10580
- const onSubmit = async () => {
10581
- const ids = Object.keys(rowSelection).filter(
10582
- (id) => !items.find((i) => i.variant_id === id)
10583
- );
10584
- await mutateAsync(
10585
- {
10586
- items: ids.map((id) => ({
10587
- variant_id: id,
10588
- quantity: 1
10589
- }))
10590
- },
10591
- {
10592
- onSuccess: () => {
10593
- setRowSelection({});
10594
- setIsOpen(STACKED_MODAL_ID, false);
10595
- },
10596
- onError: (e) => {
10597
- ui.toast.error(e.message);
10598
- }
10599
- }
10600
- );
10601
- };
10602
- if (isError) {
10603
- throw error;
10604
- }
10605
- return /* @__PURE__ */ jsxRuntime.jsxs(
10606
- StackedFocusModal.Content,
10607
- {
10608
- onOpenAutoFocus: (e) => {
10609
- e.preventDefault();
10610
- const searchInput = document.querySelector(
10611
- "[data-modal-id='modal-search-input']"
10612
- );
10613
- if (searchInput) {
10614
- searchInput.focus();
10615
- }
10616
- },
10617
- children: [
10618
- /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
10619
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10620
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10672
+ React.useEffect(() => {
10673
+ document.addEventListener("keydown", onKeydown);
10674
+ return () => {
10675
+ document.removeEventListener("keydown", onKeydown);
10676
+ };
10677
+ }, [onKeydown]);
10678
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10679
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10680
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
10681
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
10682
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10683
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
10684
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
10621
10685
  ] }),
10622
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
10623
- DataTable,
10624
- {
10625
- data: variants,
10626
- columns,
10627
- isLoading: isPending,
10628
- getRowId: (row) => row.id,
10629
- rowCount: count,
10630
- prefix: VARIANT_PREFIX,
10631
- layout: "fill",
10632
- rowSelection: {
10633
- state: rowSelection,
10634
- onRowSelectionChange: setRowSelection,
10635
- enableRowSelection: (row) => {
10636
- return !items.find((i) => i.variant_id === row.original.id);
10637
- }
10638
- },
10639
- autoFocusSearch: true
10640
- }
10641
- ) }),
10642
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10643
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10644
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10645
- ] }) })
10646
- ]
10647
- }
10648
- );
10649
- };
10650
- const columnHelper = ui.createDataTableColumnHelper();
10651
- const useColumns = () => {
10652
- return React.useMemo(() => {
10653
- return [
10654
- columnHelper.select(),
10655
- columnHelper.accessor("product.title", {
10656
- header: "Product",
10657
- cell: ({ row }) => {
10658
- var _a, _b, _c;
10659
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
10686
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10687
+ /* @__PURE__ */ jsxRuntime.jsx(radixUi.Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
10688
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
10660
10689
  /* @__PURE__ */ jsxRuntime.jsx(
10661
- Thumbnail,
10690
+ ui.Text,
10662
10691
  {
10663
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10664
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10665
- }
10666
- ),
10667
- /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10668
- ] });
10669
- },
10670
- enableSorting: true
10671
- }),
10672
- columnHelper.accessor("title", {
10673
- header: "Variant",
10674
- enableSorting: true
10675
- }),
10676
- columnHelper.accessor("sku", {
10677
- header: "SKU",
10678
- cell: ({ getValue }) => {
10679
- return getValue() ?? "-";
10680
- },
10681
- enableSorting: true
10682
- }),
10683
- columnHelper.accessor("updated_at", {
10684
- header: "Updated",
10685
- cell: ({ getValue }) => {
10686
- return /* @__PURE__ */ jsxRuntime.jsx(
10687
- ui.Tooltip,
10688
- {
10689
- content: getFullDate({ date: getValue(), includeTime: true }),
10690
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10691
- }
10692
- );
10693
- },
10694
- enableSorting: true,
10695
- sortAscLabel: "Oldest first",
10696
- sortDescLabel: "Newest first"
10697
- }),
10698
- columnHelper.accessor("created_at", {
10699
- header: "Created",
10700
- cell: ({ getValue }) => {
10701
- return /* @__PURE__ */ jsxRuntime.jsx(
10702
- ui.Tooltip,
10703
- {
10704
- content: getFullDate({ date: getValue(), includeTime: true }),
10705
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10706
- }
10707
- );
10708
- },
10709
- enableSorting: true,
10710
- sortAscLabel: "Oldest first",
10711
- sortDescLabel: "Newest first"
10712
- })
10713
- ];
10714
- }, []);
10715
- };
10716
- const CustomItemForm = ({ orderId, currencyCode }) => {
10717
- const { setIsOpen } = useStackedModal();
10718
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10719
- const form = reactHookForm.useForm({
10720
- defaultValues: {
10721
- title: "",
10722
- quantity: 1,
10723
- unit_price: ""
10724
- },
10725
- resolver: zod.zodResolver(customItemSchema)
10726
- });
10727
- const onSubmit = form.handleSubmit(async (data) => {
10728
- await addItems(
10729
- {
10730
- items: [
10731
- {
10732
- title: data.title,
10733
- quantity: data.quantity,
10734
- unit_price: convertNumber(data.unit_price)
10735
- }
10736
- ]
10737
- },
10738
- {
10739
- onSuccess: () => {
10740
- setIsOpen(STACKED_MODAL_ID, false);
10741
- },
10742
- onError: (e) => {
10743
- ui.toast.error(e.message);
10744
- }
10745
- }
10746
- );
10747
- });
10748
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
10749
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
10750
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
10751
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10752
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
10753
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
10754
- ] }),
10755
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10756
- /* @__PURE__ */ jsxRuntime.jsx(
10757
- Form$2.Field,
10758
- {
10759
- control: form.control,
10760
- name: "title",
10761
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10762
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10763
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
10764
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
10765
- ] }),
10766
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10767
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10768
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10769
- ] })
10770
- ] }) })
10771
- }
10772
- ),
10773
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10774
- /* @__PURE__ */ jsxRuntime.jsx(
10775
- Form$2.Field,
10776
- {
10777
- control: form.control,
10778
- name: "unit_price",
10779
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10780
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10781
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
10782
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10783
- ] }),
10784
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10785
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10786
- ui.CurrencyInput,
10787
- {
10788
- symbol: getNativeSymbol(currencyCode),
10789
- code: currencyCode,
10790
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10791
- ...field
10792
- }
10793
- ) }),
10794
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10795
- ] })
10796
- ] }) })
10797
- }
10798
- ),
10799
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10800
- /* @__PURE__ */ jsxRuntime.jsx(
10801
- Form$2.Field,
10802
- {
10803
- control: form.control,
10804
- name: "quantity",
10805
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10806
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10807
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
10808
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10809
- ] }),
10810
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
10811
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10812
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10813
- ] })
10814
- ] }) })
10815
- }
10816
- )
10817
- ] }) }) }),
10818
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10819
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10820
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10821
- ] }) })
10822
- ] }) }) });
10823
- };
10824
- const customItemSchema = objectType({
10825
- title: stringType().min(1),
10826
- quantity: numberType(),
10827
- unit_price: unionType([numberType(), stringType()])
10828
- });
10829
- const InlineTip = React.forwardRef(
10830
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10831
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10832
- return /* @__PURE__ */ jsxRuntime.jsxs(
10833
- "div",
10834
- {
10835
- ref,
10836
- className: ui.clx(
10837
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10838
- className
10839
- ),
10840
- ...props,
10841
- children: [
10842
- /* @__PURE__ */ jsxRuntime.jsx(
10843
- "div",
10844
- {
10845
- role: "presentation",
10846
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10847
- "bg-ui-tag-orange-icon": variant === "warning"
10848
- })
10849
- }
10850
- ),
10851
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10852
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10853
- labelValue,
10854
- ":"
10855
- ] }),
10856
- " ",
10857
- children
10858
- ] })
10859
- ]
10860
- }
10861
- );
10862
- }
10863
- );
10864
- InlineTip.displayName = "InlineTip";
10865
- const MetadataFieldSchema = objectType({
10866
- key: stringType(),
10867
- disabled: booleanType().optional(),
10868
- value: anyType()
10869
- });
10870
- const MetadataSchema = objectType({
10871
- metadata: arrayType(MetadataFieldSchema)
10872
- });
10873
- const Metadata = () => {
10874
- const { id } = reactRouterDom.useParams();
10875
- const { order, isPending, isError, error } = useOrder(id, {
10876
- fields: "metadata"
10877
- });
10878
- if (isError) {
10879
- throw error;
10880
- }
10881
- const isReady = !isPending && !!order;
10882
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10883
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10884
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10885
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10886
- ] }),
10887
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10888
- ] });
10889
- };
10890
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10891
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10892
- const MetadataForm = ({ orderId, metadata }) => {
10893
- const { handleSuccess } = useRouteModal();
10894
- const hasUneditableRows = getHasUneditableRows(metadata);
10895
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10896
- const form = reactHookForm.useForm({
10897
- defaultValues: {
10898
- metadata: getDefaultValues(metadata)
10899
- },
10900
- resolver: zod.zodResolver(MetadataSchema)
10901
- });
10902
- const handleSubmit = form.handleSubmit(async (data) => {
10903
- const parsedData = parseValues(data);
10904
- await mutateAsync(
10905
- {
10906
- metadata: parsedData
10907
- },
10908
- {
10909
- onSuccess: () => {
10910
- ui.toast.success("Metadata updated");
10911
- handleSuccess();
10912
- },
10913
- onError: (error) => {
10914
- ui.toast.error(error.message);
10915
- }
10916
- }
10917
- );
10918
- });
10919
- const { fields, insert, remove } = reactHookForm.useFieldArray({
10920
- control: form.control,
10921
- name: "metadata"
10922
- });
10923
- function deleteRow(index) {
10924
- remove(index);
10925
- if (fields.length === 1) {
10926
- insert(0, {
10927
- key: "",
10928
- value: "",
10929
- disabled: false
10930
- });
10931
- }
10932
- }
10933
- function insertRow(index, position) {
10934
- insert(index + (position === "above" ? 0 : 1), {
10935
- key: "",
10936
- value: "",
10937
- disabled: false
10938
- });
10939
- }
10940
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10941
- KeyboundForm,
10942
- {
10943
- onSubmit: handleSubmit,
10944
- className: "flex flex-1 flex-col overflow-hidden",
10945
- children: [
10946
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10947
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10948
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10949
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
10950
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
10951
- ] }),
10952
- fields.map((field, index) => {
10953
- const isDisabled = field.disabled || false;
10954
- let placeholder = "-";
10955
- if (typeof field.value === "object") {
10956
- placeholder = "{ ... }";
10957
- }
10958
- if (Array.isArray(field.value)) {
10959
- placeholder = "[ ... ]";
10960
- }
10961
- return /* @__PURE__ */ jsxRuntime.jsx(
10962
- ConditionalTooltip,
10963
- {
10964
- showTooltip: isDisabled,
10965
- content: "This row is disabled because it contains non-primitive data.",
10966
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
10967
- /* @__PURE__ */ jsxRuntime.jsxs(
10968
- "div",
10969
- {
10970
- className: ui.clx("grid grid-cols-2 divide-x", {
10971
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10972
- }),
10973
- children: [
10974
- /* @__PURE__ */ jsxRuntime.jsx(
10975
- Form$2.Field,
10976
- {
10977
- control: form.control,
10978
- name: `metadata.${index}.key`,
10979
- render: ({ field: field2 }) => {
10980
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10981
- GridInput,
10982
- {
10983
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10984
- ...field2,
10985
- disabled: isDisabled,
10986
- placeholder: "Key"
10987
- }
10988
- ) }) });
10989
- }
10990
- }
10991
- ),
10992
- /* @__PURE__ */ jsxRuntime.jsx(
10993
- Form$2.Field,
10994
- {
10995
- control: form.control,
10996
- name: `metadata.${index}.value`,
10997
- render: ({ field: { value, ...field2 } }) => {
10998
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10999
- GridInput,
11000
- {
11001
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
11002
- ...field2,
11003
- value: isDisabled ? placeholder : value,
11004
- disabled: isDisabled,
11005
- placeholder: "Value"
11006
- }
11007
- ) }) });
11008
- }
11009
- }
11010
- )
11011
- ]
11012
- }
11013
- ),
11014
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
11015
- /* @__PURE__ */ jsxRuntime.jsx(
11016
- ui.DropdownMenu.Trigger,
11017
- {
11018
- className: ui.clx(
11019
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11020
- {
11021
- hidden: isDisabled
11022
- }
11023
- ),
11024
- disabled: isDisabled,
11025
- asChild: true,
11026
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
11027
- }
11028
- ),
11029
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11030
- /* @__PURE__ */ jsxRuntime.jsxs(
11031
- ui.DropdownMenu.Item,
11032
- {
11033
- className: "gap-x-2",
11034
- onClick: () => insertRow(index, "above"),
11035
- children: [
11036
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11037
- "Insert row above"
11038
- ]
11039
- }
11040
- ),
11041
- /* @__PURE__ */ jsxRuntime.jsxs(
11042
- ui.DropdownMenu.Item,
11043
- {
11044
- className: "gap-x-2",
11045
- onClick: () => insertRow(index, "below"),
11046
- children: [
11047
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11048
- "Insert row below"
11049
- ]
11050
- }
11051
- ),
11052
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11053
- /* @__PURE__ */ jsxRuntime.jsxs(
11054
- ui.DropdownMenu.Item,
11055
- {
11056
- className: "gap-x-2",
11057
- onClick: () => deleteRow(index),
11058
- children: [
11059
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11060
- "Delete row"
11061
- ]
11062
- }
11063
- )
11064
- ] })
11065
- ] })
11066
- ] })
11067
- },
11068
- field.id
11069
- );
11070
- })
11071
- ] }),
11072
- hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
11073
- ] }),
11074
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11075
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11076
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11077
- ] }) })
11078
- ]
11079
- }
11080
- ) });
11081
- };
11082
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11083
- return /* @__PURE__ */ jsxRuntime.jsx(
11084
- "input",
11085
- {
11086
- ref,
11087
- ...props,
11088
- autoComplete: "off",
11089
- className: ui.clx(
11090
- "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",
11091
- className
11092
- )
11093
- }
11094
- );
11095
- });
11096
- GridInput.displayName = "MetadataForm.GridInput";
11097
- const PlaceholderInner = () => {
11098
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11099
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11100
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11101
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11102
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11103
- ] }) })
11104
- ] });
11105
- };
11106
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11107
- function getDefaultValues(metadata) {
11108
- if (!metadata || !Object.keys(metadata).length) {
11109
- return [
11110
- {
11111
- key: "",
11112
- value: "",
11113
- disabled: false
11114
- }
11115
- ];
11116
- }
11117
- return Object.entries(metadata).map(([key, value]) => {
11118
- if (!EDITABLE_TYPES.includes(typeof value)) {
11119
- return {
11120
- key,
11121
- value,
11122
- disabled: true
11123
- };
11124
- }
11125
- let stringValue = value;
11126
- if (typeof value !== "string") {
11127
- stringValue = JSON.stringify(value);
11128
- }
11129
- return {
11130
- key,
11131
- value: stringValue,
11132
- original_key: key
11133
- };
11134
- });
11135
- }
11136
- function parseValues(values) {
11137
- const metadata = values.metadata;
11138
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11139
- if (isEmpty) {
11140
- return null;
11141
- }
11142
- const update = {};
11143
- metadata.forEach((field) => {
11144
- let key = field.key;
11145
- let value = field.value;
11146
- const disabled = field.disabled;
11147
- if (!key || !value) {
11148
- return;
11149
- }
11150
- if (disabled) {
11151
- update[key] = value;
11152
- return;
11153
- }
11154
- key = key.trim();
11155
- value = value.trim();
11156
- if (value === "true") {
11157
- update[key] = true;
11158
- } else if (value === "false") {
11159
- update[key] = false;
11160
- } else {
11161
- const parsedNumber = parseFloat(value);
11162
- if (!isNaN(parsedNumber)) {
11163
- update[key] = parsedNumber;
11164
- } else {
11165
- update[key] = value;
11166
- }
11167
- }
11168
- });
11169
- return update;
11170
- }
11171
- function getHasUneditableRows(metadata) {
11172
- if (!metadata) {
11173
- return false;
11174
- }
11175
- return Object.values(metadata).some(
11176
- (value) => !EDITABLE_TYPES.includes(typeof value)
11177
- );
11178
- }
11179
- const PROMOTION_QUERY_KEY = "promotions";
11180
- const promotionsQueryKeys = {
11181
- list: (query2) => [
11182
- PROMOTION_QUERY_KEY,
11183
- query2 ? query2 : void 0
11184
- ],
11185
- detail: (id, query2) => [
11186
- PROMOTION_QUERY_KEY,
11187
- id,
11188
- query2 ? query2 : void 0
11189
- ]
11190
- };
11191
- const usePromotions = (query2, options) => {
11192
- const { data, ...rest } = reactQuery.useQuery({
11193
- queryKey: promotionsQueryKeys.list(query2),
11194
- queryFn: async () => sdk.admin.promotion.list(query2),
11195
- ...options
11196
- });
11197
- return { ...data, ...rest };
11198
- };
11199
- const Promotions = () => {
11200
- const { id } = reactRouterDom.useParams();
11201
- const {
11202
- order: preview,
11203
- isError: isPreviewError,
11204
- error: previewError
11205
- } = useOrderPreview(id, void 0);
11206
- useInitiateOrderEdit({ preview });
11207
- const { onCancel } = useCancelOrderEdit({ preview });
11208
- if (isPreviewError) {
11209
- throw previewError;
11210
- }
11211
- const isReady = !!preview;
11212
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11213
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11214
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11215
- ] });
11216
- };
11217
- const PromotionForm = ({ preview }) => {
11218
- const { items, shipping_methods } = preview;
11219
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11220
- const [comboboxValue, setComboboxValue] = React.useState("");
11221
- const { handleSuccess } = useRouteModal();
11222
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11223
- const promoIds = getPromotionIds(items, shipping_methods);
11224
- const { promotions, isPending, isError, error } = usePromotions(
11225
- {
11226
- id: promoIds
11227
- },
11228
- {
11229
- enabled: !!promoIds.length
11230
- }
11231
- );
11232
- const comboboxData = useComboboxData({
11233
- queryKey: ["promotions", "combobox", promoIds],
11234
- queryFn: async (params) => {
11235
- return await sdk.admin.promotion.list({
11236
- ...params,
11237
- id: {
11238
- $nin: promoIds
11239
- }
11240
- });
11241
- },
11242
- getOptions: (data) => {
11243
- return data.promotions.map((promotion) => ({
11244
- label: promotion.code,
11245
- value: promotion.code
11246
- }));
11247
- }
11248
- });
11249
- const add = async (value) => {
11250
- if (!value) {
11251
- return;
11252
- }
11253
- addPromotions(
11254
- {
11255
- promo_codes: [value]
11256
- },
11257
- {
11258
- onError: (e) => {
11259
- ui.toast.error(e.message);
11260
- comboboxData.onSearchValueChange("");
11261
- setComboboxValue("");
11262
- },
11263
- onSuccess: () => {
11264
- comboboxData.onSearchValueChange("");
11265
- setComboboxValue("");
11266
- }
11267
- }
11268
- );
11269
- };
11270
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11271
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11272
- const onSubmit = async () => {
11273
- setIsSubmitting(true);
11274
- let requestSucceeded = false;
11275
- await requestOrderEdit(void 0, {
11276
- onError: (e) => {
11277
- ui.toast.error(e.message);
11278
- },
11279
- onSuccess: () => {
11280
- requestSucceeded = true;
11281
- }
11282
- });
11283
- if (!requestSucceeded) {
11284
- setIsSubmitting(false);
11285
- return;
11286
- }
11287
- await confirmOrderEdit(void 0, {
11288
- onError: (e) => {
11289
- ui.toast.error(e.message);
11290
- },
11291
- onSuccess: () => {
11292
- handleSuccess();
11293
- },
11294
- onSettled: () => {
11295
- setIsSubmitting(false);
11296
- }
11297
- });
11298
- };
11299
- if (isError) {
11300
- throw error;
11301
- }
11302
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11303
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11304
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11305
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11306
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11307
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11308
- ] }),
11309
- /* @__PURE__ */ jsxRuntime.jsx(
11310
- Combobox,
11311
- {
11312
- id: "promotion-combobox",
11313
- "aria-describedby": "promotion-combobox-hint",
11314
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11315
- fetchNextPage: comboboxData.fetchNextPage,
11316
- options: comboboxData.options,
11317
- onSearchValueChange: comboboxData.onSearchValueChange,
11318
- searchValue: comboboxData.searchValue,
11319
- disabled: comboboxData.disabled || isAddingPromotions,
11320
- onChange: add,
11321
- value: comboboxValue
11322
- }
11323
- )
11324
- ] }),
11325
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11326
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11327
- PromotionItem,
11328
- {
11329
- promotion,
11330
- orderId: preview.id,
11331
- isLoading: isPending
11332
- },
11333
- promotion.id
11334
- )) })
11335
- ] }) }),
11336
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11337
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11338
- /* @__PURE__ */ jsxRuntime.jsx(
11339
- ui.Button,
11340
- {
11341
- size: "small",
11342
- type: "submit",
11343
- isLoading: isSubmitting || isAddingPromotions,
11344
- children: "Save"
11345
- }
11346
- )
11347
- ] }) })
11348
- ] });
11349
- };
11350
- const PromotionItem = ({
11351
- promotion,
11352
- orderId,
11353
- isLoading
11354
- }) => {
11355
- var _a;
11356
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11357
- const onRemove = async () => {
11358
- removePromotions(
11359
- {
11360
- promo_codes: [promotion.code]
11361
- },
11362
- {
11363
- onError: (e) => {
11364
- ui.toast.error(e.message);
11365
- }
11366
- }
11367
- );
11368
- };
11369
- const displayValue = getDisplayValue(promotion);
11370
- return /* @__PURE__ */ jsxRuntime.jsxs(
11371
- "div",
11372
- {
11373
- className: ui.clx(
11374
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11375
- {
11376
- "animate-pulse": isLoading
11377
- }
11378
- ),
11379
- children: [
11380
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11381
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11382
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11383
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11384
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11385
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11386
- ] }),
11387
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11388
- ] })
11389
- ] }),
11390
- /* @__PURE__ */ jsxRuntime.jsx(
11391
- ui.IconButton,
11392
- {
11393
- size: "small",
11394
- type: "button",
11395
- variant: "transparent",
11396
- onClick: onRemove,
11397
- isLoading: isPending || isLoading,
11398
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11399
- }
11400
- )
11401
- ]
11402
- },
11403
- promotion.id
11404
- );
11405
- };
11406
- function getDisplayValue(promotion) {
11407
- var _a, _b, _c, _d;
11408
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11409
- if (!value) {
11410
- return null;
11411
- }
11412
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11413
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11414
- if (!currency) {
11415
- return null;
11416
- }
11417
- return getLocaleAmount(value, currency);
11418
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11419
- return formatPercentage(value);
11420
- }
11421
- return null;
11422
- }
11423
- const formatter = new Intl.NumberFormat([], {
11424
- style: "percent",
11425
- minimumFractionDigits: 2
11426
- });
11427
- const formatPercentage = (value, isPercentageValue = false) => {
11428
- let val = value || 0;
11429
- if (!isPercentageValue) {
11430
- val = val / 100;
11431
- }
11432
- return formatter.format(val);
11433
- };
11434
- function getPromotionIds(items, shippingMethods) {
11435
- const promotionIds = /* @__PURE__ */ new Set();
11436
- for (const item of items) {
11437
- if (item.adjustments) {
11438
- for (const adjustment of item.adjustments) {
11439
- if (adjustment.promotion_id) {
11440
- promotionIds.add(adjustment.promotion_id);
11441
- }
11442
- }
11443
- }
11444
- }
11445
- for (const shippingMethod of shippingMethods) {
11446
- if (shippingMethod.adjustments) {
11447
- for (const adjustment of shippingMethod.adjustments) {
11448
- if (adjustment.promotion_id) {
11449
- promotionIds.add(adjustment.promotion_id);
11450
- }
11451
- }
11452
- }
11453
- }
11454
- return Array.from(promotionIds);
11455
- }
11456
- const SalesChannel = () => {
11457
- const { id } = reactRouterDom.useParams();
11458
- const { draft_order, isPending, isError, error } = useDraftOrder(
11459
- id,
11460
- {
11461
- fields: "+sales_channel_id"
11462
- },
11463
- {
11464
- enabled: !!id
11465
- }
11466
- );
11467
- if (isError) {
11468
- throw error;
11469
- }
11470
- const ISrEADY = !!draft_order && !isPending;
11471
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11472
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11473
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11474
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11475
- ] }),
11476
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11477
- ] });
11478
- };
11479
- const SalesChannelForm = ({ order }) => {
11480
- const form = reactHookForm.useForm({
11481
- defaultValues: {
11482
- sales_channel_id: order.sales_channel_id || ""
11483
- },
11484
- resolver: zod.zodResolver(schema$2)
11485
- });
11486
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11487
- const { handleSuccess } = useRouteModal();
11488
- const onSubmit = form.handleSubmit(async (data) => {
11489
- await mutateAsync(
11490
- {
11491
- sales_channel_id: data.sales_channel_id
11492
- },
11493
- {
11494
- onSuccess: () => {
11495
- ui.toast.success("Sales channel updated");
11496
- handleSuccess();
11497
- },
11498
- onError: (error) => {
11499
- ui.toast.error(error.message);
11500
- }
11501
- }
11502
- );
11503
- });
11504
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11505
- KeyboundForm,
11506
- {
11507
- className: "flex flex-1 flex-col overflow-hidden",
11508
- onSubmit,
11509
- children: [
11510
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11511
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11512
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11513
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11514
- ] }) })
11515
- ]
11516
- }
11517
- ) });
11518
- };
11519
- const SalesChannelField = ({ control, order }) => {
11520
- const salesChannels = useComboboxData({
11521
- queryFn: async (params) => {
11522
- return await sdk.admin.salesChannel.list(params);
11523
- },
11524
- queryKey: ["sales-channels"],
11525
- getOptions: (data) => {
11526
- return data.sales_channels.map((salesChannel) => ({
11527
- label: salesChannel.name,
11528
- value: salesChannel.id
11529
- }));
11530
- },
11531
- defaultValue: order.sales_channel_id || void 0
11532
- });
11533
- return /* @__PURE__ */ jsxRuntime.jsx(
11534
- Form$2.Field,
11535
- {
11536
- control,
11537
- name: "sales_channel_id",
11538
- render: ({ field }) => {
11539
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11540
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11541
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11542
- Combobox,
11543
- {
11544
- options: salesChannels.options,
11545
- fetchNextPage: salesChannels.fetchNextPage,
11546
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11547
- searchValue: salesChannels.searchValue,
11548
- onSearchValueChange: salesChannels.onSearchValueChange,
11549
- placeholder: "Select sales channel",
11550
- ...field
11551
- }
11552
- ) }),
11553
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11554
- ] });
11555
- }
11556
- }
11557
- );
11558
- };
11559
- const schema$2 = objectType({
11560
- sales_channel_id: stringType().min(1)
11561
- });
11562
- const ShippingAddress = () => {
11563
- const { id } = reactRouterDom.useParams();
11564
- const { order, isPending, isError, error } = useOrder(id, {
11565
- fields: "+shipping_address"
11566
- });
11567
- if (isError) {
11568
- throw error;
11569
- }
11570
- const isReady = !isPending && !!order;
11571
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11572
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11573
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Shipping Address" }) }),
11574
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11575
- ] }),
11576
- isReady && /* @__PURE__ */ jsxRuntime.jsx(ShippingAddressForm, { order })
11577
- ] });
11578
- };
11579
- const ShippingAddressForm = ({ order }) => {
11580
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11581
- const form = reactHookForm.useForm({
11582
- defaultValues: {
11583
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11584
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11585
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11586
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11587
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11588
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11589
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11590
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11591
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11592
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11593
- },
11594
- resolver: zod.zodResolver(schema$1)
11595
- });
11596
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11597
- const { handleSuccess } = useRouteModal();
11598
- const onSubmit = form.handleSubmit(async (data) => {
11599
- await mutateAsync(
11600
- {
11601
- shipping_address: {
11602
- first_name: data.first_name,
11603
- last_name: data.last_name,
11604
- company: data.company,
11605
- address_1: data.address_1,
11606
- address_2: data.address_2,
11607
- city: data.city,
11608
- province: data.province,
11609
- country_code: data.country_code,
11610
- postal_code: data.postal_code,
11611
- phone: data.phone
11612
- }
11613
- },
11614
- {
11615
- onSuccess: () => {
11616
- handleSuccess();
11617
- },
11618
- onError: (error) => {
11619
- ui.toast.error(error.message);
11620
- }
11621
- }
11622
- );
11623
- });
11624
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11625
- KeyboundForm,
11626
- {
11627
- className: "flex flex-1 flex-col overflow-hidden",
11628
- onSubmit,
11629
- children: [
11630
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
11631
- /* @__PURE__ */ jsxRuntime.jsx(
11632
- Form$2.Field,
11633
- {
11634
- control: form.control,
11635
- name: "country_code",
11636
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11637
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
11638
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
11639
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11640
- ] })
11641
- }
11642
- ),
11643
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11644
- /* @__PURE__ */ jsxRuntime.jsx(
11645
- Form$2.Field,
11646
- {
11647
- control: form.control,
11648
- name: "first_name",
11649
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11650
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
11651
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11652
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11653
- ] })
11654
- }
11655
- ),
11656
- /* @__PURE__ */ jsxRuntime.jsx(
11657
- Form$2.Field,
11658
- {
11659
- control: form.control,
11660
- name: "last_name",
11661
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11662
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
11663
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11664
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11665
- ] })
11666
- }
11667
- )
11668
- ] }),
11669
- /* @__PURE__ */ jsxRuntime.jsx(
11670
- Form$2.Field,
11671
- {
11672
- control: form.control,
11673
- name: "company",
11674
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11675
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
11676
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11677
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11678
- ] })
11679
- }
11680
- ),
11681
- /* @__PURE__ */ jsxRuntime.jsx(
11682
- Form$2.Field,
11683
- {
11684
- control: form.control,
11685
- name: "address_1",
11686
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11687
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
11688
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11689
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11690
- ] })
11691
- }
11692
- ),
11693
- /* @__PURE__ */ jsxRuntime.jsx(
11694
- Form$2.Field,
11695
- {
11696
- control: form.control,
11697
- name: "address_2",
11698
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11699
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11700
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11701
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11702
- ] })
11703
- }
11704
- ),
11705
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11706
- /* @__PURE__ */ jsxRuntime.jsx(
11707
- Form$2.Field,
11708
- {
11709
- control: form.control,
11710
- name: "postal_code",
11711
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11712
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
11713
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11714
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11715
- ] })
11716
- }
11717
- ),
11718
- /* @__PURE__ */ jsxRuntime.jsx(
11719
- Form$2.Field,
11720
- {
11721
- control: form.control,
11722
- name: "city",
11723
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11724
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
11725
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11726
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11727
- ] })
11728
- }
11729
- )
11730
- ] }),
11731
- /* @__PURE__ */ jsxRuntime.jsx(
11732
- Form$2.Field,
11733
- {
11734
- control: form.control,
11735
- name: "province",
11736
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11737
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11738
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11739
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11740
- ] })
11741
- }
11742
- ),
11743
- /* @__PURE__ */ jsxRuntime.jsx(
11744
- Form$2.Field,
11745
- {
11746
- control: form.control,
11747
- name: "phone",
11748
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11749
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
11750
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11751
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11752
- ] })
11753
- }
11754
- )
11755
- ] }) }),
11756
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11757
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11758
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11759
- ] }) })
11760
- ]
11761
- }
11762
- ) });
11763
- };
11764
- const schema$1 = addressSchema;
11765
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11766
- const Shipping = () => {
11767
- var _a;
11768
- const { id } = reactRouterDom.useParams();
11769
- const { order, isPending, isError, error } = useOrder(id, {
11770
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11771
- });
11772
- const {
11773
- order: preview,
11774
- isPending: isPreviewPending,
11775
- isError: isPreviewError,
11776
- error: previewError
11777
- } = useOrderPreview(id);
11778
- useInitiateOrderEdit({ preview });
11779
- const { onCancel } = useCancelOrderEdit({ preview });
11780
- if (isError) {
11781
- throw error;
11782
- }
11783
- if (isPreviewError) {
11784
- throw previewError;
11785
- }
11786
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11787
- const isReady = preview && !isPreviewPending && order && !isPending;
11788
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11789
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11790
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11791
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11792
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11793
- ] }) }) }),
11794
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11795
- ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11796
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11797
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11798
- ] }) });
11799
- };
11800
- const ShippingForm = ({ preview, order }) => {
11801
- var _a;
11802
- const { setIsOpen } = useStackedModal();
11803
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11804
- const [data, setData] = React.useState(null);
11805
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11806
- const { shipping_options } = useShippingOptions(
11807
- {
11808
- id: appliedShippingOptionIds,
11809
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11810
- },
11811
- {
11812
- enabled: appliedShippingOptionIds.length > 0
11813
- }
11814
- );
11815
- const uniqueShippingProfiles = React.useMemo(() => {
11816
- const profiles = /* @__PURE__ */ new Map();
11817
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11818
- profiles.set(profile.id, profile);
11819
- });
11820
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11821
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11822
- });
11823
- return Array.from(profiles.values());
11824
- }, [order.items, shipping_options]);
11825
- const { handleSuccess } = useRouteModal();
11826
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11827
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11828
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11829
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11830
- const onSubmit = async () => {
11831
- setIsSubmitting(true);
11832
- let requestSucceeded = false;
11833
- await requestOrderEdit(void 0, {
11834
- onError: (e) => {
11835
- ui.toast.error(`Failed to request order edit: ${e.message}`);
11836
- },
11837
- onSuccess: () => {
11838
- requestSucceeded = true;
11839
- }
11840
- });
11841
- if (!requestSucceeded) {
11842
- setIsSubmitting(false);
11843
- return;
11844
- }
11845
- await confirmOrderEdit(void 0, {
11846
- onError: (e) => {
11847
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
11848
- },
11849
- onSuccess: () => {
11850
- handleSuccess();
11851
- },
11852
- onSettled: () => {
11853
- setIsSubmitting(false);
11854
- }
11855
- });
11856
- };
11857
- const onKeydown = React.useCallback(
11858
- (e) => {
11859
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11860
- if (data || isSubmitting) {
11861
- return;
11862
- }
11863
- onSubmit();
11864
- }
11865
- },
11866
- [data, isSubmitting, onSubmit]
11867
- );
11868
- React.useEffect(() => {
11869
- document.addEventListener("keydown", onKeydown);
11870
- return () => {
11871
- document.removeEventListener("keydown", onKeydown);
11872
- };
11873
- }, [onKeydown]);
11874
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11875
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11876
- /* @__PURE__ */ jsxRuntime.jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11877
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11878
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11879
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11880
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
11881
- ] }),
11882
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11883
- /* @__PURE__ */ jsxRuntime.jsx(radixUi.Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11884
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11885
- /* @__PURE__ */ jsxRuntime.jsx(
11886
- ui.Text,
11887
- {
11888
- size: "xsmall",
11889
- weight: "plus",
11890
- className: "text-ui-fg-muted",
11891
- children: "Shipping profile"
10692
+ size: "xsmall",
10693
+ weight: "plus",
10694
+ className: "text-ui-fg-muted",
10695
+ children: "Shipping profile"
11892
10696
  }
11893
10697
  ),
11894
10698
  /* @__PURE__ */ jsxRuntime.jsx(
@@ -12069,7 +10873,7 @@ const ShippingForm = ({ preview, order }) => {
12069
10873
  ]
12070
10874
  }
12071
10875
  ) : /* @__PURE__ */ jsxRuntime.jsx(
12072
- StackedModalTrigger,
10876
+ StackedModalTrigger$1,
12073
10877
  {
12074
10878
  shippingProfileId: profile.id,
12075
10879
  shippingOption,
@@ -12180,7 +10984,7 @@ const ShippingForm = ({ preview, order }) => {
12180
10984
  ] }) })
12181
10985
  ] });
12182
10986
  };
12183
- const StackedModalTrigger = ({
10987
+ const StackedModalTrigger$1 = ({
12184
10988
  shippingProfileId,
12185
10989
  shippingOption,
12186
10990
  shippingMethod,
@@ -12563,12 +11367,215 @@ const CustomAmountField = ({
12563
11367
  symbol: getNativeSymbol(currencyCode),
12564
11368
  code: currencyCode
12565
11369
  }
12566
- ) })
12567
- ] });
12568
- }
11370
+ ) })
11371
+ ] });
11372
+ }
11373
+ }
11374
+ );
11375
+ };
11376
+ const ShippingAddress = () => {
11377
+ const { id } = reactRouterDom.useParams();
11378
+ const { order, isPending, isError, error } = useOrder(id, {
11379
+ fields: "+shipping_address"
11380
+ });
11381
+ if (isError) {
11382
+ throw error;
11383
+ }
11384
+ const isReady = !isPending && !!order;
11385
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11386
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11387
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Shipping Address" }) }),
11388
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11389
+ ] }),
11390
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(ShippingAddressForm, { order })
11391
+ ] });
11392
+ };
11393
+ const ShippingAddressForm = ({ order }) => {
11394
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11395
+ const form = reactHookForm.useForm({
11396
+ defaultValues: {
11397
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11398
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11399
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11400
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11401
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11402
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11403
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11404
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11405
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11406
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11407
+ },
11408
+ resolver: zod.zodResolver(schema$2)
11409
+ });
11410
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11411
+ const { handleSuccess } = useRouteModal();
11412
+ const onSubmit = form.handleSubmit(async (data) => {
11413
+ await mutateAsync(
11414
+ {
11415
+ shipping_address: {
11416
+ first_name: data.first_name,
11417
+ last_name: data.last_name,
11418
+ company: data.company,
11419
+ address_1: data.address_1,
11420
+ address_2: data.address_2,
11421
+ city: data.city,
11422
+ province: data.province,
11423
+ country_code: data.country_code,
11424
+ postal_code: data.postal_code,
11425
+ phone: data.phone
11426
+ }
11427
+ },
11428
+ {
11429
+ onSuccess: () => {
11430
+ handleSuccess();
11431
+ },
11432
+ onError: (error) => {
11433
+ ui.toast.error(error.message);
11434
+ }
11435
+ }
11436
+ );
11437
+ });
11438
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11439
+ KeyboundForm,
11440
+ {
11441
+ className: "flex flex-1 flex-col overflow-hidden",
11442
+ onSubmit,
11443
+ children: [
11444
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
11445
+ /* @__PURE__ */ jsxRuntime.jsx(
11446
+ Form$2.Field,
11447
+ {
11448
+ control: form.control,
11449
+ name: "country_code",
11450
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11451
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
11452
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
11453
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11454
+ ] })
11455
+ }
11456
+ ),
11457
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11458
+ /* @__PURE__ */ jsxRuntime.jsx(
11459
+ Form$2.Field,
11460
+ {
11461
+ control: form.control,
11462
+ name: "first_name",
11463
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11464
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
11465
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11466
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11467
+ ] })
11468
+ }
11469
+ ),
11470
+ /* @__PURE__ */ jsxRuntime.jsx(
11471
+ Form$2.Field,
11472
+ {
11473
+ control: form.control,
11474
+ name: "last_name",
11475
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11476
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
11477
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11478
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11479
+ ] })
11480
+ }
11481
+ )
11482
+ ] }),
11483
+ /* @__PURE__ */ jsxRuntime.jsx(
11484
+ Form$2.Field,
11485
+ {
11486
+ control: form.control,
11487
+ name: "company",
11488
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11489
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
11490
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11491
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11492
+ ] })
11493
+ }
11494
+ ),
11495
+ /* @__PURE__ */ jsxRuntime.jsx(
11496
+ Form$2.Field,
11497
+ {
11498
+ control: form.control,
11499
+ name: "address_1",
11500
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11501
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
11502
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11503
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11504
+ ] })
11505
+ }
11506
+ ),
11507
+ /* @__PURE__ */ jsxRuntime.jsx(
11508
+ Form$2.Field,
11509
+ {
11510
+ control: form.control,
11511
+ name: "address_2",
11512
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11513
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11514
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11515
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11516
+ ] })
11517
+ }
11518
+ ),
11519
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11520
+ /* @__PURE__ */ jsxRuntime.jsx(
11521
+ Form$2.Field,
11522
+ {
11523
+ control: form.control,
11524
+ name: "postal_code",
11525
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11526
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
11527
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11528
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11529
+ ] })
11530
+ }
11531
+ ),
11532
+ /* @__PURE__ */ jsxRuntime.jsx(
11533
+ Form$2.Field,
11534
+ {
11535
+ control: form.control,
11536
+ name: "city",
11537
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11538
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
11539
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11540
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11541
+ ] })
11542
+ }
11543
+ )
11544
+ ] }),
11545
+ /* @__PURE__ */ jsxRuntime.jsx(
11546
+ Form$2.Field,
11547
+ {
11548
+ control: form.control,
11549
+ name: "province",
11550
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11551
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11552
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11553
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11554
+ ] })
11555
+ }
11556
+ ),
11557
+ /* @__PURE__ */ jsxRuntime.jsx(
11558
+ Form$2.Field,
11559
+ {
11560
+ control: form.control,
11561
+ name: "phone",
11562
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11563
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
11564
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11565
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11566
+ ] })
11567
+ }
11568
+ )
11569
+ ] }) }),
11570
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11571
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11572
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11573
+ ] }) })
11574
+ ]
12569
11575
  }
12570
- );
11576
+ ) });
12571
11577
  };
11578
+ const schema$2 = addressSchema;
12572
11579
  const TransferOwnership = () => {
12573
11580
  const { id } = reactRouterDom.useParams();
12574
11581
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12592,7 +11599,7 @@ const TransferOwnershipForm = ({ order }) => {
12592
11599
  defaultValues: {
12593
11600
  customer_id: order.customer_id || ""
12594
11601
  },
12595
- resolver: zod.zodResolver(schema)
11602
+ resolver: zod.zodResolver(schema$1)
12596
11603
  });
12597
11604
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12598
11605
  const { handleSuccess } = useRouteModal();
@@ -12904,146 +11911,1139 @@ const Illustration = () => {
12904
11911
  transform: "matrix(0.865865 0.500278 -0.871576 0.490261 109.758 60.0944)",
12905
11912
  fill: "#A1A1AA"
12906
11913
  }
12907
- ),
12908
- /* @__PURE__ */ jsxRuntime.jsx(
12909
- "rect",
12910
- {
12911
- width: "12",
12912
- height: "3",
12913
- rx: "1.5",
12914
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 105.4 62.5457)",
12915
- fill: "#A1A1AA"
11914
+ ),
11915
+ /* @__PURE__ */ jsxRuntime.jsx(
11916
+ "rect",
11917
+ {
11918
+ width: "12",
11919
+ height: "3",
11920
+ rx: "1.5",
11921
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 105.4 62.5457)",
11922
+ fill: "#A1A1AA"
11923
+ }
11924
+ ),
11925
+ /* @__PURE__ */ jsxRuntime.jsx(
11926
+ "path",
11927
+ {
11928
+ d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
11929
+ fill: "#52525B"
11930
+ }
11931
+ ),
11932
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11933
+ "path",
11934
+ {
11935
+ d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
11936
+ stroke: "#A1A1AA",
11937
+ strokeWidth: "1.5",
11938
+ strokeLinecap: "round",
11939
+ strokeLinejoin: "round"
11940
+ }
11941
+ ) }),
11942
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11943
+ "path",
11944
+ {
11945
+ d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
11946
+ stroke: "#A1A1AA",
11947
+ strokeWidth: "1.5",
11948
+ strokeLinecap: "round",
11949
+ strokeLinejoin: "round"
11950
+ }
11951
+ ) }),
11952
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11953
+ "path",
11954
+ {
11955
+ d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
11956
+ stroke: "#A1A1AA",
11957
+ strokeWidth: "1.5",
11958
+ strokeLinecap: "round",
11959
+ strokeLinejoin: "round"
11960
+ }
11961
+ ) }),
11962
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11963
+ "path",
11964
+ {
11965
+ d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
11966
+ stroke: "#A1A1AA",
11967
+ strokeWidth: "1.5",
11968
+ strokeLinecap: "round",
11969
+ strokeLinejoin: "round"
11970
+ }
11971
+ ) }),
11972
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11973
+ "path",
11974
+ {
11975
+ d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
11976
+ stroke: "#A1A1AA",
11977
+ strokeWidth: "1.5",
11978
+ strokeLinecap: "round",
11979
+ strokeLinejoin: "round"
11980
+ }
11981
+ ) }),
11982
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11983
+ "path",
11984
+ {
11985
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
11986
+ stroke: "#A1A1AA",
11987
+ strokeWidth: "1.5",
11988
+ strokeLinecap: "round",
11989
+ strokeLinejoin: "round"
11990
+ }
11991
+ ) }),
11992
+ /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
11993
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11994
+ "rect",
11995
+ {
11996
+ width: "12",
11997
+ height: "12",
11998
+ fill: "white",
11999
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12000
+ }
12001
+ ) }),
12002
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12003
+ "rect",
12004
+ {
12005
+ width: "12",
12006
+ height: "12",
12007
+ fill: "white",
12008
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12009
+ }
12010
+ ) }),
12011
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12012
+ "rect",
12013
+ {
12014
+ width: "12",
12015
+ height: "12",
12016
+ fill: "white",
12017
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12018
+ }
12019
+ ) }),
12020
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12021
+ "rect",
12022
+ {
12023
+ width: "12",
12024
+ height: "12",
12025
+ fill: "white",
12026
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12027
+ }
12028
+ ) }),
12029
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12030
+ "rect",
12031
+ {
12032
+ width: "12",
12033
+ height: "12",
12034
+ fill: "white",
12035
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12036
+ }
12037
+ ) }),
12038
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12039
+ "rect",
12040
+ {
12041
+ width: "12",
12042
+ height: "12",
12043
+ fill: "white",
12044
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12045
+ }
12046
+ ) })
12047
+ ] })
12048
+ ]
12049
+ }
12050
+ );
12051
+ };
12052
+ const schema$1 = objectType({
12053
+ customer_id: stringType().min(1)
12054
+ });
12055
+ const NumberInput = React.forwardRef(
12056
+ ({
12057
+ value,
12058
+ onChange,
12059
+ size = "base",
12060
+ min = 0,
12061
+ max = 100,
12062
+ step = 1,
12063
+ className,
12064
+ disabled,
12065
+ ...props
12066
+ }, ref) => {
12067
+ const handleChange = (event) => {
12068
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
12069
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
12070
+ onChange(newValue);
12071
+ }
12072
+ };
12073
+ const handleIncrement = () => {
12074
+ const newValue = value + step;
12075
+ if (max === void 0 || newValue <= max) {
12076
+ onChange(newValue);
12077
+ }
12078
+ };
12079
+ const handleDecrement = () => {
12080
+ const newValue = value - step;
12081
+ if (min === void 0 || newValue >= min) {
12082
+ onChange(newValue);
12083
+ }
12084
+ };
12085
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12086
+ "div",
12087
+ {
12088
+ className: ui.clx(
12089
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
12090
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
12091
+ {
12092
+ "h-7": size === "small",
12093
+ "h-8": size === "base"
12094
+ },
12095
+ className
12096
+ ),
12097
+ children: [
12098
+ /* @__PURE__ */ jsxRuntime.jsx(
12099
+ "input",
12100
+ {
12101
+ ref,
12102
+ type: "number",
12103
+ value,
12104
+ onChange: handleChange,
12105
+ min,
12106
+ max,
12107
+ step,
12108
+ className: ui.clx(
12109
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
12110
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
12111
+ "placeholder:text-ui-fg-muted"
12112
+ ),
12113
+ ...props
12114
+ }
12115
+ ),
12116
+ /* @__PURE__ */ jsxRuntime.jsxs(
12117
+ "button",
12118
+ {
12119
+ className: ui.clx(
12120
+ "flex items-center justify-center outline-none transition-fg",
12121
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12122
+ "focus:bg-ui-bg-field-component-hover",
12123
+ "hover:bg-ui-bg-field-component-hover",
12124
+ {
12125
+ "size-7": size === "small",
12126
+ "size-8": size === "base"
12127
+ }
12128
+ ),
12129
+ type: "button",
12130
+ onClick: handleDecrement,
12131
+ disabled: min !== void 0 && value <= min || disabled,
12132
+ children: [
12133
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
12134
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
12135
+ ]
12136
+ }
12137
+ ),
12138
+ /* @__PURE__ */ jsxRuntime.jsxs(
12139
+ "button",
12140
+ {
12141
+ className: ui.clx(
12142
+ "flex items-center justify-center outline-none transition-fg",
12143
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12144
+ "focus:bg-ui-bg-field-hover",
12145
+ "hover:bg-ui-bg-field-hover",
12146
+ {
12147
+ "size-7": size === "small",
12148
+ "size-8": size === "base"
12149
+ }
12150
+ ),
12151
+ type: "button",
12152
+ onClick: handleIncrement,
12153
+ disabled: max !== void 0 && value >= max || disabled,
12154
+ children: [
12155
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
12156
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
12157
+ ]
12158
+ }
12159
+ )
12160
+ ]
12161
+ }
12162
+ );
12163
+ }
12164
+ );
12165
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
12166
+ const productVariantsQueryKeys = {
12167
+ list: (query2) => [
12168
+ PRODUCT_VARIANTS_QUERY_KEY,
12169
+ query2 ? query2 : void 0
12170
+ ]
12171
+ };
12172
+ const useProductVariants = (query2, options) => {
12173
+ const { data, ...rest } = reactQuery.useQuery({
12174
+ queryKey: productVariantsQueryKeys.list(query2),
12175
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
12176
+ ...options
12177
+ });
12178
+ return { ...data, ...rest };
12179
+ };
12180
+ const STACKED_MODAL_ID = "items_stacked_modal";
12181
+ const Items = () => {
12182
+ const { id } = reactRouterDom.useParams();
12183
+ const {
12184
+ order: preview,
12185
+ isPending: isPreviewPending,
12186
+ isError: isPreviewError,
12187
+ error: previewError
12188
+ } = useOrderPreview(id, void 0, {
12189
+ placeholderData: reactQuery.keepPreviousData
12190
+ });
12191
+ useInitiateOrderEdit({ preview });
12192
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12193
+ id,
12194
+ {
12195
+ fields: "currency_code"
12196
+ },
12197
+ {
12198
+ enabled: !!id
12199
+ }
12200
+ );
12201
+ const { onCancel } = useCancelOrderEdit({ preview });
12202
+ if (isError) {
12203
+ throw error;
12204
+ }
12205
+ if (isPreviewError) {
12206
+ throw previewError;
12207
+ }
12208
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
12209
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12210
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
12211
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
12212
+ ] }) });
12213
+ };
12214
+ const ItemsForm = ({ preview, currencyCode }) => {
12215
+ var _a;
12216
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
12217
+ const [modalContent, setModalContent] = React.useState(
12218
+ null
12219
+ );
12220
+ const { handleSuccess } = useRouteModal();
12221
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
12222
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12223
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
12224
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
12225
+ const matches = React.useMemo(() => {
12226
+ return matchSorter.matchSorter(preview.items, query2, {
12227
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
12228
+ });
12229
+ }, [preview.items, query2]);
12230
+ const onSubmit = async () => {
12231
+ setIsSubmitting(true);
12232
+ let requestSucceeded = false;
12233
+ await requestOrderEdit(void 0, {
12234
+ onError: (e) => {
12235
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
12236
+ },
12237
+ onSuccess: () => {
12238
+ requestSucceeded = true;
12239
+ }
12240
+ });
12241
+ if (!requestSucceeded) {
12242
+ setIsSubmitting(false);
12243
+ return;
12244
+ }
12245
+ await confirmOrderEdit(void 0, {
12246
+ onError: (e) => {
12247
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
12248
+ },
12249
+ onSuccess: () => {
12250
+ handleSuccess();
12251
+ },
12252
+ onSettled: () => {
12253
+ setIsSubmitting(false);
12254
+ }
12255
+ });
12256
+ };
12257
+ const onKeyDown = React.useCallback(
12258
+ (e) => {
12259
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
12260
+ if (modalContent || isSubmitting) {
12261
+ return;
12262
+ }
12263
+ onSubmit();
12264
+ }
12265
+ },
12266
+ [modalContent, isSubmitting, onSubmit]
12267
+ );
12268
+ React.useEffect(() => {
12269
+ document.addEventListener("keydown", onKeyDown);
12270
+ return () => {
12271
+ document.removeEventListener("keydown", onKeyDown);
12272
+ };
12273
+ }, [onKeyDown]);
12274
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
12275
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
12276
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
12277
+ StackedFocusModal,
12278
+ {
12279
+ id: STACKED_MODAL_ID,
12280
+ onOpenChangeCallback: (open) => {
12281
+ if (!open) {
12282
+ setModalContent(null);
12283
+ }
12284
+ },
12285
+ children: [
12286
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
12287
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12288
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
12289
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Edit the items in the draft order" }) })
12290
+ ] }),
12291
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12292
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
12293
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
12294
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12295
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
12296
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
12297
+ ] }),
12298
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
12299
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
12300
+ ui.Input,
12301
+ {
12302
+ type: "search",
12303
+ placeholder: "Search items",
12304
+ value: searchValue,
12305
+ onChange: (e) => onSearchValueChange(e.target.value)
12306
+ }
12307
+ ) }),
12308
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
12309
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
12310
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
12311
+ /* @__PURE__ */ jsxRuntime.jsx(
12312
+ StackedModalTrigger,
12313
+ {
12314
+ type: "add-items",
12315
+ setModalContent
12316
+ }
12317
+ ),
12318
+ /* @__PURE__ */ jsxRuntime.jsx(
12319
+ StackedModalTrigger,
12320
+ {
12321
+ type: "add-custom-item",
12322
+ setModalContent
12323
+ }
12324
+ )
12325
+ ] })
12326
+ ] })
12327
+ ] })
12328
+ ] }),
12329
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12330
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-muted grid grid-cols-[2fr_1fr_2fr_28px] gap-3 px-4 py-2", children: [
12331
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
12332
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
12333
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
12334
+ /* @__PURE__ */ jsxRuntime.jsx("div", {})
12335
+ ] }) }),
12336
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12337
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
12338
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
12339
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
12340
+ Item,
12341
+ {
12342
+ item,
12343
+ preview,
12344
+ currencyCode
12345
+ },
12346
+ item.id
12347
+ )) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12348
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12349
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
12350
+ 'No items found for "',
12351
+ query2,
12352
+ '".'
12353
+ ] })
12354
+ ] }) })
12355
+ ] })
12356
+ ] }),
12357
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12358
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
12359
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
12360
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
12361
+ ui.Text,
12362
+ {
12363
+ size: "small",
12364
+ leading: "compact",
12365
+ className: "text-ui-fg-subtle",
12366
+ children: [
12367
+ itemCount,
12368
+ " ",
12369
+ itemCount === 1 ? "item" : "items"
12370
+ ]
12371
+ }
12372
+ ) }),
12373
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
12374
+ ] })
12375
+ ] }) }),
12376
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
12377
+ CustomItemForm,
12378
+ {
12379
+ orderId: preview.id,
12380
+ currencyCode
12381
+ }
12382
+ ) : null)
12383
+ ]
12384
+ }
12385
+ ) }),
12386
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12387
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12388
+ /* @__PURE__ */ jsxRuntime.jsx(
12389
+ ui.Button,
12390
+ {
12391
+ size: "small",
12392
+ type: "button",
12393
+ onClick: onSubmit,
12394
+ isLoading: isSubmitting,
12395
+ children: "Save"
12396
+ }
12397
+ )
12398
+ ] }) })
12399
+ ] });
12400
+ };
12401
+ const Item = ({ item, preview, currencyCode }) => {
12402
+ if (item.variant_id) {
12403
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
12404
+ }
12405
+ return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
12406
+ };
12407
+ const VariantItem = ({ item, preview, currencyCode }) => {
12408
+ const [editing, setEditing] = React.useState(false);
12409
+ const form = reactHookForm.useForm({
12410
+ defaultValues: {
12411
+ quantity: item.quantity,
12412
+ unit_price: item.unit_price
12413
+ },
12414
+ resolver: zod.zodResolver(variantItemSchema)
12415
+ });
12416
+ const actionId = React.useMemo(() => {
12417
+ var _a, _b;
12418
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12419
+ }, [item]);
12420
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12421
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12422
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12423
+ const onSubmit = form.handleSubmit(async (data) => {
12424
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
12425
+ setEditing(false);
12426
+ return;
12427
+ }
12428
+ if (!actionId) {
12429
+ await updateOriginalItem(
12430
+ {
12431
+ item_id: item.id,
12432
+ quantity: data.quantity,
12433
+ unit_price: convertNumber(data.unit_price)
12434
+ },
12435
+ {
12436
+ onSuccess: () => {
12437
+ setEditing(false);
12438
+ },
12439
+ onError: (e) => {
12440
+ ui.toast.error(e.message);
12916
12441
  }
12917
- ),
12442
+ }
12443
+ );
12444
+ return;
12445
+ }
12446
+ await updateActionItem(
12447
+ {
12448
+ action_id: actionId,
12449
+ quantity: data.quantity,
12450
+ unit_price: convertNumber(data.unit_price)
12451
+ },
12452
+ {
12453
+ onSuccess: () => {
12454
+ setEditing(false);
12455
+ },
12456
+ onError: (e) => {
12457
+ ui.toast.error(e.message);
12458
+ }
12459
+ }
12460
+ );
12461
+ });
12462
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
12463
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
12464
+ /* @__PURE__ */ jsxRuntime.jsx(
12465
+ Thumbnail,
12466
+ {
12467
+ thumbnail: item.thumbnail,
12468
+ alt: item.product_title ?? void 0
12469
+ }
12470
+ ),
12471
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12472
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
12473
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12474
+ /* @__PURE__ */ jsxRuntime.jsxs(
12475
+ ui.Text,
12476
+ {
12477
+ size: "small",
12478
+ leading: "compact",
12479
+ className: "text-ui-fg-subtle",
12480
+ children: [
12481
+ "(",
12482
+ item.variant_title,
12483
+ ")"
12484
+ ]
12485
+ }
12486
+ )
12487
+ ] }),
12918
12488
  /* @__PURE__ */ jsxRuntime.jsx(
12919
- "path",
12920
- {
12921
- d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
12922
- fill: "#52525B"
12923
- }
12924
- ),
12925
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12926
- "path",
12489
+ ui.Text,
12927
12490
  {
12928
- d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
12929
- stroke: "#A1A1AA",
12930
- strokeWidth: "1.5",
12931
- strokeLinecap: "round",
12932
- strokeLinejoin: "round"
12491
+ size: "small",
12492
+ leading: "compact",
12493
+ className: "text-ui-fg-subtle",
12494
+ children: item.variant_sku
12933
12495
  }
12934
- ) }),
12935
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12936
- "path",
12937
- {
12938
- d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
12939
- stroke: "#A1A1AA",
12940
- strokeWidth: "1.5",
12941
- strokeLinecap: "round",
12942
- strokeLinejoin: "round"
12496
+ )
12497
+ ] })
12498
+ ] }),
12499
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
12500
+ Form$2.Field,
12501
+ {
12502
+ control: form.control,
12503
+ name: "quantity",
12504
+ render: ({ field }) => {
12505
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
12506
+ }
12507
+ }
12508
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
12509
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
12510
+ Form$2.Field,
12511
+ {
12512
+ control: form.control,
12513
+ name: "unit_price",
12514
+ render: ({ field: { onChange, ...field } }) => {
12515
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12516
+ ui.CurrencyInput,
12517
+ {
12518
+ ...field,
12519
+ symbol: getNativeSymbol(currencyCode),
12520
+ code: currencyCode,
12521
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12522
+ }
12523
+ ) }) });
12524
+ }
12525
+ }
12526
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12527
+ /* @__PURE__ */ jsxRuntime.jsx(
12528
+ ui.IconButton,
12529
+ {
12530
+ type: "button",
12531
+ size: "small",
12532
+ onClick: editing ? onSubmit : () => {
12533
+ setEditing(true);
12534
+ },
12535
+ disabled: isPending,
12536
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
12537
+ }
12538
+ )
12539
+ ] }) }) });
12540
+ };
12541
+ const variantItemSchema = objectType({
12542
+ quantity: numberType(),
12543
+ unit_price: unionType([numberType(), stringType()])
12544
+ });
12545
+ const CustomItem = ({ item, preview, currencyCode }) => {
12546
+ const [editing, setEditing] = React.useState(false);
12547
+ const { quantity, unit_price, title } = item;
12548
+ const form = reactHookForm.useForm({
12549
+ defaultValues: {
12550
+ title,
12551
+ quantity,
12552
+ unit_price
12553
+ },
12554
+ resolver: zod.zodResolver(customItemSchema)
12555
+ });
12556
+ React.useEffect(() => {
12557
+ form.reset({
12558
+ title,
12559
+ quantity,
12560
+ unit_price
12561
+ });
12562
+ }, [form, title, quantity, unit_price]);
12563
+ const actionId = React.useMemo(() => {
12564
+ var _a, _b;
12565
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12566
+ }, [item]);
12567
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12568
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
12569
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12570
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12571
+ const onSubmit = form.handleSubmit(async (data) => {
12572
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
12573
+ setEditing(false);
12574
+ return;
12575
+ }
12576
+ if (!actionId) {
12577
+ await updateOriginalItem(
12578
+ {
12579
+ item_id: item.id,
12580
+ quantity: data.quantity,
12581
+ unit_price: convertNumber(data.unit_price)
12582
+ },
12583
+ {
12584
+ onSuccess: () => {
12585
+ setEditing(false);
12586
+ },
12587
+ onError: (e) => {
12588
+ ui.toast.error(e.message);
12943
12589
  }
12944
- ) }),
12945
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12946
- "path",
12947
- {
12948
- d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12949
- stroke: "#A1A1AA",
12950
- strokeWidth: "1.5",
12951
- strokeLinecap: "round",
12952
- strokeLinejoin: "round"
12590
+ }
12591
+ );
12592
+ return;
12593
+ }
12594
+ if (data.quantity === 0) {
12595
+ await removeActionItem(actionId, {
12596
+ onSuccess: () => {
12597
+ setEditing(false);
12598
+ },
12599
+ onError: (e) => {
12600
+ ui.toast.error(e.message);
12601
+ }
12602
+ });
12603
+ return;
12604
+ }
12605
+ await updateActionItem(
12606
+ {
12607
+ action_id: actionId,
12608
+ quantity: data.quantity,
12609
+ unit_price: convertNumber(data.unit_price)
12610
+ },
12611
+ {
12612
+ onSuccess: () => {
12613
+ setEditing(false);
12614
+ },
12615
+ onError: (e) => {
12616
+ ui.toast.error(e.message);
12617
+ }
12618
+ }
12619
+ );
12620
+ });
12621
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
12622
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
12623
+ /* @__PURE__ */ jsxRuntime.jsx(
12624
+ Thumbnail,
12625
+ {
12626
+ thumbnail: item.thumbnail,
12627
+ alt: item.title ?? void 0
12628
+ }
12629
+ ),
12630
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12631
+ Form$2.Field,
12632
+ {
12633
+ control: form.control,
12634
+ name: "title",
12635
+ render: ({ field }) => {
12636
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
12953
12637
  }
12954
- ) }),
12955
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12956
- "path",
12638
+ }
12639
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
12640
+ ] }),
12641
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12642
+ Form$2.Field,
12643
+ {
12644
+ control: form.control,
12645
+ name: "quantity",
12646
+ render: ({ field }) => {
12647
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
12648
+ }
12649
+ }
12650
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
12651
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12652
+ Form$2.Field,
12653
+ {
12654
+ control: form.control,
12655
+ name: "unit_price",
12656
+ render: ({ field: { onChange, ...field } }) => {
12657
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12658
+ ui.CurrencyInput,
12659
+ {
12660
+ ...field,
12661
+ symbol: getNativeSymbol(currencyCode),
12662
+ code: currencyCode,
12663
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12664
+ }
12665
+ ) }) });
12666
+ }
12667
+ }
12668
+ ) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12669
+ /* @__PURE__ */ jsxRuntime.jsx(
12670
+ ui.IconButton,
12671
+ {
12672
+ type: "button",
12673
+ size: "small",
12674
+ onClick: editing ? onSubmit : () => {
12675
+ setEditing(true);
12676
+ },
12677
+ disabled: isPending,
12678
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
12679
+ }
12680
+ )
12681
+ ] }) }) });
12682
+ };
12683
+ const StackedModalTrigger = ({
12684
+ type,
12685
+ setModalContent
12686
+ }) => {
12687
+ const { setIsOpen } = useStackedModal();
12688
+ const onClick = React.useCallback(() => {
12689
+ setModalContent(type);
12690
+ setIsOpen(STACKED_MODAL_ID, true);
12691
+ }, [setModalContent, setIsOpen, type]);
12692
+ return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
12693
+ };
12694
+ const VARIANT_PREFIX = "items";
12695
+ const LIMIT = 50;
12696
+ const ExistingItemsForm = ({ orderId, items }) => {
12697
+ const { setIsOpen } = useStackedModal();
12698
+ const [rowSelection, setRowSelection] = React.useState(
12699
+ items.reduce((acc, item) => {
12700
+ acc[item.variant_id] = true;
12701
+ return acc;
12702
+ }, {})
12703
+ );
12704
+ React.useEffect(() => {
12705
+ setRowSelection(
12706
+ items.reduce((acc, item) => {
12707
+ if (item.variant_id) {
12708
+ acc[item.variant_id] = true;
12709
+ }
12710
+ return acc;
12711
+ }, {})
12712
+ );
12713
+ }, [items]);
12714
+ const { q, order, offset } = useQueryParams(
12715
+ ["q", "order", "offset"],
12716
+ VARIANT_PREFIX
12717
+ );
12718
+ const { variants, count, isPending, isError, error } = useProductVariants(
12719
+ {
12720
+ q,
12721
+ order,
12722
+ offset: offset ? parseInt(offset) : void 0,
12723
+ limit: LIMIT
12724
+ },
12725
+ {
12726
+ placeholderData: reactQuery.keepPreviousData
12727
+ }
12728
+ );
12729
+ const columns = useColumns();
12730
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
12731
+ const onSubmit = async () => {
12732
+ const ids = Object.keys(rowSelection).filter(
12733
+ (id) => !items.find((i) => i.variant_id === id)
12734
+ );
12735
+ await mutateAsync(
12736
+ {
12737
+ items: ids.map((id) => ({
12738
+ variant_id: id,
12739
+ quantity: 1
12740
+ }))
12741
+ },
12742
+ {
12743
+ onSuccess: () => {
12744
+ setRowSelection({});
12745
+ setIsOpen(STACKED_MODAL_ID, false);
12746
+ },
12747
+ onError: (e) => {
12748
+ ui.toast.error(e.message);
12749
+ }
12750
+ }
12751
+ );
12752
+ };
12753
+ if (isError) {
12754
+ throw error;
12755
+ }
12756
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12757
+ StackedFocusModal.Content,
12758
+ {
12759
+ onOpenAutoFocus: (e) => {
12760
+ e.preventDefault();
12761
+ const searchInput = document.querySelector(
12762
+ "[data-modal-id='modal-search-input']"
12763
+ );
12764
+ if (searchInput) {
12765
+ searchInput.focus();
12766
+ }
12767
+ },
12768
+ children: [
12769
+ /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
12770
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
12771
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
12772
+ ] }),
12773
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
12774
+ DataTable,
12957
12775
  {
12958
- d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12959
- stroke: "#A1A1AA",
12960
- strokeWidth: "1.5",
12961
- strokeLinecap: "round",
12962
- strokeLinejoin: "round"
12776
+ data: variants,
12777
+ columns,
12778
+ isLoading: isPending,
12779
+ getRowId: (row) => row.id,
12780
+ rowCount: count,
12781
+ prefix: VARIANT_PREFIX,
12782
+ layout: "fill",
12783
+ rowSelection: {
12784
+ state: rowSelection,
12785
+ onRowSelectionChange: setRowSelection,
12786
+ enableRowSelection: (row) => {
12787
+ return !items.find((i) => i.variant_id === row.original.id);
12788
+ }
12789
+ },
12790
+ autoFocusSearch: true
12963
12791
  }
12964
12792
  ) }),
12965
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12966
- "path",
12793
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12794
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12795
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
12796
+ ] }) })
12797
+ ]
12798
+ }
12799
+ );
12800
+ };
12801
+ const columnHelper = ui.createDataTableColumnHelper();
12802
+ const useColumns = () => {
12803
+ return React.useMemo(() => {
12804
+ return [
12805
+ columnHelper.select(),
12806
+ columnHelper.accessor("product.title", {
12807
+ header: "Product",
12808
+ cell: ({ row }) => {
12809
+ var _a, _b, _c;
12810
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
12811
+ /* @__PURE__ */ jsxRuntime.jsx(
12812
+ Thumbnail,
12813
+ {
12814
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
12815
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
12816
+ }
12817
+ ),
12818
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
12819
+ ] });
12820
+ },
12821
+ enableSorting: true
12822
+ }),
12823
+ columnHelper.accessor("title", {
12824
+ header: "Variant",
12825
+ enableSorting: true
12826
+ }),
12827
+ columnHelper.accessor("sku", {
12828
+ header: "SKU",
12829
+ cell: ({ getValue }) => {
12830
+ return getValue() ?? "-";
12831
+ },
12832
+ enableSorting: true
12833
+ }),
12834
+ columnHelper.accessor("updated_at", {
12835
+ header: "Updated",
12836
+ cell: ({ getValue }) => {
12837
+ return /* @__PURE__ */ jsxRuntime.jsx(
12838
+ ui.Tooltip,
12839
+ {
12840
+ content: getFullDate({ date: getValue(), includeTime: true }),
12841
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
12842
+ }
12843
+ );
12844
+ },
12845
+ enableSorting: true,
12846
+ sortAscLabel: "Oldest first",
12847
+ sortDescLabel: "Newest first"
12848
+ }),
12849
+ columnHelper.accessor("created_at", {
12850
+ header: "Created",
12851
+ cell: ({ getValue }) => {
12852
+ return /* @__PURE__ */ jsxRuntime.jsx(
12853
+ ui.Tooltip,
12854
+ {
12855
+ content: getFullDate({ date: getValue(), includeTime: true }),
12856
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
12857
+ }
12858
+ );
12859
+ },
12860
+ enableSorting: true,
12861
+ sortAscLabel: "Oldest first",
12862
+ sortDescLabel: "Newest first"
12863
+ })
12864
+ ];
12865
+ }, []);
12866
+ };
12867
+ const CustomItemForm = ({ orderId, currencyCode }) => {
12868
+ const { setIsOpen } = useStackedModal();
12869
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
12870
+ const form = reactHookForm.useForm({
12871
+ defaultValues: {
12872
+ title: "",
12873
+ quantity: 1,
12874
+ unit_price: ""
12875
+ },
12876
+ resolver: zod.zodResolver(customItemSchema)
12877
+ });
12878
+ const onSubmit = form.handleSubmit(async (data) => {
12879
+ await addItems(
12880
+ {
12881
+ items: [
12967
12882
  {
12968
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12969
- stroke: "#A1A1AA",
12970
- strokeWidth: "1.5",
12971
- strokeLinecap: "round",
12972
- strokeLinejoin: "round"
12883
+ title: data.title,
12884
+ quantity: data.quantity,
12885
+ unit_price: convertNumber(data.unit_price)
12973
12886
  }
12974
- ) }),
12975
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12976
- "path",
12887
+ ]
12888
+ },
12889
+ {
12890
+ onSuccess: () => {
12891
+ setIsOpen(STACKED_MODAL_ID, false);
12892
+ },
12893
+ onError: (e) => {
12894
+ ui.toast.error(e.message);
12895
+ }
12896
+ }
12897
+ );
12898
+ });
12899
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
12900
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
12901
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
12902
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12903
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
12904
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
12905
+ ] }),
12906
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12907
+ /* @__PURE__ */ jsxRuntime.jsx(
12908
+ Form$2.Field,
12909
+ {
12910
+ control: form.control,
12911
+ name: "title",
12912
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12913
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12914
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
12915
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
12916
+ ] }),
12917
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12918
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12919
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12920
+ ] })
12921
+ ] }) })
12922
+ }
12923
+ ),
12924
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12925
+ /* @__PURE__ */ jsxRuntime.jsx(
12926
+ Form$2.Field,
12927
+ {
12928
+ control: form.control,
12929
+ name: "unit_price",
12930
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12931
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12932
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
12933
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
12934
+ ] }),
12935
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12936
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12937
+ ui.CurrencyInput,
12938
+ {
12939
+ symbol: getNativeSymbol(currencyCode),
12940
+ code: currencyCode,
12941
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
12942
+ ...field
12943
+ }
12944
+ ) }),
12945
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12946
+ ] })
12947
+ ] }) })
12948
+ }
12949
+ ),
12950
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12951
+ /* @__PURE__ */ jsxRuntime.jsx(
12952
+ Form$2.Field,
12953
+ {
12954
+ control: form.control,
12955
+ name: "quantity",
12956
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12957
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12958
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
12959
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
12960
+ ] }),
12961
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
12962
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field, className: "w-full" }) }) }),
12963
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12964
+ ] })
12965
+ ] }) })
12966
+ }
12967
+ )
12968
+ ] }) }) }),
12969
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12970
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12971
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
12972
+ ] }) })
12973
+ ] }) }) });
12974
+ };
12975
+ const customItemSchema = objectType({
12976
+ title: stringType().min(1),
12977
+ quantity: numberType(),
12978
+ unit_price: unionType([numberType(), stringType()])
12979
+ });
12980
+ const Email = () => {
12981
+ const { id } = reactRouterDom.useParams();
12982
+ const { order, isPending, isError, error } = useOrder(id, {
12983
+ fields: "+email"
12984
+ });
12985
+ if (isError) {
12986
+ throw error;
12987
+ }
12988
+ const isReady = !isPending && !!order;
12989
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12990
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12991
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
12992
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
12993
+ ] }),
12994
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
12995
+ ] });
12996
+ };
12997
+ const EmailForm = ({ order }) => {
12998
+ const form = reactHookForm.useForm({
12999
+ defaultValues: {
13000
+ email: order.email ?? ""
13001
+ },
13002
+ resolver: zod.zodResolver(schema)
13003
+ });
13004
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
13005
+ const { handleSuccess } = useRouteModal();
13006
+ const onSubmit = form.handleSubmit(async (data) => {
13007
+ await mutateAsync(
13008
+ { email: data.email },
13009
+ {
13010
+ onSuccess: () => {
13011
+ handleSuccess();
13012
+ },
13013
+ onError: (error) => {
13014
+ ui.toast.error(error.message);
13015
+ }
13016
+ }
13017
+ );
13018
+ });
13019
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
13020
+ KeyboundForm,
13021
+ {
13022
+ className: "flex flex-1 flex-col overflow-hidden",
13023
+ onSubmit,
13024
+ children: [
13025
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
13026
+ Form$2.Field,
12977
13027
  {
12978
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12979
- stroke: "#A1A1AA",
12980
- strokeWidth: "1.5",
12981
- strokeLinecap: "round",
12982
- strokeLinejoin: "round"
13028
+ control: form.control,
13029
+ name: "email",
13030
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13031
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
13032
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13033
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13034
+ ] })
12983
13035
  }
12984
13036
  ) }),
12985
- /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12986
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12987
- "rect",
12988
- {
12989
- width: "12",
12990
- height: "12",
12991
- fill: "white",
12992
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12993
- }
12994
- ) }),
12995
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12996
- "rect",
12997
- {
12998
- width: "12",
12999
- height: "12",
13000
- fill: "white",
13001
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
13002
- }
13003
- ) }),
13004
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13005
- "rect",
13006
- {
13007
- width: "12",
13008
- height: "12",
13009
- fill: "white",
13010
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
13011
- }
13012
- ) }),
13013
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13014
- "rect",
13015
- {
13016
- width: "12",
13017
- height: "12",
13018
- fill: "white",
13019
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
13020
- }
13021
- ) }),
13022
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13023
- "rect",
13024
- {
13025
- width: "12",
13026
- height: "12",
13027
- fill: "white",
13028
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
13029
- }
13030
- ) }),
13031
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13032
- "rect",
13033
- {
13034
- width: "12",
13035
- height: "12",
13036
- fill: "white",
13037
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
13038
- }
13039
- ) })
13040
- ] })
13037
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
13038
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13039
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13040
+ ] }) })
13041
13041
  ]
13042
13042
  }
13043
- );
13043
+ ) });
13044
13044
  };
13045
13045
  const schema = objectType({
13046
- customer_id: stringType().min(1)
13046
+ email: stringType().email()
13047
13047
  });
13048
13048
  const widgetModule = { widgets: [] };
13049
13049
  const routeModule = {
@@ -13065,21 +13065,13 @@ const routeModule = {
13065
13065
  handle,
13066
13066
  loader,
13067
13067
  children: [
13068
- {
13069
- Component: CustomItems,
13070
- path: "/draft-orders/:id/custom-items"
13071
- },
13072
13068
  {
13073
13069
  Component: BillingAddress,
13074
13070
  path: "/draft-orders/:id/billing-address"
13075
13071
  },
13076
13072
  {
13077
- Component: Email,
13078
- path: "/draft-orders/:id/email"
13079
- },
13080
- {
13081
- Component: Items,
13082
- path: "/draft-orders/:id/items"
13073
+ Component: CustomItems,
13074
+ path: "/draft-orders/:id/custom-items"
13083
13075
  },
13084
13076
  {
13085
13077
  Component: Metadata,
@@ -13093,17 +13085,25 @@ const routeModule = {
13093
13085
  Component: SalesChannel,
13094
13086
  path: "/draft-orders/:id/sales-channel"
13095
13087
  },
13096
- {
13097
- Component: ShippingAddress,
13098
- path: "/draft-orders/:id/shipping-address"
13099
- },
13100
13088
  {
13101
13089
  Component: Shipping,
13102
13090
  path: "/draft-orders/:id/shipping"
13103
13091
  },
13092
+ {
13093
+ Component: ShippingAddress,
13094
+ path: "/draft-orders/:id/shipping-address"
13095
+ },
13104
13096
  {
13105
13097
  Component: TransferOwnership,
13106
13098
  path: "/draft-orders/:id/transfer-ownership"
13099
+ },
13100
+ {
13101
+ Component: Items,
13102
+ path: "/draft-orders/:id/items"
13103
+ },
13104
+ {
13105
+ Component: Email,
13106
+ path: "/draft-orders/:id/email"
13107
13107
  }
13108
13108
  ]
13109
13109
  }