@medusajs/draft-order 2.11.0-preview-20251016060156 → 2.11.0-preview-20251016180154

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