@medusajs/draft-order 2.19.0 → 2.19.1-preview-20260813160157

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.
@@ -7220,88 +7220,46 @@ const CustomItemsForm = () => {
7220
7220
  const schema$3 = z.object({
7221
7221
  email: z.string().email()
7222
7222
  });
7223
- const InlineTip = forwardRef(
7224
- ({ variant = "tip", label, className, children, ...props }, ref) => {
7225
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
7226
- return /* @__PURE__ */ jsxs(
7227
- "div",
7228
- {
7229
- ref,
7230
- className: clx(
7231
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
7232
- className
7233
- ),
7234
- ...props,
7235
- children: [
7236
- /* @__PURE__ */ jsx(
7237
- "div",
7238
- {
7239
- role: "presentation",
7240
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
7241
- "bg-ui-tag-orange-icon": variant === "warning"
7242
- })
7243
- }
7244
- ),
7245
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
7246
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
7247
- labelValue,
7248
- ":"
7249
- ] }),
7250
- " ",
7251
- children
7252
- ] })
7253
- ]
7254
- }
7255
- );
7256
- }
7257
- );
7258
- InlineTip.displayName = "InlineTip";
7259
- const MetadataFieldSchema = z.object({
7260
- key: z.string(),
7261
- disabled: z.boolean().optional(),
7262
- value: z.any()
7263
- });
7264
- const MetadataSchema = z.object({
7265
- metadata: z.array(MetadataFieldSchema)
7266
- });
7267
- const Metadata = () => {
7223
+ const SalesChannel = () => {
7268
7224
  const { id } = useParams();
7269
- const { order, isPending, isError, error } = useOrder(id, {
7270
- fields: "metadata"
7271
- });
7225
+ const { draft_order, isPending, isError, error } = useDraftOrder(
7226
+ id,
7227
+ {
7228
+ fields: "+sales_channel_id"
7229
+ },
7230
+ {
7231
+ enabled: !!id
7232
+ }
7233
+ );
7272
7234
  if (isError) {
7273
7235
  throw error;
7274
7236
  }
7275
- const isReady = !isPending && !!order;
7237
+ const ISrEADY = !!draft_order && !isPending;
7276
7238
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
7277
7239
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
7278
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
7279
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
7240
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
7241
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
7280
7242
  ] }),
7281
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order?.metadata })
7243
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
7282
7244
  ] });
7283
7245
  };
7284
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
7285
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
7286
- const MetadataForm = ({ orderId, metadata }) => {
7287
- const { handleSuccess } = useRouteModal();
7288
- const hasUneditableRows = getHasUneditableRows(metadata);
7289
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
7246
+ const SalesChannelForm = ({ order }) => {
7290
7247
  const form = useForm({
7291
7248
  defaultValues: {
7292
- metadata: getDefaultValues(metadata)
7249
+ sales_channel_id: order.sales_channel_id || ""
7293
7250
  },
7294
- resolver: zodResolver(MetadataSchema)
7251
+ resolver: zodResolver(schema$2)
7295
7252
  });
7296
- const handleSubmit = form.handleSubmit(async (data) => {
7297
- const parsedData = parseValues(data);
7253
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
7254
+ const { handleSuccess } = useRouteModal();
7255
+ const onSubmit = form.handleSubmit(async (data) => {
7298
7256
  await mutateAsync(
7299
7257
  {
7300
- metadata: parsedData
7258
+ sales_channel_id: data.sales_channel_id
7301
7259
  },
7302
7260
  {
7303
7261
  onSuccess: () => {
7304
- toast.success("Metadata updated");
7262
+ toast.success("Sales channel updated");
7305
7263
  handleSuccess();
7306
7264
  },
7307
7265
  onError: (error) => {
@@ -7310,365 +7268,133 @@ const MetadataForm = ({ orderId, metadata }) => {
7310
7268
  }
7311
7269
  );
7312
7270
  });
7313
- const { fields, insert, remove } = useFieldArray({
7314
- control: form.control,
7315
- name: "metadata"
7316
- });
7317
- function deleteRow(index) {
7318
- remove(index);
7319
- if (fields.length === 1) {
7320
- insert(0, {
7321
- key: "",
7322
- value: "",
7323
- disabled: false
7324
- });
7325
- }
7326
- }
7327
- function insertRow(index, position) {
7328
- insert(index + (position === "above" ? 0 : 1), {
7329
- key: "",
7330
- value: "",
7331
- disabled: false
7332
- });
7333
- }
7334
7271
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
7335
7272
  KeyboundForm,
7336
7273
  {
7337
- onSubmit: handleSubmit,
7338
7274
  className: "flex flex-1 flex-col overflow-hidden",
7275
+ onSubmit,
7339
7276
  children: [
7340
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
7341
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
7342
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
7343
- /* @__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" }) }),
7344
- /* @__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" }) })
7345
- ] }),
7346
- fields.map((field, index) => {
7347
- const isDisabled = field.disabled || false;
7348
- let placeholder = "-";
7349
- if (typeof field.value === "object") {
7350
- placeholder = "{ ... }";
7351
- }
7352
- if (Array.isArray(field.value)) {
7353
- placeholder = "[ ... ]";
7354
- }
7355
- return /* @__PURE__ */ jsx(
7356
- ConditionalTooltip,
7357
- {
7358
- showTooltip: isDisabled,
7359
- content: "This row is disabled because it contains non-primitive data.",
7360
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
7361
- /* @__PURE__ */ jsxs(
7362
- "div",
7363
- {
7364
- className: clx("grid grid-cols-2 divide-x", {
7365
- "overflow-hidden rounded-b-lg": index === fields.length - 1
7366
- }),
7367
- children: [
7368
- /* @__PURE__ */ jsx(
7369
- Form$2.Field,
7370
- {
7371
- control: form.control,
7372
- name: `metadata.${index}.key`,
7373
- render: ({ field: field2 }) => {
7374
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
7375
- GridInput,
7376
- {
7377
- "aria-labelledby": METADATA_KEY_LABEL_ID,
7378
- ...field2,
7379
- disabled: isDisabled,
7380
- placeholder: "Key"
7381
- }
7382
- ) }) });
7383
- }
7384
- }
7385
- ),
7386
- /* @__PURE__ */ jsx(
7387
- Form$2.Field,
7388
- {
7389
- control: form.control,
7390
- name: `metadata.${index}.value`,
7391
- render: ({ field: { value, ...field2 } }) => {
7392
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
7393
- GridInput,
7394
- {
7395
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
7396
- ...field2,
7397
- value: isDisabled ? placeholder : value,
7398
- disabled: isDisabled,
7399
- placeholder: "Value"
7400
- }
7401
- ) }) });
7402
- }
7403
- }
7404
- )
7405
- ]
7406
- }
7407
- ),
7408
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
7409
- /* @__PURE__ */ jsx(
7410
- DropdownMenu.Trigger,
7411
- {
7412
- className: clx(
7413
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
7414
- {
7415
- hidden: isDisabled
7416
- }
7417
- ),
7418
- disabled: isDisabled,
7419
- asChild: true,
7420
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
7421
- }
7422
- ),
7423
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
7424
- /* @__PURE__ */ jsxs(
7425
- DropdownMenu.Item,
7426
- {
7427
- className: "gap-x-2",
7428
- onClick: () => insertRow(index, "above"),
7429
- children: [
7430
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
7431
- "Insert row above"
7432
- ]
7433
- }
7434
- ),
7435
- /* @__PURE__ */ jsxs(
7436
- DropdownMenu.Item,
7437
- {
7438
- className: "gap-x-2",
7439
- onClick: () => insertRow(index, "below"),
7440
- children: [
7441
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
7442
- "Insert row below"
7443
- ]
7444
- }
7445
- ),
7446
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
7447
- /* @__PURE__ */ jsxs(
7448
- DropdownMenu.Item,
7449
- {
7450
- className: "gap-x-2",
7451
- onClick: () => deleteRow(index),
7452
- children: [
7453
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
7454
- "Delete row"
7455
- ]
7456
- }
7457
- )
7458
- ] })
7459
- ] })
7460
- ] })
7461
- },
7462
- field.id
7463
- );
7464
- })
7465
- ] }),
7466
- 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." })
7467
- ] }),
7468
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
7469
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
7277
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
7278
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
7279
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7470
7280
  /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
7471
7281
  ] }) })
7472
7282
  ]
7473
7283
  }
7474
7284
  ) });
7475
7285
  };
7476
- const GridInput = forwardRef(({ className, ...props }, ref) => {
7477
- return /* @__PURE__ */ jsx(
7478
- "input",
7479
- {
7480
- ref,
7481
- ...props,
7482
- autoComplete: "off",
7483
- className: clx(
7484
- "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",
7485
- className
7486
- )
7487
- }
7488
- );
7489
- });
7490
- GridInput.displayName = "MetadataForm.GridInput";
7491
- const PlaceholderInner = () => {
7492
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
7493
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
7494
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
7495
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
7496
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
7497
- ] }) })
7498
- ] });
7499
- };
7500
- const EDITABLE_TYPES = ["string", "number", "boolean"];
7501
- function getDefaultValues(metadata) {
7502
- if (!metadata || !Object.keys(metadata).length) {
7503
- return [
7504
- {
7505
- key: "",
7506
- value: "",
7507
- disabled: false
7508
- }
7509
- ];
7510
- }
7511
- return Object.entries(metadata).map(([key, value]) => {
7512
- if (!EDITABLE_TYPES.includes(typeof value)) {
7513
- return {
7514
- key,
7515
- value,
7516
- disabled: true
7517
- };
7518
- }
7519
- let stringValue = value;
7520
- if (typeof value !== "string") {
7521
- stringValue = JSON.stringify(value);
7522
- }
7523
- return {
7524
- key,
7525
- value: stringValue,
7526
- original_key: key
7527
- };
7286
+ const SalesChannelField = ({ control, order }) => {
7287
+ const salesChannels = useComboboxData({
7288
+ queryFn: async (params) => {
7289
+ return await sdk.admin.salesChannel.list(params);
7290
+ },
7291
+ queryKey: ["sales-channels"],
7292
+ getOptions: (data) => {
7293
+ return data.sales_channels.map((salesChannel) => ({
7294
+ label: salesChannel.name,
7295
+ value: salesChannel.id
7296
+ }));
7297
+ },
7298
+ defaultValue: order.sales_channel_id || void 0
7528
7299
  });
7529
- }
7530
- function parseValues(values) {
7531
- const metadata = values.metadata;
7532
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
7533
- if (isEmpty) {
7534
- return null;
7535
- }
7536
- const update = {};
7537
- metadata.forEach((field) => {
7538
- let key = field.key;
7539
- let value = field.value;
7540
- const disabled = field.disabled;
7541
- if (!key || !value) {
7542
- return;
7543
- }
7544
- if (disabled) {
7545
- update[key] = value;
7546
- return;
7547
- }
7548
- key = key.trim();
7549
- value = value.trim();
7550
- if (value === "true") {
7551
- update[key] = true;
7552
- } else if (value === "false") {
7553
- update[key] = false;
7554
- } else {
7555
- const parsedNumber = parseFloat(value);
7556
- if (!isNaN(parsedNumber)) {
7557
- update[key] = parsedNumber;
7558
- } else {
7559
- update[key] = value;
7300
+ return /* @__PURE__ */ jsx(
7301
+ Form$2.Field,
7302
+ {
7303
+ control,
7304
+ name: "sales_channel_id",
7305
+ render: ({ field }) => {
7306
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
7307
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
7308
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
7309
+ Combobox,
7310
+ {
7311
+ options: salesChannels.options,
7312
+ fetchNextPage: salesChannels.fetchNextPage,
7313
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
7314
+ searchValue: salesChannels.searchValue,
7315
+ onSearchValueChange: salesChannels.onSearchValueChange,
7316
+ placeholder: "Select sales channel",
7317
+ ...field
7318
+ }
7319
+ ) }),
7320
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
7321
+ ] });
7560
7322
  }
7561
7323
  }
7562
- });
7563
- return update;
7564
- }
7565
- function getHasUneditableRows(metadata) {
7566
- if (!metadata) {
7567
- return false;
7568
- }
7569
- return Object.values(metadata).some(
7570
- (value) => !EDITABLE_TYPES.includes(typeof value)
7571
7324
  );
7572
- }
7573
- const PROMOTION_QUERY_KEY = "promotions";
7574
- const promotionsQueryKeys = {
7575
- list: (query2) => [
7576
- PROMOTION_QUERY_KEY,
7577
- query2 ? query2 : void 0
7578
- ],
7579
- detail: (id, query2) => [
7580
- PROMOTION_QUERY_KEY,
7581
- id,
7582
- query2 ? query2 : void 0
7583
- ]
7584
- };
7585
- const usePromotions = (query2, options) => {
7586
- const { data, ...rest } = useQuery({
7587
- queryKey: promotionsQueryKeys.list(query2),
7588
- queryFn: async () => sdk.admin.promotion.list(query2),
7589
- ...options
7590
- });
7591
- return { ...data, ...rest };
7592
7325
  };
7593
- const Promotions = () => {
7326
+ const schema$2 = z.object({
7327
+ sales_channel_id: z.string().min(1)
7328
+ });
7329
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
7330
+ const Shipping = () => {
7594
7331
  const { id } = useParams();
7332
+ const { order, isPending, isError, error } = useOrder(id, {
7333
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
7334
+ });
7595
7335
  const {
7596
7336
  order: preview,
7337
+ isPending: isPreviewPending,
7597
7338
  isError: isPreviewError,
7598
7339
  error: previewError
7599
- } = useOrderPreview(id, void 0);
7340
+ } = useOrderPreview(id);
7600
7341
  useInitiateOrderEdit({ preview });
7601
7342
  const { onCancel } = useCancelOrderEdit({ preview });
7343
+ if (isError) {
7344
+ throw error;
7345
+ }
7602
7346
  if (isPreviewError) {
7603
7347
  throw previewError;
7604
7348
  }
7605
- const isReady = !!preview;
7606
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
7607
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
7608
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
7609
- ] });
7349
+ const orderHasItems = (order?.items?.length || 0) > 0;
7350
+ const isReady = preview && !isPreviewPending && order && !isPending;
7351
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
7352
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
7353
+ /* @__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: [
7354
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
7355
+ /* @__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." }) })
7356
+ ] }) }) }),
7357
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
7358
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
7359
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
7360
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
7361
+ ] }) });
7610
7362
  };
7611
- const PromotionForm = ({ preview }) => {
7612
- const { items, shipping_methods } = preview;
7363
+ const ShippingForm = ({ preview, order }) => {
7364
+ const { setIsOpen } = useStackedModal();
7613
7365
  const [isSubmitting, setIsSubmitting] = useState(false);
7614
- const [comboboxValue, setComboboxValue] = useState("");
7615
- const { handleSuccess } = useRouteModal();
7616
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
7617
- const promoIds = getPromotionIds(items, shipping_methods);
7618
- const { promotions, isPending, isError, error } = usePromotions(
7366
+ const [data, setData] = useState(null);
7367
+ const appliedShippingOptionIds = preview.shipping_methods?.map((method) => method.shipping_option_id).filter(Boolean);
7368
+ const { shipping_options } = useShippingOptions(
7619
7369
  {
7620
- id: promoIds
7370
+ id: appliedShippingOptionIds,
7371
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
7621
7372
  },
7622
7373
  {
7623
- enabled: !!promoIds.length
7374
+ enabled: appliedShippingOptionIds.length > 0
7624
7375
  }
7625
7376
  );
7626
- const comboboxData = useComboboxData({
7627
- queryKey: ["promotions", "combobox", promoIds],
7628
- queryFn: async (params) => {
7629
- return await sdk.admin.promotion.list({
7630
- ...params,
7631
- id: {
7632
- $nin: promoIds
7633
- }
7634
- });
7635
- },
7636
- getOptions: (data) => {
7637
- return data.promotions.map((promotion) => ({
7638
- label: promotion.code,
7639
- value: promotion.code
7640
- }));
7641
- }
7642
- });
7643
- const add = async (value) => {
7644
- if (!value) {
7645
- return;
7646
- }
7647
- addPromotions(
7648
- {
7649
- promo_codes: [value]
7650
- },
7651
- {
7652
- onError: (e) => {
7653
- toast.error(e.message);
7654
- comboboxData.onSearchValueChange("");
7655
- setComboboxValue("");
7656
- },
7657
- onSuccess: () => {
7658
- comboboxData.onSearchValueChange("");
7659
- setComboboxValue("");
7660
- }
7661
- }
7662
- );
7663
- };
7377
+ const uniqueShippingProfiles = useMemo(() => {
7378
+ const profiles = /* @__PURE__ */ new Map();
7379
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
7380
+ profiles.set(profile.id, profile);
7381
+ });
7382
+ shipping_options?.forEach((option) => {
7383
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
7384
+ });
7385
+ return Array.from(profiles.values());
7386
+ }, [order.items, shipping_options]);
7387
+ const { handleSuccess } = useRouteModal();
7664
7388
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
7665
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
7389
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
7390
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
7391
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
7666
7392
  const onSubmit = async () => {
7667
7393
  setIsSubmitting(true);
7668
7394
  let requestSucceeded = false;
7669
7395
  await requestOrderEdit(void 0, {
7670
7396
  onError: (e) => {
7671
- toast.error(e.message);
7397
+ toast.error(`Failed to request order edit: ${e.message}`);
7672
7398
  },
7673
7399
  onSuccess: () => {
7674
7400
  requestSucceeded = true;
@@ -7680,358 +7406,7 @@ const PromotionForm = ({ preview }) => {
7680
7406
  }
7681
7407
  await confirmOrderEdit(void 0, {
7682
7408
  onError: (e) => {
7683
- toast.error(e.message);
7684
- },
7685
- onSuccess: () => {
7686
- handleSuccess();
7687
- },
7688
- onSettled: () => {
7689
- setIsSubmitting(false);
7690
- }
7691
- });
7692
- };
7693
- if (isError) {
7694
- throw error;
7695
- }
7696
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
7697
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
7698
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
7699
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
7700
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
7701
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
7702
- ] }),
7703
- /* @__PURE__ */ jsx(
7704
- Combobox,
7705
- {
7706
- id: "promotion-combobox",
7707
- "aria-describedby": "promotion-combobox-hint",
7708
- isFetchingNextPage: comboboxData.isFetchingNextPage,
7709
- fetchNextPage: comboboxData.fetchNextPage,
7710
- options: comboboxData.options,
7711
- onSearchValueChange: comboboxData.onSearchValueChange,
7712
- searchValue: comboboxData.searchValue,
7713
- disabled: comboboxData.disabled || isAddingPromotions,
7714
- onChange: add,
7715
- value: comboboxValue
7716
- }
7717
- )
7718
- ] }),
7719
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
7720
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions?.map((promotion) => /* @__PURE__ */ jsx(
7721
- PromotionItem,
7722
- {
7723
- promotion,
7724
- orderId: preview.id,
7725
- isLoading: isPending
7726
- },
7727
- promotion.id
7728
- )) })
7729
- ] }) }),
7730
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
7731
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7732
- /* @__PURE__ */ jsx(
7733
- Button,
7734
- {
7735
- size: "small",
7736
- type: "submit",
7737
- isLoading: isSubmitting || isAddingPromotions,
7738
- children: "Save"
7739
- }
7740
- )
7741
- ] }) })
7742
- ] });
7743
- };
7744
- const PromotionItem = ({
7745
- promotion,
7746
- orderId,
7747
- isLoading
7748
- }) => {
7749
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
7750
- const onRemove = async () => {
7751
- removePromotions(
7752
- {
7753
- promo_codes: [promotion.code]
7754
- },
7755
- {
7756
- onError: (e) => {
7757
- toast.error(e.message);
7758
- }
7759
- }
7760
- );
7761
- };
7762
- const displayValue = getDisplayValue(promotion);
7763
- return /* @__PURE__ */ jsxs(
7764
- "div",
7765
- {
7766
- className: clx(
7767
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
7768
- {
7769
- "animate-pulse": isLoading
7770
- }
7771
- ),
7772
- children: [
7773
- /* @__PURE__ */ jsxs("div", { children: [
7774
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
7775
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
7776
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
7777
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
7778
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
7779
- ] }),
7780
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: promotion.application_method?.allocation })
7781
- ] })
7782
- ] }),
7783
- /* @__PURE__ */ jsx(
7784
- IconButton,
7785
- {
7786
- size: "small",
7787
- type: "button",
7788
- variant: "transparent",
7789
- onClick: onRemove,
7790
- isLoading: isPending || isLoading,
7791
- children: /* @__PURE__ */ jsx(XMark, {})
7792
- }
7793
- )
7794
- ]
7795
- },
7796
- promotion.id
7797
- );
7798
- };
7799
- function getDisplayValue(promotion) {
7800
- const value = promotion.application_method?.value;
7801
- if (!value) {
7802
- return null;
7803
- }
7804
- if (promotion.application_method?.type === "fixed") {
7805
- const currency = promotion.application_method?.currency_code;
7806
- if (!currency) {
7807
- return null;
7808
- }
7809
- return getLocaleAmount(value, currency);
7810
- } else if (promotion.application_method?.type === "percentage") {
7811
- return formatPercentage(value);
7812
- }
7813
- return null;
7814
- }
7815
- const formatter = new Intl.NumberFormat([], {
7816
- style: "percent",
7817
- minimumFractionDigits: 2
7818
- });
7819
- const formatPercentage = (value, isPercentageValue = false) => {
7820
- let val = value || 0;
7821
- if (!isPercentageValue) {
7822
- val = val / 100;
7823
- }
7824
- return formatter.format(val);
7825
- };
7826
- function getPromotionIds(items, shippingMethods) {
7827
- const promotionIds = /* @__PURE__ */ new Set();
7828
- for (const item of items) {
7829
- if (item.adjustments) {
7830
- for (const adjustment of item.adjustments) {
7831
- if (adjustment.promotion_id) {
7832
- promotionIds.add(adjustment.promotion_id);
7833
- }
7834
- }
7835
- }
7836
- }
7837
- for (const shippingMethod of shippingMethods) {
7838
- if (shippingMethod.adjustments) {
7839
- for (const adjustment of shippingMethod.adjustments) {
7840
- if (adjustment.promotion_id) {
7841
- promotionIds.add(adjustment.promotion_id);
7842
- }
7843
- }
7844
- }
7845
- }
7846
- return Array.from(promotionIds);
7847
- }
7848
- const SalesChannel = () => {
7849
- const { id } = useParams();
7850
- const { draft_order, isPending, isError, error } = useDraftOrder(
7851
- id,
7852
- {
7853
- fields: "+sales_channel_id"
7854
- },
7855
- {
7856
- enabled: !!id
7857
- }
7858
- );
7859
- if (isError) {
7860
- throw error;
7861
- }
7862
- const ISrEADY = !!draft_order && !isPending;
7863
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
7864
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
7865
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
7866
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
7867
- ] }),
7868
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
7869
- ] });
7870
- };
7871
- const SalesChannelForm = ({ order }) => {
7872
- const form = useForm({
7873
- defaultValues: {
7874
- sales_channel_id: order.sales_channel_id || ""
7875
- },
7876
- resolver: zodResolver(schema$2)
7877
- });
7878
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
7879
- const { handleSuccess } = useRouteModal();
7880
- const onSubmit = form.handleSubmit(async (data) => {
7881
- await mutateAsync(
7882
- {
7883
- sales_channel_id: data.sales_channel_id
7884
- },
7885
- {
7886
- onSuccess: () => {
7887
- toast.success("Sales channel updated");
7888
- handleSuccess();
7889
- },
7890
- onError: (error) => {
7891
- toast.error(error.message);
7892
- }
7893
- }
7894
- );
7895
- });
7896
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
7897
- KeyboundForm,
7898
- {
7899
- className: "flex flex-1 flex-col overflow-hidden",
7900
- onSubmit,
7901
- children: [
7902
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
7903
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
7904
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7905
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
7906
- ] }) })
7907
- ]
7908
- }
7909
- ) });
7910
- };
7911
- const SalesChannelField = ({ control, order }) => {
7912
- const salesChannels = useComboboxData({
7913
- queryFn: async (params) => {
7914
- return await sdk.admin.salesChannel.list(params);
7915
- },
7916
- queryKey: ["sales-channels"],
7917
- getOptions: (data) => {
7918
- return data.sales_channels.map((salesChannel) => ({
7919
- label: salesChannel.name,
7920
- value: salesChannel.id
7921
- }));
7922
- },
7923
- defaultValue: order.sales_channel_id || void 0
7924
- });
7925
- return /* @__PURE__ */ jsx(
7926
- Form$2.Field,
7927
- {
7928
- control,
7929
- name: "sales_channel_id",
7930
- render: ({ field }) => {
7931
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
7932
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
7933
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
7934
- Combobox,
7935
- {
7936
- options: salesChannels.options,
7937
- fetchNextPage: salesChannels.fetchNextPage,
7938
- isFetchingNextPage: salesChannels.isFetchingNextPage,
7939
- searchValue: salesChannels.searchValue,
7940
- onSearchValueChange: salesChannels.onSearchValueChange,
7941
- placeholder: "Select sales channel",
7942
- ...field
7943
- }
7944
- ) }),
7945
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
7946
- ] });
7947
- }
7948
- }
7949
- );
7950
- };
7951
- const schema$2 = z.object({
7952
- sales_channel_id: z.string().min(1)
7953
- });
7954
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
7955
- const Shipping = () => {
7956
- const { id } = useParams();
7957
- const { order, isPending, isError, error } = useOrder(id, {
7958
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
7959
- });
7960
- const {
7961
- order: preview,
7962
- isPending: isPreviewPending,
7963
- isError: isPreviewError,
7964
- error: previewError
7965
- } = useOrderPreview(id);
7966
- useInitiateOrderEdit({ preview });
7967
- const { onCancel } = useCancelOrderEdit({ preview });
7968
- if (isError) {
7969
- throw error;
7970
- }
7971
- if (isPreviewError) {
7972
- throw previewError;
7973
- }
7974
- const orderHasItems = (order?.items?.length || 0) > 0;
7975
- const isReady = preview && !isPreviewPending && order && !isPending;
7976
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
7977
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
7978
- /* @__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: [
7979
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
7980
- /* @__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." }) })
7981
- ] }) }) }),
7982
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
7983
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
7984
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
7985
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
7986
- ] }) });
7987
- };
7988
- const ShippingForm = ({ preview, order }) => {
7989
- const { setIsOpen } = useStackedModal();
7990
- const [isSubmitting, setIsSubmitting] = useState(false);
7991
- const [data, setData] = useState(null);
7992
- const appliedShippingOptionIds = preview.shipping_methods?.map((method) => method.shipping_option_id).filter(Boolean);
7993
- const { shipping_options } = useShippingOptions(
7994
- {
7995
- id: appliedShippingOptionIds,
7996
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
7997
- },
7998
- {
7999
- enabled: appliedShippingOptionIds.length > 0
8000
- }
8001
- );
8002
- const uniqueShippingProfiles = useMemo(() => {
8003
- const profiles = /* @__PURE__ */ new Map();
8004
- getUniqueShippingProfiles(order.items).forEach((profile) => {
8005
- profiles.set(profile.id, profile);
8006
- });
8007
- shipping_options?.forEach((option) => {
8008
- profiles.set(option.shipping_profile_id, option.shipping_profile);
8009
- });
8010
- return Array.from(profiles.values());
8011
- }, [order.items, shipping_options]);
8012
- const { handleSuccess } = useRouteModal();
8013
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
8014
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
8015
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
8016
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
8017
- const onSubmit = async () => {
8018
- setIsSubmitting(true);
8019
- let requestSucceeded = false;
8020
- await requestOrderEdit(void 0, {
8021
- onError: (e) => {
8022
- toast.error(`Failed to request order edit: ${e.message}`);
8023
- },
8024
- onSuccess: () => {
8025
- requestSucceeded = true;
8026
- }
8027
- });
8028
- if (!requestSucceeded) {
8029
- setIsSubmitting(false);
8030
- return;
8031
- }
8032
- await confirmOrderEdit(void 0, {
8033
- onError: (e) => {
8034
- toast.error(`Failed to confirm order edit: ${e.message}`);
7409
+ toast.error(`Failed to confirm order edit: ${e.message}`);
8035
7410
  },
8036
7411
  onSuccess: () => {
8037
7412
  handleSuccess();
@@ -9352,76 +8727,701 @@ const Illustration = () => {
9352
8727
  /* @__PURE__ */ jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsx(
9353
8728
  "path",
9354
8729
  {
9355
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
9356
- stroke: "#A1A1AA",
9357
- strokeWidth: "1.5",
9358
- strokeLinecap: "round",
9359
- strokeLinejoin: "round"
8730
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
8731
+ stroke: "#A1A1AA",
8732
+ strokeWidth: "1.5",
8733
+ strokeLinecap: "round",
8734
+ strokeLinejoin: "round"
8735
+ }
8736
+ ) }),
8737
+ /* @__PURE__ */ jsxs("defs", { children: [
8738
+ /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
8739
+ "rect",
8740
+ {
8741
+ width: "12",
8742
+ height: "12",
8743
+ fill: "white",
8744
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
8745
+ }
8746
+ ) }),
8747
+ /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
8748
+ "rect",
8749
+ {
8750
+ width: "12",
8751
+ height: "12",
8752
+ fill: "white",
8753
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
8754
+ }
8755
+ ) }),
8756
+ /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
8757
+ "rect",
8758
+ {
8759
+ width: "12",
8760
+ height: "12",
8761
+ fill: "white",
8762
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
8763
+ }
8764
+ ) }),
8765
+ /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
8766
+ "rect",
8767
+ {
8768
+ width: "12",
8769
+ height: "12",
8770
+ fill: "white",
8771
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
8772
+ }
8773
+ ) }),
8774
+ /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
8775
+ "rect",
8776
+ {
8777
+ width: "12",
8778
+ height: "12",
8779
+ fill: "white",
8780
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
8781
+ }
8782
+ ) }),
8783
+ /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
8784
+ "rect",
8785
+ {
8786
+ width: "12",
8787
+ height: "12",
8788
+ fill: "white",
8789
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
8790
+ }
8791
+ ) })
8792
+ ] })
8793
+ ]
8794
+ }
8795
+ );
8796
+ };
8797
+ const schema = z.object({
8798
+ customer_id: z.string().min(1)
8799
+ });
8800
+ const PROMOTION_QUERY_KEY = "promotions";
8801
+ const promotionsQueryKeys = {
8802
+ list: (query2) => [
8803
+ PROMOTION_QUERY_KEY,
8804
+ query2 ? query2 : void 0
8805
+ ],
8806
+ detail: (id, query2) => [
8807
+ PROMOTION_QUERY_KEY,
8808
+ id,
8809
+ query2 ? query2 : void 0
8810
+ ]
8811
+ };
8812
+ const usePromotions = (query2, options) => {
8813
+ const { data, ...rest } = useQuery({
8814
+ queryKey: promotionsQueryKeys.list(query2),
8815
+ queryFn: async () => sdk.admin.promotion.list(query2),
8816
+ ...options
8817
+ });
8818
+ return { ...data, ...rest };
8819
+ };
8820
+ const Promotions = () => {
8821
+ const { id } = useParams();
8822
+ const {
8823
+ order: preview,
8824
+ isError: isPreviewError,
8825
+ error: previewError
8826
+ } = useOrderPreview(id, void 0);
8827
+ useInitiateOrderEdit({ preview });
8828
+ const { onCancel } = useCancelOrderEdit({ preview });
8829
+ if (isPreviewError) {
8830
+ throw previewError;
8831
+ }
8832
+ const isReady = !!preview;
8833
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
8834
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
8835
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
8836
+ ] });
8837
+ };
8838
+ const PromotionForm = ({ preview }) => {
8839
+ const { items, shipping_methods } = preview;
8840
+ const [isSubmitting, setIsSubmitting] = useState(false);
8841
+ const [comboboxValue, setComboboxValue] = useState("");
8842
+ const { handleSuccess } = useRouteModal();
8843
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
8844
+ const promoIds = getPromotionIds(items, shipping_methods);
8845
+ const { promotions, isPending, isError, error } = usePromotions(
8846
+ {
8847
+ id: promoIds
8848
+ },
8849
+ {
8850
+ enabled: !!promoIds.length
8851
+ }
8852
+ );
8853
+ const comboboxData = useComboboxData({
8854
+ queryKey: ["promotions", "combobox", promoIds],
8855
+ queryFn: async (params) => {
8856
+ return await sdk.admin.promotion.list({
8857
+ ...params,
8858
+ id: {
8859
+ $nin: promoIds
8860
+ }
8861
+ });
8862
+ },
8863
+ getOptions: (data) => {
8864
+ return data.promotions.map((promotion) => ({
8865
+ label: promotion.code,
8866
+ value: promotion.code
8867
+ }));
8868
+ }
8869
+ });
8870
+ const add = async (value) => {
8871
+ if (!value) {
8872
+ return;
8873
+ }
8874
+ addPromotions(
8875
+ {
8876
+ promo_codes: [value]
8877
+ },
8878
+ {
8879
+ onError: (e) => {
8880
+ toast.error(e.message);
8881
+ comboboxData.onSearchValueChange("");
8882
+ setComboboxValue("");
8883
+ },
8884
+ onSuccess: () => {
8885
+ comboboxData.onSearchValueChange("");
8886
+ setComboboxValue("");
8887
+ }
8888
+ }
8889
+ );
8890
+ };
8891
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
8892
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
8893
+ const onSubmit = async () => {
8894
+ setIsSubmitting(true);
8895
+ let requestSucceeded = false;
8896
+ await requestOrderEdit(void 0, {
8897
+ onError: (e) => {
8898
+ toast.error(e.message);
8899
+ },
8900
+ onSuccess: () => {
8901
+ requestSucceeded = true;
8902
+ }
8903
+ });
8904
+ if (!requestSucceeded) {
8905
+ setIsSubmitting(false);
8906
+ return;
8907
+ }
8908
+ await confirmOrderEdit(void 0, {
8909
+ onError: (e) => {
8910
+ toast.error(e.message);
8911
+ },
8912
+ onSuccess: () => {
8913
+ handleSuccess();
8914
+ },
8915
+ onSettled: () => {
8916
+ setIsSubmitting(false);
8917
+ }
8918
+ });
8919
+ };
8920
+ if (isError) {
8921
+ throw error;
8922
+ }
8923
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
8924
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
8925
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
8926
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
8927
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
8928
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
8929
+ ] }),
8930
+ /* @__PURE__ */ jsx(
8931
+ Combobox,
8932
+ {
8933
+ id: "promotion-combobox",
8934
+ "aria-describedby": "promotion-combobox-hint",
8935
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
8936
+ fetchNextPage: comboboxData.fetchNextPage,
8937
+ options: comboboxData.options,
8938
+ onSearchValueChange: comboboxData.onSearchValueChange,
8939
+ searchValue: comboboxData.searchValue,
8940
+ disabled: comboboxData.disabled || isAddingPromotions,
8941
+ onChange: add,
8942
+ value: comboboxValue
8943
+ }
8944
+ )
8945
+ ] }),
8946
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
8947
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions?.map((promotion) => /* @__PURE__ */ jsx(
8948
+ PromotionItem,
8949
+ {
8950
+ promotion,
8951
+ orderId: preview.id,
8952
+ isLoading: isPending
8953
+ },
8954
+ promotion.id
8955
+ )) })
8956
+ ] }) }),
8957
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
8958
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
8959
+ /* @__PURE__ */ jsx(
8960
+ Button,
8961
+ {
8962
+ size: "small",
8963
+ type: "submit",
8964
+ isLoading: isSubmitting || isAddingPromotions,
8965
+ children: "Save"
8966
+ }
8967
+ )
8968
+ ] }) })
8969
+ ] });
8970
+ };
8971
+ const PromotionItem = ({
8972
+ promotion,
8973
+ orderId,
8974
+ isLoading
8975
+ }) => {
8976
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
8977
+ const onRemove = async () => {
8978
+ removePromotions(
8979
+ {
8980
+ promo_codes: [promotion.code]
8981
+ },
8982
+ {
8983
+ onError: (e) => {
8984
+ toast.error(e.message);
8985
+ }
8986
+ }
8987
+ );
8988
+ };
8989
+ const displayValue = getDisplayValue(promotion);
8990
+ return /* @__PURE__ */ jsxs(
8991
+ "div",
8992
+ {
8993
+ className: clx(
8994
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
8995
+ {
8996
+ "animate-pulse": isLoading
8997
+ }
8998
+ ),
8999
+ children: [
9000
+ /* @__PURE__ */ jsxs("div", { children: [
9001
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
9002
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
9003
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
9004
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
9005
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
9006
+ ] }),
9007
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: promotion.application_method?.allocation })
9008
+ ] })
9009
+ ] }),
9010
+ /* @__PURE__ */ jsx(
9011
+ IconButton,
9012
+ {
9013
+ size: "small",
9014
+ type: "button",
9015
+ variant: "transparent",
9016
+ onClick: onRemove,
9017
+ isLoading: isPending || isLoading,
9018
+ children: /* @__PURE__ */ jsx(XMark, {})
9360
9019
  }
9361
- ) }),
9362
- /* @__PURE__ */ jsxs("defs", { children: [
9363
- /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
9364
- "rect",
9365
- {
9366
- width: "12",
9367
- height: "12",
9368
- fill: "white",
9369
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
9370
- }
9371
- ) }),
9372
- /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
9373
- "rect",
9374
- {
9375
- width: "12",
9376
- height: "12",
9377
- fill: "white",
9378
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
9379
- }
9380
- ) }),
9381
- /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
9382
- "rect",
9383
- {
9384
- width: "12",
9385
- height: "12",
9386
- fill: "white",
9387
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
9388
- }
9389
- ) }),
9390
- /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
9391
- "rect",
9392
- {
9393
- width: "12",
9394
- height: "12",
9395
- fill: "white",
9396
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
9397
- }
9398
- ) }),
9399
- /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
9400
- "rect",
9401
- {
9402
- width: "12",
9403
- height: "12",
9404
- fill: "white",
9405
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
9406
- }
9407
- ) }),
9408
- /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
9409
- "rect",
9020
+ )
9021
+ ]
9022
+ },
9023
+ promotion.id
9024
+ );
9025
+ };
9026
+ function getDisplayValue(promotion) {
9027
+ const value = promotion.application_method?.value;
9028
+ if (!value) {
9029
+ return null;
9030
+ }
9031
+ if (promotion.application_method?.type === "fixed") {
9032
+ const currency = promotion.application_method?.currency_code;
9033
+ if (!currency) {
9034
+ return null;
9035
+ }
9036
+ return getLocaleAmount(value, currency);
9037
+ } else if (promotion.application_method?.type === "percentage") {
9038
+ return formatPercentage(value);
9039
+ }
9040
+ return null;
9041
+ }
9042
+ const formatter = new Intl.NumberFormat([], {
9043
+ style: "percent",
9044
+ minimumFractionDigits: 2
9045
+ });
9046
+ const formatPercentage = (value, isPercentageValue = false) => {
9047
+ let val = value || 0;
9048
+ if (!isPercentageValue) {
9049
+ val = val / 100;
9050
+ }
9051
+ return formatter.format(val);
9052
+ };
9053
+ function getPromotionIds(items, shippingMethods) {
9054
+ const promotionIds = /* @__PURE__ */ new Set();
9055
+ for (const item of items) {
9056
+ if (item.adjustments) {
9057
+ for (const adjustment of item.adjustments) {
9058
+ if (adjustment.promotion_id) {
9059
+ promotionIds.add(adjustment.promotion_id);
9060
+ }
9061
+ }
9062
+ }
9063
+ }
9064
+ for (const shippingMethod of shippingMethods) {
9065
+ if (shippingMethod.adjustments) {
9066
+ for (const adjustment of shippingMethod.adjustments) {
9067
+ if (adjustment.promotion_id) {
9068
+ promotionIds.add(adjustment.promotion_id);
9069
+ }
9070
+ }
9071
+ }
9072
+ }
9073
+ return Array.from(promotionIds);
9074
+ }
9075
+ const InlineTip = forwardRef(
9076
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9077
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9078
+ return /* @__PURE__ */ jsxs(
9079
+ "div",
9080
+ {
9081
+ ref,
9082
+ className: clx(
9083
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9084
+ className
9085
+ ),
9086
+ ...props,
9087
+ children: [
9088
+ /* @__PURE__ */ jsx(
9089
+ "div",
9410
9090
  {
9411
- width: "12",
9412
- height: "12",
9413
- fill: "white",
9414
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
9091
+ role: "presentation",
9092
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9093
+ "bg-ui-tag-orange-icon": variant === "warning"
9094
+ })
9415
9095
  }
9416
- ) })
9417
- ] })
9096
+ ),
9097
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9098
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9099
+ labelValue,
9100
+ ":"
9101
+ ] }),
9102
+ " ",
9103
+ children
9104
+ ] })
9105
+ ]
9106
+ }
9107
+ );
9108
+ }
9109
+ );
9110
+ InlineTip.displayName = "InlineTip";
9111
+ const MetadataFieldSchema = z.object({
9112
+ key: z.string(),
9113
+ disabled: z.boolean().optional(),
9114
+ value: z.any()
9115
+ });
9116
+ const MetadataSchema = z.object({
9117
+ metadata: z.array(MetadataFieldSchema)
9118
+ });
9119
+ const Metadata = () => {
9120
+ const { id } = useParams();
9121
+ const { order, isPending, isError, error } = useOrder(id, {
9122
+ fields: "metadata"
9123
+ });
9124
+ if (isError) {
9125
+ throw error;
9126
+ }
9127
+ const isReady = !isPending && !!order;
9128
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9129
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9130
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9131
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9132
+ ] }),
9133
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order?.metadata })
9134
+ ] });
9135
+ };
9136
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9137
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9138
+ const MetadataForm = ({ orderId, metadata }) => {
9139
+ const { handleSuccess } = useRouteModal();
9140
+ const hasUneditableRows = getHasUneditableRows(metadata);
9141
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9142
+ const form = useForm({
9143
+ defaultValues: {
9144
+ metadata: getDefaultValues(metadata)
9145
+ },
9146
+ resolver: zodResolver(MetadataSchema)
9147
+ });
9148
+ const handleSubmit = form.handleSubmit(async (data) => {
9149
+ const parsedData = parseValues(data);
9150
+ await mutateAsync(
9151
+ {
9152
+ metadata: parsedData
9153
+ },
9154
+ {
9155
+ onSuccess: () => {
9156
+ toast.success("Metadata updated");
9157
+ handleSuccess();
9158
+ },
9159
+ onError: (error) => {
9160
+ toast.error(error.message);
9161
+ }
9162
+ }
9163
+ );
9164
+ });
9165
+ const { fields, insert, remove } = useFieldArray({
9166
+ control: form.control,
9167
+ name: "metadata"
9168
+ });
9169
+ function deleteRow(index) {
9170
+ remove(index);
9171
+ if (fields.length === 1) {
9172
+ insert(0, {
9173
+ key: "",
9174
+ value: "",
9175
+ disabled: false
9176
+ });
9177
+ }
9178
+ }
9179
+ function insertRow(index, position) {
9180
+ insert(index + (position === "above" ? 0 : 1), {
9181
+ key: "",
9182
+ value: "",
9183
+ disabled: false
9184
+ });
9185
+ }
9186
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9187
+ KeyboundForm,
9188
+ {
9189
+ onSubmit: handleSubmit,
9190
+ className: "flex flex-1 flex-col overflow-hidden",
9191
+ children: [
9192
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9193
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9194
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9195
+ /* @__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" }) }),
9196
+ /* @__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" }) })
9197
+ ] }),
9198
+ fields.map((field, index) => {
9199
+ const isDisabled = field.disabled || false;
9200
+ let placeholder = "-";
9201
+ if (typeof field.value === "object") {
9202
+ placeholder = "{ ... }";
9203
+ }
9204
+ if (Array.isArray(field.value)) {
9205
+ placeholder = "[ ... ]";
9206
+ }
9207
+ return /* @__PURE__ */ jsx(
9208
+ ConditionalTooltip,
9209
+ {
9210
+ showTooltip: isDisabled,
9211
+ content: "This row is disabled because it contains non-primitive data.",
9212
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9213
+ /* @__PURE__ */ jsxs(
9214
+ "div",
9215
+ {
9216
+ className: clx("grid grid-cols-2 divide-x", {
9217
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9218
+ }),
9219
+ children: [
9220
+ /* @__PURE__ */ jsx(
9221
+ Form$2.Field,
9222
+ {
9223
+ control: form.control,
9224
+ name: `metadata.${index}.key`,
9225
+ render: ({ field: field2 }) => {
9226
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9227
+ GridInput,
9228
+ {
9229
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
9230
+ ...field2,
9231
+ disabled: isDisabled,
9232
+ placeholder: "Key"
9233
+ }
9234
+ ) }) });
9235
+ }
9236
+ }
9237
+ ),
9238
+ /* @__PURE__ */ jsx(
9239
+ Form$2.Field,
9240
+ {
9241
+ control: form.control,
9242
+ name: `metadata.${index}.value`,
9243
+ render: ({ field: { value, ...field2 } }) => {
9244
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9245
+ GridInput,
9246
+ {
9247
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
9248
+ ...field2,
9249
+ value: isDisabled ? placeholder : value,
9250
+ disabled: isDisabled,
9251
+ placeholder: "Value"
9252
+ }
9253
+ ) }) });
9254
+ }
9255
+ }
9256
+ )
9257
+ ]
9258
+ }
9259
+ ),
9260
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
9261
+ /* @__PURE__ */ jsx(
9262
+ DropdownMenu.Trigger,
9263
+ {
9264
+ className: clx(
9265
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9266
+ {
9267
+ hidden: isDisabled
9268
+ }
9269
+ ),
9270
+ disabled: isDisabled,
9271
+ asChild: true,
9272
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
9273
+ }
9274
+ ),
9275
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
9276
+ /* @__PURE__ */ jsxs(
9277
+ DropdownMenu.Item,
9278
+ {
9279
+ className: "gap-x-2",
9280
+ onClick: () => insertRow(index, "above"),
9281
+ children: [
9282
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
9283
+ "Insert row above"
9284
+ ]
9285
+ }
9286
+ ),
9287
+ /* @__PURE__ */ jsxs(
9288
+ DropdownMenu.Item,
9289
+ {
9290
+ className: "gap-x-2",
9291
+ onClick: () => insertRow(index, "below"),
9292
+ children: [
9293
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
9294
+ "Insert row below"
9295
+ ]
9296
+ }
9297
+ ),
9298
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
9299
+ /* @__PURE__ */ jsxs(
9300
+ DropdownMenu.Item,
9301
+ {
9302
+ className: "gap-x-2",
9303
+ onClick: () => deleteRow(index),
9304
+ children: [
9305
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
9306
+ "Delete row"
9307
+ ]
9308
+ }
9309
+ )
9310
+ ] })
9311
+ ] })
9312
+ ] })
9313
+ },
9314
+ field.id
9315
+ );
9316
+ })
9317
+ ] }),
9318
+ 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." })
9319
+ ] }),
9320
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9321
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
9322
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9323
+ ] }) })
9418
9324
  ]
9419
9325
  }
9420
- );
9326
+ ) });
9421
9327
  };
9422
- const schema = z.object({
9423
- customer_id: z.string().min(1)
9328
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
9329
+ return /* @__PURE__ */ jsx(
9330
+ "input",
9331
+ {
9332
+ ref,
9333
+ ...props,
9334
+ autoComplete: "off",
9335
+ className: clx(
9336
+ "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",
9337
+ className
9338
+ )
9339
+ }
9340
+ );
9424
9341
  });
9342
+ GridInput.displayName = "MetadataForm.GridInput";
9343
+ const PlaceholderInner = () => {
9344
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
9345
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
9346
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9347
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
9348
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
9349
+ ] }) })
9350
+ ] });
9351
+ };
9352
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
9353
+ function getDefaultValues(metadata) {
9354
+ if (!metadata || !Object.keys(metadata).length) {
9355
+ return [
9356
+ {
9357
+ key: "",
9358
+ value: "",
9359
+ disabled: false
9360
+ }
9361
+ ];
9362
+ }
9363
+ return Object.entries(metadata).map(([key, value]) => {
9364
+ if (!EDITABLE_TYPES.includes(typeof value)) {
9365
+ return {
9366
+ key,
9367
+ value,
9368
+ disabled: true
9369
+ };
9370
+ }
9371
+ let stringValue = value;
9372
+ if (typeof value !== "string") {
9373
+ stringValue = JSON.stringify(value);
9374
+ }
9375
+ return {
9376
+ key,
9377
+ value: stringValue,
9378
+ original_key: key
9379
+ };
9380
+ });
9381
+ }
9382
+ function parseValues(values) {
9383
+ const metadata = values.metadata;
9384
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
9385
+ if (isEmpty) {
9386
+ return null;
9387
+ }
9388
+ const update = {};
9389
+ metadata.forEach((field) => {
9390
+ let key = field.key;
9391
+ let value = field.value;
9392
+ const disabled = field.disabled;
9393
+ if (!key || !value) {
9394
+ return;
9395
+ }
9396
+ if (disabled) {
9397
+ update[key] = value;
9398
+ return;
9399
+ }
9400
+ key = key.trim();
9401
+ value = value.trim();
9402
+ if (value === "true") {
9403
+ update[key] = true;
9404
+ } else if (value === "false") {
9405
+ update[key] = false;
9406
+ } else {
9407
+ const parsedNumber = parseFloat(value);
9408
+ if (!isNaN(parsedNumber)) {
9409
+ update[key] = parsedNumber;
9410
+ } else {
9411
+ update[key] = value;
9412
+ }
9413
+ }
9414
+ });
9415
+ return update;
9416
+ }
9417
+ function getHasUneditableRows(metadata) {
9418
+ if (!metadata) {
9419
+ return false;
9420
+ }
9421
+ return Object.values(metadata).some(
9422
+ (value) => !EDITABLE_TYPES.includes(typeof value)
9423
+ );
9424
+ }
9425
9425
  const widgetModule = { widgets: [] };
9426
9426
  const routeModule = {
9427
9427
  routes: [
@@ -9458,14 +9458,6 @@ const routeModule = {
9458
9458
  Component: CustomItems,
9459
9459
  path: "/draft-orders/:id/custom-items"
9460
9460
  },
9461
- {
9462
- Component: Metadata,
9463
- path: "/draft-orders/:id/metadata"
9464
- },
9465
- {
9466
- Component: Promotions,
9467
- path: "/draft-orders/:id/promotions"
9468
- },
9469
9461
  {
9470
9462
  Component: SalesChannel,
9471
9463
  path: "/draft-orders/:id/sales-channel"
@@ -9481,6 +9473,14 @@ const routeModule = {
9481
9473
  {
9482
9474
  Component: TransferOwnership,
9483
9475
  path: "/draft-orders/:id/transfer-ownership"
9476
+ },
9477
+ {
9478
+ Component: Promotions,
9479
+ path: "/draft-orders/:id/promotions"
9480
+ },
9481
+ {
9482
+ Component: Metadata,
9483
+ path: "/draft-orders/:id/metadata"
9484
9484
  }
9485
9485
  ]
9486
9486
  }
@@ -9505,6 +9505,7 @@ const displayModule = {
9505
9505
  const i18nModule = { resources: {} };
9506
9506
  const cellRendererModule = {};
9507
9507
  const layoutModule = { layouts: [] };
9508
+ const searchEntityModule = {};
9508
9509
  const plugin = {
9509
9510
  widgetModule,
9510
9511
  routeModule,
@@ -9513,7 +9514,8 @@ const plugin = {
9513
9514
  displayModule,
9514
9515
  i18nModule,
9515
9516
  cellRendererModule,
9516
- layoutModule
9517
+ layoutModule,
9518
+ searchEntityModule
9517
9519
  };
9518
9520
  export {
9519
9521
  plugin as default