@medusajs/draft-order 2.10.0-snapshot-20250828114928 → 2.10.0-snapshot-20250828124740

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.
@@ -9839,127 +9839,22 @@ const EmailForm = ({ order }) => {
9839
9839
  const schema$3 = objectType({
9840
9840
  email: stringType().email()
9841
9841
  });
9842
- const NumberInput = React.forwardRef(
9843
- ({
9844
- value,
9845
- onChange,
9846
- size = "base",
9847
- min = 0,
9848
- max = 100,
9849
- step = 1,
9850
- className,
9851
- disabled,
9852
- ...props
9853
- }, ref) => {
9854
- const handleChange = (event) => {
9855
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9856
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9857
- onChange(newValue);
9858
- }
9859
- };
9860
- const handleIncrement = () => {
9861
- const newValue = value + step;
9862
- if (max === void 0 || newValue <= max) {
9863
- onChange(newValue);
9864
- }
9865
- };
9866
- const handleDecrement = () => {
9867
- const newValue = value - step;
9868
- if (min === void 0 || newValue >= min) {
9869
- onChange(newValue);
9870
- }
9871
- };
9872
- return /* @__PURE__ */ jsxRuntime.jsxs(
9873
- "div",
9874
- {
9875
- className: ui.clx(
9876
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9877
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9878
- {
9879
- "h-7": size === "small",
9880
- "h-8": size === "base"
9881
- },
9882
- className
9883
- ),
9884
- children: [
9885
- /* @__PURE__ */ jsxRuntime.jsx(
9886
- "input",
9887
- {
9888
- ref,
9889
- type: "number",
9890
- value,
9891
- onChange: handleChange,
9892
- min,
9893
- max,
9894
- step,
9895
- className: ui.clx(
9896
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9897
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9898
- "placeholder:text-ui-fg-muted"
9899
- ),
9900
- ...props
9901
- }
9902
- ),
9903
- /* @__PURE__ */ jsxRuntime.jsxs(
9904
- "button",
9905
- {
9906
- className: ui.clx(
9907
- "flex items-center justify-center outline-none transition-fg",
9908
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9909
- "focus:bg-ui-bg-field-component-hover",
9910
- "hover:bg-ui-bg-field-component-hover",
9911
- {
9912
- "size-7": size === "small",
9913
- "size-8": size === "base"
9914
- }
9915
- ),
9916
- type: "button",
9917
- onClick: handleDecrement,
9918
- disabled: min !== void 0 && value <= min || disabled,
9919
- children: [
9920
- /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
9921
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9922
- ]
9923
- }
9924
- ),
9925
- /* @__PURE__ */ jsxRuntime.jsxs(
9926
- "button",
9927
- {
9928
- className: ui.clx(
9929
- "flex items-center justify-center outline-none transition-fg",
9930
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9931
- "focus:bg-ui-bg-field-hover",
9932
- "hover:bg-ui-bg-field-hover",
9933
- {
9934
- "size-7": size === "small",
9935
- "size-8": size === "base"
9936
- }
9937
- ),
9938
- type: "button",
9939
- onClick: handleIncrement,
9940
- disabled: max !== void 0 && value >= max || disabled,
9941
- children: [
9942
- /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
9943
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9944
- ]
9945
- }
9946
- )
9947
- ]
9948
- }
9949
- );
9950
- }
9951
- );
9952
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9953
- const productVariantsQueryKeys = {
9842
+ const PROMOTION_QUERY_KEY = "promotions";
9843
+ const promotionsQueryKeys = {
9954
9844
  list: (query2) => [
9955
- PRODUCT_VARIANTS_QUERY_KEY,
9845
+ PROMOTION_QUERY_KEY,
9846
+ query2 ? query2 : void 0
9847
+ ],
9848
+ detail: (id, query2) => [
9849
+ PROMOTION_QUERY_KEY,
9850
+ id,
9956
9851
  query2 ? query2 : void 0
9957
9852
  ]
9958
9853
  };
9959
- const useProductVariants = (query2, options) => {
9854
+ const usePromotions = (query2, options) => {
9960
9855
  const { data, ...rest } = reactQuery.useQuery({
9961
- queryKey: productVariantsQueryKeys.list(query2),
9962
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9856
+ queryKey: promotionsQueryKeys.list(query2),
9857
+ queryFn: async () => sdk.admin.promotion.list(query2),
9963
9858
  ...options
9964
9859
  });
9965
9860
  return { ...data, ...rest };
@@ -10010,65 +9905,85 @@ const useInitiateOrderEdit = ({
10010
9905
  run();
10011
9906
  }, [preview, navigate, mutateAsync]);
10012
9907
  };
10013
- function convertNumber(value) {
10014
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10015
- }
10016
- const STACKED_MODAL_ID = "items_stacked_modal";
10017
- const Items = () => {
9908
+ const Promotions = () => {
10018
9909
  const { id } = reactRouterDom.useParams();
10019
9910
  const {
10020
9911
  order: preview,
10021
- isPending: isPreviewPending,
10022
9912
  isError: isPreviewError,
10023
9913
  error: previewError
10024
- } = useOrderPreview(id, void 0, {
10025
- placeholderData: reactQuery.keepPreviousData
10026
- });
9914
+ } = useOrderPreview(id, void 0);
10027
9915
  useInitiateOrderEdit({ preview });
10028
- const { draft_order, isPending, isError, error } = useDraftOrder(
10029
- id,
10030
- {
10031
- fields: "currency_code"
10032
- },
10033
- {
10034
- enabled: !!id
10035
- }
10036
- );
10037
9916
  const { onCancel } = useCancelOrderEdit({ preview });
10038
- if (isError) {
10039
- throw error;
10040
- }
10041
9917
  if (isPreviewError) {
10042
9918
  throw previewError;
10043
9919
  }
10044
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10045
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10046
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10047
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10048
- ] }) });
9920
+ const isReady = !!preview;
9921
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
9922
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
9923
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
9924
+ ] });
10049
9925
  };
10050
- const ItemsForm = ({ preview, currencyCode }) => {
10051
- var _a;
9926
+ const PromotionForm = ({ preview }) => {
9927
+ const { items, shipping_methods } = preview;
10052
9928
  const [isSubmitting, setIsSubmitting] = React.useState(false);
10053
- const [modalContent, setModalContent] = React.useState(
10054
- null
10055
- );
9929
+ const [comboboxValue, setComboboxValue] = React.useState("");
10056
9930
  const { handleSuccess } = useRouteModal();
10057
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
9931
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
9932
+ const promoCodes = getPromotionCodes(items, shipping_methods);
9933
+ const { promotions, isPending, isError, error } = usePromotions(
9934
+ {
9935
+ code: promoCodes
9936
+ },
9937
+ {
9938
+ enabled: !!promoCodes.length
9939
+ }
9940
+ );
9941
+ const comboboxData = useComboboxData({
9942
+ queryKey: ["promotions", "combobox", promoCodes],
9943
+ queryFn: async (params) => {
9944
+ return await sdk.admin.promotion.list({
9945
+ ...params,
9946
+ code: {
9947
+ $nin: promoCodes
9948
+ }
9949
+ });
9950
+ },
9951
+ getOptions: (data) => {
9952
+ return data.promotions.map((promotion) => ({
9953
+ label: promotion.code,
9954
+ value: promotion.code
9955
+ }));
9956
+ }
9957
+ });
9958
+ const add = async (value) => {
9959
+ if (!value) {
9960
+ return;
9961
+ }
9962
+ addPromotions(
9963
+ {
9964
+ promo_codes: [value]
9965
+ },
9966
+ {
9967
+ onError: (e) => {
9968
+ ui.toast.error(e.message);
9969
+ comboboxData.onSearchValueChange("");
9970
+ setComboboxValue("");
9971
+ },
9972
+ onSuccess: () => {
9973
+ comboboxData.onSearchValueChange("");
9974
+ setComboboxValue("");
9975
+ }
9976
+ }
9977
+ );
9978
+ };
10058
9979
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10059
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10060
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10061
- const matches = React.useMemo(() => {
10062
- return matchSorter.matchSorter(preview.items, query2, {
10063
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10064
- });
10065
- }, [preview.items, query2]);
9980
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10066
9981
  const onSubmit = async () => {
10067
9982
  setIsSubmitting(true);
10068
9983
  let requestSucceeded = false;
10069
9984
  await requestOrderEdit(void 0, {
10070
9985
  onError: (e) => {
10071
- ui.toast.error(`Failed to request order edit: ${e.message}`);
9986
+ ui.toast.error(e.message);
10072
9987
  },
10073
9988
  onSuccess: () => {
10074
9989
  requestSucceeded = true;
@@ -10080,7 +9995,7 @@ const ItemsForm = ({ preview, currencyCode }) => {
10080
9995
  }
10081
9996
  await confirmOrderEdit(void 0, {
10082
9997
  onError: (e) => {
10083
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
9998
+ ui.toast.error(e.message);
10084
9999
  },
10085
10000
  onSuccess: () => {
10086
10001
  handleSuccess();
@@ -10090,860 +10005,294 @@ const ItemsForm = ({ preview, currencyCode }) => {
10090
10005
  }
10091
10006
  });
10092
10007
  };
10093
- const onKeyDown = React.useCallback(
10094
- (e) => {
10095
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10096
- if (modalContent || isSubmitting) {
10097
- return;
10098
- }
10099
- onSubmit();
10100
- }
10101
- },
10102
- [modalContent, isSubmitting, onSubmit]
10103
- );
10104
- React.useEffect(() => {
10105
- document.addEventListener("keydown", onKeyDown);
10106
- return () => {
10107
- document.removeEventListener("keydown", onKeyDown);
10108
- };
10109
- }, [onKeyDown]);
10110
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10111
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10112
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10113
- StackedFocusModal,
10114
- {
10115
- id: STACKED_MODAL_ID,
10116
- onOpenChangeCallback: (open) => {
10117
- if (!open) {
10118
- setModalContent(null);
10119
- }
10008
+ if (isError) {
10009
+ throw error;
10010
+ }
10011
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10012
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
10013
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
10014
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10015
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10016
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10017
+ ] }),
10018
+ /* @__PURE__ */ jsxRuntime.jsx(
10019
+ Combobox,
10020
+ {
10021
+ id: "promotion-combobox",
10022
+ "aria-describedby": "promotion-combobox-hint",
10023
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10024
+ fetchNextPage: comboboxData.fetchNextPage,
10025
+ options: comboboxData.options,
10026
+ onSearchValueChange: comboboxData.onSearchValueChange,
10027
+ searchValue: comboboxData.searchValue,
10028
+ disabled: comboboxData.disabled || isAddingPromotions,
10029
+ onChange: add,
10030
+ value: comboboxValue
10031
+ }
10032
+ )
10033
+ ] }),
10034
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10035
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
10036
+ PromotionItem,
10037
+ {
10038
+ promotion,
10039
+ orderId: preview.id,
10040
+ isLoading: isPending
10120
10041
  },
10121
- children: [
10122
- /* @__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: [
10123
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10124
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10125
- /* @__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." }) })
10126
- ] }),
10127
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10128
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10129
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10130
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10131
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10132
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10133
- ] }),
10134
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10135
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10136
- ui.Input,
10137
- {
10138
- type: "search",
10139
- placeholder: "Search items",
10140
- value: searchValue,
10141
- onChange: (e) => onSearchValueChange(e.target.value)
10142
- }
10143
- ) }),
10144
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10145
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10146
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10147
- /* @__PURE__ */ jsxRuntime.jsx(
10148
- StackedModalTrigger$1,
10149
- {
10150
- type: "add-items",
10151
- setModalContent
10152
- }
10153
- ),
10154
- /* @__PURE__ */ jsxRuntime.jsx(
10155
- StackedModalTrigger$1,
10156
- {
10157
- type: "add-custom-item",
10158
- setModalContent
10159
- }
10160
- )
10161
- ] })
10162
- ] })
10163
- ] })
10164
- ] }),
10165
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10166
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_1fr_1fr_28px] gap-3 px-4 py-2 text-ui-fg-muted", children: [
10167
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10168
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10169
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10170
- /* @__PURE__ */ jsxRuntime.jsx("div", {})
10171
- ] }) }),
10172
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__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: [
10173
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10174
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10175
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10176
- Item,
10177
- {
10178
- item,
10179
- preview,
10180
- currencyCode
10181
- },
10182
- item.id
10183
- )) : /* @__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: [
10184
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10185
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10186
- 'No items found for "',
10187
- query2,
10188
- '".'
10189
- ] })
10190
- ] }) })
10191
- ] })
10192
- ] }),
10193
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10194
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10195
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10196
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10197
- ui.Text,
10198
- {
10199
- size: "small",
10200
- leading: "compact",
10201
- className: "text-ui-fg-subtle",
10202
- children: [
10203
- itemCount,
10204
- " ",
10205
- itemCount === 1 ? "item" : "items"
10206
- ]
10207
- }
10208
- ) }),
10209
- /* @__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) }) })
10210
- ] })
10211
- ] }) }),
10212
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10213
- CustomItemForm,
10214
- {
10215
- orderId: preview.id,
10216
- currencyCode
10217
- }
10218
- ) : null)
10219
- ]
10220
- }
10221
- ) }),
10222
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
10223
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10042
+ promotion.id
10043
+ )) })
10044
+ ] }) }),
10045
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10046
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10224
10047
  /* @__PURE__ */ jsxRuntime.jsx(
10225
10048
  ui.Button,
10226
10049
  {
10227
10050
  size: "small",
10228
- type: "button",
10229
- onClick: onSubmit,
10230
- isLoading: isSubmitting,
10051
+ type: "submit",
10052
+ isLoading: isSubmitting || isAddingPromotions,
10231
10053
  children: "Save"
10232
10054
  }
10233
10055
  )
10234
10056
  ] }) })
10235
10057
  ] });
10236
10058
  };
10237
- const Item = ({ item, preview, currencyCode }) => {
10238
- if (item.variant_id) {
10239
- return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10240
- }
10241
- return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10242
- };
10243
- const VariantItem = ({ item, preview, currencyCode }) => {
10244
- const [editing, setEditing] = React.useState(false);
10245
- const form = reactHookForm.useForm({
10246
- defaultValues: {
10247
- quantity: item.quantity,
10248
- unit_price: item.unit_price
10249
- },
10250
- resolver: zod.zodResolver(variantItemSchema)
10251
- });
10252
- const actionId = React.useMemo(() => {
10253
- var _a, _b;
10254
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10255
- }, [item]);
10256
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10257
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10258
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10259
- const onSubmit = form.handleSubmit(async (data) => {
10260
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10261
- setEditing(false);
10262
- return;
10263
- }
10264
- if (!actionId) {
10265
- await updateOriginalItem(
10266
- {
10267
- item_id: item.id,
10268
- quantity: data.quantity,
10269
- unit_price: convertNumber(data.unit_price)
10270
- },
10271
- {
10272
- onSuccess: () => {
10273
- setEditing(false);
10274
- },
10275
- onError: (e) => {
10276
- ui.toast.error(e.message);
10277
- }
10278
- }
10279
- );
10280
- return;
10281
- }
10282
- await updateActionItem(
10059
+ const PromotionItem = ({
10060
+ promotion,
10061
+ orderId,
10062
+ isLoading
10063
+ }) => {
10064
+ var _a;
10065
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10066
+ const onRemove = async () => {
10067
+ removePromotions(
10283
10068
  {
10284
- action_id: actionId,
10285
- quantity: data.quantity,
10286
- unit_price: convertNumber(data.unit_price)
10069
+ promo_codes: [promotion.code]
10287
10070
  },
10288
10071
  {
10289
- onSuccess: () => {
10290
- setEditing(false);
10291
- },
10292
10072
  onError: (e) => {
10293
10073
  ui.toast.error(e.message);
10294
10074
  }
10295
10075
  }
10296
10076
  );
10297
- });
10298
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)_28px] gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center", children: [
10299
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3 w-full", children: [
10300
- /* @__PURE__ */ jsxRuntime.jsx(
10301
- Thumbnail,
10077
+ };
10078
+ const displayValue = getDisplayValue(promotion);
10079
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10080
+ "div",
10081
+ {
10082
+ className: ui.clx(
10083
+ "px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between",
10302
10084
  {
10303
- thumbnail: item.thumbnail,
10304
- alt: item.product_title ?? void 0
10085
+ "animate-pulse": isLoading
10305
10086
  }
10306
10087
  ),
10307
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10308
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10309
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10310
- /* @__PURE__ */ jsxRuntime.jsxs(
10311
- ui.Text,
10312
- {
10313
- size: "small",
10314
- leading: "compact",
10315
- className: "text-ui-fg-subtle",
10316
- children: [
10317
- "(",
10318
- item.variant_title,
10319
- ")"
10320
- ]
10321
- }
10322
- )
10088
+ children: [
10089
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10090
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
10091
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5 text-ui-fg-subtle", children: [
10092
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
10093
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
10094
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
10095
+ ] }),
10096
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
10097
+ ] })
10323
10098
  ] }),
10324
10099
  /* @__PURE__ */ jsxRuntime.jsx(
10325
- ui.Text,
10100
+ ui.IconButton,
10326
10101
  {
10327
10102
  size: "small",
10328
- leading: "compact",
10329
- className: "text-ui-fg-subtle",
10330
- children: item.variant_sku
10331
- }
10332
- )
10333
- ] })
10334
- ] }),
10335
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsxRuntime.jsx(
10336
- Form$2.Field,
10337
- {
10338
- control: form.control,
10339
- name: "quantity",
10340
- render: ({ field }) => {
10341
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10103
+ type: "button",
10104
+ variant: "transparent",
10105
+ onClick: onRemove,
10106
+ isLoading: isPending || isLoading,
10107
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
10108
+ }
10109
+ )
10110
+ ]
10111
+ },
10112
+ promotion.id
10113
+ );
10114
+ };
10115
+ function getDisplayValue(promotion) {
10116
+ var _a, _b, _c, _d;
10117
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
10118
+ if (!value) {
10119
+ return null;
10120
+ }
10121
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
10122
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
10123
+ if (!currency) {
10124
+ return null;
10125
+ }
10126
+ return getLocaleAmount(value, currency);
10127
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
10128
+ return formatPercentage(value);
10129
+ }
10130
+ return null;
10131
+ }
10132
+ const formatter = new Intl.NumberFormat([], {
10133
+ style: "percent",
10134
+ minimumFractionDigits: 2
10135
+ });
10136
+ const formatPercentage = (value, isPercentageValue = false) => {
10137
+ let val = value || 0;
10138
+ if (!isPercentageValue) {
10139
+ val = val / 100;
10140
+ }
10141
+ return formatter.format(val);
10142
+ };
10143
+ function getPromotionCodes(items, shippingMethods) {
10144
+ const codes = /* @__PURE__ */ new Set();
10145
+ for (const item of items) {
10146
+ if (item.adjustments) {
10147
+ for (const adjustment of item.adjustments) {
10148
+ if (adjustment.code) {
10149
+ codes.add(adjustment.code);
10342
10150
  }
10343
10151
  }
10344
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10345
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsxRuntime.jsx(
10346
- Form$2.Field,
10347
- {
10348
- control: form.control,
10349
- name: "unit_price",
10350
- render: ({ field: { onChange, ...field } }) => {
10351
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10352
- ui.CurrencyInput,
10353
- {
10354
- ...field,
10355
- symbol: getNativeSymbol(currencyCode),
10356
- code: currencyCode,
10357
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10358
- }
10359
- ) }) });
10152
+ }
10153
+ }
10154
+ for (const shippingMethod of shippingMethods) {
10155
+ if (shippingMethod.adjustments) {
10156
+ for (const adjustment of shippingMethod.adjustments) {
10157
+ if (adjustment.code) {
10158
+ codes.add(adjustment.code);
10360
10159
  }
10361
10160
  }
10362
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 flex items-center justify-end w-full", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10363
- /* @__PURE__ */ jsxRuntime.jsx(
10364
- ui.IconButton,
10161
+ }
10162
+ }
10163
+ return Array.from(codes);
10164
+ }
10165
+ const InlineTip = React.forwardRef(
10166
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
10167
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10168
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10169
+ "div",
10365
10170
  {
10366
- type: "button",
10367
- size: "small",
10368
- onClick: editing ? onSubmit : () => {
10369
- setEditing(true);
10370
- },
10371
- disabled: isPending,
10372
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10171
+ ref,
10172
+ className: ui.clx(
10173
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10174
+ className
10175
+ ),
10176
+ ...props,
10177
+ children: [
10178
+ /* @__PURE__ */ jsxRuntime.jsx(
10179
+ "div",
10180
+ {
10181
+ role: "presentation",
10182
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10183
+ "bg-ui-tag-orange-icon": variant === "warning"
10184
+ })
10185
+ }
10186
+ ),
10187
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10188
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10189
+ labelValue,
10190
+ ":"
10191
+ ] }),
10192
+ " ",
10193
+ children
10194
+ ] })
10195
+ ]
10373
10196
  }
10374
- )
10375
- ] }) }) });
10376
- };
10377
- const variantItemSchema = objectType({
10378
- quantity: numberType(),
10379
- unit_price: unionType([numberType(), stringType()])
10197
+ );
10198
+ }
10199
+ );
10200
+ InlineTip.displayName = "InlineTip";
10201
+ const MetadataFieldSchema = objectType({
10202
+ key: stringType(),
10203
+ disabled: booleanType().optional(),
10204
+ value: anyType()
10380
10205
  });
10381
- const CustomItem = ({ item, preview, currencyCode }) => {
10382
- const [editing, setEditing] = React.useState(false);
10383
- const { quantity, unit_price, title } = item;
10206
+ const MetadataSchema = objectType({
10207
+ metadata: arrayType(MetadataFieldSchema)
10208
+ });
10209
+ const Metadata = () => {
10210
+ const { id } = reactRouterDom.useParams();
10211
+ const { order, isPending, isError, error } = useOrder(id, {
10212
+ fields: "metadata"
10213
+ });
10214
+ if (isError) {
10215
+ throw error;
10216
+ }
10217
+ const isReady = !isPending && !!order;
10218
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10219
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10220
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10221
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10222
+ ] }),
10223
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10224
+ ] });
10225
+ };
10226
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10227
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10228
+ const MetadataForm = ({ orderId, metadata }) => {
10229
+ const { handleSuccess } = useRouteModal();
10230
+ const hasUneditableRows = getHasUneditableRows(metadata);
10231
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10384
10232
  const form = reactHookForm.useForm({
10385
10233
  defaultValues: {
10386
- title,
10387
- quantity,
10388
- unit_price
10234
+ metadata: getDefaultValues(metadata)
10389
10235
  },
10390
- resolver: zod.zodResolver(customItemSchema)
10236
+ resolver: zod.zodResolver(MetadataSchema)
10391
10237
  });
10392
- React.useEffect(() => {
10393
- form.reset({
10394
- title,
10395
- quantity,
10396
- unit_price
10397
- });
10398
- }, [form, title, quantity, unit_price]);
10399
- const actionId = React.useMemo(() => {
10400
- var _a, _b;
10401
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10402
- }, [item]);
10403
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10404
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10405
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10406
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10407
- const onSubmit = form.handleSubmit(async (data) => {
10408
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10409
- setEditing(false);
10410
- return;
10411
- }
10412
- if (!actionId) {
10413
- await updateOriginalItem(
10414
- {
10415
- item_id: item.id,
10416
- quantity: data.quantity,
10417
- unit_price: convertNumber(data.unit_price)
10418
- },
10419
- {
10420
- onSuccess: () => {
10421
- setEditing(false);
10422
- },
10423
- onError: (e) => {
10424
- ui.toast.error(e.message);
10425
- }
10426
- }
10427
- );
10428
- return;
10429
- }
10430
- if (data.quantity === 0) {
10431
- await removeActionItem(actionId, {
10432
- onSuccess: () => {
10433
- setEditing(false);
10434
- },
10435
- onError: (e) => {
10436
- ui.toast.error(e.message);
10437
- }
10438
- });
10439
- return;
10440
- }
10441
- await updateActionItem(
10238
+ const handleSubmit = form.handleSubmit(async (data) => {
10239
+ const parsedData = parseValues(data);
10240
+ await mutateAsync(
10442
10241
  {
10443
- action_id: actionId,
10444
- quantity: data.quantity,
10445
- unit_price: convertNumber(data.unit_price)
10242
+ metadata: parsedData
10446
10243
  },
10447
10244
  {
10448
10245
  onSuccess: () => {
10449
- setEditing(false);
10246
+ ui.toast.success("Metadata updated");
10247
+ handleSuccess();
10450
10248
  },
10451
- onError: (e) => {
10452
- ui.toast.error(e.message);
10249
+ onError: (error) => {
10250
+ ui.toast.error(error.message);
10453
10251
  }
10454
10252
  }
10455
10253
  );
10456
10254
  });
10457
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)_28px] gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center", children: [
10458
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10459
- /* @__PURE__ */ jsxRuntime.jsx(
10460
- Thumbnail,
10461
- {
10462
- thumbnail: item.thumbnail,
10463
- alt: item.title ?? void 0
10464
- }
10465
- ),
10466
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10467
- Form$2.Field,
10468
- {
10469
- control: form.control,
10470
- name: "title",
10471
- render: ({ field }) => {
10472
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
10473
- }
10474
- }
10475
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
10476
- ] }),
10477
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10478
- Form$2.Field,
10479
- {
10480
- control: form.control,
10481
- name: "quantity",
10482
- render: ({ field }) => {
10483
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10484
- }
10485
- }
10486
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
10487
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10488
- Form$2.Field,
10489
- {
10490
- control: form.control,
10491
- name: "unit_price",
10492
- render: ({ field: { onChange, ...field } }) => {
10493
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10494
- ui.CurrencyInput,
10495
- {
10496
- ...field,
10497
- symbol: getNativeSymbol(currencyCode),
10498
- code: currencyCode,
10499
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10500
- }
10501
- ) }) });
10502
- }
10503
- }
10504
- ) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 flex items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10505
- /* @__PURE__ */ jsxRuntime.jsx(
10506
- ui.IconButton,
10507
- {
10508
- type: "button",
10509
- size: "small",
10510
- onClick: editing ? onSubmit : () => {
10511
- setEditing(true);
10512
- },
10513
- disabled: isPending,
10514
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10515
- }
10516
- )
10517
- ] }) }) });
10518
- };
10519
- const StackedModalTrigger$1 = ({
10520
- type,
10521
- setModalContent
10522
- }) => {
10523
- const { setIsOpen } = useStackedModal();
10524
- const onClick = React.useCallback(() => {
10525
- setModalContent(type);
10526
- setIsOpen(STACKED_MODAL_ID, true);
10527
- }, [setModalContent, setIsOpen, type]);
10528
- return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10529
- };
10530
- const VARIANT_PREFIX = "items";
10531
- const LIMIT = 50;
10532
- const ExistingItemsForm = ({ orderId, items }) => {
10533
- const { setIsOpen } = useStackedModal();
10534
- const [rowSelection, setRowSelection] = React.useState(
10535
- items.reduce((acc, item) => {
10536
- acc[item.variant_id] = true;
10537
- return acc;
10538
- }, {})
10539
- );
10540
- React.useEffect(() => {
10541
- setRowSelection(
10542
- items.reduce((acc, item) => {
10543
- if (item.variant_id) {
10544
- acc[item.variant_id] = true;
10545
- }
10546
- return acc;
10547
- }, {})
10548
- );
10549
- }, [items]);
10550
- const { q, order, offset } = useQueryParams(
10551
- ["q", "order", "offset"],
10552
- VARIANT_PREFIX
10553
- );
10554
- const { variants, count, isPending, isError, error } = useProductVariants(
10555
- {
10556
- q,
10557
- order,
10558
- offset: offset ? parseInt(offset) : void 0,
10559
- limit: LIMIT
10560
- },
10561
- {
10562
- placeholderData: reactQuery.keepPreviousData
10255
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
10256
+ control: form.control,
10257
+ name: "metadata"
10258
+ });
10259
+ function deleteRow(index) {
10260
+ remove(index);
10261
+ if (fields.length === 1) {
10262
+ insert(0, {
10263
+ key: "",
10264
+ value: "",
10265
+ disabled: false
10266
+ });
10563
10267
  }
10564
- );
10565
- const columns = useColumns();
10566
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10567
- const onSubmit = async () => {
10568
- const ids = Object.keys(rowSelection).filter(
10569
- (id) => !items.find((i) => i.variant_id === id)
10570
- );
10571
- await mutateAsync(
10572
- {
10573
- items: ids.map((id) => ({
10574
- variant_id: id,
10575
- quantity: 1
10576
- }))
10577
- },
10578
- {
10579
- onSuccess: () => {
10580
- setRowSelection({});
10581
- setIsOpen(STACKED_MODAL_ID, false);
10582
- },
10583
- onError: (e) => {
10584
- ui.toast.error(e.message);
10585
- }
10586
- }
10587
- );
10588
- };
10589
- if (isError) {
10590
- throw error;
10591
10268
  }
10592
- return /* @__PURE__ */ jsxRuntime.jsxs(
10593
- StackedFocusModal.Content,
10269
+ function insertRow(index, position) {
10270
+ insert(index + (position === "above" ? 0 : 1), {
10271
+ key: "",
10272
+ value: "",
10273
+ disabled: false
10274
+ });
10275
+ }
10276
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10277
+ KeyboundForm,
10594
10278
  {
10595
- onOpenAutoFocus: (e) => {
10596
- e.preventDefault();
10597
- const searchInput = document.querySelector(
10598
- "[data-modal-id='modal-search-input']"
10599
- );
10600
- if (searchInput) {
10601
- searchInput.focus();
10602
- }
10603
- },
10279
+ onSubmit: handleSubmit,
10280
+ className: "flex flex-1 flex-col overflow-hidden",
10604
10281
  children: [
10605
- /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
10606
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10607
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10608
- ] }),
10609
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
10610
- DataTable,
10611
- {
10612
- data: variants,
10613
- columns,
10614
- isLoading: isPending,
10615
- getRowId: (row) => row.id,
10616
- rowCount: count,
10617
- prefix: VARIANT_PREFIX,
10618
- layout: "fill",
10619
- rowSelection: {
10620
- state: rowSelection,
10621
- onRowSelectionChange: setRowSelection,
10622
- enableRowSelection: (row) => {
10623
- return !items.find((i) => i.variant_id === row.original.id);
10282
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10283
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10284
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10285
+ /* @__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" }) }),
10286
+ /* @__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" }) })
10287
+ ] }),
10288
+ fields.map((field, index) => {
10289
+ const isDisabled = field.disabled || false;
10290
+ let placeholder = "-";
10291
+ if (typeof field.value === "object") {
10292
+ placeholder = "{ ... }";
10624
10293
  }
10625
- },
10626
- autoFocusSearch: true
10627
- }
10628
- ) }),
10629
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
10630
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10631
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10632
- ] }) })
10633
- ]
10634
- }
10635
- );
10636
- };
10637
- const columnHelper = ui.createDataTableColumnHelper();
10638
- const useColumns = () => {
10639
- return React.useMemo(() => {
10640
- return [
10641
- columnHelper.select(),
10642
- columnHelper.accessor("product.title", {
10643
- header: "Product",
10644
- cell: ({ row }) => {
10645
- var _a, _b, _c;
10646
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
10647
- /* @__PURE__ */ jsxRuntime.jsx(
10648
- Thumbnail,
10649
- {
10650
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10651
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10652
- }
10653
- ),
10654
- /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10655
- ] });
10656
- },
10657
- enableSorting: true
10658
- }),
10659
- columnHelper.accessor("title", {
10660
- header: "Variant",
10661
- enableSorting: true
10662
- }),
10663
- columnHelper.accessor("sku", {
10664
- header: "SKU",
10665
- cell: ({ getValue }) => {
10666
- return getValue() ?? "-";
10667
- },
10668
- enableSorting: true
10669
- }),
10670
- columnHelper.accessor("updated_at", {
10671
- header: "Updated",
10672
- cell: ({ getValue }) => {
10673
- return /* @__PURE__ */ jsxRuntime.jsx(
10674
- ui.Tooltip,
10675
- {
10676
- content: getFullDate({ date: getValue(), includeTime: true }),
10677
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10678
- }
10679
- );
10680
- },
10681
- enableSorting: true,
10682
- sortAscLabel: "Oldest first",
10683
- sortDescLabel: "Newest first"
10684
- }),
10685
- columnHelper.accessor("created_at", {
10686
- header: "Created",
10687
- cell: ({ getValue }) => {
10688
- return /* @__PURE__ */ jsxRuntime.jsx(
10689
- ui.Tooltip,
10690
- {
10691
- content: getFullDate({ date: getValue(), includeTime: true }),
10692
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10693
- }
10694
- );
10695
- },
10696
- enableSorting: true,
10697
- sortAscLabel: "Oldest first",
10698
- sortDescLabel: "Newest first"
10699
- })
10700
- ];
10701
- }, []);
10702
- };
10703
- const CustomItemForm = ({ orderId, currencyCode }) => {
10704
- const { setIsOpen } = useStackedModal();
10705
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10706
- const form = reactHookForm.useForm({
10707
- defaultValues: {
10708
- title: "",
10709
- quantity: 1,
10710
- unit_price: ""
10711
- },
10712
- resolver: zod.zodResolver(customItemSchema)
10713
- });
10714
- const onSubmit = form.handleSubmit(async (data) => {
10715
- await addItems(
10716
- {
10717
- items: [
10718
- {
10719
- title: data.title,
10720
- quantity: data.quantity,
10721
- unit_price: convertNumber(data.unit_price)
10722
- }
10723
- ]
10724
- },
10725
- {
10726
- onSuccess: () => {
10727
- setIsOpen(STACKED_MODAL_ID, false);
10728
- },
10729
- onError: (e) => {
10730
- ui.toast.error(e.message);
10731
- }
10732
- }
10733
- );
10734
- });
10735
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
10736
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
10737
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
10738
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10739
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
10740
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
10741
- ] }),
10742
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10743
- /* @__PURE__ */ jsxRuntime.jsx(
10744
- Form$2.Field,
10745
- {
10746
- control: form.control,
10747
- name: "title",
10748
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10749
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10750
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
10751
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
10752
- ] }),
10753
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10754
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10755
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10756
- ] })
10757
- ] }) })
10758
- }
10759
- ),
10760
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10761
- /* @__PURE__ */ jsxRuntime.jsx(
10762
- Form$2.Field,
10763
- {
10764
- control: form.control,
10765
- name: "unit_price",
10766
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10767
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10768
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
10769
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10770
- ] }),
10771
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10772
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10773
- ui.CurrencyInput,
10774
- {
10775
- symbol: getNativeSymbol(currencyCode),
10776
- code: currencyCode,
10777
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10778
- ...field
10779
- }
10780
- ) }),
10781
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10782
- ] })
10783
- ] }) })
10784
- }
10785
- ),
10786
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10787
- /* @__PURE__ */ jsxRuntime.jsx(
10788
- Form$2.Field,
10789
- {
10790
- control: form.control,
10791
- name: "quantity",
10792
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10793
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10794
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
10795
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10796
- ] }),
10797
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex-1 w-full", children: [
10798
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10799
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10800
- ] })
10801
- ] }) })
10802
- }
10803
- )
10804
- ] }) }) }),
10805
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
10806
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10807
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10808
- ] }) })
10809
- ] }) }) });
10810
- };
10811
- const customItemSchema = objectType({
10812
- title: stringType().min(1),
10813
- quantity: numberType(),
10814
- unit_price: unionType([numberType(), stringType()])
10815
- });
10816
- const InlineTip = React.forwardRef(
10817
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10818
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10819
- return /* @__PURE__ */ jsxRuntime.jsxs(
10820
- "div",
10821
- {
10822
- ref,
10823
- className: ui.clx(
10824
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10825
- className
10826
- ),
10827
- ...props,
10828
- children: [
10829
- /* @__PURE__ */ jsxRuntime.jsx(
10830
- "div",
10831
- {
10832
- role: "presentation",
10833
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10834
- "bg-ui-tag-orange-icon": variant === "warning"
10835
- })
10836
- }
10837
- ),
10838
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10839
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10840
- labelValue,
10841
- ":"
10842
- ] }),
10843
- " ",
10844
- children
10845
- ] })
10846
- ]
10847
- }
10848
- );
10849
- }
10850
- );
10851
- InlineTip.displayName = "InlineTip";
10852
- const MetadataFieldSchema = objectType({
10853
- key: stringType(),
10854
- disabled: booleanType().optional(),
10855
- value: anyType()
10856
- });
10857
- const MetadataSchema = objectType({
10858
- metadata: arrayType(MetadataFieldSchema)
10859
- });
10860
- const Metadata = () => {
10861
- const { id } = reactRouterDom.useParams();
10862
- const { order, isPending, isError, error } = useOrder(id, {
10863
- fields: "metadata"
10864
- });
10865
- if (isError) {
10866
- throw error;
10867
- }
10868
- const isReady = !isPending && !!order;
10869
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10870
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10871
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10872
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10873
- ] }),
10874
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10875
- ] });
10876
- };
10877
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10878
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10879
- const MetadataForm = ({ orderId, metadata }) => {
10880
- const { handleSuccess } = useRouteModal();
10881
- const hasUneditableRows = getHasUneditableRows(metadata);
10882
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10883
- const form = reactHookForm.useForm({
10884
- defaultValues: {
10885
- metadata: getDefaultValues(metadata)
10886
- },
10887
- resolver: zod.zodResolver(MetadataSchema)
10888
- });
10889
- const handleSubmit = form.handleSubmit(async (data) => {
10890
- const parsedData = parseValues(data);
10891
- await mutateAsync(
10892
- {
10893
- metadata: parsedData
10894
- },
10895
- {
10896
- onSuccess: () => {
10897
- ui.toast.success("Metadata updated");
10898
- handleSuccess();
10899
- },
10900
- onError: (error) => {
10901
- ui.toast.error(error.message);
10902
- }
10903
- }
10904
- );
10905
- });
10906
- const { fields, insert, remove } = reactHookForm.useFieldArray({
10907
- control: form.control,
10908
- name: "metadata"
10909
- });
10910
- function deleteRow(index) {
10911
- remove(index);
10912
- if (fields.length === 1) {
10913
- insert(0, {
10914
- key: "",
10915
- value: "",
10916
- disabled: false
10917
- });
10918
- }
10919
- }
10920
- function insertRow(index, position) {
10921
- insert(index + (position === "above" ? 0 : 1), {
10922
- key: "",
10923
- value: "",
10924
- disabled: false
10925
- });
10926
- }
10927
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10928
- KeyboundForm,
10929
- {
10930
- onSubmit: handleSubmit,
10931
- className: "flex flex-1 flex-col overflow-hidden",
10932
- children: [
10933
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10934
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10935
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10936
- /* @__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" }) }),
10937
- /* @__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" }) })
10938
- ] }),
10939
- fields.map((field, index) => {
10940
- const isDisabled = field.disabled || false;
10941
- let placeholder = "-";
10942
- if (typeof field.value === "object") {
10943
- placeholder = "{ ... }";
10944
- }
10945
- if (Array.isArray(field.value)) {
10946
- placeholder = "[ ... ]";
10294
+ if (Array.isArray(field.value)) {
10295
+ placeholder = "[ ... ]";
10947
10296
  }
10948
10297
  return /* @__PURE__ */ jsxRuntime.jsx(
10949
10298
  ConditionalTooltip,
@@ -11163,105 +10512,186 @@ function getHasUneditableRows(metadata) {
11163
10512
  (value) => !EDITABLE_TYPES.includes(typeof value)
11164
10513
  );
11165
10514
  }
11166
- const PROMOTION_QUERY_KEY = "promotions";
11167
- const promotionsQueryKeys = {
11168
- list: (query2) => [
11169
- PROMOTION_QUERY_KEY,
11170
- query2 ? query2 : void 0
11171
- ],
11172
- detail: (id, query2) => [
11173
- PROMOTION_QUERY_KEY,
10515
+ const SalesChannel = () => {
10516
+ const { id } = reactRouterDom.useParams();
10517
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11174
10518
  id,
11175
- query2 ? query2 : void 0
11176
- ]
10519
+ {
10520
+ fields: "+sales_channel_id"
10521
+ },
10522
+ {
10523
+ enabled: !!id
10524
+ }
10525
+ );
10526
+ if (isError) {
10527
+ throw error;
10528
+ }
10529
+ const ISrEADY = !!draft_order && !isPending;
10530
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10531
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10532
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
10533
+ /* @__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" }) })
10534
+ ] }),
10535
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
10536
+ ] });
11177
10537
  };
11178
- const usePromotions = (query2, options) => {
11179
- const { data, ...rest } = reactQuery.useQuery({
11180
- queryKey: promotionsQueryKeys.list(query2),
11181
- queryFn: async () => sdk.admin.promotion.list(query2),
11182
- ...options
10538
+ const SalesChannelForm = ({ order }) => {
10539
+ const form = reactHookForm.useForm({
10540
+ defaultValues: {
10541
+ sales_channel_id: order.sales_channel_id || ""
10542
+ },
10543
+ resolver: zod.zodResolver(schema$2)
11183
10544
  });
11184
- return { ...data, ...rest };
10545
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10546
+ const { handleSuccess } = useRouteModal();
10547
+ const onSubmit = form.handleSubmit(async (data) => {
10548
+ await mutateAsync(
10549
+ {
10550
+ sales_channel_id: data.sales_channel_id
10551
+ },
10552
+ {
10553
+ onSuccess: () => {
10554
+ ui.toast.success("Sales channel updated");
10555
+ handleSuccess();
10556
+ },
10557
+ onError: (error) => {
10558
+ ui.toast.error(error.message);
10559
+ }
10560
+ }
10561
+ );
10562
+ });
10563
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10564
+ KeyboundForm,
10565
+ {
10566
+ className: "flex flex-1 flex-col overflow-hidden",
10567
+ onSubmit,
10568
+ children: [
10569
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
10570
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10571
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10572
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10573
+ ] }) })
10574
+ ]
10575
+ }
10576
+ ) });
11185
10577
  };
11186
- const Promotions = () => {
10578
+ const SalesChannelField = ({ control, order }) => {
10579
+ const salesChannels = useComboboxData({
10580
+ queryFn: async (params) => {
10581
+ return await sdk.admin.salesChannel.list(params);
10582
+ },
10583
+ queryKey: ["sales-channels"],
10584
+ getOptions: (data) => {
10585
+ return data.sales_channels.map((salesChannel) => ({
10586
+ label: salesChannel.name,
10587
+ value: salesChannel.id
10588
+ }));
10589
+ },
10590
+ defaultValue: order.sales_channel_id || void 0
10591
+ });
10592
+ return /* @__PURE__ */ jsxRuntime.jsx(
10593
+ Form$2.Field,
10594
+ {
10595
+ control,
10596
+ name: "sales_channel_id",
10597
+ render: ({ field }) => {
10598
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10599
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
10600
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10601
+ Combobox,
10602
+ {
10603
+ options: salesChannels.options,
10604
+ fetchNextPage: salesChannels.fetchNextPage,
10605
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
10606
+ searchValue: salesChannels.searchValue,
10607
+ onSearchValueChange: salesChannels.onSearchValueChange,
10608
+ placeholder: "Select sales channel",
10609
+ ...field
10610
+ }
10611
+ ) }),
10612
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10613
+ ] });
10614
+ }
10615
+ }
10616
+ );
10617
+ };
10618
+ const schema$2 = objectType({
10619
+ sales_channel_id: stringType().min(1)
10620
+ });
10621
+ function convertNumber(value) {
10622
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10623
+ }
10624
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
10625
+ const Shipping = () => {
10626
+ var _a;
11187
10627
  const { id } = reactRouterDom.useParams();
10628
+ const { order, isPending, isError, error } = useOrder(id, {
10629
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
10630
+ });
11188
10631
  const {
11189
10632
  order: preview,
10633
+ isPending: isPreviewPending,
11190
10634
  isError: isPreviewError,
11191
10635
  error: previewError
11192
- } = useOrderPreview(id, void 0);
10636
+ } = useOrderPreview(id);
11193
10637
  useInitiateOrderEdit({ preview });
11194
10638
  const { onCancel } = useCancelOrderEdit({ preview });
10639
+ if (isError) {
10640
+ throw error;
10641
+ }
11195
10642
  if (isPreviewError) {
11196
10643
  throw previewError;
11197
10644
  }
11198
- const isReady = !!preview;
11199
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11200
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11201
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11202
- ] });
10645
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
10646
+ const isReady = preview && !isPreviewPending && order && !isPending;
10647
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
10648
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10649
+ /* @__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: [
10650
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
10651
+ /* @__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." }) })
10652
+ ] }) }) }),
10653
+ /* @__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" }) }) })
10654
+ ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10655
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
10656
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10657
+ ] }) });
11203
10658
  };
11204
- const PromotionForm = ({ preview }) => {
11205
- const { items, shipping_methods } = preview;
10659
+ const ShippingForm = ({ preview, order }) => {
10660
+ var _a;
10661
+ const { setIsOpen } = useStackedModal();
11206
10662
  const [isSubmitting, setIsSubmitting] = React.useState(false);
11207
- const [comboboxValue, setComboboxValue] = React.useState("");
11208
- const { handleSuccess } = useRouteModal();
11209
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11210
- const promoCodes = getPromotionCodes(items, shipping_methods);
11211
- const { promotions, isPending, isError, error } = usePromotions(
10663
+ const [data, setData] = React.useState(null);
10664
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
10665
+ const { shipping_options } = useShippingOptions(
11212
10666
  {
11213
- code: promoCodes
10667
+ id: appliedShippingOptionIds,
10668
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11214
10669
  },
11215
10670
  {
11216
- enabled: !!promoCodes.length
10671
+ enabled: appliedShippingOptionIds.length > 0
11217
10672
  }
11218
10673
  );
11219
- const comboboxData = useComboboxData({
11220
- queryKey: ["promotions", "combobox", promoCodes],
11221
- queryFn: async (params) => {
11222
- return await sdk.admin.promotion.list({
11223
- ...params,
11224
- code: {
11225
- $nin: promoCodes
11226
- }
11227
- });
11228
- },
11229
- getOptions: (data) => {
11230
- return data.promotions.map((promotion) => ({
11231
- label: promotion.code,
11232
- value: promotion.code
11233
- }));
11234
- }
11235
- });
11236
- const add = async (value) => {
11237
- if (!value) {
11238
- return;
11239
- }
11240
- addPromotions(
11241
- {
11242
- promo_codes: [value]
11243
- },
11244
- {
11245
- onError: (e) => {
11246
- ui.toast.error(e.message);
11247
- comboboxData.onSearchValueChange("");
11248
- setComboboxValue("");
11249
- },
11250
- onSuccess: () => {
11251
- comboboxData.onSearchValueChange("");
11252
- setComboboxValue("");
11253
- }
11254
- }
11255
- );
11256
- };
10674
+ const uniqueShippingProfiles = React.useMemo(() => {
10675
+ const profiles = /* @__PURE__ */ new Map();
10676
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
10677
+ profiles.set(profile.id, profile);
10678
+ });
10679
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
10680
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
10681
+ });
10682
+ return Array.from(profiles.values());
10683
+ }, [order.items, shipping_options]);
10684
+ const { handleSuccess } = useRouteModal();
11257
10685
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11258
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10686
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10687
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
10688
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11259
10689
  const onSubmit = async () => {
11260
10690
  setIsSubmitting(true);
11261
10691
  let requestSucceeded = false;
11262
10692
  await requestOrderEdit(void 0, {
11263
10693
  onError: (e) => {
11264
- ui.toast.error(e.message);
10694
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
11265
10695
  },
11266
10696
  onSuccess: () => {
11267
10697
  requestSucceeded = true;
@@ -11273,7 +10703,7 @@ const PromotionForm = ({ preview }) => {
11273
10703
  }
11274
10704
  await confirmOrderEdit(void 0, {
11275
10705
  onError: (e) => {
11276
- ui.toast.error(e.message);
10706
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
11277
10707
  },
11278
10708
  onSuccess: () => {
11279
10709
  handleSuccess();
@@ -11283,385 +10713,30 @@ const PromotionForm = ({ preview }) => {
11283
10713
  }
11284
10714
  });
11285
10715
  };
11286
- if (isError) {
11287
- throw error;
11288
- }
11289
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11290
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11291
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11292
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11293
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11294
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11295
- ] }),
11296
- /* @__PURE__ */ jsxRuntime.jsx(
11297
- Combobox,
11298
- {
11299
- id: "promotion-combobox",
11300
- "aria-describedby": "promotion-combobox-hint",
11301
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11302
- fetchNextPage: comboboxData.fetchNextPage,
11303
- options: comboboxData.options,
11304
- onSearchValueChange: comboboxData.onSearchValueChange,
11305
- searchValue: comboboxData.searchValue,
11306
- disabled: comboboxData.disabled || isAddingPromotions,
11307
- onChange: add,
11308
- value: comboboxValue
11309
- }
11310
- )
11311
- ] }),
11312
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11313
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11314
- PromotionItem,
11315
- {
11316
- promotion,
11317
- orderId: preview.id,
11318
- isLoading: isPending
11319
- },
11320
- promotion.id
11321
- )) })
11322
- ] }) }),
11323
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11324
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11325
- /* @__PURE__ */ jsxRuntime.jsx(
11326
- ui.Button,
11327
- {
11328
- size: "small",
11329
- type: "submit",
11330
- isLoading: isSubmitting || isAddingPromotions,
11331
- children: "Save"
11332
- }
11333
- )
11334
- ] }) })
11335
- ] });
11336
- };
11337
- const PromotionItem = ({
11338
- promotion,
11339
- orderId,
11340
- isLoading
11341
- }) => {
11342
- var _a;
11343
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11344
- const onRemove = async () => {
11345
- removePromotions(
11346
- {
11347
- promo_codes: [promotion.code]
11348
- },
11349
- {
11350
- onError: (e) => {
11351
- ui.toast.error(e.message);
11352
- }
11353
- }
11354
- );
11355
- };
11356
- const displayValue = getDisplayValue(promotion);
11357
- return /* @__PURE__ */ jsxRuntime.jsxs(
11358
- "div",
11359
- {
11360
- className: ui.clx(
11361
- "px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between",
11362
- {
11363
- "animate-pulse": isLoading
11364
- }
11365
- ),
11366
- children: [
11367
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11368
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11369
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5 text-ui-fg-subtle", children: [
11370
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11371
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11372
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11373
- ] }),
11374
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11375
- ] })
11376
- ] }),
11377
- /* @__PURE__ */ jsxRuntime.jsx(
11378
- ui.IconButton,
11379
- {
11380
- size: "small",
11381
- type: "button",
11382
- variant: "transparent",
11383
- onClick: onRemove,
11384
- isLoading: isPending || isLoading,
11385
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11386
- }
11387
- )
11388
- ]
11389
- },
11390
- promotion.id
11391
- );
11392
- };
11393
- function getDisplayValue(promotion) {
11394
- var _a, _b, _c, _d;
11395
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11396
- if (!value) {
11397
- return null;
11398
- }
11399
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11400
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11401
- if (!currency) {
11402
- return null;
11403
- }
11404
- return getLocaleAmount(value, currency);
11405
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11406
- return formatPercentage(value);
11407
- }
11408
- return null;
11409
- }
11410
- const formatter = new Intl.NumberFormat([], {
11411
- style: "percent",
11412
- minimumFractionDigits: 2
11413
- });
11414
- const formatPercentage = (value, isPercentageValue = false) => {
11415
- let val = value || 0;
11416
- if (!isPercentageValue) {
11417
- val = val / 100;
11418
- }
11419
- return formatter.format(val);
11420
- };
11421
- function getPromotionCodes(items, shippingMethods) {
11422
- const codes = /* @__PURE__ */ new Set();
11423
- for (const item of items) {
11424
- if (item.adjustments) {
11425
- for (const adjustment of item.adjustments) {
11426
- if (adjustment.code) {
11427
- codes.add(adjustment.code);
11428
- }
11429
- }
11430
- }
11431
- }
11432
- for (const shippingMethod of shippingMethods) {
11433
- if (shippingMethod.adjustments) {
11434
- for (const adjustment of shippingMethod.adjustments) {
11435
- if (adjustment.code) {
11436
- codes.add(adjustment.code);
11437
- }
11438
- }
11439
- }
11440
- }
11441
- return Array.from(codes);
11442
- }
11443
- const SalesChannel = () => {
11444
- const { id } = reactRouterDom.useParams();
11445
- const { draft_order, isPending, isError, error } = useDraftOrder(
11446
- id,
11447
- {
11448
- fields: "+sales_channel_id"
11449
- },
11450
- {
11451
- enabled: !!id
11452
- }
11453
- );
11454
- if (isError) {
11455
- throw error;
11456
- }
11457
- const ISrEADY = !!draft_order && !isPending;
11458
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11459
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11460
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11461
- /* @__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" }) })
11462
- ] }),
11463
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11464
- ] });
11465
- };
11466
- const SalesChannelForm = ({ order }) => {
11467
- const form = reactHookForm.useForm({
11468
- defaultValues: {
11469
- sales_channel_id: order.sales_channel_id || ""
11470
- },
11471
- resolver: zod.zodResolver(schema$2)
11472
- });
11473
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11474
- const { handleSuccess } = useRouteModal();
11475
- const onSubmit = form.handleSubmit(async (data) => {
11476
- await mutateAsync(
11477
- {
11478
- sales_channel_id: data.sales_channel_id
11479
- },
11480
- {
11481
- onSuccess: () => {
11482
- ui.toast.success("Sales channel updated");
11483
- handleSuccess();
11484
- },
11485
- onError: (error) => {
11486
- ui.toast.error(error.message);
11487
- }
11488
- }
11489
- );
11490
- });
11491
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11492
- KeyboundForm,
11493
- {
11494
- className: "flex flex-1 flex-col overflow-hidden",
11495
- onSubmit,
11496
- children: [
11497
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11498
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11499
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11500
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11501
- ] }) })
11502
- ]
11503
- }
11504
- ) });
11505
- };
11506
- const SalesChannelField = ({ control, order }) => {
11507
- const salesChannels = useComboboxData({
11508
- queryFn: async (params) => {
11509
- return await sdk.admin.salesChannel.list(params);
11510
- },
11511
- queryKey: ["sales-channels"],
11512
- getOptions: (data) => {
11513
- return data.sales_channels.map((salesChannel) => ({
11514
- label: salesChannel.name,
11515
- value: salesChannel.id
11516
- }));
11517
- },
11518
- defaultValue: order.sales_channel_id || void 0
11519
- });
11520
- return /* @__PURE__ */ jsxRuntime.jsx(
11521
- Form$2.Field,
11522
- {
11523
- control,
11524
- name: "sales_channel_id",
11525
- render: ({ field }) => {
11526
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11527
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11528
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11529
- Combobox,
11530
- {
11531
- options: salesChannels.options,
11532
- fetchNextPage: salesChannels.fetchNextPage,
11533
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11534
- searchValue: salesChannels.searchValue,
11535
- onSearchValueChange: salesChannels.onSearchValueChange,
11536
- placeholder: "Select sales channel",
11537
- ...field
11538
- }
11539
- ) }),
11540
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11541
- ] });
11542
- }
11543
- }
11544
- );
11545
- };
11546
- const schema$2 = objectType({
11547
- sales_channel_id: stringType().min(1)
11548
- });
11549
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11550
- const Shipping = () => {
11551
- var _a;
11552
- const { id } = reactRouterDom.useParams();
11553
- const { order, isPending, isError, error } = useOrder(id, {
11554
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11555
- });
11556
- const {
11557
- order: preview,
11558
- isPending: isPreviewPending,
11559
- isError: isPreviewError,
11560
- error: previewError
11561
- } = useOrderPreview(id);
11562
- useInitiateOrderEdit({ preview });
11563
- const { onCancel } = useCancelOrderEdit({ preview });
11564
- if (isError) {
11565
- throw error;
11566
- }
11567
- if (isPreviewError) {
11568
- throw previewError;
11569
- }
11570
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11571
- const isReady = preview && !isPreviewPending && order && !isPending;
11572
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11573
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11574
- /* @__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: [
11575
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11576
- /* @__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." }) })
11577
- ] }) }) }),
11578
- /* @__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" }) }) })
11579
- ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11580
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11581
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11582
- ] }) });
11583
- };
11584
- const ShippingForm = ({ preview, order }) => {
11585
- var _a;
11586
- const { setIsOpen } = useStackedModal();
11587
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11588
- const [data, setData] = React.useState(null);
11589
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11590
- const { shipping_options } = useShippingOptions(
11591
- {
11592
- id: appliedShippingOptionIds,
11593
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11594
- },
11595
- {
11596
- enabled: appliedShippingOptionIds.length > 0
11597
- }
11598
- );
11599
- const uniqueShippingProfiles = React.useMemo(() => {
11600
- const profiles = /* @__PURE__ */ new Map();
11601
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11602
- profiles.set(profile.id, profile);
11603
- });
11604
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11605
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11606
- });
11607
- return Array.from(profiles.values());
11608
- }, [order.items, shipping_options]);
11609
- const { handleSuccess } = useRouteModal();
11610
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11611
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11612
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11613
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11614
- const onSubmit = async () => {
11615
- setIsSubmitting(true);
11616
- let requestSucceeded = false;
11617
- await requestOrderEdit(void 0, {
11618
- onError: (e) => {
11619
- ui.toast.error(`Failed to request order edit: ${e.message}`);
11620
- },
11621
- onSuccess: () => {
11622
- requestSucceeded = true;
11623
- }
11624
- });
11625
- if (!requestSucceeded) {
11626
- setIsSubmitting(false);
11627
- return;
11628
- }
11629
- await confirmOrderEdit(void 0, {
11630
- onError: (e) => {
11631
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
11632
- },
11633
- onSuccess: () => {
11634
- handleSuccess();
11635
- },
11636
- onSettled: () => {
11637
- setIsSubmitting(false);
11638
- }
11639
- });
11640
- };
11641
- const onKeydown = React.useCallback(
11642
- (e) => {
11643
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11644
- if (data || isSubmitting) {
11645
- return;
11646
- }
11647
- onSubmit();
11648
- }
11649
- },
11650
- [data, isSubmitting, onSubmit]
11651
- );
11652
- React.useEffect(() => {
11653
- document.addEventListener("keydown", onKeydown);
11654
- return () => {
11655
- document.removeEventListener("keydown", onKeydown);
11656
- };
11657
- }, [onKeydown]);
11658
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11659
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11660
- /* @__PURE__ */ jsxRuntime.jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11661
- /* @__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: [
11662
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11663
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11664
- /* @__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." }) })
10716
+ const onKeydown = React.useCallback(
10717
+ (e) => {
10718
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10719
+ if (data || isSubmitting) {
10720
+ return;
10721
+ }
10722
+ onSubmit();
10723
+ }
10724
+ },
10725
+ [data, isSubmitting, onSubmit]
10726
+ );
10727
+ React.useEffect(() => {
10728
+ document.addEventListener("keydown", onKeydown);
10729
+ return () => {
10730
+ document.removeEventListener("keydown", onKeydown);
10731
+ };
10732
+ }, [onKeydown]);
10733
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10734
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10735
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
10736
+ /* @__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: [
10737
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10738
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
10739
+ /* @__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." }) })
11665
10740
  ] }),
11666
10741
  /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11667
10742
  /* @__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: [
@@ -11853,7 +10928,7 @@ const ShippingForm = ({ preview, order }) => {
11853
10928
  ]
11854
10929
  }
11855
10930
  ) : /* @__PURE__ */ jsxRuntime.jsx(
11856
- StackedModalTrigger,
10931
+ StackedModalTrigger$1,
11857
10932
  {
11858
10933
  shippingProfileId: profile.id,
11859
10934
  shippingOption,
@@ -11964,7 +11039,7 @@ const ShippingForm = ({ preview, order }) => {
11964
11039
  ] }) })
11965
11040
  ] });
11966
11041
  };
11967
- const StackedModalTrigger = ({
11042
+ const StackedModalTrigger$1 = ({
11968
11043
  shippingProfileId,
11969
11044
  shippingOption,
11970
11045
  shippingMethod,
@@ -12339,20 +11414,223 @@ const CustomAmountField = ({
12339
11414
  /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12340
11415
  /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12341
11416
  ] }),
12342
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12343
- ui.CurrencyInput,
11417
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11418
+ ui.CurrencyInput,
11419
+ {
11420
+ ...field,
11421
+ onValueChange: (value) => onChange(value),
11422
+ symbol: getNativeSymbol(currencyCode),
11423
+ code: currencyCode
11424
+ }
11425
+ ) })
11426
+ ] });
11427
+ }
11428
+ }
11429
+ );
11430
+ };
11431
+ const ShippingAddress = () => {
11432
+ const { id } = reactRouterDom.useParams();
11433
+ const { order, isPending, isError, error } = useOrder(id, {
11434
+ fields: "+shipping_address"
11435
+ });
11436
+ if (isError) {
11437
+ throw error;
11438
+ }
11439
+ const isReady = !isPending && !!order;
11440
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11441
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11442
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Shipping Address" }) }),
11443
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11444
+ ] }),
11445
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(ShippingAddressForm, { order })
11446
+ ] });
11447
+ };
11448
+ const ShippingAddressForm = ({ order }) => {
11449
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11450
+ const form = reactHookForm.useForm({
11451
+ defaultValues: {
11452
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11453
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11454
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11455
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11456
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11457
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11458
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11459
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11460
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11461
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11462
+ },
11463
+ resolver: zod.zodResolver(schema$1)
11464
+ });
11465
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11466
+ const { handleSuccess } = useRouteModal();
11467
+ const onSubmit = form.handleSubmit(async (data) => {
11468
+ await mutateAsync(
11469
+ {
11470
+ shipping_address: {
11471
+ first_name: data.first_name,
11472
+ last_name: data.last_name,
11473
+ company: data.company,
11474
+ address_1: data.address_1,
11475
+ address_2: data.address_2,
11476
+ city: data.city,
11477
+ province: data.province,
11478
+ country_code: data.country_code,
11479
+ postal_code: data.postal_code,
11480
+ phone: data.phone
11481
+ }
11482
+ },
11483
+ {
11484
+ onSuccess: () => {
11485
+ handleSuccess();
11486
+ },
11487
+ onError: (error) => {
11488
+ ui.toast.error(error.message);
11489
+ }
11490
+ }
11491
+ );
11492
+ });
11493
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11494
+ KeyboundForm,
11495
+ {
11496
+ className: "flex flex-1 flex-col overflow-hidden",
11497
+ onSubmit,
11498
+ children: [
11499
+ /* @__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: [
11500
+ /* @__PURE__ */ jsxRuntime.jsx(
11501
+ Form$2.Field,
11502
+ {
11503
+ control: form.control,
11504
+ name: "country_code",
11505
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11506
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
11507
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
11508
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11509
+ ] })
11510
+ }
11511
+ ),
11512
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11513
+ /* @__PURE__ */ jsxRuntime.jsx(
11514
+ Form$2.Field,
11515
+ {
11516
+ control: form.control,
11517
+ name: "first_name",
11518
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11519
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
11520
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11521
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11522
+ ] })
11523
+ }
11524
+ ),
11525
+ /* @__PURE__ */ jsxRuntime.jsx(
11526
+ Form$2.Field,
11527
+ {
11528
+ control: form.control,
11529
+ name: "last_name",
11530
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11531
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
11532
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11533
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11534
+ ] })
11535
+ }
11536
+ )
11537
+ ] }),
11538
+ /* @__PURE__ */ jsxRuntime.jsx(
11539
+ Form$2.Field,
11540
+ {
11541
+ control: form.control,
11542
+ name: "company",
11543
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11544
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
11545
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11546
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11547
+ ] })
11548
+ }
11549
+ ),
11550
+ /* @__PURE__ */ jsxRuntime.jsx(
11551
+ Form$2.Field,
11552
+ {
11553
+ control: form.control,
11554
+ name: "address_1",
11555
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11556
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
11557
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11558
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11559
+ ] })
11560
+ }
11561
+ ),
11562
+ /* @__PURE__ */ jsxRuntime.jsx(
11563
+ Form$2.Field,
11564
+ {
11565
+ control: form.control,
11566
+ name: "address_2",
11567
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11568
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11569
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11570
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11571
+ ] })
11572
+ }
11573
+ ),
11574
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11575
+ /* @__PURE__ */ jsxRuntime.jsx(
11576
+ Form$2.Field,
11577
+ {
11578
+ control: form.control,
11579
+ name: "postal_code",
11580
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11581
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
11582
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11583
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11584
+ ] })
11585
+ }
11586
+ ),
11587
+ /* @__PURE__ */ jsxRuntime.jsx(
11588
+ Form$2.Field,
11589
+ {
11590
+ control: form.control,
11591
+ name: "city",
11592
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11593
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
11594
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11595
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11596
+ ] })
11597
+ }
11598
+ )
11599
+ ] }),
11600
+ /* @__PURE__ */ jsxRuntime.jsx(
11601
+ Form$2.Field,
12344
11602
  {
12345
- ...field,
12346
- onValueChange: (value) => onChange(value),
12347
- symbol: getNativeSymbol(currencyCode),
12348
- code: currencyCode
11603
+ control: form.control,
11604
+ name: "province",
11605
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11606
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11607
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11608
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11609
+ ] })
12349
11610
  }
12350
- ) })
12351
- ] });
12352
- }
11611
+ ),
11612
+ /* @__PURE__ */ jsxRuntime.jsx(
11613
+ Form$2.Field,
11614
+ {
11615
+ control: form.control,
11616
+ name: "phone",
11617
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11618
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
11619
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11620
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11621
+ ] })
11622
+ }
11623
+ )
11624
+ ] }) }),
11625
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11626
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11627
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11628
+ ] }) })
11629
+ ]
12353
11630
  }
12354
- );
11631
+ ) });
12355
11632
  };
11633
+ const schema$1 = addressSchema;
12356
11634
  const TransferOwnership = () => {
12357
11635
  const { id } = reactRouterDom.useParams();
12358
11636
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12376,7 +11654,7 @@ const TransferOwnershipForm = ({ order }) => {
12376
11654
  defaultValues: {
12377
11655
  customer_id: order.customer_id || ""
12378
11656
  },
12379
- resolver: zod.zodResolver(schema$1)
11657
+ resolver: zod.zodResolver(schema)
12380
11658
  });
12381
11659
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12382
11660
  const { handleSuccess } = useRouteModal();
@@ -12755,283 +12033,1005 @@ const Illustration = () => {
12755
12033
  strokeLinecap: "round",
12756
12034
  strokeLinejoin: "round"
12757
12035
  }
12758
- ) }),
12759
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12760
- "path",
12761
- {
12762
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12763
- stroke: "#A1A1AA",
12764
- strokeWidth: "1.5",
12765
- strokeLinecap: "round",
12766
- strokeLinejoin: "round"
12036
+ ) }),
12037
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12038
+ "path",
12039
+ {
12040
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12041
+ stroke: "#A1A1AA",
12042
+ strokeWidth: "1.5",
12043
+ strokeLinecap: "round",
12044
+ strokeLinejoin: "round"
12045
+ }
12046
+ ) }),
12047
+ /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12048
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12049
+ "rect",
12050
+ {
12051
+ width: "12",
12052
+ height: "12",
12053
+ fill: "white",
12054
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12055
+ }
12056
+ ) }),
12057
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12058
+ "rect",
12059
+ {
12060
+ width: "12",
12061
+ height: "12",
12062
+ fill: "white",
12063
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12064
+ }
12065
+ ) }),
12066
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12067
+ "rect",
12068
+ {
12069
+ width: "12",
12070
+ height: "12",
12071
+ fill: "white",
12072
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12073
+ }
12074
+ ) }),
12075
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12076
+ "rect",
12077
+ {
12078
+ width: "12",
12079
+ height: "12",
12080
+ fill: "white",
12081
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12082
+ }
12083
+ ) }),
12084
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12085
+ "rect",
12086
+ {
12087
+ width: "12",
12088
+ height: "12",
12089
+ fill: "white",
12090
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12091
+ }
12092
+ ) }),
12093
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12094
+ "rect",
12095
+ {
12096
+ width: "12",
12097
+ height: "12",
12098
+ fill: "white",
12099
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12100
+ }
12101
+ ) })
12102
+ ] })
12103
+ ]
12104
+ }
12105
+ );
12106
+ };
12107
+ const schema = objectType({
12108
+ customer_id: stringType().min(1)
12109
+ });
12110
+ const NumberInput = React.forwardRef(
12111
+ ({
12112
+ value,
12113
+ onChange,
12114
+ size = "base",
12115
+ min = 0,
12116
+ max = 100,
12117
+ step = 1,
12118
+ className,
12119
+ disabled,
12120
+ ...props
12121
+ }, ref) => {
12122
+ const handleChange = (event) => {
12123
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
12124
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
12125
+ onChange(newValue);
12126
+ }
12127
+ };
12128
+ const handleIncrement = () => {
12129
+ const newValue = value + step;
12130
+ if (max === void 0 || newValue <= max) {
12131
+ onChange(newValue);
12132
+ }
12133
+ };
12134
+ const handleDecrement = () => {
12135
+ const newValue = value - step;
12136
+ if (min === void 0 || newValue >= min) {
12137
+ onChange(newValue);
12138
+ }
12139
+ };
12140
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12141
+ "div",
12142
+ {
12143
+ className: ui.clx(
12144
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
12145
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
12146
+ {
12147
+ "h-7": size === "small",
12148
+ "h-8": size === "base"
12149
+ },
12150
+ className
12151
+ ),
12152
+ children: [
12153
+ /* @__PURE__ */ jsxRuntime.jsx(
12154
+ "input",
12155
+ {
12156
+ ref,
12157
+ type: "number",
12158
+ value,
12159
+ onChange: handleChange,
12160
+ min,
12161
+ max,
12162
+ step,
12163
+ className: ui.clx(
12164
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
12165
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
12166
+ "placeholder:text-ui-fg-muted"
12167
+ ),
12168
+ ...props
12169
+ }
12170
+ ),
12171
+ /* @__PURE__ */ jsxRuntime.jsxs(
12172
+ "button",
12173
+ {
12174
+ className: ui.clx(
12175
+ "flex items-center justify-center outline-none transition-fg",
12176
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12177
+ "focus:bg-ui-bg-field-component-hover",
12178
+ "hover:bg-ui-bg-field-component-hover",
12179
+ {
12180
+ "size-7": size === "small",
12181
+ "size-8": size === "base"
12182
+ }
12183
+ ),
12184
+ type: "button",
12185
+ onClick: handleDecrement,
12186
+ disabled: min !== void 0 && value <= min || disabled,
12187
+ children: [
12188
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
12189
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
12190
+ ]
12191
+ }
12192
+ ),
12193
+ /* @__PURE__ */ jsxRuntime.jsxs(
12194
+ "button",
12195
+ {
12196
+ className: ui.clx(
12197
+ "flex items-center justify-center outline-none transition-fg",
12198
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12199
+ "focus:bg-ui-bg-field-hover",
12200
+ "hover:bg-ui-bg-field-hover",
12201
+ {
12202
+ "size-7": size === "small",
12203
+ "size-8": size === "base"
12204
+ }
12205
+ ),
12206
+ type: "button",
12207
+ onClick: handleIncrement,
12208
+ disabled: max !== void 0 && value >= max || disabled,
12209
+ children: [
12210
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
12211
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
12212
+ ]
12213
+ }
12214
+ )
12215
+ ]
12216
+ }
12217
+ );
12218
+ }
12219
+ );
12220
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
12221
+ const productVariantsQueryKeys = {
12222
+ list: (query2) => [
12223
+ PRODUCT_VARIANTS_QUERY_KEY,
12224
+ query2 ? query2 : void 0
12225
+ ]
12226
+ };
12227
+ const useProductVariants = (query2, options) => {
12228
+ const { data, ...rest } = reactQuery.useQuery({
12229
+ queryKey: productVariantsQueryKeys.list(query2),
12230
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
12231
+ ...options
12232
+ });
12233
+ return { ...data, ...rest };
12234
+ };
12235
+ const STACKED_MODAL_ID = "items_stacked_modal";
12236
+ const Items = () => {
12237
+ const { id } = reactRouterDom.useParams();
12238
+ const {
12239
+ order: preview,
12240
+ isPending: isPreviewPending,
12241
+ isError: isPreviewError,
12242
+ error: previewError
12243
+ } = useOrderPreview(id, void 0, {
12244
+ placeholderData: reactQuery.keepPreviousData
12245
+ });
12246
+ useInitiateOrderEdit({ preview });
12247
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12248
+ id,
12249
+ {
12250
+ fields: "currency_code"
12251
+ },
12252
+ {
12253
+ enabled: !!id
12254
+ }
12255
+ );
12256
+ const { onCancel } = useCancelOrderEdit({ preview });
12257
+ if (isError) {
12258
+ throw error;
12259
+ }
12260
+ if (isPreviewError) {
12261
+ throw previewError;
12262
+ }
12263
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
12264
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12265
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
12266
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
12267
+ ] }) });
12268
+ };
12269
+ const ItemsForm = ({ preview, currencyCode }) => {
12270
+ var _a;
12271
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
12272
+ const [modalContent, setModalContent] = React.useState(
12273
+ null
12274
+ );
12275
+ const { handleSuccess } = useRouteModal();
12276
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
12277
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12278
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
12279
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
12280
+ const matches = React.useMemo(() => {
12281
+ return matchSorter.matchSorter(preview.items, query2, {
12282
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
12283
+ });
12284
+ }, [preview.items, query2]);
12285
+ const onSubmit = async () => {
12286
+ setIsSubmitting(true);
12287
+ let requestSucceeded = false;
12288
+ await requestOrderEdit(void 0, {
12289
+ onError: (e) => {
12290
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
12291
+ },
12292
+ onSuccess: () => {
12293
+ requestSucceeded = true;
12294
+ }
12295
+ });
12296
+ if (!requestSucceeded) {
12297
+ setIsSubmitting(false);
12298
+ return;
12299
+ }
12300
+ await confirmOrderEdit(void 0, {
12301
+ onError: (e) => {
12302
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
12303
+ },
12304
+ onSuccess: () => {
12305
+ handleSuccess();
12306
+ },
12307
+ onSettled: () => {
12308
+ setIsSubmitting(false);
12309
+ }
12310
+ });
12311
+ };
12312
+ const onKeyDown = React.useCallback(
12313
+ (e) => {
12314
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
12315
+ if (modalContent || isSubmitting) {
12316
+ return;
12317
+ }
12318
+ onSubmit();
12319
+ }
12320
+ },
12321
+ [modalContent, isSubmitting, onSubmit]
12322
+ );
12323
+ React.useEffect(() => {
12324
+ document.addEventListener("keydown", onKeyDown);
12325
+ return () => {
12326
+ document.removeEventListener("keydown", onKeyDown);
12327
+ };
12328
+ }, [onKeyDown]);
12329
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
12330
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
12331
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
12332
+ StackedFocusModal,
12333
+ {
12334
+ id: STACKED_MODAL_ID,
12335
+ onOpenChangeCallback: (open) => {
12336
+ if (!open) {
12337
+ setModalContent(null);
12338
+ }
12339
+ },
12340
+ children: [
12341
+ /* @__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: [
12342
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12343
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
12344
+ /* @__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." }) })
12345
+ ] }),
12346
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12347
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
12348
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
12349
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12350
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
12351
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
12352
+ ] }),
12353
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
12354
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
12355
+ ui.Input,
12356
+ {
12357
+ type: "search",
12358
+ placeholder: "Search items",
12359
+ value: searchValue,
12360
+ onChange: (e) => onSearchValueChange(e.target.value)
12361
+ }
12362
+ ) }),
12363
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
12364
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
12365
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
12366
+ /* @__PURE__ */ jsxRuntime.jsx(
12367
+ StackedModalTrigger,
12368
+ {
12369
+ type: "add-items",
12370
+ setModalContent
12371
+ }
12372
+ ),
12373
+ /* @__PURE__ */ jsxRuntime.jsx(
12374
+ StackedModalTrigger,
12375
+ {
12376
+ type: "add-custom-item",
12377
+ setModalContent
12378
+ }
12379
+ )
12380
+ ] })
12381
+ ] })
12382
+ ] })
12383
+ ] }),
12384
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12385
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_1fr_1fr_28px] gap-3 px-4 py-2 text-ui-fg-muted", children: [
12386
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
12387
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
12388
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
12389
+ /* @__PURE__ */ jsxRuntime.jsx("div", {})
12390
+ ] }) }),
12391
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__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: [
12392
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
12393
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
12394
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
12395
+ Item,
12396
+ {
12397
+ item,
12398
+ preview,
12399
+ currencyCode
12400
+ },
12401
+ item.id
12402
+ )) : /* @__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: [
12403
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12404
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
12405
+ 'No items found for "',
12406
+ query2,
12407
+ '".'
12408
+ ] })
12409
+ ] }) })
12410
+ ] })
12411
+ ] }),
12412
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12413
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
12414
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
12415
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
12416
+ ui.Text,
12417
+ {
12418
+ size: "small",
12419
+ leading: "compact",
12420
+ className: "text-ui-fg-subtle",
12421
+ children: [
12422
+ itemCount,
12423
+ " ",
12424
+ itemCount === 1 ? "item" : "items"
12425
+ ]
12426
+ }
12427
+ ) }),
12428
+ /* @__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) }) })
12429
+ ] })
12430
+ ] }) }),
12431
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
12432
+ CustomItemForm,
12433
+ {
12434
+ orderId: preview.id,
12435
+ currencyCode
12436
+ }
12437
+ ) : null)
12438
+ ]
12439
+ }
12440
+ ) }),
12441
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
12442
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12443
+ /* @__PURE__ */ jsxRuntime.jsx(
12444
+ ui.Button,
12445
+ {
12446
+ size: "small",
12447
+ type: "button",
12448
+ onClick: onSubmit,
12449
+ isLoading: isSubmitting,
12450
+ children: "Save"
12451
+ }
12452
+ )
12453
+ ] }) })
12454
+ ] });
12455
+ };
12456
+ const Item = ({ item, preview, currencyCode }) => {
12457
+ if (item.variant_id) {
12458
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
12459
+ }
12460
+ return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
12461
+ };
12462
+ const VariantItem = ({ item, preview, currencyCode }) => {
12463
+ const [editing, setEditing] = React.useState(false);
12464
+ const form = reactHookForm.useForm({
12465
+ defaultValues: {
12466
+ quantity: item.quantity,
12467
+ unit_price: item.unit_price
12468
+ },
12469
+ resolver: zod.zodResolver(variantItemSchema)
12470
+ });
12471
+ const actionId = React.useMemo(() => {
12472
+ var _a, _b;
12473
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12474
+ }, [item]);
12475
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12476
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12477
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12478
+ const onSubmit = form.handleSubmit(async (data) => {
12479
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
12480
+ setEditing(false);
12481
+ return;
12482
+ }
12483
+ if (!actionId) {
12484
+ await updateOriginalItem(
12485
+ {
12486
+ item_id: item.id,
12487
+ quantity: data.quantity,
12488
+ unit_price: convertNumber(data.unit_price)
12489
+ },
12490
+ {
12491
+ onSuccess: () => {
12492
+ setEditing(false);
12493
+ },
12494
+ onError: (e) => {
12495
+ ui.toast.error(e.message);
12767
12496
  }
12768
- ) }),
12769
- /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12770
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12771
- "rect",
12772
- {
12773
- width: "12",
12774
- height: "12",
12775
- fill: "white",
12776
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12777
- }
12778
- ) }),
12779
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12780
- "rect",
12781
- {
12782
- width: "12",
12783
- height: "12",
12784
- fill: "white",
12785
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12786
- }
12787
- ) }),
12788
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12789
- "rect",
12790
- {
12791
- width: "12",
12792
- height: "12",
12793
- fill: "white",
12794
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12795
- }
12796
- ) }),
12797
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12798
- "rect",
12497
+ }
12498
+ );
12499
+ return;
12500
+ }
12501
+ await updateActionItem(
12502
+ {
12503
+ action_id: actionId,
12504
+ quantity: data.quantity,
12505
+ unit_price: convertNumber(data.unit_price)
12506
+ },
12507
+ {
12508
+ onSuccess: () => {
12509
+ setEditing(false);
12510
+ },
12511
+ onError: (e) => {
12512
+ ui.toast.error(e.message);
12513
+ }
12514
+ }
12515
+ );
12516
+ });
12517
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)_28px] gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center", children: [
12518
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3 w-full", children: [
12519
+ /* @__PURE__ */ jsxRuntime.jsx(
12520
+ Thumbnail,
12521
+ {
12522
+ thumbnail: item.thumbnail,
12523
+ alt: item.product_title ?? void 0
12524
+ }
12525
+ ),
12526
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12527
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
12528
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12529
+ /* @__PURE__ */ jsxRuntime.jsxs(
12530
+ ui.Text,
12799
12531
  {
12800
- width: "12",
12801
- height: "12",
12802
- fill: "white",
12803
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12532
+ size: "small",
12533
+ leading: "compact",
12534
+ className: "text-ui-fg-subtle",
12535
+ children: [
12536
+ "(",
12537
+ item.variant_title,
12538
+ ")"
12539
+ ]
12804
12540
  }
12805
- ) }),
12806
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12807
- "rect",
12541
+ )
12542
+ ] }),
12543
+ /* @__PURE__ */ jsxRuntime.jsx(
12544
+ ui.Text,
12545
+ {
12546
+ size: "small",
12547
+ leading: "compact",
12548
+ className: "text-ui-fg-subtle",
12549
+ children: item.variant_sku
12550
+ }
12551
+ )
12552
+ ] })
12553
+ ] }),
12554
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsxRuntime.jsx(
12555
+ Form$2.Field,
12556
+ {
12557
+ control: form.control,
12558
+ name: "quantity",
12559
+ render: ({ field }) => {
12560
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
12561
+ }
12562
+ }
12563
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
12564
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsxRuntime.jsx(
12565
+ Form$2.Field,
12566
+ {
12567
+ control: form.control,
12568
+ name: "unit_price",
12569
+ render: ({ field: { onChange, ...field } }) => {
12570
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12571
+ ui.CurrencyInput,
12808
12572
  {
12809
- width: "12",
12810
- height: "12",
12811
- fill: "white",
12812
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12573
+ ...field,
12574
+ symbol: getNativeSymbol(currencyCode),
12575
+ code: currencyCode,
12576
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12813
12577
  }
12814
- ) }),
12815
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12816
- "rect",
12578
+ ) }) });
12579
+ }
12580
+ }
12581
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 flex items-center justify-end w-full", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12582
+ /* @__PURE__ */ jsxRuntime.jsx(
12583
+ ui.IconButton,
12584
+ {
12585
+ type: "button",
12586
+ size: "small",
12587
+ onClick: editing ? onSubmit : () => {
12588
+ setEditing(true);
12589
+ },
12590
+ disabled: isPending,
12591
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
12592
+ }
12593
+ )
12594
+ ] }) }) });
12595
+ };
12596
+ const variantItemSchema = objectType({
12597
+ quantity: numberType(),
12598
+ unit_price: unionType([numberType(), stringType()])
12599
+ });
12600
+ const CustomItem = ({ item, preview, currencyCode }) => {
12601
+ const [editing, setEditing] = React.useState(false);
12602
+ const { quantity, unit_price, title } = item;
12603
+ const form = reactHookForm.useForm({
12604
+ defaultValues: {
12605
+ title,
12606
+ quantity,
12607
+ unit_price
12608
+ },
12609
+ resolver: zod.zodResolver(customItemSchema)
12610
+ });
12611
+ React.useEffect(() => {
12612
+ form.reset({
12613
+ title,
12614
+ quantity,
12615
+ unit_price
12616
+ });
12617
+ }, [form, title, quantity, unit_price]);
12618
+ const actionId = React.useMemo(() => {
12619
+ var _a, _b;
12620
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12621
+ }, [item]);
12622
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12623
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
12624
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12625
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12626
+ const onSubmit = form.handleSubmit(async (data) => {
12627
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
12628
+ setEditing(false);
12629
+ return;
12630
+ }
12631
+ if (!actionId) {
12632
+ await updateOriginalItem(
12633
+ {
12634
+ item_id: item.id,
12635
+ quantity: data.quantity,
12636
+ unit_price: convertNumber(data.unit_price)
12637
+ },
12638
+ {
12639
+ onSuccess: () => {
12640
+ setEditing(false);
12641
+ },
12642
+ onError: (e) => {
12643
+ ui.toast.error(e.message);
12644
+ }
12645
+ }
12646
+ );
12647
+ return;
12648
+ }
12649
+ if (data.quantity === 0) {
12650
+ await removeActionItem(actionId, {
12651
+ onSuccess: () => {
12652
+ setEditing(false);
12653
+ },
12654
+ onError: (e) => {
12655
+ ui.toast.error(e.message);
12656
+ }
12657
+ });
12658
+ return;
12659
+ }
12660
+ await updateActionItem(
12661
+ {
12662
+ action_id: actionId,
12663
+ quantity: data.quantity,
12664
+ unit_price: convertNumber(data.unit_price)
12665
+ },
12666
+ {
12667
+ onSuccess: () => {
12668
+ setEditing(false);
12669
+ },
12670
+ onError: (e) => {
12671
+ ui.toast.error(e.message);
12672
+ }
12673
+ }
12674
+ );
12675
+ });
12676
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)_28px] gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center", children: [
12677
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
12678
+ /* @__PURE__ */ jsxRuntime.jsx(
12679
+ Thumbnail,
12680
+ {
12681
+ thumbnail: item.thumbnail,
12682
+ alt: item.title ?? void 0
12683
+ }
12684
+ ),
12685
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12686
+ Form$2.Field,
12687
+ {
12688
+ control: form.control,
12689
+ name: "title",
12690
+ render: ({ field }) => {
12691
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
12692
+ }
12693
+ }
12694
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
12695
+ ] }),
12696
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12697
+ Form$2.Field,
12698
+ {
12699
+ control: form.control,
12700
+ name: "quantity",
12701
+ render: ({ field }) => {
12702
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
12703
+ }
12704
+ }
12705
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
12706
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12707
+ Form$2.Field,
12708
+ {
12709
+ control: form.control,
12710
+ name: "unit_price",
12711
+ render: ({ field: { onChange, ...field } }) => {
12712
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12713
+ ui.CurrencyInput,
12817
12714
  {
12818
- width: "12",
12819
- height: "12",
12820
- fill: "white",
12821
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12715
+ ...field,
12716
+ symbol: getNativeSymbol(currencyCode),
12717
+ code: currencyCode,
12718
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12822
12719
  }
12823
- ) })
12824
- ] })
12825
- ]
12826
- }
12827
- );
12720
+ ) }) });
12721
+ }
12722
+ }
12723
+ ) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 flex items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12724
+ /* @__PURE__ */ jsxRuntime.jsx(
12725
+ ui.IconButton,
12726
+ {
12727
+ type: "button",
12728
+ size: "small",
12729
+ onClick: editing ? onSubmit : () => {
12730
+ setEditing(true);
12731
+ },
12732
+ disabled: isPending,
12733
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
12734
+ }
12735
+ )
12736
+ ] }) }) });
12828
12737
  };
12829
- const schema$1 = objectType({
12830
- customer_id: stringType().min(1)
12831
- });
12832
- const ShippingAddress = () => {
12833
- const { id } = reactRouterDom.useParams();
12834
- const { order, isPending, isError, error } = useOrder(id, {
12835
- fields: "+shipping_address"
12836
- });
12837
- if (isError) {
12838
- throw error;
12839
- }
12840
- const isReady = !isPending && !!order;
12841
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12842
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12843
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Shipping Address" }) }),
12844
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12845
- ] }),
12846
- isReady && /* @__PURE__ */ jsxRuntime.jsx(ShippingAddressForm, { order })
12847
- ] });
12738
+ const StackedModalTrigger = ({
12739
+ type,
12740
+ setModalContent
12741
+ }) => {
12742
+ const { setIsOpen } = useStackedModal();
12743
+ const onClick = React.useCallback(() => {
12744
+ setModalContent(type);
12745
+ setIsOpen(STACKED_MODAL_ID, true);
12746
+ }, [setModalContent, setIsOpen, type]);
12747
+ return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
12848
12748
  };
12849
- const ShippingAddressForm = ({ order }) => {
12850
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12851
- const form = reactHookForm.useForm({
12852
- defaultValues: {
12853
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12854
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12855
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12856
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12857
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12858
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12859
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12860
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12861
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12862
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12749
+ const VARIANT_PREFIX = "items";
12750
+ const LIMIT = 50;
12751
+ const ExistingItemsForm = ({ orderId, items }) => {
12752
+ const { setIsOpen } = useStackedModal();
12753
+ const [rowSelection, setRowSelection] = React.useState(
12754
+ items.reduce((acc, item) => {
12755
+ acc[item.variant_id] = true;
12756
+ return acc;
12757
+ }, {})
12758
+ );
12759
+ React.useEffect(() => {
12760
+ setRowSelection(
12761
+ items.reduce((acc, item) => {
12762
+ if (item.variant_id) {
12763
+ acc[item.variant_id] = true;
12764
+ }
12765
+ return acc;
12766
+ }, {})
12767
+ );
12768
+ }, [items]);
12769
+ const { q, order, offset } = useQueryParams(
12770
+ ["q", "order", "offset"],
12771
+ VARIANT_PREFIX
12772
+ );
12773
+ const { variants, count, isPending, isError, error } = useProductVariants(
12774
+ {
12775
+ q,
12776
+ order,
12777
+ offset: offset ? parseInt(offset) : void 0,
12778
+ limit: LIMIT
12863
12779
  },
12864
- resolver: zod.zodResolver(schema)
12865
- });
12866
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12867
- const { handleSuccess } = useRouteModal();
12868
- const onSubmit = form.handleSubmit(async (data) => {
12780
+ {
12781
+ placeholderData: reactQuery.keepPreviousData
12782
+ }
12783
+ );
12784
+ const columns = useColumns();
12785
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
12786
+ const onSubmit = async () => {
12787
+ const ids = Object.keys(rowSelection).filter(
12788
+ (id) => !items.find((i) => i.variant_id === id)
12789
+ );
12869
12790
  await mutateAsync(
12870
12791
  {
12871
- shipping_address: {
12872
- first_name: data.first_name,
12873
- last_name: data.last_name,
12874
- company: data.company,
12875
- address_1: data.address_1,
12876
- address_2: data.address_2,
12877
- city: data.city,
12878
- province: data.province,
12879
- country_code: data.country_code,
12880
- postal_code: data.postal_code,
12881
- phone: data.phone
12882
- }
12792
+ items: ids.map((id) => ({
12793
+ variant_id: id,
12794
+ quantity: 1
12795
+ }))
12883
12796
  },
12884
12797
  {
12885
12798
  onSuccess: () => {
12886
- handleSuccess();
12799
+ setRowSelection({});
12800
+ setIsOpen(STACKED_MODAL_ID, false);
12887
12801
  },
12888
- onError: (error) => {
12889
- ui.toast.error(error.message);
12802
+ onError: (e) => {
12803
+ ui.toast.error(e.message);
12890
12804
  }
12891
12805
  }
12892
12806
  );
12893
- });
12894
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12895
- KeyboundForm,
12807
+ };
12808
+ if (isError) {
12809
+ throw error;
12810
+ }
12811
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12812
+ StackedFocusModal.Content,
12896
12813
  {
12897
- className: "flex flex-1 flex-col overflow-hidden",
12898
- onSubmit,
12814
+ onOpenAutoFocus: (e) => {
12815
+ e.preventDefault();
12816
+ const searchInput = document.querySelector(
12817
+ "[data-modal-id='modal-search-input']"
12818
+ );
12819
+ if (searchInput) {
12820
+ searchInput.focus();
12821
+ }
12822
+ },
12899
12823
  children: [
12900
- /* @__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: [
12901
- /* @__PURE__ */ jsxRuntime.jsx(
12902
- Form$2.Field,
12903
- {
12904
- control: form.control,
12905
- name: "country_code",
12906
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12907
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
12908
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
12909
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12910
- ] })
12911
- }
12912
- ),
12913
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12914
- /* @__PURE__ */ jsxRuntime.jsx(
12915
- Form$2.Field,
12916
- {
12917
- control: form.control,
12918
- name: "first_name",
12919
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12920
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
12921
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12922
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12923
- ] })
12924
- }
12925
- ),
12926
- /* @__PURE__ */ jsxRuntime.jsx(
12927
- Form$2.Field,
12928
- {
12929
- control: form.control,
12930
- name: "last_name",
12931
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12932
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
12933
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12934
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12935
- ] })
12936
- }
12937
- )
12938
- ] }),
12939
- /* @__PURE__ */ jsxRuntime.jsx(
12940
- Form$2.Field,
12941
- {
12942
- control: form.control,
12943
- name: "company",
12944
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12945
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
12946
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12947
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12948
- ] })
12949
- }
12950
- ),
12951
- /* @__PURE__ */ jsxRuntime.jsx(
12952
- Form$2.Field,
12953
- {
12954
- control: form.control,
12955
- name: "address_1",
12956
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12957
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
12958
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12959
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12960
- ] })
12961
- }
12962
- ),
12963
- /* @__PURE__ */ jsxRuntime.jsx(
12964
- Form$2.Field,
12965
- {
12966
- control: form.control,
12967
- name: "address_2",
12968
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12969
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12970
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12971
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12972
- ] })
12973
- }
12974
- ),
12975
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12976
- /* @__PURE__ */ jsxRuntime.jsx(
12977
- Form$2.Field,
12978
- {
12979
- control: form.control,
12980
- name: "postal_code",
12981
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12982
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
12983
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12984
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12985
- ] })
12824
+ /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
12825
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
12826
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
12827
+ ] }),
12828
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
12829
+ DataTable,
12830
+ {
12831
+ data: variants,
12832
+ columns,
12833
+ isLoading: isPending,
12834
+ getRowId: (row) => row.id,
12835
+ rowCount: count,
12836
+ prefix: VARIANT_PREFIX,
12837
+ layout: "fill",
12838
+ rowSelection: {
12839
+ state: rowSelection,
12840
+ onRowSelectionChange: setRowSelection,
12841
+ enableRowSelection: (row) => {
12842
+ return !items.find((i) => i.variant_id === row.original.id);
12986
12843
  }
12987
- ),
12844
+ },
12845
+ autoFocusSearch: true
12846
+ }
12847
+ ) }),
12848
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
12849
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12850
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
12851
+ ] }) })
12852
+ ]
12853
+ }
12854
+ );
12855
+ };
12856
+ const columnHelper = ui.createDataTableColumnHelper();
12857
+ const useColumns = () => {
12858
+ return React.useMemo(() => {
12859
+ return [
12860
+ columnHelper.select(),
12861
+ columnHelper.accessor("product.title", {
12862
+ header: "Product",
12863
+ cell: ({ row }) => {
12864
+ var _a, _b, _c;
12865
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
12988
12866
  /* @__PURE__ */ jsxRuntime.jsx(
12989
- Form$2.Field,
12867
+ Thumbnail,
12990
12868
  {
12991
- control: form.control,
12992
- name: "city",
12993
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12994
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
12995
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12996
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12997
- ] })
12869
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
12870
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
12998
12871
  }
12999
- )
13000
- ] }),
13001
- /* @__PURE__ */ jsxRuntime.jsx(
13002
- Form$2.Field,
12872
+ ),
12873
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
12874
+ ] });
12875
+ },
12876
+ enableSorting: true
12877
+ }),
12878
+ columnHelper.accessor("title", {
12879
+ header: "Variant",
12880
+ enableSorting: true
12881
+ }),
12882
+ columnHelper.accessor("sku", {
12883
+ header: "SKU",
12884
+ cell: ({ getValue }) => {
12885
+ return getValue() ?? "-";
12886
+ },
12887
+ enableSorting: true
12888
+ }),
12889
+ columnHelper.accessor("updated_at", {
12890
+ header: "Updated",
12891
+ cell: ({ getValue }) => {
12892
+ return /* @__PURE__ */ jsxRuntime.jsx(
12893
+ ui.Tooltip,
13003
12894
  {
13004
- control: form.control,
13005
- name: "province",
13006
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13007
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
13008
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13009
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13010
- ] })
12895
+ content: getFullDate({ date: getValue(), includeTime: true }),
12896
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
13011
12897
  }
13012
- ),
13013
- /* @__PURE__ */ jsxRuntime.jsx(
13014
- Form$2.Field,
12898
+ );
12899
+ },
12900
+ enableSorting: true,
12901
+ sortAscLabel: "Oldest first",
12902
+ sortDescLabel: "Newest first"
12903
+ }),
12904
+ columnHelper.accessor("created_at", {
12905
+ header: "Created",
12906
+ cell: ({ getValue }) => {
12907
+ return /* @__PURE__ */ jsxRuntime.jsx(
12908
+ ui.Tooltip,
13015
12909
  {
13016
- control: form.control,
13017
- name: "phone",
13018
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13019
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
13020
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13021
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13022
- ] })
12910
+ content: getFullDate({ date: getValue(), includeTime: true }),
12911
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
13023
12912
  }
13024
- )
13025
- ] }) }),
13026
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
13027
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13028
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13029
- ] }) })
13030
- ]
13031
- }
13032
- ) });
12913
+ );
12914
+ },
12915
+ enableSorting: true,
12916
+ sortAscLabel: "Oldest first",
12917
+ sortDescLabel: "Newest first"
12918
+ })
12919
+ ];
12920
+ }, []);
12921
+ };
12922
+ const CustomItemForm = ({ orderId, currencyCode }) => {
12923
+ const { setIsOpen } = useStackedModal();
12924
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
12925
+ const form = reactHookForm.useForm({
12926
+ defaultValues: {
12927
+ title: "",
12928
+ quantity: 1,
12929
+ unit_price: ""
12930
+ },
12931
+ resolver: zod.zodResolver(customItemSchema)
12932
+ });
12933
+ const onSubmit = form.handleSubmit(async (data) => {
12934
+ await addItems(
12935
+ {
12936
+ items: [
12937
+ {
12938
+ title: data.title,
12939
+ quantity: data.quantity,
12940
+ unit_price: convertNumber(data.unit_price)
12941
+ }
12942
+ ]
12943
+ },
12944
+ {
12945
+ onSuccess: () => {
12946
+ setIsOpen(STACKED_MODAL_ID, false);
12947
+ },
12948
+ onError: (e) => {
12949
+ ui.toast.error(e.message);
12950
+ }
12951
+ }
12952
+ );
12953
+ });
12954
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
12955
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
12956
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
12957
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12958
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
12959
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
12960
+ ] }),
12961
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12962
+ /* @__PURE__ */ jsxRuntime.jsx(
12963
+ Form$2.Field,
12964
+ {
12965
+ control: form.control,
12966
+ name: "title",
12967
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12968
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12969
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
12970
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
12971
+ ] }),
12972
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12973
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12974
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12975
+ ] })
12976
+ ] }) })
12977
+ }
12978
+ ),
12979
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12980
+ /* @__PURE__ */ jsxRuntime.jsx(
12981
+ Form$2.Field,
12982
+ {
12983
+ control: form.control,
12984
+ name: "unit_price",
12985
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12986
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12987
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
12988
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
12989
+ ] }),
12990
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12991
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12992
+ ui.CurrencyInput,
12993
+ {
12994
+ symbol: getNativeSymbol(currencyCode),
12995
+ code: currencyCode,
12996
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
12997
+ ...field
12998
+ }
12999
+ ) }),
13000
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13001
+ ] })
13002
+ ] }) })
13003
+ }
13004
+ ),
13005
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
13006
+ /* @__PURE__ */ jsxRuntime.jsx(
13007
+ Form$2.Field,
13008
+ {
13009
+ control: form.control,
13010
+ name: "quantity",
13011
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
13012
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
13013
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
13014
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
13015
+ ] }),
13016
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex-1 w-full", children: [
13017
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field, className: "w-full" }) }) }),
13018
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13019
+ ] })
13020
+ ] }) })
13021
+ }
13022
+ )
13023
+ ] }) }) }),
13024
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
13025
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
13026
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
13027
+ ] }) })
13028
+ ] }) }) });
13033
13029
  };
13034
- const schema = addressSchema;
13030
+ const customItemSchema = objectType({
13031
+ title: stringType().min(1),
13032
+ quantity: numberType(),
13033
+ unit_price: unionType([numberType(), stringType()])
13034
+ });
13035
13035
  const widgetModule = { widgets: [] };
13036
13036
  const routeModule = {
13037
13037
  routes: [
@@ -13065,17 +13065,13 @@ const routeModule = {
13065
13065
  path: "/draft-orders/:id/email"
13066
13066
  },
13067
13067
  {
13068
- Component: Items,
13069
- path: "/draft-orders/:id/items"
13068
+ Component: Promotions,
13069
+ path: "/draft-orders/:id/promotions"
13070
13070
  },
13071
13071
  {
13072
13072
  Component: Metadata,
13073
13073
  path: "/draft-orders/:id/metadata"
13074
13074
  },
13075
- {
13076
- Component: Promotions,
13077
- path: "/draft-orders/:id/promotions"
13078
- },
13079
13075
  {
13080
13076
  Component: SalesChannel,
13081
13077
  path: "/draft-orders/:id/sales-channel"
@@ -13084,13 +13080,17 @@ const routeModule = {
13084
13080
  Component: Shipping,
13085
13081
  path: "/draft-orders/:id/shipping"
13086
13082
  },
13083
+ {
13084
+ Component: ShippingAddress,
13085
+ path: "/draft-orders/:id/shipping-address"
13086
+ },
13087
13087
  {
13088
13088
  Component: TransferOwnership,
13089
13089
  path: "/draft-orders/:id/transfer-ownership"
13090
13090
  },
13091
13091
  {
13092
- Component: ShippingAddress,
13093
- path: "/draft-orders/:id/shipping-address"
13092
+ Component: Items,
13093
+ path: "/draft-orders/:id/items"
13094
13094
  }
13095
13095
  ]
13096
13096
  }