@medusajs/draft-order 0.0.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.
Files changed (82) hide show
  1. package/.medusa/server/src/admin/components/common/action-menu.js +77 -0
  2. package/.medusa/server/src/admin/components/common/conditional-tooltip.js +15 -0
  3. package/.medusa/server/src/admin/components/common/data-table.js +249 -0
  4. package/.medusa/server/src/admin/components/common/form.js +151 -0
  5. package/.medusa/server/src/admin/components/common/inline-tip.js +42 -0
  6. package/.medusa/server/src/admin/components/common/keybound-form.js +32 -0
  7. package/.medusa/server/src/admin/components/common/page-skeleton.js +51 -0
  8. package/.medusa/server/src/admin/components/common/thumbnail.js +15 -0
  9. package/.medusa/server/src/admin/components/draft-orders/activity-section.js +205 -0
  10. package/.medusa/server/src/admin/components/draft-orders/customer-section.js +165 -0
  11. package/.medusa/server/src/admin/components/draft-orders/general-section.js +36 -0
  12. package/.medusa/server/src/admin/components/draft-orders/json-view-section.js +140 -0
  13. package/.medusa/server/src/admin/components/draft-orders/metadata-section.js +28 -0
  14. package/.medusa/server/src/admin/components/draft-orders/shipping-section.js +211 -0
  15. package/.medusa/server/src/admin/components/draft-orders/summary-section.js +148 -0
  16. package/.medusa/server/src/admin/components/inputs/combobox.js +311 -0
  17. package/.medusa/server/src/admin/components/inputs/country-select.js +59 -0
  18. package/.medusa/server/src/admin/components/inputs/number-input.js +100 -0
  19. package/.medusa/server/src/admin/components/inputs/switch-block.js +30 -0
  20. package/.medusa/server/src/admin/components/modals/index.js +19 -0
  21. package/.medusa/server/src/admin/components/modals/route-drawer/index.js +4 -0
  22. package/.medusa/server/src/admin/components/modals/route-drawer/route-drawer.js +57 -0
  23. package/.medusa/server/src/admin/components/modals/route-focus-modal/index.js +4 -0
  24. package/.medusa/server/src/admin/components/modals/route-focus-modal/route-focus-modal.js +71 -0
  25. package/.medusa/server/src/admin/components/modals/route-modal-form/index.js +4 -0
  26. package/.medusa/server/src/admin/components/modals/route-modal-form/route-modal-form.js +60 -0
  27. package/.medusa/server/src/admin/components/modals/route-modal-provider/index.js +6 -0
  28. package/.medusa/server/src/admin/components/modals/route-modal-provider/route-modal-context.js +5 -0
  29. package/.medusa/server/src/admin/components/modals/route-modal-provider/route-provider.js +30 -0
  30. package/.medusa/server/src/admin/components/modals/route-modal-provider/use-route-modal.js +12 -0
  31. package/.medusa/server/src/admin/components/modals/stacked-drawer/index.js +5 -0
  32. package/.medusa/server/src/admin/components/modals/stacked-drawer/stacked-drawer.js +55 -0
  33. package/.medusa/server/src/admin/components/modals/stacked-focus-modal/index.js +5 -0
  34. package/.medusa/server/src/admin/components/modals/stacked-focus-modal/stacked-focus-modal.js +63 -0
  35. package/.medusa/server/src/admin/components/modals/stacked-modal-provider/index.js +6 -0
  36. package/.medusa/server/src/admin/components/modals/stacked-modal-provider/stacked-modal-context.js +5 -0
  37. package/.medusa/server/src/admin/components/modals/stacked-modal-provider/stacked-modal-provider.js +47 -0
  38. package/.medusa/server/src/admin/components/modals/stacked-modal-provider/use-stacked-modal.js +14 -0
  39. package/.medusa/server/src/admin/components/utilities/generic-forward-ref.js +7 -0
  40. package/.medusa/server/src/admin/hooks/api/customers.js +53 -0
  41. package/.medusa/server/src/admin/hooks/api/draft-orders.js +161 -0
  42. package/.medusa/server/src/admin/hooks/api/orders.js +274 -0
  43. package/.medusa/server/src/admin/hooks/api/product-variants.js +21 -0
  44. package/.medusa/server/src/admin/hooks/api/regions.js +35 -0
  45. package/.medusa/server/src/admin/hooks/api/sales-channels.js +35 -0
  46. package/.medusa/server/src/admin/hooks/api/shipping-options.js +35 -0
  47. package/.medusa/server/src/admin/hooks/api/users.js +26 -0
  48. package/.medusa/server/src/admin/hooks/common/use-combobox-data.js +61 -0
  49. package/.medusa/server/src/admin/hooks/common/use-data-table-date-filters.js +89 -0
  50. package/.medusa/server/src/admin/hooks/common/use-debounced-search.js +22 -0
  51. package/.medusa/server/src/admin/hooks/common/use-query-params.js +14 -0
  52. package/.medusa/server/src/admin/hooks/order-edits/use-cancel-order-edit.js +25 -0
  53. package/.medusa/server/src/admin/hooks/order-edits/use-initiate-order-edit.js +39 -0
  54. package/.medusa/server/src/admin/lib/data/countries.js +1762 -0
  55. package/.medusa/server/src/admin/lib/data/currencies.js +36 -0
  56. package/.medusa/server/src/admin/lib/queries/draft-order-details.js +1 -0
  57. package/.medusa/server/src/admin/lib/queries/sdk.js +10 -0
  58. package/.medusa/server/src/admin/lib/schemas/address.js +16 -0
  59. package/.medusa/server/src/admin/lib/utils/address-utils.js +57 -0
  60. package/.medusa/server/src/admin/lib/utils/date-utils.js +27 -0
  61. package/.medusa/server/src/admin/lib/utils/order-utils.js +13 -0
  62. package/.medusa/server/src/admin/routes/draft-orders/@create/page.js +659 -0
  63. package/.medusa/server/src/admin/routes/draft-orders/_id_/@billing-address/page.js +228 -0
  64. package/.medusa/server/src/admin/routes/draft-orders/_id_/@custom-items/page.js +38 -0
  65. package/.medusa/server/src/admin/routes/draft-orders/_id_/@email/page.js +89 -0
  66. package/.medusa/server/src/admin/routes/draft-orders/_id_/@items/page.js +576 -0
  67. package/.medusa/server/src/admin/routes/draft-orders/_id_/@metadata/page.js +338 -0
  68. package/.medusa/server/src/admin/routes/draft-orders/_id_/@promotions/page.js +70 -0
  69. package/.medusa/server/src/admin/routes/draft-orders/_id_/@sales-channel/page.js +113 -0
  70. package/.medusa/server/src/admin/routes/draft-orders/_id_/@shipping/page.js +465 -0
  71. package/.medusa/server/src/admin/routes/draft-orders/_id_/@shipping-address/page.js +241 -0
  72. package/.medusa/server/src/admin/routes/draft-orders/_id_/page.js +70 -0
  73. package/.medusa/server/src/admin/routes/draft-orders/page.js +148 -0
  74. package/.medusa/server/src/api/admin/draft-orders/[id]/convert/route.js +18 -0
  75. package/.medusa/server/src/types/http/draft-orders/payloads.js +3 -0
  76. package/.medusa/server/src/types/http/draft-orders/responses.js +3 -0
  77. package/.medusa/server/src/types/http/orders/entity.js +3 -0
  78. package/.medusa/server/src/types/http/orders/requests.js +3 -0
  79. package/.medusa/server/src/workflows/draft-orders/convert-draft-order-workflow.js +54 -0
  80. package/.medusa/server/tailwind.config.js +12 -0
  81. package/README.md +64 -0
  82. package/package.json +104 -0
@@ -0,0 +1,338 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { zodResolver } from "/Users/oliverjuhl/Desktop/medusa/draft-order-plugin/node_modules/@hookform/resolvers/zod/dist/zod.mjs";
3
+ import { EllipsisVertical, ArrowUpMini, ArrowDownMini, Trash } from "@medusajs/icons";
4
+ import { clx, Heading, toast, DropdownMenu, IconButton, Button, Skeleton } from "@medusajs/ui";
5
+ import { forwardRef } from "react";
6
+ import { useForm, useFieldArray } from "react-hook-form";
7
+ import { z } from "/Users/oliverjuhl/Desktop/medusa/draft-order-plugin/node_modules/zod/lib/index.mjs";
8
+ import { useParams } from "react-router-dom";
9
+ import { ConditionalTooltip } from "../../../../components/common/conditional-tooltip.js";
10
+ import { Form } from "../../../../components/common/form.js";
11
+ import { InlineTip } from "../../../../components/common/inline-tip.js";
12
+ import { KeyboundForm } from "../../../../components/common/keybound-form.js";
13
+ import { RouteDrawer } from "../../../../components/modals/route-drawer/route-drawer.js";
14
+ import "../../../../components/modals/route-focus-modal/route-focus-modal.js";
15
+ import "../../../../components/modals/route-modal-provider/route-modal-context.js";
16
+ import { useRouteModal } from "../../../../components/modals/route-modal-provider/use-route-modal.js";
17
+ import "../../../../components/modals/stacked-drawer/stacked-drawer.js";
18
+ import "../../../../components/modals/stacked-focus-modal/stacked-focus-modal.js";
19
+ import "../../../../components/modals/stacked-modal-provider/stacked-modal-context.js";
20
+ import { useUpdateDraftOrder } from "../../../../hooks/api/draft-orders.js";
21
+ import { useOrder } from "../../../../hooks/api/orders.js";
22
+ const MetadataFieldSchema = z.object({
23
+ key: z.string(),
24
+ disabled: z.boolean().optional(),
25
+ value: z.any()
26
+ });
27
+ const MetadataSchema = z.object({
28
+ metadata: z.array(MetadataFieldSchema)
29
+ });
30
+ const Metadata = () => {
31
+ const { id } = useParams();
32
+ const { order, isPending, isError, error } = useOrder(id, {
33
+ fields: "metadata"
34
+ });
35
+ if (isError) {
36
+ throw error;
37
+ }
38
+ const isReady = !isPending && !!order;
39
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
40
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
41
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
42
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
43
+ ] }),
44
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
45
+ ] });
46
+ };
47
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
48
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
49
+ const MetadataForm = ({ orderId, metadata }) => {
50
+ const { handleSuccess } = useRouteModal();
51
+ const hasUneditableRows = getHasUneditableRows(metadata);
52
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
53
+ const form = useForm({
54
+ defaultValues: {
55
+ metadata: getDefaultValues(metadata)
56
+ },
57
+ resolver: zodResolver(MetadataSchema)
58
+ });
59
+ const handleSubmit = form.handleSubmit(async (data) => {
60
+ const parsedData = parseValues(data);
61
+ await mutateAsync(
62
+ {
63
+ metadata: parsedData
64
+ },
65
+ {
66
+ onSuccess: () => {
67
+ toast.success("Metadata updated");
68
+ handleSuccess();
69
+ },
70
+ onError: (error) => {
71
+ toast.error(error.message);
72
+ }
73
+ }
74
+ );
75
+ });
76
+ const { fields, insert, remove } = useFieldArray({
77
+ control: form.control,
78
+ name: "metadata"
79
+ });
80
+ function deleteRow(index) {
81
+ remove(index);
82
+ if (fields.length === 1) {
83
+ insert(0, {
84
+ key: "",
85
+ value: "",
86
+ disabled: false
87
+ });
88
+ }
89
+ }
90
+ function insertRow(index, position) {
91
+ insert(index + (position === "above" ? 0 : 1), {
92
+ key: "",
93
+ value: "",
94
+ disabled: false
95
+ });
96
+ }
97
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
98
+ KeyboundForm,
99
+ {
100
+ onSubmit: handleSubmit,
101
+ className: "flex flex-1 flex-col overflow-hidden",
102
+ children: [
103
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
104
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
105
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
106
+ /* @__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" }) }),
107
+ /* @__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" }) })
108
+ ] }),
109
+ fields.map((field, index) => {
110
+ const isDisabled = field.disabled || false;
111
+ let placeholder = "-";
112
+ if (typeof field.value === "object") {
113
+ placeholder = "{ ... }";
114
+ }
115
+ if (Array.isArray(field.value)) {
116
+ placeholder = "[ ... ]";
117
+ }
118
+ return /* @__PURE__ */ jsx(
119
+ ConditionalTooltip,
120
+ {
121
+ showTooltip: isDisabled,
122
+ content: "This row is disabled because it contains non-primitive data.",
123
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
124
+ /* @__PURE__ */ jsxs(
125
+ "div",
126
+ {
127
+ className: clx("grid grid-cols-2 divide-x", {
128
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
129
+ }),
130
+ children: [
131
+ /* @__PURE__ */ jsx(
132
+ Form.Field,
133
+ {
134
+ control: form.control,
135
+ name: `metadata.${index}.key`,
136
+ render: ({ field: field2 }) => {
137
+ return /* @__PURE__ */ jsx(Form.Item, { children: /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
138
+ GridInput,
139
+ {
140
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
141
+ ...field2,
142
+ disabled: isDisabled,
143
+ placeholder: "Key"
144
+ }
145
+ ) }) });
146
+ }
147
+ }
148
+ ),
149
+ /* @__PURE__ */ jsx(
150
+ Form.Field,
151
+ {
152
+ control: form.control,
153
+ name: `metadata.${index}.value`,
154
+ render: ({ field: { value, ...field2 } }) => {
155
+ return /* @__PURE__ */ jsx(Form.Item, { children: /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
156
+ GridInput,
157
+ {
158
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
159
+ ...field2,
160
+ value: isDisabled ? placeholder : value,
161
+ disabled: isDisabled,
162
+ placeholder: "Value"
163
+ }
164
+ ) }) });
165
+ }
166
+ }
167
+ )
168
+ ]
169
+ }
170
+ ),
171
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
172
+ /* @__PURE__ */ jsx(
173
+ DropdownMenu.Trigger,
174
+ {
175
+ className: clx(
176
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
177
+ {
178
+ hidden: isDisabled
179
+ }
180
+ ),
181
+ disabled: isDisabled,
182
+ asChild: true,
183
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
184
+ }
185
+ ),
186
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
187
+ /* @__PURE__ */ jsxs(
188
+ DropdownMenu.Item,
189
+ {
190
+ className: "gap-x-2",
191
+ onClick: () => insertRow(index, "above"),
192
+ children: [
193
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
194
+ "Insert row above"
195
+ ]
196
+ }
197
+ ),
198
+ /* @__PURE__ */ jsxs(
199
+ DropdownMenu.Item,
200
+ {
201
+ className: "gap-x-2",
202
+ onClick: () => insertRow(index, "below"),
203
+ children: [
204
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
205
+ "Insert row below"
206
+ ]
207
+ }
208
+ ),
209
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
210
+ /* @__PURE__ */ jsxs(
211
+ DropdownMenu.Item,
212
+ {
213
+ className: "gap-x-2",
214
+ onClick: () => deleteRow(index),
215
+ children: [
216
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
217
+ "Delete row"
218
+ ]
219
+ }
220
+ )
221
+ ] })
222
+ ] })
223
+ ] })
224
+ },
225
+ field.id
226
+ );
227
+ })
228
+ ] }),
229
+ 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." })
230
+ ] }),
231
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
232
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
233
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
234
+ ] }) })
235
+ ]
236
+ }
237
+ ) });
238
+ };
239
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
240
+ return /* @__PURE__ */ jsx(
241
+ "input",
242
+ {
243
+ ref,
244
+ ...props,
245
+ autoComplete: "off",
246
+ className: clx(
247
+ "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",
248
+ className
249
+ )
250
+ }
251
+ );
252
+ });
253
+ GridInput.displayName = "MetadataForm.GridInput";
254
+ const PlaceholderInner = () => {
255
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
256
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
257
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
258
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
259
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
260
+ ] }) })
261
+ ] });
262
+ };
263
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
264
+ function getDefaultValues(metadata) {
265
+ if (!metadata || !Object.keys(metadata).length) {
266
+ return [
267
+ {
268
+ key: "",
269
+ value: "",
270
+ disabled: false
271
+ }
272
+ ];
273
+ }
274
+ return Object.entries(metadata).map(([key, value]) => {
275
+ if (!EDITABLE_TYPES.includes(typeof value)) {
276
+ return {
277
+ key,
278
+ value,
279
+ disabled: true
280
+ };
281
+ }
282
+ let stringValue = value;
283
+ if (typeof value !== "string") {
284
+ stringValue = JSON.stringify(value);
285
+ }
286
+ return {
287
+ key,
288
+ value: stringValue,
289
+ original_key: key
290
+ };
291
+ });
292
+ }
293
+ function parseValues(values) {
294
+ const metadata = values.metadata;
295
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
296
+ if (isEmpty) {
297
+ return null;
298
+ }
299
+ const update = {};
300
+ metadata.forEach((field) => {
301
+ let key = field.key;
302
+ let value = field.value;
303
+ const disabled = field.disabled;
304
+ if (!key || !value) {
305
+ return;
306
+ }
307
+ if (disabled) {
308
+ update[key] = value;
309
+ return;
310
+ }
311
+ key = key.trim();
312
+ value = value.trim();
313
+ if (value === "true") {
314
+ update[key] = true;
315
+ } else if (value === "false") {
316
+ update[key] = false;
317
+ } else {
318
+ const parsedNumber = parseFloat(value);
319
+ if (!isNaN(parsedNumber)) {
320
+ update[key] = parsedNumber;
321
+ } else {
322
+ update[key] = value;
323
+ }
324
+ }
325
+ });
326
+ return update;
327
+ }
328
+ function getHasUneditableRows(metadata) {
329
+ if (!metadata) {
330
+ return false;
331
+ }
332
+ return Object.values(metadata).some(
333
+ (value) => !EDITABLE_TYPES.includes(typeof value)
334
+ );
335
+ }
336
+ export {
337
+ Metadata as default
338
+ };
@@ -0,0 +1,70 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { zodResolver } from "/Users/oliverjuhl/Desktop/medusa/draft-order-plugin/node_modules/@hookform/resolvers/zod/dist/zod.mjs";
3
+ import { Heading, toast, Input, Button } from "@medusajs/ui";
4
+ import { useForm } from "react-hook-form";
5
+ import { useParams } from "react-router-dom";
6
+ import { z } from "/Users/oliverjuhl/Desktop/medusa/draft-order-plugin/node_modules/zod/lib/index.mjs";
7
+ import { Form } from "../../../../components/common/form.js";
8
+ import { KeyboundForm } from "../../../../components/common/keybound-form.js";
9
+ import { RouteDrawer } from "../../../../components/modals/route-drawer/route-drawer.js";
10
+ import "../../../../components/modals/route-focus-modal/route-focus-modal.js";
11
+ import "react";
12
+ import "../../../../components/modals/route-modal-provider/route-modal-context.js";
13
+ import { useRouteModal } from "../../../../components/modals/route-modal-provider/use-route-modal.js";
14
+ import "../../../../components/modals/stacked-drawer/stacked-drawer.js";
15
+ import "../../../../components/modals/stacked-focus-modal/stacked-focus-modal.js";
16
+ import "../../../../components/modals/stacked-modal-provider/stacked-modal-context.js";
17
+ import { useUpdateDraftOrderPromotions } from "../../../../hooks/api/draft-orders.js";
18
+ const Promotions = () => {
19
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
20
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
21
+ /* @__PURE__ */ jsx(PromotionForm, {})
22
+ ] });
23
+ };
24
+ const PromotionForm = () => {
25
+ const { id } = useParams();
26
+ const { handleSuccess } = useRouteModal();
27
+ const form = useForm({
28
+ resolver: zodResolver(schema)
29
+ });
30
+ const { mutateAsync, isPending } = useUpdateDraftOrderPromotions(id);
31
+ const onSubmit = form.handleSubmit(async (data) => {
32
+ await mutateAsync(
33
+ {
34
+ promo_codes: [data.promo_codes]
35
+ },
36
+ {
37
+ onSuccess: () => {
38
+ handleSuccess();
39
+ },
40
+ onError: (error) => {
41
+ toast.error(error.message);
42
+ }
43
+ }
44
+ );
45
+ });
46
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
47
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(
48
+ Form.Field,
49
+ {
50
+ control: form.control,
51
+ name: "promo_codes",
52
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form.Item, { children: [
53
+ /* @__PURE__ */ jsx(Form.Label, { children: "Promo Codes" }),
54
+ /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
55
+ /* @__PURE__ */ jsx(Form.ErrorMessage, {})
56
+ ] })
57
+ }
58
+ ) }),
59
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
60
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
61
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
62
+ ] }) })
63
+ ] }) });
64
+ };
65
+ const schema = z.object({
66
+ promo_codes: z.string().optional()
67
+ });
68
+ export {
69
+ Promotions as default
70
+ };
@@ -0,0 +1,113 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { zodResolver } from "/Users/oliverjuhl/Desktop/medusa/draft-order-plugin/node_modules/@hookform/resolvers/zod/dist/zod.mjs";
3
+ import { Heading, Button } from "@medusajs/ui";
4
+ import { useForm } from "react-hook-form";
5
+ import { useParams } from "react-router-dom";
6
+ import { z } from "/Users/oliverjuhl/Desktop/medusa/draft-order-plugin/node_modules/zod/lib/index.mjs";
7
+ import { Form } from "../../../../components/common/form.js";
8
+ import { KeyboundForm } from "../../../../components/common/keybound-form.js";
9
+ import { Combobox } from "../../../../components/inputs/combobox.js";
10
+ import { SwitchBlock } from "../../../../components/inputs/switch-block.js";
11
+ import { RouteDrawer } from "../../../../components/modals/route-drawer/route-drawer.js";
12
+ import "../../../../components/modals/route-focus-modal/route-focus-modal.js";
13
+ import "react";
14
+ import "../../../../components/modals/route-modal-provider/route-modal-context.js";
15
+ import "../../../../components/modals/stacked-drawer/stacked-drawer.js";
16
+ import "../../../../components/modals/stacked-focus-modal/stacked-focus-modal.js";
17
+ import "../../../../components/modals/stacked-modal-provider/stacked-modal-context.js";
18
+ import { useDraftOrder } from "../../../../hooks/api/draft-orders.js";
19
+ import { useComboboxData } from "../../../../hooks/common/use-combobox-data.js";
20
+ import { sdk } from "../../../../lib/queries/sdk.js";
21
+ const SalesChannel = () => {
22
+ const { id } = useParams();
23
+ const { draft_order, isPending, isError, error } = useDraftOrder(
24
+ id,
25
+ {
26
+ fields: "+sales_channel_id"
27
+ },
28
+ {
29
+ enabled: !!id
30
+ }
31
+ );
32
+ if (isError) {
33
+ throw error;
34
+ }
35
+ const ready = draft_order && !isPending;
36
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
37
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
38
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
39
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
40
+ ] }),
41
+ ready && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
42
+ ] });
43
+ };
44
+ const SalesChannelForm = ({ order }) => {
45
+ const form = useForm({
46
+ defaultValues: {
47
+ sales_channel_id: order.sales_channel_id || void 0,
48
+ notify: false
49
+ },
50
+ resolver: zodResolver(schema)
51
+ });
52
+ const salesChannels = useComboboxData({
53
+ queryFn: async (params) => {
54
+ return await sdk.admin.salesChannel.list(params);
55
+ },
56
+ queryKey: ["sales-channels"],
57
+ getOptions: (data) => {
58
+ return data.sales_channels.map((salesChannel) => ({
59
+ label: salesChannel.name,
60
+ value: salesChannel.id
61
+ }));
62
+ },
63
+ defaultValue: order.sales_channel_id || void 0
64
+ });
65
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
66
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-6", children: [
67
+ /* @__PURE__ */ jsx(
68
+ Form.Field,
69
+ {
70
+ control: form.control,
71
+ name: "sales_channel_id",
72
+ render: ({ field }) => {
73
+ return /* @__PURE__ */ jsxs(Form.Item, { children: [
74
+ /* @__PURE__ */ jsx(Form.Label, { children: "Sales Channel" }),
75
+ /* @__PURE__ */ jsx(Form.Control, { children: /* @__PURE__ */ jsx(
76
+ Combobox,
77
+ {
78
+ options: salesChannels.options,
79
+ fetchNextPage: salesChannels.fetchNextPage,
80
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
81
+ searchValue: salesChannels.searchValue,
82
+ onSearchValueChange: salesChannels.onSearchValueChange,
83
+ placeholder: "Select sales channel",
84
+ ...field
85
+ }
86
+ ) })
87
+ ] });
88
+ }
89
+ }
90
+ ),
91
+ /* @__PURE__ */ jsx(
92
+ SwitchBlock,
93
+ {
94
+ label: "Send notification",
95
+ description: "Notify the customer that the sales channel has been updated.",
96
+ control: form.control,
97
+ name: "notify"
98
+ }
99
+ )
100
+ ] }),
101
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
102
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
103
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
104
+ ] }) })
105
+ ] }) });
106
+ };
107
+ const schema = z.object({
108
+ sales_channel_id: z.string().min(1),
109
+ notify: z.boolean().optional()
110
+ });
111
+ export {
112
+ SalesChannel as default
113
+ };