@medusajs/draft-order 2.10.4-preview-20251004000335 → 2.10.4-preview-20251004060158

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