@medusajs/draft-order 2.12.0 → 2.12.1

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