@medusajs/draft-order 2.11.4-preview-20251109000311 → 2.11.4-preview-20251109060124
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.
- package/.medusa/server/src/admin/index.js +365 -365
- package/.medusa/server/src/admin/index.mjs +365 -365
- package/package.json +16 -16
|
@@ -10814,283 +10814,6 @@ const customItemSchema = objectType({
|
|
|
10814
10814
|
quantity: numberType(),
|
|
10815
10815
|
unit_price: unionType([numberType(), stringType()])
|
|
10816
10816
|
});
|
|
10817
|
-
const PROMOTION_QUERY_KEY = "promotions";
|
|
10818
|
-
const promotionsQueryKeys = {
|
|
10819
|
-
list: (query2) => [
|
|
10820
|
-
PROMOTION_QUERY_KEY,
|
|
10821
|
-
query2 ? query2 : void 0
|
|
10822
|
-
],
|
|
10823
|
-
detail: (id, query2) => [
|
|
10824
|
-
PROMOTION_QUERY_KEY,
|
|
10825
|
-
id,
|
|
10826
|
-
query2 ? query2 : void 0
|
|
10827
|
-
]
|
|
10828
|
-
};
|
|
10829
|
-
const usePromotions = (query2, options) => {
|
|
10830
|
-
const { data, ...rest } = useQuery({
|
|
10831
|
-
queryKey: promotionsQueryKeys.list(query2),
|
|
10832
|
-
queryFn: async () => sdk.admin.promotion.list(query2),
|
|
10833
|
-
...options
|
|
10834
|
-
});
|
|
10835
|
-
return { ...data, ...rest };
|
|
10836
|
-
};
|
|
10837
|
-
const Promotions = () => {
|
|
10838
|
-
const { id } = useParams();
|
|
10839
|
-
const {
|
|
10840
|
-
order: preview,
|
|
10841
|
-
isError: isPreviewError,
|
|
10842
|
-
error: previewError
|
|
10843
|
-
} = useOrderPreview(id, void 0);
|
|
10844
|
-
useInitiateOrderEdit({ preview });
|
|
10845
|
-
const { onCancel } = useCancelOrderEdit({ preview });
|
|
10846
|
-
if (isPreviewError) {
|
|
10847
|
-
throw previewError;
|
|
10848
|
-
}
|
|
10849
|
-
const isReady = !!preview;
|
|
10850
|
-
return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
|
|
10851
|
-
/* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
|
|
10852
|
-
isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
|
|
10853
|
-
] });
|
|
10854
|
-
};
|
|
10855
|
-
const PromotionForm = ({ preview }) => {
|
|
10856
|
-
const { items, shipping_methods } = preview;
|
|
10857
|
-
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
10858
|
-
const [comboboxValue, setComboboxValue] = useState("");
|
|
10859
|
-
const { handleSuccess } = useRouteModal();
|
|
10860
|
-
const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
|
|
10861
|
-
const promoIds = getPromotionIds(items, shipping_methods);
|
|
10862
|
-
const { promotions, isPending, isError, error } = usePromotions(
|
|
10863
|
-
{
|
|
10864
|
-
id: promoIds
|
|
10865
|
-
},
|
|
10866
|
-
{
|
|
10867
|
-
enabled: !!promoIds.length
|
|
10868
|
-
}
|
|
10869
|
-
);
|
|
10870
|
-
const comboboxData = useComboboxData({
|
|
10871
|
-
queryKey: ["promotions", "combobox", promoIds],
|
|
10872
|
-
queryFn: async (params) => {
|
|
10873
|
-
return await sdk.admin.promotion.list({
|
|
10874
|
-
...params,
|
|
10875
|
-
id: {
|
|
10876
|
-
$nin: promoIds
|
|
10877
|
-
}
|
|
10878
|
-
});
|
|
10879
|
-
},
|
|
10880
|
-
getOptions: (data) => {
|
|
10881
|
-
return data.promotions.map((promotion) => ({
|
|
10882
|
-
label: promotion.code,
|
|
10883
|
-
value: promotion.code
|
|
10884
|
-
}));
|
|
10885
|
-
}
|
|
10886
|
-
});
|
|
10887
|
-
const add = async (value) => {
|
|
10888
|
-
if (!value) {
|
|
10889
|
-
return;
|
|
10890
|
-
}
|
|
10891
|
-
addPromotions(
|
|
10892
|
-
{
|
|
10893
|
-
promo_codes: [value]
|
|
10894
|
-
},
|
|
10895
|
-
{
|
|
10896
|
-
onError: (e) => {
|
|
10897
|
-
toast.error(e.message);
|
|
10898
|
-
comboboxData.onSearchValueChange("");
|
|
10899
|
-
setComboboxValue("");
|
|
10900
|
-
},
|
|
10901
|
-
onSuccess: () => {
|
|
10902
|
-
comboboxData.onSearchValueChange("");
|
|
10903
|
-
setComboboxValue("");
|
|
10904
|
-
}
|
|
10905
|
-
}
|
|
10906
|
-
);
|
|
10907
|
-
};
|
|
10908
|
-
const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
|
|
10909
|
-
const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
|
|
10910
|
-
const onSubmit = async () => {
|
|
10911
|
-
setIsSubmitting(true);
|
|
10912
|
-
let requestSucceeded = false;
|
|
10913
|
-
await requestOrderEdit(void 0, {
|
|
10914
|
-
onError: (e) => {
|
|
10915
|
-
toast.error(e.message);
|
|
10916
|
-
},
|
|
10917
|
-
onSuccess: () => {
|
|
10918
|
-
requestSucceeded = true;
|
|
10919
|
-
}
|
|
10920
|
-
});
|
|
10921
|
-
if (!requestSucceeded) {
|
|
10922
|
-
setIsSubmitting(false);
|
|
10923
|
-
return;
|
|
10924
|
-
}
|
|
10925
|
-
await confirmOrderEdit(void 0, {
|
|
10926
|
-
onError: (e) => {
|
|
10927
|
-
toast.error(e.message);
|
|
10928
|
-
},
|
|
10929
|
-
onSuccess: () => {
|
|
10930
|
-
handleSuccess();
|
|
10931
|
-
},
|
|
10932
|
-
onSettled: () => {
|
|
10933
|
-
setIsSubmitting(false);
|
|
10934
|
-
}
|
|
10935
|
-
});
|
|
10936
|
-
};
|
|
10937
|
-
if (isError) {
|
|
10938
|
-
throw error;
|
|
10939
|
-
}
|
|
10940
|
-
return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
|
|
10941
|
-
/* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
|
|
10942
|
-
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
|
|
10943
|
-
/* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
|
|
10944
|
-
/* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
|
|
10945
|
-
/* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
|
|
10946
|
-
] }),
|
|
10947
|
-
/* @__PURE__ */ jsx(
|
|
10948
|
-
Combobox,
|
|
10949
|
-
{
|
|
10950
|
-
id: "promotion-combobox",
|
|
10951
|
-
"aria-describedby": "promotion-combobox-hint",
|
|
10952
|
-
isFetchingNextPage: comboboxData.isFetchingNextPage,
|
|
10953
|
-
fetchNextPage: comboboxData.fetchNextPage,
|
|
10954
|
-
options: comboboxData.options,
|
|
10955
|
-
onSearchValueChange: comboboxData.onSearchValueChange,
|
|
10956
|
-
searchValue: comboboxData.searchValue,
|
|
10957
|
-
disabled: comboboxData.disabled || isAddingPromotions,
|
|
10958
|
-
onChange: add,
|
|
10959
|
-
value: comboboxValue
|
|
10960
|
-
}
|
|
10961
|
-
)
|
|
10962
|
-
] }),
|
|
10963
|
-
/* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
|
|
10964
|
-
/* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
|
|
10965
|
-
PromotionItem,
|
|
10966
|
-
{
|
|
10967
|
-
promotion,
|
|
10968
|
-
orderId: preview.id,
|
|
10969
|
-
isLoading: isPending
|
|
10970
|
-
},
|
|
10971
|
-
promotion.id
|
|
10972
|
-
)) })
|
|
10973
|
-
] }) }),
|
|
10974
|
-
/* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
|
|
10975
|
-
/* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
|
|
10976
|
-
/* @__PURE__ */ jsx(
|
|
10977
|
-
Button,
|
|
10978
|
-
{
|
|
10979
|
-
size: "small",
|
|
10980
|
-
type: "submit",
|
|
10981
|
-
isLoading: isSubmitting || isAddingPromotions,
|
|
10982
|
-
children: "Save"
|
|
10983
|
-
}
|
|
10984
|
-
)
|
|
10985
|
-
] }) })
|
|
10986
|
-
] });
|
|
10987
|
-
};
|
|
10988
|
-
const PromotionItem = ({
|
|
10989
|
-
promotion,
|
|
10990
|
-
orderId,
|
|
10991
|
-
isLoading
|
|
10992
|
-
}) => {
|
|
10993
|
-
var _a;
|
|
10994
|
-
const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
|
|
10995
|
-
const onRemove = async () => {
|
|
10996
|
-
removePromotions(
|
|
10997
|
-
{
|
|
10998
|
-
promo_codes: [promotion.code]
|
|
10999
|
-
},
|
|
11000
|
-
{
|
|
11001
|
-
onError: (e) => {
|
|
11002
|
-
toast.error(e.message);
|
|
11003
|
-
}
|
|
11004
|
-
}
|
|
11005
|
-
);
|
|
11006
|
-
};
|
|
11007
|
-
const displayValue = getDisplayValue(promotion);
|
|
11008
|
-
return /* @__PURE__ */ jsxs(
|
|
11009
|
-
"div",
|
|
11010
|
-
{
|
|
11011
|
-
className: clx(
|
|
11012
|
-
"bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
|
|
11013
|
-
{
|
|
11014
|
-
"animate-pulse": isLoading
|
|
11015
|
-
}
|
|
11016
|
-
),
|
|
11017
|
-
children: [
|
|
11018
|
-
/* @__PURE__ */ jsxs("div", { children: [
|
|
11019
|
-
/* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
|
|
11020
|
-
/* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
|
|
11021
|
-
displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
|
|
11022
|
-
/* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
|
|
11023
|
-
/* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
|
|
11024
|
-
] }),
|
|
11025
|
-
/* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
|
|
11026
|
-
] })
|
|
11027
|
-
] }),
|
|
11028
|
-
/* @__PURE__ */ jsx(
|
|
11029
|
-
IconButton,
|
|
11030
|
-
{
|
|
11031
|
-
size: "small",
|
|
11032
|
-
type: "button",
|
|
11033
|
-
variant: "transparent",
|
|
11034
|
-
onClick: onRemove,
|
|
11035
|
-
isLoading: isPending || isLoading,
|
|
11036
|
-
children: /* @__PURE__ */ jsx(XMark, {})
|
|
11037
|
-
}
|
|
11038
|
-
)
|
|
11039
|
-
]
|
|
11040
|
-
},
|
|
11041
|
-
promotion.id
|
|
11042
|
-
);
|
|
11043
|
-
};
|
|
11044
|
-
function getDisplayValue(promotion) {
|
|
11045
|
-
var _a, _b, _c, _d;
|
|
11046
|
-
const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
|
|
11047
|
-
if (!value) {
|
|
11048
|
-
return null;
|
|
11049
|
-
}
|
|
11050
|
-
if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
|
|
11051
|
-
const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
|
|
11052
|
-
if (!currency) {
|
|
11053
|
-
return null;
|
|
11054
|
-
}
|
|
11055
|
-
return getLocaleAmount(value, currency);
|
|
11056
|
-
} else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
|
|
11057
|
-
return formatPercentage(value);
|
|
11058
|
-
}
|
|
11059
|
-
return null;
|
|
11060
|
-
}
|
|
11061
|
-
const formatter = new Intl.NumberFormat([], {
|
|
11062
|
-
style: "percent",
|
|
11063
|
-
minimumFractionDigits: 2
|
|
11064
|
-
});
|
|
11065
|
-
const formatPercentage = (value, isPercentageValue = false) => {
|
|
11066
|
-
let val = value || 0;
|
|
11067
|
-
if (!isPercentageValue) {
|
|
11068
|
-
val = val / 100;
|
|
11069
|
-
}
|
|
11070
|
-
return formatter.format(val);
|
|
11071
|
-
};
|
|
11072
|
-
function getPromotionIds(items, shippingMethods) {
|
|
11073
|
-
const promotionIds = /* @__PURE__ */ new Set();
|
|
11074
|
-
for (const item of items) {
|
|
11075
|
-
if (item.adjustments) {
|
|
11076
|
-
for (const adjustment of item.adjustments) {
|
|
11077
|
-
if (adjustment.promotion_id) {
|
|
11078
|
-
promotionIds.add(adjustment.promotion_id);
|
|
11079
|
-
}
|
|
11080
|
-
}
|
|
11081
|
-
}
|
|
11082
|
-
}
|
|
11083
|
-
for (const shippingMethod of shippingMethods) {
|
|
11084
|
-
if (shippingMethod.adjustments) {
|
|
11085
|
-
for (const adjustment of shippingMethod.adjustments) {
|
|
11086
|
-
if (adjustment.promotion_id) {
|
|
11087
|
-
promotionIds.add(adjustment.promotion_id);
|
|
11088
|
-
}
|
|
11089
|
-
}
|
|
11090
|
-
}
|
|
11091
|
-
}
|
|
11092
|
-
return Array.from(promotionIds);
|
|
11093
|
-
}
|
|
11094
10817
|
const InlineTip = forwardRef(
|
|
11095
10818
|
({ variant = "tip", label, className, children, ...props }, ref) => {
|
|
11096
10819
|
const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
|
|
@@ -11342,104 +11065,381 @@ const MetadataForm = ({ orderId, metadata }) => {
|
|
|
11342
11065
|
] }) })
|
|
11343
11066
|
]
|
|
11344
11067
|
}
|
|
11345
|
-
) });
|
|
11346
|
-
};
|
|
11347
|
-
const GridInput = forwardRef(({ className, ...props }, ref) => {
|
|
11348
|
-
return /* @__PURE__ */ jsx(
|
|
11349
|
-
"input",
|
|
11350
|
-
{
|
|
11351
|
-
ref,
|
|
11352
|
-
...props,
|
|
11353
|
-
autoComplete: "off",
|
|
11354
|
-
className: clx(
|
|
11355
|
-
"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",
|
|
11356
|
-
className
|
|
11068
|
+
) });
|
|
11069
|
+
};
|
|
11070
|
+
const GridInput = forwardRef(({ className, ...props }, ref) => {
|
|
11071
|
+
return /* @__PURE__ */ jsx(
|
|
11072
|
+
"input",
|
|
11073
|
+
{
|
|
11074
|
+
ref,
|
|
11075
|
+
...props,
|
|
11076
|
+
autoComplete: "off",
|
|
11077
|
+
className: clx(
|
|
11078
|
+
"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",
|
|
11079
|
+
className
|
|
11080
|
+
)
|
|
11081
|
+
}
|
|
11082
|
+
);
|
|
11083
|
+
});
|
|
11084
|
+
GridInput.displayName = "MetadataForm.GridInput";
|
|
11085
|
+
const PlaceholderInner = () => {
|
|
11086
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
|
|
11087
|
+
/* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
|
|
11088
|
+
/* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
|
|
11089
|
+
/* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
|
|
11090
|
+
/* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
|
|
11091
|
+
] }) })
|
|
11092
|
+
] });
|
|
11093
|
+
};
|
|
11094
|
+
const EDITABLE_TYPES = ["string", "number", "boolean"];
|
|
11095
|
+
function getDefaultValues(metadata) {
|
|
11096
|
+
if (!metadata || !Object.keys(metadata).length) {
|
|
11097
|
+
return [
|
|
11098
|
+
{
|
|
11099
|
+
key: "",
|
|
11100
|
+
value: "",
|
|
11101
|
+
disabled: false
|
|
11102
|
+
}
|
|
11103
|
+
];
|
|
11104
|
+
}
|
|
11105
|
+
return Object.entries(metadata).map(([key, value]) => {
|
|
11106
|
+
if (!EDITABLE_TYPES.includes(typeof value)) {
|
|
11107
|
+
return {
|
|
11108
|
+
key,
|
|
11109
|
+
value,
|
|
11110
|
+
disabled: true
|
|
11111
|
+
};
|
|
11112
|
+
}
|
|
11113
|
+
let stringValue = value;
|
|
11114
|
+
if (typeof value !== "string") {
|
|
11115
|
+
stringValue = JSON.stringify(value);
|
|
11116
|
+
}
|
|
11117
|
+
return {
|
|
11118
|
+
key,
|
|
11119
|
+
value: stringValue,
|
|
11120
|
+
original_key: key
|
|
11121
|
+
};
|
|
11122
|
+
});
|
|
11123
|
+
}
|
|
11124
|
+
function parseValues(values) {
|
|
11125
|
+
const metadata = values.metadata;
|
|
11126
|
+
const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
|
|
11127
|
+
if (isEmpty) {
|
|
11128
|
+
return null;
|
|
11129
|
+
}
|
|
11130
|
+
const update = {};
|
|
11131
|
+
metadata.forEach((field) => {
|
|
11132
|
+
let key = field.key;
|
|
11133
|
+
let value = field.value;
|
|
11134
|
+
const disabled = field.disabled;
|
|
11135
|
+
if (!key || !value) {
|
|
11136
|
+
return;
|
|
11137
|
+
}
|
|
11138
|
+
if (disabled) {
|
|
11139
|
+
update[key] = value;
|
|
11140
|
+
return;
|
|
11141
|
+
}
|
|
11142
|
+
key = key.trim();
|
|
11143
|
+
value = value.trim();
|
|
11144
|
+
if (value === "true") {
|
|
11145
|
+
update[key] = true;
|
|
11146
|
+
} else if (value === "false") {
|
|
11147
|
+
update[key] = false;
|
|
11148
|
+
} else {
|
|
11149
|
+
const parsedNumber = parseFloat(value);
|
|
11150
|
+
if (!isNaN(parsedNumber)) {
|
|
11151
|
+
update[key] = parsedNumber;
|
|
11152
|
+
} else {
|
|
11153
|
+
update[key] = value;
|
|
11154
|
+
}
|
|
11155
|
+
}
|
|
11156
|
+
});
|
|
11157
|
+
return update;
|
|
11158
|
+
}
|
|
11159
|
+
function getHasUneditableRows(metadata) {
|
|
11160
|
+
if (!metadata) {
|
|
11161
|
+
return false;
|
|
11162
|
+
}
|
|
11163
|
+
return Object.values(metadata).some(
|
|
11164
|
+
(value) => !EDITABLE_TYPES.includes(typeof value)
|
|
11165
|
+
);
|
|
11166
|
+
}
|
|
11167
|
+
const PROMOTION_QUERY_KEY = "promotions";
|
|
11168
|
+
const promotionsQueryKeys = {
|
|
11169
|
+
list: (query2) => [
|
|
11170
|
+
PROMOTION_QUERY_KEY,
|
|
11171
|
+
query2 ? query2 : void 0
|
|
11172
|
+
],
|
|
11173
|
+
detail: (id, query2) => [
|
|
11174
|
+
PROMOTION_QUERY_KEY,
|
|
11175
|
+
id,
|
|
11176
|
+
query2 ? query2 : void 0
|
|
11177
|
+
]
|
|
11178
|
+
};
|
|
11179
|
+
const usePromotions = (query2, options) => {
|
|
11180
|
+
const { data, ...rest } = useQuery({
|
|
11181
|
+
queryKey: promotionsQueryKeys.list(query2),
|
|
11182
|
+
queryFn: async () => sdk.admin.promotion.list(query2),
|
|
11183
|
+
...options
|
|
11184
|
+
});
|
|
11185
|
+
return { ...data, ...rest };
|
|
11186
|
+
};
|
|
11187
|
+
const Promotions = () => {
|
|
11188
|
+
const { id } = useParams();
|
|
11189
|
+
const {
|
|
11190
|
+
order: preview,
|
|
11191
|
+
isError: isPreviewError,
|
|
11192
|
+
error: previewError
|
|
11193
|
+
} = useOrderPreview(id, void 0);
|
|
11194
|
+
useInitiateOrderEdit({ preview });
|
|
11195
|
+
const { onCancel } = useCancelOrderEdit({ preview });
|
|
11196
|
+
if (isPreviewError) {
|
|
11197
|
+
throw previewError;
|
|
11198
|
+
}
|
|
11199
|
+
const isReady = !!preview;
|
|
11200
|
+
return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
|
|
11201
|
+
/* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
|
|
11202
|
+
isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
|
|
11203
|
+
] });
|
|
11204
|
+
};
|
|
11205
|
+
const PromotionForm = ({ preview }) => {
|
|
11206
|
+
const { items, shipping_methods } = preview;
|
|
11207
|
+
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
11208
|
+
const [comboboxValue, setComboboxValue] = useState("");
|
|
11209
|
+
const { handleSuccess } = useRouteModal();
|
|
11210
|
+
const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
|
|
11211
|
+
const promoIds = getPromotionIds(items, shipping_methods);
|
|
11212
|
+
const { promotions, isPending, isError, error } = usePromotions(
|
|
11213
|
+
{
|
|
11214
|
+
id: promoIds
|
|
11215
|
+
},
|
|
11216
|
+
{
|
|
11217
|
+
enabled: !!promoIds.length
|
|
11218
|
+
}
|
|
11219
|
+
);
|
|
11220
|
+
const comboboxData = useComboboxData({
|
|
11221
|
+
queryKey: ["promotions", "combobox", promoIds],
|
|
11222
|
+
queryFn: async (params) => {
|
|
11223
|
+
return await sdk.admin.promotion.list({
|
|
11224
|
+
...params,
|
|
11225
|
+
id: {
|
|
11226
|
+
$nin: promoIds
|
|
11227
|
+
}
|
|
11228
|
+
});
|
|
11229
|
+
},
|
|
11230
|
+
getOptions: (data) => {
|
|
11231
|
+
return data.promotions.map((promotion) => ({
|
|
11232
|
+
label: promotion.code,
|
|
11233
|
+
value: promotion.code
|
|
11234
|
+
}));
|
|
11235
|
+
}
|
|
11236
|
+
});
|
|
11237
|
+
const add = async (value) => {
|
|
11238
|
+
if (!value) {
|
|
11239
|
+
return;
|
|
11240
|
+
}
|
|
11241
|
+
addPromotions(
|
|
11242
|
+
{
|
|
11243
|
+
promo_codes: [value]
|
|
11244
|
+
},
|
|
11245
|
+
{
|
|
11246
|
+
onError: (e) => {
|
|
11247
|
+
toast.error(e.message);
|
|
11248
|
+
comboboxData.onSearchValueChange("");
|
|
11249
|
+
setComboboxValue("");
|
|
11250
|
+
},
|
|
11251
|
+
onSuccess: () => {
|
|
11252
|
+
comboboxData.onSearchValueChange("");
|
|
11253
|
+
setComboboxValue("");
|
|
11254
|
+
}
|
|
11255
|
+
}
|
|
11256
|
+
);
|
|
11257
|
+
};
|
|
11258
|
+
const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
|
|
11259
|
+
const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
|
|
11260
|
+
const onSubmit = async () => {
|
|
11261
|
+
setIsSubmitting(true);
|
|
11262
|
+
let requestSucceeded = false;
|
|
11263
|
+
await requestOrderEdit(void 0, {
|
|
11264
|
+
onError: (e) => {
|
|
11265
|
+
toast.error(e.message);
|
|
11266
|
+
},
|
|
11267
|
+
onSuccess: () => {
|
|
11268
|
+
requestSucceeded = true;
|
|
11269
|
+
}
|
|
11270
|
+
});
|
|
11271
|
+
if (!requestSucceeded) {
|
|
11272
|
+
setIsSubmitting(false);
|
|
11273
|
+
return;
|
|
11274
|
+
}
|
|
11275
|
+
await confirmOrderEdit(void 0, {
|
|
11276
|
+
onError: (e) => {
|
|
11277
|
+
toast.error(e.message);
|
|
11278
|
+
},
|
|
11279
|
+
onSuccess: () => {
|
|
11280
|
+
handleSuccess();
|
|
11281
|
+
},
|
|
11282
|
+
onSettled: () => {
|
|
11283
|
+
setIsSubmitting(false);
|
|
11284
|
+
}
|
|
11285
|
+
});
|
|
11286
|
+
};
|
|
11287
|
+
if (isError) {
|
|
11288
|
+
throw error;
|
|
11289
|
+
}
|
|
11290
|
+
return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
|
|
11291
|
+
/* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
|
|
11292
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
|
|
11293
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
|
|
11294
|
+
/* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
|
|
11295
|
+
/* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
|
|
11296
|
+
] }),
|
|
11297
|
+
/* @__PURE__ */ jsx(
|
|
11298
|
+
Combobox,
|
|
11299
|
+
{
|
|
11300
|
+
id: "promotion-combobox",
|
|
11301
|
+
"aria-describedby": "promotion-combobox-hint",
|
|
11302
|
+
isFetchingNextPage: comboboxData.isFetchingNextPage,
|
|
11303
|
+
fetchNextPage: comboboxData.fetchNextPage,
|
|
11304
|
+
options: comboboxData.options,
|
|
11305
|
+
onSearchValueChange: comboboxData.onSearchValueChange,
|
|
11306
|
+
searchValue: comboboxData.searchValue,
|
|
11307
|
+
disabled: comboboxData.disabled || isAddingPromotions,
|
|
11308
|
+
onChange: add,
|
|
11309
|
+
value: comboboxValue
|
|
11310
|
+
}
|
|
11311
|
+
)
|
|
11312
|
+
] }),
|
|
11313
|
+
/* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
|
|
11314
|
+
/* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
|
|
11315
|
+
PromotionItem,
|
|
11316
|
+
{
|
|
11317
|
+
promotion,
|
|
11318
|
+
orderId: preview.id,
|
|
11319
|
+
isLoading: isPending
|
|
11320
|
+
},
|
|
11321
|
+
promotion.id
|
|
11322
|
+
)) })
|
|
11323
|
+
] }) }),
|
|
11324
|
+
/* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
|
|
11325
|
+
/* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
|
|
11326
|
+
/* @__PURE__ */ jsx(
|
|
11327
|
+
Button,
|
|
11328
|
+
{
|
|
11329
|
+
size: "small",
|
|
11330
|
+
type: "submit",
|
|
11331
|
+
isLoading: isSubmitting || isAddingPromotions,
|
|
11332
|
+
children: "Save"
|
|
11333
|
+
}
|
|
11357
11334
|
)
|
|
11358
|
-
}
|
|
11359
|
-
);
|
|
11360
|
-
});
|
|
11361
|
-
GridInput.displayName = "MetadataForm.GridInput";
|
|
11362
|
-
const PlaceholderInner = () => {
|
|
11363
|
-
return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
|
|
11364
|
-
/* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
|
|
11365
|
-
/* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
|
|
11366
|
-
/* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
|
|
11367
|
-
/* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
|
|
11368
11335
|
] }) })
|
|
11369
11336
|
] });
|
|
11370
11337
|
};
|
|
11371
|
-
const
|
|
11372
|
-
|
|
11373
|
-
|
|
11374
|
-
|
|
11338
|
+
const PromotionItem = ({
|
|
11339
|
+
promotion,
|
|
11340
|
+
orderId,
|
|
11341
|
+
isLoading
|
|
11342
|
+
}) => {
|
|
11343
|
+
var _a;
|
|
11344
|
+
const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
|
|
11345
|
+
const onRemove = async () => {
|
|
11346
|
+
removePromotions(
|
|
11375
11347
|
{
|
|
11376
|
-
|
|
11377
|
-
|
|
11378
|
-
|
|
11348
|
+
promo_codes: [promotion.code]
|
|
11349
|
+
},
|
|
11350
|
+
{
|
|
11351
|
+
onError: (e) => {
|
|
11352
|
+
toast.error(e.message);
|
|
11353
|
+
}
|
|
11379
11354
|
}
|
|
11380
|
-
|
|
11355
|
+
);
|
|
11356
|
+
};
|
|
11357
|
+
const displayValue = getDisplayValue(promotion);
|
|
11358
|
+
return /* @__PURE__ */ jsxs(
|
|
11359
|
+
"div",
|
|
11360
|
+
{
|
|
11361
|
+
className: clx(
|
|
11362
|
+
"bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
|
|
11363
|
+
{
|
|
11364
|
+
"animate-pulse": isLoading
|
|
11365
|
+
}
|
|
11366
|
+
),
|
|
11367
|
+
children: [
|
|
11368
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
11369
|
+
/* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
|
|
11370
|
+
/* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
|
|
11371
|
+
displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
|
|
11372
|
+
/* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
|
|
11373
|
+
/* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
|
|
11374
|
+
] }),
|
|
11375
|
+
/* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
|
|
11376
|
+
] })
|
|
11377
|
+
] }),
|
|
11378
|
+
/* @__PURE__ */ jsx(
|
|
11379
|
+
IconButton,
|
|
11380
|
+
{
|
|
11381
|
+
size: "small",
|
|
11382
|
+
type: "button",
|
|
11383
|
+
variant: "transparent",
|
|
11384
|
+
onClick: onRemove,
|
|
11385
|
+
isLoading: isPending || isLoading,
|
|
11386
|
+
children: /* @__PURE__ */ jsx(XMark, {})
|
|
11387
|
+
}
|
|
11388
|
+
)
|
|
11389
|
+
]
|
|
11390
|
+
},
|
|
11391
|
+
promotion.id
|
|
11392
|
+
);
|
|
11393
|
+
};
|
|
11394
|
+
function getDisplayValue(promotion) {
|
|
11395
|
+
var _a, _b, _c, _d;
|
|
11396
|
+
const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
|
|
11397
|
+
if (!value) {
|
|
11398
|
+
return null;
|
|
11381
11399
|
}
|
|
11382
|
-
|
|
11383
|
-
|
|
11384
|
-
|
|
11385
|
-
|
|
11386
|
-
value,
|
|
11387
|
-
disabled: true
|
|
11388
|
-
};
|
|
11389
|
-
}
|
|
11390
|
-
let stringValue = value;
|
|
11391
|
-
if (typeof value !== "string") {
|
|
11392
|
-
stringValue = JSON.stringify(value);
|
|
11400
|
+
if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
|
|
11401
|
+
const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
|
|
11402
|
+
if (!currency) {
|
|
11403
|
+
return null;
|
|
11393
11404
|
}
|
|
11394
|
-
return
|
|
11395
|
-
|
|
11396
|
-
|
|
11397
|
-
|
|
11398
|
-
|
|
11399
|
-
});
|
|
11405
|
+
return getLocaleAmount(value, currency);
|
|
11406
|
+
} else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
|
|
11407
|
+
return formatPercentage(value);
|
|
11408
|
+
}
|
|
11409
|
+
return null;
|
|
11400
11410
|
}
|
|
11401
|
-
|
|
11402
|
-
|
|
11403
|
-
|
|
11404
|
-
|
|
11405
|
-
|
|
11411
|
+
const formatter = new Intl.NumberFormat([], {
|
|
11412
|
+
style: "percent",
|
|
11413
|
+
minimumFractionDigits: 2
|
|
11414
|
+
});
|
|
11415
|
+
const formatPercentage = (value, isPercentageValue = false) => {
|
|
11416
|
+
let val = value || 0;
|
|
11417
|
+
if (!isPercentageValue) {
|
|
11418
|
+
val = val / 100;
|
|
11406
11419
|
}
|
|
11407
|
-
|
|
11408
|
-
|
|
11409
|
-
|
|
11410
|
-
|
|
11411
|
-
|
|
11412
|
-
if (
|
|
11413
|
-
|
|
11414
|
-
|
|
11415
|
-
|
|
11416
|
-
|
|
11417
|
-
|
|
11420
|
+
return formatter.format(val);
|
|
11421
|
+
};
|
|
11422
|
+
function getPromotionIds(items, shippingMethods) {
|
|
11423
|
+
const promotionIds = /* @__PURE__ */ new Set();
|
|
11424
|
+
for (const item of items) {
|
|
11425
|
+
if (item.adjustments) {
|
|
11426
|
+
for (const adjustment of item.adjustments) {
|
|
11427
|
+
if (adjustment.promotion_id) {
|
|
11428
|
+
promotionIds.add(adjustment.promotion_id);
|
|
11429
|
+
}
|
|
11430
|
+
}
|
|
11418
11431
|
}
|
|
11419
|
-
|
|
11420
|
-
|
|
11421
|
-
if (
|
|
11422
|
-
|
|
11423
|
-
|
|
11424
|
-
|
|
11425
|
-
|
|
11426
|
-
const parsedNumber = parseFloat(value);
|
|
11427
|
-
if (!isNaN(parsedNumber)) {
|
|
11428
|
-
update[key] = parsedNumber;
|
|
11429
|
-
} else {
|
|
11430
|
-
update[key] = value;
|
|
11432
|
+
}
|
|
11433
|
+
for (const shippingMethod of shippingMethods) {
|
|
11434
|
+
if (shippingMethod.adjustments) {
|
|
11435
|
+
for (const adjustment of shippingMethod.adjustments) {
|
|
11436
|
+
if (adjustment.promotion_id) {
|
|
11437
|
+
promotionIds.add(adjustment.promotion_id);
|
|
11438
|
+
}
|
|
11431
11439
|
}
|
|
11432
11440
|
}
|
|
11433
|
-
});
|
|
11434
|
-
return update;
|
|
11435
|
-
}
|
|
11436
|
-
function getHasUneditableRows(metadata) {
|
|
11437
|
-
if (!metadata) {
|
|
11438
|
-
return false;
|
|
11439
11441
|
}
|
|
11440
|
-
return
|
|
11441
|
-
(value) => !EDITABLE_TYPES.includes(typeof value)
|
|
11442
|
-
);
|
|
11442
|
+
return Array.from(promotionIds);
|
|
11443
11443
|
}
|
|
11444
11444
|
const SalesChannel = () => {
|
|
11445
11445
|
const { id } = useParams();
|
|
@@ -13069,14 +13069,14 @@ const routeModule = {
|
|
|
13069
13069
|
Component: Items,
|
|
13070
13070
|
path: "/draft-orders/:id/items"
|
|
13071
13071
|
},
|
|
13072
|
-
{
|
|
13073
|
-
Component: Promotions,
|
|
13074
|
-
path: "/draft-orders/:id/promotions"
|
|
13075
|
-
},
|
|
13076
13072
|
{
|
|
13077
13073
|
Component: Metadata,
|
|
13078
13074
|
path: "/draft-orders/:id/metadata"
|
|
13079
13075
|
},
|
|
13076
|
+
{
|
|
13077
|
+
Component: Promotions,
|
|
13078
|
+
path: "/draft-orders/:id/promotions"
|
|
13079
|
+
},
|
|
13080
13080
|
{
|
|
13081
13081
|
Component: SalesChannel,
|
|
13082
13082
|
path: "/draft-orders/:id/sales-channel"
|