@lodashventure/medusa-campaign 1.4.10 → 1.4.12
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/package.json +2 -3
- package/.medusa/server/src/admin/index.js +0 -4164
- package/.medusa/server/src/admin/index.mjs +0 -4159
|
@@ -1,4164 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const jsxRuntime = require("react/jsx-runtime");
|
|
3
|
-
const adminSdk = require("@medusajs/admin-sdk");
|
|
4
|
-
const ui = require("@medusajs/ui");
|
|
5
|
-
const icons = require("@medusajs/icons");
|
|
6
|
-
const React = require("react");
|
|
7
|
-
const reactRouterDom = require("react-router-dom");
|
|
8
|
-
const dayjs = require("dayjs");
|
|
9
|
-
const axios = require("axios");
|
|
10
|
-
const z = require("zod");
|
|
11
|
-
const lodash = require("lodash");
|
|
12
|
-
const Medusa = require("@medusajs/js-sdk");
|
|
13
|
-
const _interopDefault = (e2) => e2 && e2.__esModule ? e2 : { default: e2 };
|
|
14
|
-
const React__default = /* @__PURE__ */ _interopDefault(React);
|
|
15
|
-
const dayjs__default = /* @__PURE__ */ _interopDefault(dayjs);
|
|
16
|
-
const axios__default = /* @__PURE__ */ _interopDefault(axios);
|
|
17
|
-
const z__default = /* @__PURE__ */ _interopDefault(z);
|
|
18
|
-
const Medusa__default = /* @__PURE__ */ _interopDefault(Medusa);
|
|
19
|
-
const CampaignDetailWidget = ({
|
|
20
|
-
data: promotion
|
|
21
|
-
}) => {
|
|
22
|
-
const navigate = reactRouterDom.useNavigate();
|
|
23
|
-
const [campaign, setCampaign] = React.useState(null);
|
|
24
|
-
const [campaignDetail, setCampaignDetail] = React.useState(
|
|
25
|
-
null
|
|
26
|
-
);
|
|
27
|
-
const [loading, setLoading] = React.useState(true);
|
|
28
|
-
const [error, setError] = React.useState(null);
|
|
29
|
-
React.useEffect(() => {
|
|
30
|
-
if (promotion == null ? void 0 : promotion.campaign_id) {
|
|
31
|
-
fetchCampaignAndDetail();
|
|
32
|
-
} else {
|
|
33
|
-
setLoading(false);
|
|
34
|
-
}
|
|
35
|
-
}, [promotion == null ? void 0 : promotion.campaign_id]);
|
|
36
|
-
const fetchCampaignAndDetail = async () => {
|
|
37
|
-
if (!(promotion == null ? void 0 : promotion.campaign_id)) return;
|
|
38
|
-
setLoading(true);
|
|
39
|
-
setError(null);
|
|
40
|
-
try {
|
|
41
|
-
const campaignResponse = await fetch(
|
|
42
|
-
`/admin/flash-sales/${promotion.campaign_id}`,
|
|
43
|
-
{
|
|
44
|
-
credentials: "include"
|
|
45
|
-
}
|
|
46
|
-
);
|
|
47
|
-
if (campaignResponse.ok) {
|
|
48
|
-
const campaignData = await campaignResponse.json();
|
|
49
|
-
setCampaign(campaignData);
|
|
50
|
-
if (campaignData.campaign_detail) {
|
|
51
|
-
setCampaignDetail(campaignData.campaign_detail);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
} catch (err) {
|
|
55
|
-
console.error("Error fetching campaign:", err);
|
|
56
|
-
setError("Failed to load campaign information");
|
|
57
|
-
} finally {
|
|
58
|
-
setLoading(false);
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
const handleEditCampaign = () => {
|
|
62
|
-
if (campaign == null ? void 0 : campaign.id) {
|
|
63
|
-
navigate(`/flash-sales/${campaign.id}`);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
const handleViewCampaign = () => {
|
|
67
|
-
if (campaign == null ? void 0 : campaign.id) {
|
|
68
|
-
navigate(`/flash-sales/${campaign.id}#details`);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
if (!(promotion == null ? void 0 : promotion.campaign_id)) {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
if (loading) {
|
|
75
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "divide-y px-0 pb-0 pt-0", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-6 py-6", children: [
|
|
76
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-3 mb-4", children: [
|
|
77
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.Sparkles, { className: "text-ui-fg-subtle" }),
|
|
78
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: "Campaign" })
|
|
79
|
-
] }),
|
|
80
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-subtle", children: "Loading campaign..." })
|
|
81
|
-
] }) });
|
|
82
|
-
}
|
|
83
|
-
if (error) {
|
|
84
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "divide-y px-0 pb-0 pt-0", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-6 py-6", children: [
|
|
85
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-3 mb-4", children: [
|
|
86
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.Sparkles, { className: "text-ui-fg-subtle" }),
|
|
87
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: "Campaign" })
|
|
88
|
-
] }),
|
|
89
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Alert, { variant: "error", children: error })
|
|
90
|
-
] }) });
|
|
91
|
-
}
|
|
92
|
-
if (!campaign) {
|
|
93
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "divide-y px-0 pb-0 pt-0", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-6 py-6", children: [
|
|
94
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-3 mb-4", children: [
|
|
95
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.Sparkles, { className: "text-ui-fg-subtle" }),
|
|
96
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: "Campaign" })
|
|
97
|
-
] }),
|
|
98
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-subtle", children: "This promotion is not part of a campaign" })
|
|
99
|
-
] }) });
|
|
100
|
-
}
|
|
101
|
-
const hasCampaignDetail = !!campaignDetail;
|
|
102
|
-
const hasImages = (campaignDetail == null ? void 0 : campaignDetail.image_url) || (campaignDetail == null ? void 0 : campaignDetail.thumbnail_url);
|
|
103
|
-
const hasContent = (campaignDetail == null ? void 0 : campaignDetail.detail_content) || (campaignDetail == null ? void 0 : campaignDetail.terms_and_conditions);
|
|
104
|
-
const hasSEO = (campaignDetail == null ? void 0 : campaignDetail.meta_title) || (campaignDetail == null ? void 0 : campaignDetail.meta_description);
|
|
105
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "divide-y px-0 pb-0 pt-0", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-6 py-6", children: [
|
|
106
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-3 mb-4", children: [
|
|
107
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.Sparkles, { className: "text-ui-fg-subtle" }),
|
|
108
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: "Campaign" })
|
|
109
|
-
] }),
|
|
110
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mb-4 p-4 rounded-lg border bg-ui-bg-subtle", children: [
|
|
111
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-start justify-between mb-3", children: [
|
|
112
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex-1", children: [
|
|
113
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "font-semibold text-lg mb-1", children: campaign.name }),
|
|
114
|
-
campaign.description && /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-sm text-ui-fg-subtle line-clamp-2", children: campaign.description })
|
|
115
|
-
] }),
|
|
116
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Badge, { color: "purple", size: "small", children: [
|
|
117
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.Sparkles, { className: "mr-1" }),
|
|
118
|
-
"Campaign"
|
|
119
|
-
] })
|
|
120
|
-
] }),
|
|
121
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-2 mt-3 pt-3 border-t", children: [
|
|
122
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
|
|
123
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
124
|
-
icons.PhotoSolid,
|
|
125
|
-
{
|
|
126
|
-
className: `h-4 w-4 ${hasImages ? "text-green-500" : "text-ui-fg-muted"}`
|
|
127
|
-
}
|
|
128
|
-
),
|
|
129
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-xs", children: hasImages ? "Images added" : "No images" })
|
|
130
|
-
] }),
|
|
131
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
|
|
132
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
133
|
-
icons.PencilSquare,
|
|
134
|
-
{
|
|
135
|
-
className: `h-4 w-4 ${hasContent ? "text-green-500" : "text-ui-fg-muted"}`
|
|
136
|
-
}
|
|
137
|
-
),
|
|
138
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-xs", children: hasContent ? "Content added" : "No content" })
|
|
139
|
-
] })
|
|
140
|
-
] })
|
|
141
|
-
] }),
|
|
142
|
-
hasCampaignDetail && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-3 mb-4", children: [
|
|
143
|
-
campaignDetail.thumbnail_url && /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
144
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-xs font-medium text-ui-fg-subtle mb-2", children: "Thumbnail" }),
|
|
145
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
146
|
-
"img",
|
|
147
|
-
{
|
|
148
|
-
src: campaignDetail.thumbnail_url,
|
|
149
|
-
alt: "Campaign thumbnail",
|
|
150
|
-
className: "w-full h-32 object-cover rounded-lg border"
|
|
151
|
-
}
|
|
152
|
-
)
|
|
153
|
-
] }),
|
|
154
|
-
campaignDetail.detail_content && /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
155
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-xs font-medium text-ui-fg-subtle mb-1", children: "Content Preview" }),
|
|
156
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { className: "text-sm line-clamp-3 text-ui-fg-muted", children: [
|
|
157
|
-
campaignDetail.detail_content.substring(0, 150),
|
|
158
|
-
"..."
|
|
159
|
-
] })
|
|
160
|
-
] }),
|
|
161
|
-
campaignDetail.link_url && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2 text-xs", children: [
|
|
162
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { size: "xsmall", color: "blue", children: "CTA Link" }),
|
|
163
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-subtle truncate", children: campaignDetail.link_text || campaignDetail.link_url })
|
|
164
|
-
] }),
|
|
165
|
-
hasSEO && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2 text-xs", children: [
|
|
166
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { size: "xsmall", color: "green", children: "SEO" }),
|
|
167
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-subtle", children: "Meta fields configured" })
|
|
168
|
-
] })
|
|
169
|
-
] }),
|
|
170
|
-
!hasCampaignDetail && /* @__PURE__ */ jsxRuntime.jsx(ui.Alert, { variant: "info", className: "mb-4", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-sm", children: "No campaign details added yet. Add images, content, and SEO to enhance this campaign." }) }),
|
|
171
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
|
|
172
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Button, { variant: "secondary", size: "small", onClick: handleEditCampaign, children: [
|
|
173
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, { className: "mr-1" }),
|
|
174
|
-
"Edit Campaign"
|
|
175
|
-
] }),
|
|
176
|
-
hasCampaignDetail && /* @__PURE__ */ jsxRuntime.jsxs(
|
|
177
|
-
ui.Button,
|
|
178
|
-
{
|
|
179
|
-
variant: "secondary",
|
|
180
|
-
size: "small",
|
|
181
|
-
onClick: handleViewCampaign,
|
|
182
|
-
children: [
|
|
183
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.Eye, { className: "mr-1" }),
|
|
184
|
-
"View Details"
|
|
185
|
-
]
|
|
186
|
-
}
|
|
187
|
-
)
|
|
188
|
-
] }),
|
|
189
|
-
campaignDetail && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mt-4 pt-4 border-t grid grid-cols-2 gap-3 text-xs", children: [
|
|
190
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
191
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-subtle", children: "Display Order" }),
|
|
192
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "font-medium", children: campaignDetail.display_order })
|
|
193
|
-
] }),
|
|
194
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
195
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-subtle", children: "Status" }),
|
|
196
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: hasCampaignDetail ? "green" : "grey", size: "xsmall", children: hasCampaignDetail ? "Complete" : "Incomplete" })
|
|
197
|
-
] })
|
|
198
|
-
] })
|
|
199
|
-
] }) });
|
|
200
|
-
};
|
|
201
|
-
adminSdk.defineWidgetConfig({
|
|
202
|
-
zone: "promotion.details.side.after"
|
|
203
|
-
});
|
|
204
|
-
const useCoupons = ({ limit, offset }) => {
|
|
205
|
-
const [data, setData] = React.useState(null);
|
|
206
|
-
const [isLoading, setIsLoading] = React.useState(true);
|
|
207
|
-
const [error, setError] = React.useState(null);
|
|
208
|
-
const fetchCoupons = async () => {
|
|
209
|
-
setIsLoading(true);
|
|
210
|
-
setError(null);
|
|
211
|
-
try {
|
|
212
|
-
const response = await axios__default.default.get("/admin/coupons", {
|
|
213
|
-
params: { limit, offset }
|
|
214
|
-
});
|
|
215
|
-
setData(response.data);
|
|
216
|
-
} catch (err) {
|
|
217
|
-
setError(err instanceof Error ? err : new Error("Failed to fetch coupons"));
|
|
218
|
-
} finally {
|
|
219
|
-
setIsLoading(false);
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
React.useEffect(() => {
|
|
223
|
-
fetchCoupons();
|
|
224
|
-
}, [limit, offset]);
|
|
225
|
-
return {
|
|
226
|
-
data,
|
|
227
|
-
isLoading,
|
|
228
|
-
error,
|
|
229
|
-
refetch: fetchCoupons
|
|
230
|
-
};
|
|
231
|
-
};
|
|
232
|
-
const columnHelper$1 = ui.createDataTableColumnHelper();
|
|
233
|
-
const useCouponColumns = () => React.useMemo(
|
|
234
|
-
() => [
|
|
235
|
-
columnHelper$1.accessor("name", {
|
|
236
|
-
header: "Name",
|
|
237
|
-
cell: (info) => info.getValue()
|
|
238
|
-
}),
|
|
239
|
-
columnHelper$1.display({
|
|
240
|
-
id: "code",
|
|
241
|
-
header: "Code",
|
|
242
|
-
cell: (info) => {
|
|
243
|
-
var _a, _b;
|
|
244
|
-
return ((_b = (_a = info.row.original.promotions) == null ? void 0 : _a[0]) == null ? void 0 : _b.code) ?? "-";
|
|
245
|
-
}
|
|
246
|
-
}),
|
|
247
|
-
columnHelper$1.display({
|
|
248
|
-
id: "status",
|
|
249
|
-
header: "Status",
|
|
250
|
-
cell: (info) => {
|
|
251
|
-
const endsAt = info.row.original.ends_at;
|
|
252
|
-
if (!endsAt) {
|
|
253
|
-
return "";
|
|
254
|
-
}
|
|
255
|
-
const isActive = dayjs__default.default(endsAt).isAfter(dayjs__default.default());
|
|
256
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: isActive ? "green" : "grey", children: isActive ? "Active" : "Inactive" });
|
|
257
|
-
}
|
|
258
|
-
}),
|
|
259
|
-
columnHelper$1.accessor("starts_at", {
|
|
260
|
-
header: "Start Date",
|
|
261
|
-
cell: (info) => info.getValue() ? dayjs__default.default(info.getValue()).format("YYYY-MM-DD HH:mm") : ""
|
|
262
|
-
}),
|
|
263
|
-
columnHelper$1.accessor("ends_at", {
|
|
264
|
-
header: "End Date",
|
|
265
|
-
cell: (info) => info.getValue() ? dayjs__default.default(info.getValue()).format("YYYY-MM-DD HH:mm") : ""
|
|
266
|
-
})
|
|
267
|
-
],
|
|
268
|
-
[]
|
|
269
|
-
);
|
|
270
|
-
const CouponPage = () => {
|
|
271
|
-
const navigate = reactRouterDom.useNavigate();
|
|
272
|
-
const limit = 20;
|
|
273
|
-
const [pagination, setPagination] = React.useState({
|
|
274
|
-
pageSize: limit,
|
|
275
|
-
pageIndex: 0
|
|
276
|
-
});
|
|
277
|
-
const offset = pagination.pageIndex * limit;
|
|
278
|
-
const { data } = useCoupons({ limit, offset });
|
|
279
|
-
const columns2 = useCouponColumns();
|
|
280
|
-
const table = ui.useDataTable({
|
|
281
|
-
data: (data == null ? void 0 : data.campaigns) ?? [],
|
|
282
|
-
columns: columns2,
|
|
283
|
-
getRowId: (row) => row.id,
|
|
284
|
-
pagination: {
|
|
285
|
-
state: pagination,
|
|
286
|
-
onPaginationChange: setPagination
|
|
287
|
-
},
|
|
288
|
-
rowCount: (data == null ? void 0 : data.count) ?? 0,
|
|
289
|
-
onRowClick: (_, row) => {
|
|
290
|
-
navigate(`/coupons/${row.id}`);
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { children: [
|
|
294
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-between items-center", children: [
|
|
295
|
-
/* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-xl font-semibold", children: "Coupons" }),
|
|
296
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: () => navigate("/coupons/create"), children: "Create Coupon" })
|
|
297
|
-
] }),
|
|
298
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.DataTable, { instance: table, children: [
|
|
299
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.DataTable.Table, {}),
|
|
300
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.DataTable.Pagination, {})
|
|
301
|
-
] })
|
|
302
|
-
] });
|
|
303
|
-
};
|
|
304
|
-
const Coupons = () => {
|
|
305
|
-
return /* @__PURE__ */ jsxRuntime.jsx(CouponPage, {});
|
|
306
|
-
};
|
|
307
|
-
const config$3 = adminSdk.defineRouteConfig({
|
|
308
|
-
label: "Coupons",
|
|
309
|
-
icon: icons.Tag
|
|
310
|
-
});
|
|
311
|
-
const useFlashSales = (pagination) => {
|
|
312
|
-
const [data, setData] = React.useState(null);
|
|
313
|
-
const [isLoading, setIsLoading] = React.useState(true);
|
|
314
|
-
const [error, setError] = React.useState(null);
|
|
315
|
-
const fetchFlashSales = async () => {
|
|
316
|
-
setIsLoading(true);
|
|
317
|
-
setError(null);
|
|
318
|
-
try {
|
|
319
|
-
const response = await axios__default.default.get("/admin/flash-sales", {
|
|
320
|
-
params: pagination
|
|
321
|
-
});
|
|
322
|
-
setData(response.data);
|
|
323
|
-
} catch (err) {
|
|
324
|
-
setError(
|
|
325
|
-
err instanceof Error ? err : new Error("Failed to fetch flash sales")
|
|
326
|
-
);
|
|
327
|
-
} finally {
|
|
328
|
-
setIsLoading(false);
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
React.useEffect(() => {
|
|
332
|
-
fetchFlashSales();
|
|
333
|
-
}, [pagination.limit, pagination.offset]);
|
|
334
|
-
return {
|
|
335
|
-
data,
|
|
336
|
-
isLoading,
|
|
337
|
-
error,
|
|
338
|
-
refetch: fetchFlashSales
|
|
339
|
-
};
|
|
340
|
-
};
|
|
341
|
-
const FlashSalePage = () => {
|
|
342
|
-
const navigate = reactRouterDom.useNavigate();
|
|
343
|
-
const limit = 20;
|
|
344
|
-
const [pagination, setPagination] = React.useState({
|
|
345
|
-
pageSize: limit,
|
|
346
|
-
pageIndex: 0
|
|
347
|
-
});
|
|
348
|
-
const offset = React.useMemo(() => {
|
|
349
|
-
return pagination.pageIndex * limit;
|
|
350
|
-
}, [pagination]);
|
|
351
|
-
const columnHelper2 = ui.createDataTableColumnHelper();
|
|
352
|
-
const columns2 = [
|
|
353
|
-
columnHelper2.accessor("name", {
|
|
354
|
-
header: "Name",
|
|
355
|
-
cell: (info) => info.getValue()
|
|
356
|
-
}),
|
|
357
|
-
columnHelper2.accessor("ends_at", {
|
|
358
|
-
header: "Status",
|
|
359
|
-
cell: (info) => {
|
|
360
|
-
const date = info.getValue();
|
|
361
|
-
if (!date) {
|
|
362
|
-
return "";
|
|
363
|
-
}
|
|
364
|
-
const isActive = dayjs__default.default(date).isAfter(dayjs__default.default());
|
|
365
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Badge, { color: isActive ? "green" : "grey", children: isActive ? "Active" : "Inactive" });
|
|
366
|
-
}
|
|
367
|
-
}),
|
|
368
|
-
columnHelper2.accessor("starts_at", {
|
|
369
|
-
header: "Start Date",
|
|
370
|
-
cell: (info) => {
|
|
371
|
-
const date = info.getValue();
|
|
372
|
-
if (!date) {
|
|
373
|
-
return "";
|
|
374
|
-
}
|
|
375
|
-
return dayjs__default.default(date).format("YYYY-MM-DD HH:mm");
|
|
376
|
-
}
|
|
377
|
-
}),
|
|
378
|
-
columnHelper2.accessor("ends_at", {
|
|
379
|
-
header: "End Date",
|
|
380
|
-
cell: (info) => {
|
|
381
|
-
const date = info.getValue();
|
|
382
|
-
if (!date) {
|
|
383
|
-
return "";
|
|
384
|
-
}
|
|
385
|
-
return dayjs__default.default(date).format("YYYY-MM-DD HH:mm");
|
|
386
|
-
}
|
|
387
|
-
})
|
|
388
|
-
];
|
|
389
|
-
const { data } = useFlashSales({ limit, offset });
|
|
390
|
-
const table = ui.useDataTable({
|
|
391
|
-
data: (data == null ? void 0 : data.campaigns) || [],
|
|
392
|
-
columns: columns2,
|
|
393
|
-
getRowId: (campaign) => campaign.id,
|
|
394
|
-
pagination: {
|
|
395
|
-
state: pagination,
|
|
396
|
-
onPaginationChange: setPagination
|
|
397
|
-
},
|
|
398
|
-
rowCount: (data == null ? void 0 : data.count) || 0,
|
|
399
|
-
onRowClick: (_, row) => {
|
|
400
|
-
navigate(`/flash-sales/${row.id}`);
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { children: [
|
|
404
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-between items-center", children: [
|
|
405
|
-
/* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-xl font-semibold", children: "Campaigns" }),
|
|
406
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: () => navigate("/flash-sales/create"), children: "Create Campaign" })
|
|
407
|
-
] }),
|
|
408
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.DataTable, { instance: table, children: [
|
|
409
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.DataTable.Table, {}),
|
|
410
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.DataTable.Pagination, {})
|
|
411
|
-
] })
|
|
412
|
-
] });
|
|
413
|
-
};
|
|
414
|
-
const FlashSale = () => {
|
|
415
|
-
return /* @__PURE__ */ jsxRuntime.jsx(FlashSalePage, {});
|
|
416
|
-
};
|
|
417
|
-
const config$2 = adminSdk.defineRouteConfig({
|
|
418
|
-
label: "Flash Sale",
|
|
419
|
-
icon: icons.Sparkles
|
|
420
|
-
});
|
|
421
|
-
var isCheckBoxInput = (element) => element.type === "checkbox";
|
|
422
|
-
var isDateObject = (value) => value instanceof Date;
|
|
423
|
-
var isNullOrUndefined = (value) => value == null;
|
|
424
|
-
const isObjectType = (value) => typeof value === "object";
|
|
425
|
-
var isObject = (value) => !isNullOrUndefined(value) && !Array.isArray(value) && isObjectType(value) && !isDateObject(value);
|
|
426
|
-
var getEventValue = (event) => isObject(event) && event.target ? isCheckBoxInput(event.target) ? event.target.checked : event.target.value : event;
|
|
427
|
-
var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/)) || name;
|
|
428
|
-
var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
|
|
429
|
-
var isPlainObject = (tempObject) => {
|
|
430
|
-
const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
|
|
431
|
-
return isObject(prototypeCopy) && prototypeCopy.hasOwnProperty("isPrototypeOf");
|
|
432
|
-
};
|
|
433
|
-
var isWeb = typeof window !== "undefined" && typeof window.HTMLElement !== "undefined" && typeof document !== "undefined";
|
|
434
|
-
function cloneObject(data) {
|
|
435
|
-
let copy;
|
|
436
|
-
const isArray = Array.isArray(data);
|
|
437
|
-
if (data instanceof Date) {
|
|
438
|
-
copy = new Date(data);
|
|
439
|
-
} else if (data instanceof Set) {
|
|
440
|
-
copy = new Set(data);
|
|
441
|
-
} else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) && (isArray || isObject(data))) {
|
|
442
|
-
copy = isArray ? [] : {};
|
|
443
|
-
if (!isArray && !isPlainObject(data)) {
|
|
444
|
-
copy = data;
|
|
445
|
-
} else {
|
|
446
|
-
for (const key in data) {
|
|
447
|
-
if (data.hasOwnProperty(key)) {
|
|
448
|
-
copy[key] = cloneObject(data[key]);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
} else {
|
|
453
|
-
return data;
|
|
454
|
-
}
|
|
455
|
-
return copy;
|
|
456
|
-
}
|
|
457
|
-
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
|
458
|
-
var isUndefined = (val) => val === void 0;
|
|
459
|
-
var get = (object, path, defaultValue) => {
|
|
460
|
-
if (!path || !isObject(object)) {
|
|
461
|
-
return defaultValue;
|
|
462
|
-
}
|
|
463
|
-
const result = compact(path.split(/[,[\].]+?/)).reduce((result2, key) => isNullOrUndefined(result2) ? result2 : result2[key], object);
|
|
464
|
-
return isUndefined(result) || result === object ? isUndefined(object[path]) ? defaultValue : object[path] : result;
|
|
465
|
-
};
|
|
466
|
-
var isBoolean = (value) => typeof value === "boolean";
|
|
467
|
-
const EVENTS = {
|
|
468
|
-
BLUR: "blur",
|
|
469
|
-
FOCUS_OUT: "focusout",
|
|
470
|
-
CHANGE: "change"
|
|
471
|
-
};
|
|
472
|
-
const VALIDATION_MODE = {
|
|
473
|
-
onBlur: "onBlur",
|
|
474
|
-
onChange: "onChange",
|
|
475
|
-
onSubmit: "onSubmit",
|
|
476
|
-
onTouched: "onTouched",
|
|
477
|
-
all: "all"
|
|
478
|
-
};
|
|
479
|
-
const INPUT_VALIDATION_RULES = {
|
|
480
|
-
max: "max",
|
|
481
|
-
min: "min",
|
|
482
|
-
maxLength: "maxLength",
|
|
483
|
-
minLength: "minLength",
|
|
484
|
-
pattern: "pattern",
|
|
485
|
-
required: "required",
|
|
486
|
-
validate: "validate"
|
|
487
|
-
};
|
|
488
|
-
const HookFormContext = React__default.default.createContext(null);
|
|
489
|
-
const useFormContext = () => React__default.default.useContext(HookFormContext);
|
|
490
|
-
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
|
491
|
-
const result = {
|
|
492
|
-
defaultValues: control._defaultValues
|
|
493
|
-
};
|
|
494
|
-
for (const key in formState) {
|
|
495
|
-
Object.defineProperty(result, key, {
|
|
496
|
-
get: () => {
|
|
497
|
-
const _key = key;
|
|
498
|
-
if (control._proxyFormState[_key] !== VALIDATION_MODE.all) {
|
|
499
|
-
control._proxyFormState[_key] = !isRoot || VALIDATION_MODE.all;
|
|
500
|
-
}
|
|
501
|
-
localProxyFormState && (localProxyFormState[_key] = true);
|
|
502
|
-
return formState[_key];
|
|
503
|
-
}
|
|
504
|
-
});
|
|
505
|
-
}
|
|
506
|
-
return result;
|
|
507
|
-
};
|
|
508
|
-
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
|
509
|
-
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
|
510
|
-
updateFormState(formStateData);
|
|
511
|
-
const { name, ...formState } = formStateData;
|
|
512
|
-
return isEmptyObject(formState) || Object.keys(formState).length >= Object.keys(_proxyFormState).length || Object.keys(formState).find((key) => _proxyFormState[key] === (!isRoot || VALIDATION_MODE.all));
|
|
513
|
-
};
|
|
514
|
-
var convertToArrayPayload = (value) => Array.isArray(value) ? value : [value];
|
|
515
|
-
var shouldSubscribeByName = (name, signalName, exact) => !name || !signalName || name === signalName || convertToArrayPayload(name).some((currentName) => currentName && (exact ? currentName === signalName : currentName.startsWith(signalName) || signalName.startsWith(currentName)));
|
|
516
|
-
function useSubscribe(props) {
|
|
517
|
-
const _props = React__default.default.useRef(props);
|
|
518
|
-
_props.current = props;
|
|
519
|
-
React__default.default.useEffect(() => {
|
|
520
|
-
const subscription = !props.disabled && _props.current.subject && _props.current.subject.subscribe({
|
|
521
|
-
next: _props.current.next
|
|
522
|
-
});
|
|
523
|
-
return () => {
|
|
524
|
-
subscription && subscription.unsubscribe();
|
|
525
|
-
};
|
|
526
|
-
}, [props.disabled]);
|
|
527
|
-
}
|
|
528
|
-
function useFormState(props) {
|
|
529
|
-
const methods = useFormContext();
|
|
530
|
-
const { control = methods.control, disabled, name, exact } = props || {};
|
|
531
|
-
const [formState, updateFormState] = React__default.default.useState(control._formState);
|
|
532
|
-
const _mounted = React__default.default.useRef(true);
|
|
533
|
-
const _localProxyFormState = React__default.default.useRef({
|
|
534
|
-
isDirty: false,
|
|
535
|
-
isLoading: false,
|
|
536
|
-
dirtyFields: false,
|
|
537
|
-
touchedFields: false,
|
|
538
|
-
isValidating: false,
|
|
539
|
-
isValid: false,
|
|
540
|
-
errors: false
|
|
541
|
-
});
|
|
542
|
-
const _name = React__default.default.useRef(name);
|
|
543
|
-
_name.current = name;
|
|
544
|
-
useSubscribe({
|
|
545
|
-
disabled,
|
|
546
|
-
next: (value) => _mounted.current && shouldSubscribeByName(_name.current, value.name, exact) && shouldRenderFormState(value, _localProxyFormState.current, control._updateFormState) && updateFormState({
|
|
547
|
-
...control._formState,
|
|
548
|
-
...value
|
|
549
|
-
}),
|
|
550
|
-
subject: control._subjects.state
|
|
551
|
-
});
|
|
552
|
-
React__default.default.useEffect(() => {
|
|
553
|
-
_mounted.current = true;
|
|
554
|
-
_localProxyFormState.current.isValid && control._updateValid(true);
|
|
555
|
-
return () => {
|
|
556
|
-
_mounted.current = false;
|
|
557
|
-
};
|
|
558
|
-
}, [control]);
|
|
559
|
-
return getProxyFormState(formState, control, _localProxyFormState.current, false);
|
|
560
|
-
}
|
|
561
|
-
var isString = (value) => typeof value === "string";
|
|
562
|
-
var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) => {
|
|
563
|
-
if (isString(names)) {
|
|
564
|
-
isGlobal && _names.watch.add(names);
|
|
565
|
-
return get(formValues, names, defaultValue);
|
|
566
|
-
}
|
|
567
|
-
if (Array.isArray(names)) {
|
|
568
|
-
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
|
|
569
|
-
}
|
|
570
|
-
isGlobal && (_names.watchAll = true);
|
|
571
|
-
return formValues;
|
|
572
|
-
};
|
|
573
|
-
function useWatch(props) {
|
|
574
|
-
const methods = useFormContext();
|
|
575
|
-
const { control = methods.control, name, defaultValue, disabled, exact } = props || {};
|
|
576
|
-
const _name = React__default.default.useRef(name);
|
|
577
|
-
_name.current = name;
|
|
578
|
-
useSubscribe({
|
|
579
|
-
disabled,
|
|
580
|
-
subject: control._subjects.values,
|
|
581
|
-
next: (formState) => {
|
|
582
|
-
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
|
583
|
-
updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
});
|
|
587
|
-
const [value, updateValue] = React__default.default.useState(control._getWatch(name, defaultValue));
|
|
588
|
-
React__default.default.useEffect(() => control._removeUnmounted());
|
|
589
|
-
return value;
|
|
590
|
-
}
|
|
591
|
-
var isKey = (value) => /^\w*$/.test(value);
|
|
592
|
-
var stringToPath = (input) => compact(input.replace(/["|']|\]/g, "").split(/\.|\[/));
|
|
593
|
-
var set = (object, path, value) => {
|
|
594
|
-
let index = -1;
|
|
595
|
-
const tempPath = isKey(path) ? [path] : stringToPath(path);
|
|
596
|
-
const length = tempPath.length;
|
|
597
|
-
const lastIndex = length - 1;
|
|
598
|
-
while (++index < length) {
|
|
599
|
-
const key = tempPath[index];
|
|
600
|
-
let newValue = value;
|
|
601
|
-
if (index !== lastIndex) {
|
|
602
|
-
const objValue = object[key];
|
|
603
|
-
newValue = isObject(objValue) || Array.isArray(objValue) ? objValue : !isNaN(+tempPath[index + 1]) ? [] : {};
|
|
604
|
-
}
|
|
605
|
-
object[key] = newValue;
|
|
606
|
-
object = object[key];
|
|
607
|
-
}
|
|
608
|
-
return object;
|
|
609
|
-
};
|
|
610
|
-
function useController(props) {
|
|
611
|
-
const methods = useFormContext();
|
|
612
|
-
const { name, disabled, control = methods.control, shouldUnregister } = props;
|
|
613
|
-
const isArrayField = isNameInFieldArray(control._names.array, name);
|
|
614
|
-
const value = useWatch({
|
|
615
|
-
control,
|
|
616
|
-
name,
|
|
617
|
-
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
|
618
|
-
exact: true
|
|
619
|
-
});
|
|
620
|
-
const formState = useFormState({
|
|
621
|
-
control,
|
|
622
|
-
name
|
|
623
|
-
});
|
|
624
|
-
const _registerProps = React__default.default.useRef(control.register(name, {
|
|
625
|
-
...props.rules,
|
|
626
|
-
value,
|
|
627
|
-
...isBoolean(props.disabled) ? { disabled: props.disabled } : {}
|
|
628
|
-
}));
|
|
629
|
-
React__default.default.useEffect(() => {
|
|
630
|
-
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
631
|
-
const updateMounted = (name2, value2) => {
|
|
632
|
-
const field = get(control._fields, name2);
|
|
633
|
-
if (field) {
|
|
634
|
-
field._f.mount = value2;
|
|
635
|
-
}
|
|
636
|
-
};
|
|
637
|
-
updateMounted(name, true);
|
|
638
|
-
if (_shouldUnregisterField) {
|
|
639
|
-
const value2 = cloneObject(get(control._options.defaultValues, name));
|
|
640
|
-
set(control._defaultValues, name, value2);
|
|
641
|
-
if (isUndefined(get(control._formValues, name))) {
|
|
642
|
-
set(control._formValues, name, value2);
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
return () => {
|
|
646
|
-
(isArrayField ? _shouldUnregisterField && !control._state.action : _shouldUnregisterField) ? control.unregister(name) : updateMounted(name, false);
|
|
647
|
-
};
|
|
648
|
-
}, [name, control, isArrayField, shouldUnregister]);
|
|
649
|
-
React__default.default.useEffect(() => {
|
|
650
|
-
if (get(control._fields, name)) {
|
|
651
|
-
control._updateDisabledField({
|
|
652
|
-
disabled,
|
|
653
|
-
fields: control._fields,
|
|
654
|
-
name,
|
|
655
|
-
value: get(control._fields, name)._f.value
|
|
656
|
-
});
|
|
657
|
-
}
|
|
658
|
-
}, [disabled, name, control]);
|
|
659
|
-
return {
|
|
660
|
-
field: {
|
|
661
|
-
name,
|
|
662
|
-
value,
|
|
663
|
-
...isBoolean(disabled) || isBoolean(formState.disabled) ? { disabled: formState.disabled || disabled } : {},
|
|
664
|
-
onChange: React__default.default.useCallback((event) => _registerProps.current.onChange({
|
|
665
|
-
target: {
|
|
666
|
-
value: getEventValue(event),
|
|
667
|
-
name
|
|
668
|
-
},
|
|
669
|
-
type: EVENTS.CHANGE
|
|
670
|
-
}), [name]),
|
|
671
|
-
onBlur: React__default.default.useCallback(() => _registerProps.current.onBlur({
|
|
672
|
-
target: {
|
|
673
|
-
value: get(control._formValues, name),
|
|
674
|
-
name
|
|
675
|
-
},
|
|
676
|
-
type: EVENTS.BLUR
|
|
677
|
-
}), [name, control]),
|
|
678
|
-
ref: (elm) => {
|
|
679
|
-
const field = get(control._fields, name);
|
|
680
|
-
if (field && elm) {
|
|
681
|
-
field._f.ref = {
|
|
682
|
-
focus: () => elm.focus(),
|
|
683
|
-
select: () => elm.select(),
|
|
684
|
-
setCustomValidity: (message) => elm.setCustomValidity(message),
|
|
685
|
-
reportValidity: () => elm.reportValidity()
|
|
686
|
-
};
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
},
|
|
690
|
-
formState,
|
|
691
|
-
fieldState: Object.defineProperties({}, {
|
|
692
|
-
invalid: {
|
|
693
|
-
enumerable: true,
|
|
694
|
-
get: () => !!get(formState.errors, name)
|
|
695
|
-
},
|
|
696
|
-
isDirty: {
|
|
697
|
-
enumerable: true,
|
|
698
|
-
get: () => !!get(formState.dirtyFields, name)
|
|
699
|
-
},
|
|
700
|
-
isTouched: {
|
|
701
|
-
enumerable: true,
|
|
702
|
-
get: () => !!get(formState.touchedFields, name)
|
|
703
|
-
},
|
|
704
|
-
error: {
|
|
705
|
-
enumerable: true,
|
|
706
|
-
get: () => get(formState.errors, name)
|
|
707
|
-
}
|
|
708
|
-
})
|
|
709
|
-
};
|
|
710
|
-
}
|
|
711
|
-
const Controller = (props) => props.render(useController(props));
|
|
712
|
-
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria ? {
|
|
713
|
-
...errors[name],
|
|
714
|
-
types: {
|
|
715
|
-
...errors[name] && errors[name].types ? errors[name].types : {},
|
|
716
|
-
[type]: message || true
|
|
717
|
-
}
|
|
718
|
-
} : {};
|
|
719
|
-
var generateId = () => {
|
|
720
|
-
const d = typeof performance === "undefined" ? Date.now() : performance.now() * 1e3;
|
|
721
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c2) => {
|
|
722
|
-
const r = (Math.random() * 16 + d) % 16 | 0;
|
|
723
|
-
return (c2 == "x" ? r : r & 3 | 8).toString(16);
|
|
724
|
-
});
|
|
725
|
-
};
|
|
726
|
-
var getFocusFieldName = (name, index, options = {}) => options.shouldFocus || isUndefined(options.shouldFocus) ? options.focusName || `${name}.${isUndefined(options.focusIndex) ? index : options.focusIndex}.` : "";
|
|
727
|
-
var getValidationModes = (mode) => ({
|
|
728
|
-
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
729
|
-
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
730
|
-
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
731
|
-
isOnAll: mode === VALIDATION_MODE.all,
|
|
732
|
-
isOnTouch: mode === VALIDATION_MODE.onTouched
|
|
733
|
-
});
|
|
734
|
-
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent && (_names.watchAll || _names.watch.has(name) || [..._names.watch].some((watchName) => name.startsWith(watchName) && /^\.\w+/.test(name.slice(watchName.length))));
|
|
735
|
-
const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
|
|
736
|
-
for (const key of fieldsNames || Object.keys(fields)) {
|
|
737
|
-
const field = get(fields, key);
|
|
738
|
-
if (field) {
|
|
739
|
-
const { _f, ...currentField } = field;
|
|
740
|
-
if (_f) {
|
|
741
|
-
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
|
|
742
|
-
break;
|
|
743
|
-
} else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
|
|
744
|
-
break;
|
|
745
|
-
} else {
|
|
746
|
-
iterateFieldsByAction(currentField, action);
|
|
747
|
-
}
|
|
748
|
-
} else if (isObject(currentField)) {
|
|
749
|
-
iterateFieldsByAction(currentField, action);
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
};
|
|
754
|
-
var updateFieldArrayRootError = (errors, error, name) => {
|
|
755
|
-
const fieldArrayErrors = compact(get(errors, name));
|
|
756
|
-
set(fieldArrayErrors, "root", error[name]);
|
|
757
|
-
set(errors, name, fieldArrayErrors);
|
|
758
|
-
return errors;
|
|
759
|
-
};
|
|
760
|
-
var isFileInput = (element) => element.type === "file";
|
|
761
|
-
var isFunction = (value) => typeof value === "function";
|
|
762
|
-
var isHTMLElement = (value) => {
|
|
763
|
-
if (!isWeb) {
|
|
764
|
-
return false;
|
|
765
|
-
}
|
|
766
|
-
const owner = value ? value.ownerDocument : 0;
|
|
767
|
-
return value instanceof (owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement);
|
|
768
|
-
};
|
|
769
|
-
var isMessage = (value) => isString(value);
|
|
770
|
-
var isRadioInput = (element) => element.type === "radio";
|
|
771
|
-
var isRegex = (value) => value instanceof RegExp;
|
|
772
|
-
const defaultResult = {
|
|
773
|
-
value: false,
|
|
774
|
-
isValid: false
|
|
775
|
-
};
|
|
776
|
-
const validResult = { value: true, isValid: true };
|
|
777
|
-
var getCheckboxValue = (options) => {
|
|
778
|
-
if (Array.isArray(options)) {
|
|
779
|
-
if (options.length > 1) {
|
|
780
|
-
const values = options.filter((option) => option && option.checked && !option.disabled).map((option) => option.value);
|
|
781
|
-
return { value: values, isValid: !!values.length };
|
|
782
|
-
}
|
|
783
|
-
return options[0].checked && !options[0].disabled ? (
|
|
784
|
-
// @ts-expect-error expected to work in the browser
|
|
785
|
-
options[0].attributes && !isUndefined(options[0].attributes.value) ? isUndefined(options[0].value) || options[0].value === "" ? validResult : { value: options[0].value, isValid: true } : validResult
|
|
786
|
-
) : defaultResult;
|
|
787
|
-
}
|
|
788
|
-
return defaultResult;
|
|
789
|
-
};
|
|
790
|
-
const defaultReturn = {
|
|
791
|
-
isValid: false,
|
|
792
|
-
value: null
|
|
793
|
-
};
|
|
794
|
-
var getRadioValue = (options) => Array.isArray(options) ? options.reduce((previous, option) => option && option.checked && !option.disabled ? {
|
|
795
|
-
isValid: true,
|
|
796
|
-
value: option.value
|
|
797
|
-
} : previous, defaultReturn) : defaultReturn;
|
|
798
|
-
function getValidateError(result, ref, type = "validate") {
|
|
799
|
-
if (isMessage(result) || Array.isArray(result) && result.every(isMessage) || isBoolean(result) && !result) {
|
|
800
|
-
return {
|
|
801
|
-
type,
|
|
802
|
-
message: isMessage(result) ? result : "",
|
|
803
|
-
ref
|
|
804
|
-
};
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
var getValueAndMessage = (validationData) => isObject(validationData) && !isRegex(validationData) ? validationData : {
|
|
808
|
-
value: validationData,
|
|
809
|
-
message: ""
|
|
810
|
-
};
|
|
811
|
-
var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
|
812
|
-
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled } = field._f;
|
|
813
|
-
const inputValue = get(formValues, name);
|
|
814
|
-
if (!mount || disabled) {
|
|
815
|
-
return {};
|
|
816
|
-
}
|
|
817
|
-
const inputRef = refs ? refs[0] : ref;
|
|
818
|
-
const setCustomValidity = (message) => {
|
|
819
|
-
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
|
820
|
-
inputRef.setCustomValidity(isBoolean(message) ? "" : message || "");
|
|
821
|
-
inputRef.reportValidity();
|
|
822
|
-
}
|
|
823
|
-
};
|
|
824
|
-
const error = {};
|
|
825
|
-
const isRadio = isRadioInput(ref);
|
|
826
|
-
const isCheckBox = isCheckBoxInput(ref);
|
|
827
|
-
const isRadioOrCheckbox2 = isRadio || isCheckBox;
|
|
828
|
-
const isEmpty = (valueAsNumber || isFileInput(ref)) && isUndefined(ref.value) && isUndefined(inputValue) || isHTMLElement(ref) && ref.value === "" || inputValue === "" || Array.isArray(inputValue) && !inputValue.length;
|
|
829
|
-
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
830
|
-
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
|
831
|
-
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
832
|
-
error[name] = {
|
|
833
|
-
type: exceedMax ? maxType : minType,
|
|
834
|
-
message,
|
|
835
|
-
ref,
|
|
836
|
-
...appendErrorsCurry(exceedMax ? maxType : minType, message)
|
|
837
|
-
};
|
|
838
|
-
};
|
|
839
|
-
if (isFieldArray ? !Array.isArray(inputValue) || !inputValue.length : required && (!isRadioOrCheckbox2 && (isEmpty || isNullOrUndefined(inputValue)) || isBoolean(inputValue) && !inputValue || isCheckBox && !getCheckboxValue(refs).isValid || isRadio && !getRadioValue(refs).isValid)) {
|
|
840
|
-
const { value, message } = isMessage(required) ? { value: !!required, message: required } : getValueAndMessage(required);
|
|
841
|
-
if (value) {
|
|
842
|
-
error[name] = {
|
|
843
|
-
type: INPUT_VALIDATION_RULES.required,
|
|
844
|
-
message,
|
|
845
|
-
ref: inputRef,
|
|
846
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message)
|
|
847
|
-
};
|
|
848
|
-
if (!validateAllFieldCriteria) {
|
|
849
|
-
setCustomValidity(message);
|
|
850
|
-
return error;
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
|
855
|
-
let exceedMax;
|
|
856
|
-
let exceedMin;
|
|
857
|
-
const maxOutput = getValueAndMessage(max);
|
|
858
|
-
const minOutput = getValueAndMessage(min);
|
|
859
|
-
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
|
860
|
-
const valueNumber = ref.valueAsNumber || (inputValue ? +inputValue : inputValue);
|
|
861
|
-
if (!isNullOrUndefined(maxOutput.value)) {
|
|
862
|
-
exceedMax = valueNumber > maxOutput.value;
|
|
863
|
-
}
|
|
864
|
-
if (!isNullOrUndefined(minOutput.value)) {
|
|
865
|
-
exceedMin = valueNumber < minOutput.value;
|
|
866
|
-
}
|
|
867
|
-
} else {
|
|
868
|
-
const valueDate = ref.valueAsDate || new Date(inputValue);
|
|
869
|
-
const convertTimeToDate = (time) => /* @__PURE__ */ new Date((/* @__PURE__ */ new Date()).toDateString() + " " + time);
|
|
870
|
-
const isTime = ref.type == "time";
|
|
871
|
-
const isWeek = ref.type == "week";
|
|
872
|
-
if (isString(maxOutput.value) && inputValue) {
|
|
873
|
-
exceedMax = isTime ? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value) : isWeek ? inputValue > maxOutput.value : valueDate > new Date(maxOutput.value);
|
|
874
|
-
}
|
|
875
|
-
if (isString(minOutput.value) && inputValue) {
|
|
876
|
-
exceedMin = isTime ? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value) : isWeek ? inputValue < minOutput.value : valueDate < new Date(minOutput.value);
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
if (exceedMax || exceedMin) {
|
|
880
|
-
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
881
|
-
if (!validateAllFieldCriteria) {
|
|
882
|
-
setCustomValidity(error[name].message);
|
|
883
|
-
return error;
|
|
884
|
-
}
|
|
885
|
-
}
|
|
886
|
-
}
|
|
887
|
-
if ((maxLength || minLength) && !isEmpty && (isString(inputValue) || isFieldArray && Array.isArray(inputValue))) {
|
|
888
|
-
const maxLengthOutput = getValueAndMessage(maxLength);
|
|
889
|
-
const minLengthOutput = getValueAndMessage(minLength);
|
|
890
|
-
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) && inputValue.length > +maxLengthOutput.value;
|
|
891
|
-
const exceedMin = !isNullOrUndefined(minLengthOutput.value) && inputValue.length < +minLengthOutput.value;
|
|
892
|
-
if (exceedMax || exceedMin) {
|
|
893
|
-
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
|
894
|
-
if (!validateAllFieldCriteria) {
|
|
895
|
-
setCustomValidity(error[name].message);
|
|
896
|
-
return error;
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
if (pattern && !isEmpty && isString(inputValue)) {
|
|
901
|
-
const { value: patternValue, message } = getValueAndMessage(pattern);
|
|
902
|
-
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
|
903
|
-
error[name] = {
|
|
904
|
-
type: INPUT_VALIDATION_RULES.pattern,
|
|
905
|
-
message,
|
|
906
|
-
ref,
|
|
907
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message)
|
|
908
|
-
};
|
|
909
|
-
if (!validateAllFieldCriteria) {
|
|
910
|
-
setCustomValidity(message);
|
|
911
|
-
return error;
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
if (validate) {
|
|
916
|
-
if (isFunction(validate)) {
|
|
917
|
-
const result = await validate(inputValue, formValues);
|
|
918
|
-
const validateError = getValidateError(result, inputRef);
|
|
919
|
-
if (validateError) {
|
|
920
|
-
error[name] = {
|
|
921
|
-
...validateError,
|
|
922
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message)
|
|
923
|
-
};
|
|
924
|
-
if (!validateAllFieldCriteria) {
|
|
925
|
-
setCustomValidity(validateError.message);
|
|
926
|
-
return error;
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
} else if (isObject(validate)) {
|
|
930
|
-
let validationResult = {};
|
|
931
|
-
for (const key in validate) {
|
|
932
|
-
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
933
|
-
break;
|
|
934
|
-
}
|
|
935
|
-
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
|
936
|
-
if (validateError) {
|
|
937
|
-
validationResult = {
|
|
938
|
-
...validateError,
|
|
939
|
-
...appendErrorsCurry(key, validateError.message)
|
|
940
|
-
};
|
|
941
|
-
setCustomValidity(validateError.message);
|
|
942
|
-
if (validateAllFieldCriteria) {
|
|
943
|
-
error[name] = validationResult;
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
if (!isEmptyObject(validationResult)) {
|
|
948
|
-
error[name] = {
|
|
949
|
-
ref: inputRef,
|
|
950
|
-
...validationResult
|
|
951
|
-
};
|
|
952
|
-
if (!validateAllFieldCriteria) {
|
|
953
|
-
return error;
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
setCustomValidity(true);
|
|
959
|
-
return error;
|
|
960
|
-
};
|
|
961
|
-
var appendAt = (data, value) => [
|
|
962
|
-
...data,
|
|
963
|
-
...convertToArrayPayload(value)
|
|
964
|
-
];
|
|
965
|
-
var fillEmptyArray = (value) => Array.isArray(value) ? value.map(() => void 0) : void 0;
|
|
966
|
-
function insert(data, index, value) {
|
|
967
|
-
return [
|
|
968
|
-
...data.slice(0, index),
|
|
969
|
-
...convertToArrayPayload(value),
|
|
970
|
-
...data.slice(index)
|
|
971
|
-
];
|
|
972
|
-
}
|
|
973
|
-
var moveArrayAt = (data, from, to) => {
|
|
974
|
-
if (!Array.isArray(data)) {
|
|
975
|
-
return [];
|
|
976
|
-
}
|
|
977
|
-
if (isUndefined(data[to])) {
|
|
978
|
-
data[to] = void 0;
|
|
979
|
-
}
|
|
980
|
-
data.splice(to, 0, data.splice(from, 1)[0]);
|
|
981
|
-
return data;
|
|
982
|
-
};
|
|
983
|
-
var prependAt = (data, value) => [
|
|
984
|
-
...convertToArrayPayload(value),
|
|
985
|
-
...convertToArrayPayload(data)
|
|
986
|
-
];
|
|
987
|
-
function removeAtIndexes(data, indexes) {
|
|
988
|
-
let i2 = 0;
|
|
989
|
-
const temp = [...data];
|
|
990
|
-
for (const index of indexes) {
|
|
991
|
-
temp.splice(index - i2, 1);
|
|
992
|
-
i2++;
|
|
993
|
-
}
|
|
994
|
-
return compact(temp).length ? temp : [];
|
|
995
|
-
}
|
|
996
|
-
var removeArrayAt = (data, index) => isUndefined(index) ? [] : removeAtIndexes(data, convertToArrayPayload(index).sort((a2, b) => a2 - b));
|
|
997
|
-
var swapArrayAt = (data, indexA, indexB) => {
|
|
998
|
-
[data[indexA], data[indexB]] = [data[indexB], data[indexA]];
|
|
999
|
-
};
|
|
1000
|
-
function baseGet(object, updatePath) {
|
|
1001
|
-
const length = updatePath.slice(0, -1).length;
|
|
1002
|
-
let index = 0;
|
|
1003
|
-
while (index < length) {
|
|
1004
|
-
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
1005
|
-
}
|
|
1006
|
-
return object;
|
|
1007
|
-
}
|
|
1008
|
-
function isEmptyArray(obj) {
|
|
1009
|
-
for (const key in obj) {
|
|
1010
|
-
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
1011
|
-
return false;
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
return true;
|
|
1015
|
-
}
|
|
1016
|
-
function unset(object, path) {
|
|
1017
|
-
const paths = Array.isArray(path) ? path : isKey(path) ? [path] : stringToPath(path);
|
|
1018
|
-
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
1019
|
-
const index = paths.length - 1;
|
|
1020
|
-
const key = paths[index];
|
|
1021
|
-
if (childObject) {
|
|
1022
|
-
delete childObject[key];
|
|
1023
|
-
}
|
|
1024
|
-
if (index !== 0 && (isObject(childObject) && isEmptyObject(childObject) || Array.isArray(childObject) && isEmptyArray(childObject))) {
|
|
1025
|
-
unset(object, paths.slice(0, -1));
|
|
1026
|
-
}
|
|
1027
|
-
return object;
|
|
1028
|
-
}
|
|
1029
|
-
var updateAt = (fieldValues, index, value) => {
|
|
1030
|
-
fieldValues[index] = value;
|
|
1031
|
-
return fieldValues;
|
|
1032
|
-
};
|
|
1033
|
-
function useFieldArray(props) {
|
|
1034
|
-
const methods = useFormContext();
|
|
1035
|
-
const { control = methods.control, name, keyName = "id", shouldUnregister } = props;
|
|
1036
|
-
const [fields, setFields] = React__default.default.useState(control._getFieldArray(name));
|
|
1037
|
-
const ids = React__default.default.useRef(control._getFieldArray(name).map(generateId));
|
|
1038
|
-
const _fieldIds = React__default.default.useRef(fields);
|
|
1039
|
-
const _name = React__default.default.useRef(name);
|
|
1040
|
-
const _actioned = React__default.default.useRef(false);
|
|
1041
|
-
_name.current = name;
|
|
1042
|
-
_fieldIds.current = fields;
|
|
1043
|
-
control._names.array.add(name);
|
|
1044
|
-
props.rules && control.register(name, props.rules);
|
|
1045
|
-
useSubscribe({
|
|
1046
|
-
next: ({ values, name: fieldArrayName }) => {
|
|
1047
|
-
if (fieldArrayName === _name.current || !fieldArrayName) {
|
|
1048
|
-
const fieldValues = get(values, _name.current);
|
|
1049
|
-
if (Array.isArray(fieldValues)) {
|
|
1050
|
-
setFields(fieldValues);
|
|
1051
|
-
ids.current = fieldValues.map(generateId);
|
|
1052
|
-
}
|
|
1053
|
-
}
|
|
1054
|
-
},
|
|
1055
|
-
subject: control._subjects.array
|
|
1056
|
-
});
|
|
1057
|
-
const updateValues = React__default.default.useCallback((updatedFieldArrayValues) => {
|
|
1058
|
-
_actioned.current = true;
|
|
1059
|
-
control._updateFieldArray(name, updatedFieldArrayValues);
|
|
1060
|
-
}, [control, name]);
|
|
1061
|
-
const append = (value, options) => {
|
|
1062
|
-
const appendValue = convertToArrayPayload(cloneObject(value));
|
|
1063
|
-
const updatedFieldArrayValues = appendAt(control._getFieldArray(name), appendValue);
|
|
1064
|
-
control._names.focus = getFocusFieldName(name, updatedFieldArrayValues.length - 1, options);
|
|
1065
|
-
ids.current = appendAt(ids.current, appendValue.map(generateId));
|
|
1066
|
-
updateValues(updatedFieldArrayValues);
|
|
1067
|
-
setFields(updatedFieldArrayValues);
|
|
1068
|
-
control._updateFieldArray(name, updatedFieldArrayValues, appendAt, {
|
|
1069
|
-
argA: fillEmptyArray(value)
|
|
1070
|
-
});
|
|
1071
|
-
};
|
|
1072
|
-
const prepend = (value, options) => {
|
|
1073
|
-
const prependValue = convertToArrayPayload(cloneObject(value));
|
|
1074
|
-
const updatedFieldArrayValues = prependAt(control._getFieldArray(name), prependValue);
|
|
1075
|
-
control._names.focus = getFocusFieldName(name, 0, options);
|
|
1076
|
-
ids.current = prependAt(ids.current, prependValue.map(generateId));
|
|
1077
|
-
updateValues(updatedFieldArrayValues);
|
|
1078
|
-
setFields(updatedFieldArrayValues);
|
|
1079
|
-
control._updateFieldArray(name, updatedFieldArrayValues, prependAt, {
|
|
1080
|
-
argA: fillEmptyArray(value)
|
|
1081
|
-
});
|
|
1082
|
-
};
|
|
1083
|
-
const remove = (index) => {
|
|
1084
|
-
const updatedFieldArrayValues = removeArrayAt(control._getFieldArray(name), index);
|
|
1085
|
-
ids.current = removeArrayAt(ids.current, index);
|
|
1086
|
-
updateValues(updatedFieldArrayValues);
|
|
1087
|
-
setFields(updatedFieldArrayValues);
|
|
1088
|
-
control._updateFieldArray(name, updatedFieldArrayValues, removeArrayAt, {
|
|
1089
|
-
argA: index
|
|
1090
|
-
});
|
|
1091
|
-
};
|
|
1092
|
-
const insert$1 = (index, value, options) => {
|
|
1093
|
-
const insertValue = convertToArrayPayload(cloneObject(value));
|
|
1094
|
-
const updatedFieldArrayValues = insert(control._getFieldArray(name), index, insertValue);
|
|
1095
|
-
control._names.focus = getFocusFieldName(name, index, options);
|
|
1096
|
-
ids.current = insert(ids.current, index, insertValue.map(generateId));
|
|
1097
|
-
updateValues(updatedFieldArrayValues);
|
|
1098
|
-
setFields(updatedFieldArrayValues);
|
|
1099
|
-
control._updateFieldArray(name, updatedFieldArrayValues, insert, {
|
|
1100
|
-
argA: index,
|
|
1101
|
-
argB: fillEmptyArray(value)
|
|
1102
|
-
});
|
|
1103
|
-
};
|
|
1104
|
-
const swap = (indexA, indexB) => {
|
|
1105
|
-
const updatedFieldArrayValues = control._getFieldArray(name);
|
|
1106
|
-
swapArrayAt(updatedFieldArrayValues, indexA, indexB);
|
|
1107
|
-
swapArrayAt(ids.current, indexA, indexB);
|
|
1108
|
-
updateValues(updatedFieldArrayValues);
|
|
1109
|
-
setFields(updatedFieldArrayValues);
|
|
1110
|
-
control._updateFieldArray(name, updatedFieldArrayValues, swapArrayAt, {
|
|
1111
|
-
argA: indexA,
|
|
1112
|
-
argB: indexB
|
|
1113
|
-
}, false);
|
|
1114
|
-
};
|
|
1115
|
-
const move = (from, to) => {
|
|
1116
|
-
const updatedFieldArrayValues = control._getFieldArray(name);
|
|
1117
|
-
moveArrayAt(updatedFieldArrayValues, from, to);
|
|
1118
|
-
moveArrayAt(ids.current, from, to);
|
|
1119
|
-
updateValues(updatedFieldArrayValues);
|
|
1120
|
-
setFields(updatedFieldArrayValues);
|
|
1121
|
-
control._updateFieldArray(name, updatedFieldArrayValues, moveArrayAt, {
|
|
1122
|
-
argA: from,
|
|
1123
|
-
argB: to
|
|
1124
|
-
}, false);
|
|
1125
|
-
};
|
|
1126
|
-
const update = (index, value) => {
|
|
1127
|
-
const updateValue = cloneObject(value);
|
|
1128
|
-
const updatedFieldArrayValues = updateAt(control._getFieldArray(name), index, updateValue);
|
|
1129
|
-
ids.current = [...updatedFieldArrayValues].map((item, i2) => !item || i2 === index ? generateId() : ids.current[i2]);
|
|
1130
|
-
updateValues(updatedFieldArrayValues);
|
|
1131
|
-
setFields([...updatedFieldArrayValues]);
|
|
1132
|
-
control._updateFieldArray(name, updatedFieldArrayValues, updateAt, {
|
|
1133
|
-
argA: index,
|
|
1134
|
-
argB: updateValue
|
|
1135
|
-
}, true, false);
|
|
1136
|
-
};
|
|
1137
|
-
const replace = (value) => {
|
|
1138
|
-
const updatedFieldArrayValues = convertToArrayPayload(cloneObject(value));
|
|
1139
|
-
ids.current = updatedFieldArrayValues.map(generateId);
|
|
1140
|
-
updateValues([...updatedFieldArrayValues]);
|
|
1141
|
-
setFields([...updatedFieldArrayValues]);
|
|
1142
|
-
control._updateFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
|
|
1143
|
-
};
|
|
1144
|
-
React__default.default.useEffect(() => {
|
|
1145
|
-
control._state.action = false;
|
|
1146
|
-
isWatched(name, control._names) && control._subjects.state.next({
|
|
1147
|
-
...control._formState
|
|
1148
|
-
});
|
|
1149
|
-
if (_actioned.current && (!getValidationModes(control._options.mode).isOnSubmit || control._formState.isSubmitted)) {
|
|
1150
|
-
if (control._options.resolver) {
|
|
1151
|
-
control._executeSchema([name]).then((result) => {
|
|
1152
|
-
const error = get(result.errors, name);
|
|
1153
|
-
const existingError = get(control._formState.errors, name);
|
|
1154
|
-
if (existingError ? !error && existingError.type || error && (existingError.type !== error.type || existingError.message !== error.message) : error && error.type) {
|
|
1155
|
-
error ? set(control._formState.errors, name, error) : unset(control._formState.errors, name);
|
|
1156
|
-
control._subjects.state.next({
|
|
1157
|
-
errors: control._formState.errors
|
|
1158
|
-
});
|
|
1159
|
-
}
|
|
1160
|
-
});
|
|
1161
|
-
} else {
|
|
1162
|
-
const field = get(control._fields, name);
|
|
1163
|
-
if (field && field._f) {
|
|
1164
|
-
validateField(field, control._formValues, control._options.criteriaMode === VALIDATION_MODE.all, control._options.shouldUseNativeValidation, true).then((error) => !isEmptyObject(error) && control._subjects.state.next({
|
|
1165
|
-
errors: updateFieldArrayRootError(control._formState.errors, error, name)
|
|
1166
|
-
}));
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
control._subjects.values.next({
|
|
1171
|
-
name,
|
|
1172
|
-
values: { ...control._formValues }
|
|
1173
|
-
});
|
|
1174
|
-
control._names.focus && iterateFieldsByAction(control._fields, (ref, key) => {
|
|
1175
|
-
if (control._names.focus && key.startsWith(control._names.focus) && ref.focus) {
|
|
1176
|
-
ref.focus();
|
|
1177
|
-
return 1;
|
|
1178
|
-
}
|
|
1179
|
-
return;
|
|
1180
|
-
});
|
|
1181
|
-
control._names.focus = "";
|
|
1182
|
-
control._updateValid();
|
|
1183
|
-
_actioned.current = false;
|
|
1184
|
-
}, [fields, name, control]);
|
|
1185
|
-
React__default.default.useEffect(() => {
|
|
1186
|
-
!get(control._formValues, name) && control._updateFieldArray(name);
|
|
1187
|
-
return () => {
|
|
1188
|
-
(control._options.shouldUnregister || shouldUnregister) && control.unregister(name);
|
|
1189
|
-
};
|
|
1190
|
-
}, [name, control, keyName, shouldUnregister]);
|
|
1191
|
-
return {
|
|
1192
|
-
swap: React__default.default.useCallback(swap, [updateValues, name, control]),
|
|
1193
|
-
move: React__default.default.useCallback(move, [updateValues, name, control]),
|
|
1194
|
-
prepend: React__default.default.useCallback(prepend, [updateValues, name, control]),
|
|
1195
|
-
append: React__default.default.useCallback(append, [updateValues, name, control]),
|
|
1196
|
-
remove: React__default.default.useCallback(remove, [updateValues, name, control]),
|
|
1197
|
-
insert: React__default.default.useCallback(insert$1, [updateValues, name, control]),
|
|
1198
|
-
update: React__default.default.useCallback(update, [updateValues, name, control]),
|
|
1199
|
-
replace: React__default.default.useCallback(replace, [updateValues, name, control]),
|
|
1200
|
-
fields: React__default.default.useMemo(() => fields.map((field, index) => ({
|
|
1201
|
-
...field,
|
|
1202
|
-
[keyName]: ids.current[index] || generateId()
|
|
1203
|
-
})), [fields, keyName])
|
|
1204
|
-
};
|
|
1205
|
-
}
|
|
1206
|
-
var createSubject = () => {
|
|
1207
|
-
let _observers = [];
|
|
1208
|
-
const next = (value) => {
|
|
1209
|
-
for (const observer of _observers) {
|
|
1210
|
-
observer.next && observer.next(value);
|
|
1211
|
-
}
|
|
1212
|
-
};
|
|
1213
|
-
const subscribe = (observer) => {
|
|
1214
|
-
_observers.push(observer);
|
|
1215
|
-
return {
|
|
1216
|
-
unsubscribe: () => {
|
|
1217
|
-
_observers = _observers.filter((o2) => o2 !== observer);
|
|
1218
|
-
}
|
|
1219
|
-
};
|
|
1220
|
-
};
|
|
1221
|
-
const unsubscribe = () => {
|
|
1222
|
-
_observers = [];
|
|
1223
|
-
};
|
|
1224
|
-
return {
|
|
1225
|
-
get observers() {
|
|
1226
|
-
return _observers;
|
|
1227
|
-
},
|
|
1228
|
-
next,
|
|
1229
|
-
subscribe,
|
|
1230
|
-
unsubscribe
|
|
1231
|
-
};
|
|
1232
|
-
};
|
|
1233
|
-
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
1234
|
-
function deepEqual(object1, object2) {
|
|
1235
|
-
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
1236
|
-
return object1 === object2;
|
|
1237
|
-
}
|
|
1238
|
-
if (isDateObject(object1) && isDateObject(object2)) {
|
|
1239
|
-
return object1.getTime() === object2.getTime();
|
|
1240
|
-
}
|
|
1241
|
-
const keys1 = Object.keys(object1);
|
|
1242
|
-
const keys2 = Object.keys(object2);
|
|
1243
|
-
if (keys1.length !== keys2.length) {
|
|
1244
|
-
return false;
|
|
1245
|
-
}
|
|
1246
|
-
for (const key of keys1) {
|
|
1247
|
-
const val1 = object1[key];
|
|
1248
|
-
if (!keys2.includes(key)) {
|
|
1249
|
-
return false;
|
|
1250
|
-
}
|
|
1251
|
-
if (key !== "ref") {
|
|
1252
|
-
const val2 = object2[key];
|
|
1253
|
-
if (isDateObject(val1) && isDateObject(val2) || isObject(val1) && isObject(val2) || Array.isArray(val1) && Array.isArray(val2) ? !deepEqual(val1, val2) : val1 !== val2) {
|
|
1254
|
-
return false;
|
|
1255
|
-
}
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1258
|
-
return true;
|
|
1259
|
-
}
|
|
1260
|
-
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
|
1261
|
-
var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
|
1262
|
-
var live = (ref) => isHTMLElement(ref) && ref.isConnected;
|
|
1263
|
-
var objectHasFunction = (data) => {
|
|
1264
|
-
for (const key in data) {
|
|
1265
|
-
if (isFunction(data[key])) {
|
|
1266
|
-
return true;
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
return false;
|
|
1270
|
-
};
|
|
1271
|
-
function markFieldsDirty(data, fields = {}) {
|
|
1272
|
-
const isParentNodeArray = Array.isArray(data);
|
|
1273
|
-
if (isObject(data) || isParentNodeArray) {
|
|
1274
|
-
for (const key in data) {
|
|
1275
|
-
if (Array.isArray(data[key]) || isObject(data[key]) && !objectHasFunction(data[key])) {
|
|
1276
|
-
fields[key] = Array.isArray(data[key]) ? [] : {};
|
|
1277
|
-
markFieldsDirty(data[key], fields[key]);
|
|
1278
|
-
} else if (!isNullOrUndefined(data[key])) {
|
|
1279
|
-
fields[key] = true;
|
|
1280
|
-
}
|
|
1281
|
-
}
|
|
1282
|
-
}
|
|
1283
|
-
return fields;
|
|
1284
|
-
}
|
|
1285
|
-
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
|
1286
|
-
const isParentNodeArray = Array.isArray(data);
|
|
1287
|
-
if (isObject(data) || isParentNodeArray) {
|
|
1288
|
-
for (const key in data) {
|
|
1289
|
-
if (Array.isArray(data[key]) || isObject(data[key]) && !objectHasFunction(data[key])) {
|
|
1290
|
-
if (isUndefined(formValues) || isPrimitive(dirtyFieldsFromValues[key])) {
|
|
1291
|
-
dirtyFieldsFromValues[key] = Array.isArray(data[key]) ? markFieldsDirty(data[key], []) : { ...markFieldsDirty(data[key]) };
|
|
1292
|
-
} else {
|
|
1293
|
-
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
|
1294
|
-
}
|
|
1295
|
-
} else {
|
|
1296
|
-
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
|
1297
|
-
}
|
|
1298
|
-
}
|
|
1299
|
-
}
|
|
1300
|
-
return dirtyFieldsFromValues;
|
|
1301
|
-
}
|
|
1302
|
-
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
|
1303
|
-
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value) ? value : valueAsNumber ? value === "" ? NaN : value ? +value : value : valueAsDate && isString(value) ? new Date(value) : setValueAs ? setValueAs(value) : value;
|
|
1304
|
-
function getFieldValue(_f) {
|
|
1305
|
-
const ref = _f.ref;
|
|
1306
|
-
if (_f.refs ? _f.refs.every((ref2) => ref2.disabled) : ref.disabled) {
|
|
1307
|
-
return;
|
|
1308
|
-
}
|
|
1309
|
-
if (isFileInput(ref)) {
|
|
1310
|
-
return ref.files;
|
|
1311
|
-
}
|
|
1312
|
-
if (isRadioInput(ref)) {
|
|
1313
|
-
return getRadioValue(_f.refs).value;
|
|
1314
|
-
}
|
|
1315
|
-
if (isMultipleSelect(ref)) {
|
|
1316
|
-
return [...ref.selectedOptions].map(({ value }) => value);
|
|
1317
|
-
}
|
|
1318
|
-
if (isCheckBoxInput(ref)) {
|
|
1319
|
-
return getCheckboxValue(_f.refs).value;
|
|
1320
|
-
}
|
|
1321
|
-
return getFieldValueAs(isUndefined(ref.value) ? _f.ref.value : ref.value, _f);
|
|
1322
|
-
}
|
|
1323
|
-
var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeValidation) => {
|
|
1324
|
-
const fields = {};
|
|
1325
|
-
for (const name of fieldsNames) {
|
|
1326
|
-
const field = get(_fields, name);
|
|
1327
|
-
field && set(fields, name, field._f);
|
|
1328
|
-
}
|
|
1329
|
-
return {
|
|
1330
|
-
criteriaMode,
|
|
1331
|
-
names: [...fieldsNames],
|
|
1332
|
-
fields,
|
|
1333
|
-
shouldUseNativeValidation
|
|
1334
|
-
};
|
|
1335
|
-
};
|
|
1336
|
-
var getRuleValue = (rule) => isUndefined(rule) ? rule : isRegex(rule) ? rule.source : isObject(rule) ? isRegex(rule.value) ? rule.value.source : rule.value : rule;
|
|
1337
|
-
var hasValidation = (options) => options.mount && (options.required || options.min || options.max || options.maxLength || options.minLength || options.pattern || options.validate);
|
|
1338
|
-
function schemaErrorLookup(errors, _fields, name) {
|
|
1339
|
-
const error = get(errors, name);
|
|
1340
|
-
if (error || isKey(name)) {
|
|
1341
|
-
return {
|
|
1342
|
-
error,
|
|
1343
|
-
name
|
|
1344
|
-
};
|
|
1345
|
-
}
|
|
1346
|
-
const names = name.split(".");
|
|
1347
|
-
while (names.length) {
|
|
1348
|
-
const fieldName = names.join(".");
|
|
1349
|
-
const field = get(_fields, fieldName);
|
|
1350
|
-
const foundError = get(errors, fieldName);
|
|
1351
|
-
if (field && !Array.isArray(field) && name !== fieldName) {
|
|
1352
|
-
return { name };
|
|
1353
|
-
}
|
|
1354
|
-
if (foundError && foundError.type) {
|
|
1355
|
-
return {
|
|
1356
|
-
name: fieldName,
|
|
1357
|
-
error: foundError
|
|
1358
|
-
};
|
|
1359
|
-
}
|
|
1360
|
-
names.pop();
|
|
1361
|
-
}
|
|
1362
|
-
return {
|
|
1363
|
-
name
|
|
1364
|
-
};
|
|
1365
|
-
}
|
|
1366
|
-
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
|
1367
|
-
if (mode.isOnAll) {
|
|
1368
|
-
return false;
|
|
1369
|
-
} else if (!isSubmitted && mode.isOnTouch) {
|
|
1370
|
-
return !(isTouched || isBlurEvent);
|
|
1371
|
-
} else if (isSubmitted ? reValidateMode.isOnBlur : mode.isOnBlur) {
|
|
1372
|
-
return !isBlurEvent;
|
|
1373
|
-
} else if (isSubmitted ? reValidateMode.isOnChange : mode.isOnChange) {
|
|
1374
|
-
return isBlurEvent;
|
|
1375
|
-
}
|
|
1376
|
-
return true;
|
|
1377
|
-
};
|
|
1378
|
-
var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
|
|
1379
|
-
const defaultOptions = {
|
|
1380
|
-
mode: VALIDATION_MODE.onSubmit,
|
|
1381
|
-
reValidateMode: VALIDATION_MODE.onChange,
|
|
1382
|
-
shouldFocusError: true
|
|
1383
|
-
};
|
|
1384
|
-
function createFormControl(props = {}, flushRootRender) {
|
|
1385
|
-
let _options = {
|
|
1386
|
-
...defaultOptions,
|
|
1387
|
-
...props
|
|
1388
|
-
};
|
|
1389
|
-
let _formState = {
|
|
1390
|
-
submitCount: 0,
|
|
1391
|
-
isDirty: false,
|
|
1392
|
-
isLoading: isFunction(_options.defaultValues),
|
|
1393
|
-
isValidating: false,
|
|
1394
|
-
isSubmitted: false,
|
|
1395
|
-
isSubmitting: false,
|
|
1396
|
-
isSubmitSuccessful: false,
|
|
1397
|
-
isValid: false,
|
|
1398
|
-
touchedFields: {},
|
|
1399
|
-
dirtyFields: {},
|
|
1400
|
-
errors: _options.errors || {},
|
|
1401
|
-
disabled: false
|
|
1402
|
-
};
|
|
1403
|
-
let _fields = {};
|
|
1404
|
-
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values) ? cloneObject(_options.defaultValues || _options.values) || {} : {};
|
|
1405
|
-
let _formValues = _options.shouldUnregister ? {} : cloneObject(_defaultValues);
|
|
1406
|
-
let _state = {
|
|
1407
|
-
action: false,
|
|
1408
|
-
mount: false,
|
|
1409
|
-
watch: false
|
|
1410
|
-
};
|
|
1411
|
-
let _names = {
|
|
1412
|
-
mount: /* @__PURE__ */ new Set(),
|
|
1413
|
-
unMount: /* @__PURE__ */ new Set(),
|
|
1414
|
-
array: /* @__PURE__ */ new Set(),
|
|
1415
|
-
watch: /* @__PURE__ */ new Set()
|
|
1416
|
-
};
|
|
1417
|
-
let delayErrorCallback;
|
|
1418
|
-
let timer = 0;
|
|
1419
|
-
const _proxyFormState = {
|
|
1420
|
-
isDirty: false,
|
|
1421
|
-
dirtyFields: false,
|
|
1422
|
-
touchedFields: false,
|
|
1423
|
-
isValidating: false,
|
|
1424
|
-
isValid: false,
|
|
1425
|
-
errors: false
|
|
1426
|
-
};
|
|
1427
|
-
const _subjects = {
|
|
1428
|
-
values: createSubject(),
|
|
1429
|
-
array: createSubject(),
|
|
1430
|
-
state: createSubject()
|
|
1431
|
-
};
|
|
1432
|
-
const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
|
|
1433
|
-
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
|
1434
|
-
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
|
1435
|
-
const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
|
|
1436
|
-
const debounce = (callback) => (wait) => {
|
|
1437
|
-
clearTimeout(timer);
|
|
1438
|
-
timer = setTimeout(callback, wait);
|
|
1439
|
-
};
|
|
1440
|
-
const _updateValid = async (shouldUpdateValid) => {
|
|
1441
|
-
if (_proxyFormState.isValid || shouldUpdateValid) {
|
|
1442
|
-
const isValid = _options.resolver ? isEmptyObject((await _executeSchema()).errors) : await executeBuiltInValidation(_fields, true);
|
|
1443
|
-
if (isValid !== _formState.isValid) {
|
|
1444
|
-
_subjects.state.next({
|
|
1445
|
-
isValid
|
|
1446
|
-
});
|
|
1447
|
-
}
|
|
1448
|
-
}
|
|
1449
|
-
};
|
|
1450
|
-
const _updateIsValidating = (value) => _proxyFormState.isValidating && _subjects.state.next({
|
|
1451
|
-
isValidating: value
|
|
1452
|
-
});
|
|
1453
|
-
const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
|
1454
|
-
if (args && method) {
|
|
1455
|
-
_state.action = true;
|
|
1456
|
-
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
|
1457
|
-
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
|
1458
|
-
shouldSetValues && set(_fields, name, fieldValues);
|
|
1459
|
-
}
|
|
1460
|
-
if (shouldUpdateFieldsAndState && Array.isArray(get(_formState.errors, name))) {
|
|
1461
|
-
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
|
1462
|
-
shouldSetValues && set(_formState.errors, name, errors);
|
|
1463
|
-
unsetEmptyArray(_formState.errors, name);
|
|
1464
|
-
}
|
|
1465
|
-
if (_proxyFormState.touchedFields && shouldUpdateFieldsAndState && Array.isArray(get(_formState.touchedFields, name))) {
|
|
1466
|
-
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
|
1467
|
-
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
|
1468
|
-
}
|
|
1469
|
-
if (_proxyFormState.dirtyFields) {
|
|
1470
|
-
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
|
1471
|
-
}
|
|
1472
|
-
_subjects.state.next({
|
|
1473
|
-
name,
|
|
1474
|
-
isDirty: _getDirty(name, values),
|
|
1475
|
-
dirtyFields: _formState.dirtyFields,
|
|
1476
|
-
errors: _formState.errors,
|
|
1477
|
-
isValid: _formState.isValid
|
|
1478
|
-
});
|
|
1479
|
-
} else {
|
|
1480
|
-
set(_formValues, name, values);
|
|
1481
|
-
}
|
|
1482
|
-
};
|
|
1483
|
-
const updateErrors = (name, error) => {
|
|
1484
|
-
set(_formState.errors, name, error);
|
|
1485
|
-
_subjects.state.next({
|
|
1486
|
-
errors: _formState.errors
|
|
1487
|
-
});
|
|
1488
|
-
};
|
|
1489
|
-
const _setErrors = (errors) => {
|
|
1490
|
-
_formState.errors = errors;
|
|
1491
|
-
_subjects.state.next({
|
|
1492
|
-
errors: _formState.errors,
|
|
1493
|
-
isValid: false
|
|
1494
|
-
});
|
|
1495
|
-
};
|
|
1496
|
-
const updateValidAndValue = (name, shouldSkipSetValueAs, value, ref) => {
|
|
1497
|
-
const field = get(_fields, name);
|
|
1498
|
-
if (field) {
|
|
1499
|
-
const defaultValue = get(_formValues, name, isUndefined(value) ? get(_defaultValues, name) : value);
|
|
1500
|
-
isUndefined(defaultValue) || ref && ref.defaultChecked || shouldSkipSetValueAs ? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f)) : setFieldValue(name, defaultValue);
|
|
1501
|
-
_state.mount && _updateValid();
|
|
1502
|
-
}
|
|
1503
|
-
};
|
|
1504
|
-
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
|
1505
|
-
let shouldUpdateField = false;
|
|
1506
|
-
let isPreviousDirty = false;
|
|
1507
|
-
const output = {
|
|
1508
|
-
name
|
|
1509
|
-
};
|
|
1510
|
-
const disabledField = get(_fields, name) && get(_fields, name)._f.disabled;
|
|
1511
|
-
if (!isBlurEvent || shouldDirty) {
|
|
1512
|
-
if (_proxyFormState.isDirty) {
|
|
1513
|
-
isPreviousDirty = _formState.isDirty;
|
|
1514
|
-
_formState.isDirty = output.isDirty = _getDirty();
|
|
1515
|
-
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
|
1516
|
-
}
|
|
1517
|
-
const isCurrentFieldPristine = disabledField || deepEqual(get(_defaultValues, name), fieldValue);
|
|
1518
|
-
isPreviousDirty = !disabledField && get(_formState.dirtyFields, name);
|
|
1519
|
-
isCurrentFieldPristine || disabledField ? unset(_formState.dirtyFields, name) : set(_formState.dirtyFields, name, true);
|
|
1520
|
-
output.dirtyFields = _formState.dirtyFields;
|
|
1521
|
-
shouldUpdateField = shouldUpdateField || _proxyFormState.dirtyFields && isPreviousDirty !== !isCurrentFieldPristine;
|
|
1522
|
-
}
|
|
1523
|
-
if (isBlurEvent) {
|
|
1524
|
-
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
|
1525
|
-
if (!isPreviousFieldTouched) {
|
|
1526
|
-
set(_formState.touchedFields, name, isBlurEvent);
|
|
1527
|
-
output.touchedFields = _formState.touchedFields;
|
|
1528
|
-
shouldUpdateField = shouldUpdateField || _proxyFormState.touchedFields && isPreviousFieldTouched !== isBlurEvent;
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
|
1532
|
-
return shouldUpdateField ? output : {};
|
|
1533
|
-
};
|
|
1534
|
-
const shouldRenderByError = (name, isValid, error, fieldState) => {
|
|
1535
|
-
const previousFieldError = get(_formState.errors, name);
|
|
1536
|
-
const shouldUpdateValid = _proxyFormState.isValid && isBoolean(isValid) && _formState.isValid !== isValid;
|
|
1537
|
-
if (props.delayError && error) {
|
|
1538
|
-
delayErrorCallback = debounce(() => updateErrors(name, error));
|
|
1539
|
-
delayErrorCallback(props.delayError);
|
|
1540
|
-
} else {
|
|
1541
|
-
clearTimeout(timer);
|
|
1542
|
-
delayErrorCallback = null;
|
|
1543
|
-
error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
|
|
1544
|
-
}
|
|
1545
|
-
if ((error ? !deepEqual(previousFieldError, error) : previousFieldError) || !isEmptyObject(fieldState) || shouldUpdateValid) {
|
|
1546
|
-
const updatedFormState = {
|
|
1547
|
-
...fieldState,
|
|
1548
|
-
...shouldUpdateValid && isBoolean(isValid) ? { isValid } : {},
|
|
1549
|
-
errors: _formState.errors,
|
|
1550
|
-
name
|
|
1551
|
-
};
|
|
1552
|
-
_formState = {
|
|
1553
|
-
..._formState,
|
|
1554
|
-
...updatedFormState
|
|
1555
|
-
};
|
|
1556
|
-
_subjects.state.next(updatedFormState);
|
|
1557
|
-
}
|
|
1558
|
-
_updateIsValidating(false);
|
|
1559
|
-
};
|
|
1560
|
-
const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
|
1561
|
-
const executeSchemaAndUpdateState = async (names) => {
|
|
1562
|
-
const { errors } = await _executeSchema(names);
|
|
1563
|
-
if (names) {
|
|
1564
|
-
for (const name of names) {
|
|
1565
|
-
const error = get(errors, name);
|
|
1566
|
-
error ? set(_formState.errors, name, error) : unset(_formState.errors, name);
|
|
1567
|
-
}
|
|
1568
|
-
} else {
|
|
1569
|
-
_formState.errors = errors;
|
|
1570
|
-
}
|
|
1571
|
-
return errors;
|
|
1572
|
-
};
|
|
1573
|
-
const executeBuiltInValidation = async (fields, shouldOnlyCheckValid, context = {
|
|
1574
|
-
valid: true
|
|
1575
|
-
}) => {
|
|
1576
|
-
for (const name in fields) {
|
|
1577
|
-
const field = fields[name];
|
|
1578
|
-
if (field) {
|
|
1579
|
-
const { _f, ...fieldValue } = field;
|
|
1580
|
-
if (_f) {
|
|
1581
|
-
const isFieldArrayRoot = _names.array.has(_f.name);
|
|
1582
|
-
const fieldError = await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
|
1583
|
-
if (fieldError[_f.name]) {
|
|
1584
|
-
context.valid = false;
|
|
1585
|
-
if (shouldOnlyCheckValid) {
|
|
1586
|
-
break;
|
|
1587
|
-
}
|
|
1588
|
-
}
|
|
1589
|
-
!shouldOnlyCheckValid && (get(fieldError, _f.name) ? isFieldArrayRoot ? updateFieldArrayRootError(_formState.errors, fieldError, _f.name) : set(_formState.errors, _f.name, fieldError[_f.name]) : unset(_formState.errors, _f.name));
|
|
1590
|
-
}
|
|
1591
|
-
fieldValue && await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context);
|
|
1592
|
-
}
|
|
1593
|
-
}
|
|
1594
|
-
return context.valid;
|
|
1595
|
-
};
|
|
1596
|
-
const _removeUnmounted = () => {
|
|
1597
|
-
for (const name of _names.unMount) {
|
|
1598
|
-
const field = get(_fields, name);
|
|
1599
|
-
field && (field._f.refs ? field._f.refs.every((ref) => !live(ref)) : !live(field._f.ref)) && unregister(name);
|
|
1600
|
-
}
|
|
1601
|
-
_names.unMount = /* @__PURE__ */ new Set();
|
|
1602
|
-
};
|
|
1603
|
-
const _getDirty = (name, data) => (name && data && set(_formValues, name, data), !deepEqual(getValues(), _defaultValues));
|
|
1604
|
-
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
|
1605
|
-
..._state.mount ? _formValues : isUndefined(defaultValue) ? _defaultValues : isString(names) ? { [names]: defaultValue } : defaultValue
|
|
1606
|
-
}, isGlobal, defaultValue);
|
|
1607
|
-
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
|
|
1608
|
-
const setFieldValue = (name, value, options = {}) => {
|
|
1609
|
-
const field = get(_fields, name);
|
|
1610
|
-
let fieldValue = value;
|
|
1611
|
-
if (field) {
|
|
1612
|
-
const fieldReference = field._f;
|
|
1613
|
-
if (fieldReference) {
|
|
1614
|
-
!fieldReference.disabled && set(_formValues, name, getFieldValueAs(value, fieldReference));
|
|
1615
|
-
fieldValue = isHTMLElement(fieldReference.ref) && isNullOrUndefined(value) ? "" : value;
|
|
1616
|
-
if (isMultipleSelect(fieldReference.ref)) {
|
|
1617
|
-
[...fieldReference.ref.options].forEach((optionRef) => optionRef.selected = fieldValue.includes(optionRef.value));
|
|
1618
|
-
} else if (fieldReference.refs) {
|
|
1619
|
-
if (isCheckBoxInput(fieldReference.ref)) {
|
|
1620
|
-
fieldReference.refs.length > 1 ? fieldReference.refs.forEach((checkboxRef) => (!checkboxRef.defaultChecked || !checkboxRef.disabled) && (checkboxRef.checked = Array.isArray(fieldValue) ? !!fieldValue.find((data) => data === checkboxRef.value) : fieldValue === checkboxRef.value)) : fieldReference.refs[0] && (fieldReference.refs[0].checked = !!fieldValue);
|
|
1621
|
-
} else {
|
|
1622
|
-
fieldReference.refs.forEach((radioRef) => radioRef.checked = radioRef.value === fieldValue);
|
|
1623
|
-
}
|
|
1624
|
-
} else if (isFileInput(fieldReference.ref)) {
|
|
1625
|
-
fieldReference.ref.value = "";
|
|
1626
|
-
} else {
|
|
1627
|
-
fieldReference.ref.value = fieldValue;
|
|
1628
|
-
if (!fieldReference.ref.type) {
|
|
1629
|
-
_subjects.values.next({
|
|
1630
|
-
name,
|
|
1631
|
-
values: { ..._formValues }
|
|
1632
|
-
});
|
|
1633
|
-
}
|
|
1634
|
-
}
|
|
1635
|
-
}
|
|
1636
|
-
}
|
|
1637
|
-
(options.shouldDirty || options.shouldTouch) && updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
|
|
1638
|
-
options.shouldValidate && trigger(name);
|
|
1639
|
-
};
|
|
1640
|
-
const setValues = (name, value, options) => {
|
|
1641
|
-
for (const fieldKey in value) {
|
|
1642
|
-
const fieldValue = value[fieldKey];
|
|
1643
|
-
const fieldName = `${name}.${fieldKey}`;
|
|
1644
|
-
const field = get(_fields, fieldName);
|
|
1645
|
-
(_names.array.has(name) || !isPrimitive(fieldValue) || field && !field._f) && !isDateObject(fieldValue) ? setValues(fieldName, fieldValue, options) : setFieldValue(fieldName, fieldValue, options);
|
|
1646
|
-
}
|
|
1647
|
-
};
|
|
1648
|
-
const setValue = (name, value, options = {}) => {
|
|
1649
|
-
const field = get(_fields, name);
|
|
1650
|
-
const isFieldArray = _names.array.has(name);
|
|
1651
|
-
const cloneValue = cloneObject(value);
|
|
1652
|
-
set(_formValues, name, cloneValue);
|
|
1653
|
-
if (isFieldArray) {
|
|
1654
|
-
_subjects.array.next({
|
|
1655
|
-
name,
|
|
1656
|
-
values: { ..._formValues }
|
|
1657
|
-
});
|
|
1658
|
-
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) && options.shouldDirty) {
|
|
1659
|
-
_subjects.state.next({
|
|
1660
|
-
name,
|
|
1661
|
-
dirtyFields: getDirtyFields(_defaultValues, _formValues),
|
|
1662
|
-
isDirty: _getDirty(name, cloneValue)
|
|
1663
|
-
});
|
|
1664
|
-
}
|
|
1665
|
-
} else {
|
|
1666
|
-
field && !field._f && !isNullOrUndefined(cloneValue) ? setValues(name, cloneValue, options) : setFieldValue(name, cloneValue, options);
|
|
1667
|
-
}
|
|
1668
|
-
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
|
1669
|
-
_subjects.values.next({
|
|
1670
|
-
name,
|
|
1671
|
-
values: { ..._formValues }
|
|
1672
|
-
});
|
|
1673
|
-
!_state.mount && flushRootRender();
|
|
1674
|
-
};
|
|
1675
|
-
const onChange = async (event) => {
|
|
1676
|
-
const target = event.target;
|
|
1677
|
-
let name = target.name;
|
|
1678
|
-
let isFieldValueUpdated = true;
|
|
1679
|
-
const field = get(_fields, name);
|
|
1680
|
-
const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
|
|
1681
|
-
const _updateIsFieldValueUpdated = (fieldValue) => {
|
|
1682
|
-
isFieldValueUpdated = Number.isNaN(fieldValue) || fieldValue === get(_formValues, name, fieldValue);
|
|
1683
|
-
};
|
|
1684
|
-
if (field) {
|
|
1685
|
-
let error;
|
|
1686
|
-
let isValid;
|
|
1687
|
-
const fieldValue = getCurrentFieldValue();
|
|
1688
|
-
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
|
1689
|
-
const shouldSkipValidation = !hasValidation(field._f) && !_options.resolver && !get(_formState.errors, name) && !field._f.deps || skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
|
1690
|
-
const watched = isWatched(name, _names, isBlurEvent);
|
|
1691
|
-
set(_formValues, name, fieldValue);
|
|
1692
|
-
if (isBlurEvent) {
|
|
1693
|
-
field._f.onBlur && field._f.onBlur(event);
|
|
1694
|
-
delayErrorCallback && delayErrorCallback(0);
|
|
1695
|
-
} else if (field._f.onChange) {
|
|
1696
|
-
field._f.onChange(event);
|
|
1697
|
-
}
|
|
1698
|
-
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
|
|
1699
|
-
const shouldRender = !isEmptyObject(fieldState) || watched;
|
|
1700
|
-
!isBlurEvent && _subjects.values.next({
|
|
1701
|
-
name,
|
|
1702
|
-
type: event.type,
|
|
1703
|
-
values: { ..._formValues }
|
|
1704
|
-
});
|
|
1705
|
-
if (shouldSkipValidation) {
|
|
1706
|
-
_proxyFormState.isValid && _updateValid();
|
|
1707
|
-
return shouldRender && _subjects.state.next({ name, ...watched ? {} : fieldState });
|
|
1708
|
-
}
|
|
1709
|
-
!isBlurEvent && watched && _subjects.state.next({ ..._formState });
|
|
1710
|
-
_updateIsValidating(true);
|
|
1711
|
-
if (_options.resolver) {
|
|
1712
|
-
const { errors } = await _executeSchema([name]);
|
|
1713
|
-
_updateIsFieldValueUpdated(fieldValue);
|
|
1714
|
-
if (isFieldValueUpdated) {
|
|
1715
|
-
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
|
1716
|
-
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
|
1717
|
-
error = errorLookupResult.error;
|
|
1718
|
-
name = errorLookupResult.name;
|
|
1719
|
-
isValid = isEmptyObject(errors);
|
|
1720
|
-
}
|
|
1721
|
-
} else {
|
|
1722
|
-
error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
|
1723
|
-
_updateIsFieldValueUpdated(fieldValue);
|
|
1724
|
-
if (isFieldValueUpdated) {
|
|
1725
|
-
if (error) {
|
|
1726
|
-
isValid = false;
|
|
1727
|
-
} else if (_proxyFormState.isValid) {
|
|
1728
|
-
isValid = await executeBuiltInValidation(_fields, true);
|
|
1729
|
-
}
|
|
1730
|
-
}
|
|
1731
|
-
}
|
|
1732
|
-
if (isFieldValueUpdated) {
|
|
1733
|
-
field._f.deps && trigger(field._f.deps);
|
|
1734
|
-
shouldRenderByError(name, isValid, error, fieldState);
|
|
1735
|
-
}
|
|
1736
|
-
}
|
|
1737
|
-
};
|
|
1738
|
-
const _focusInput = (ref, key) => {
|
|
1739
|
-
if (get(_formState.errors, key) && ref.focus) {
|
|
1740
|
-
ref.focus();
|
|
1741
|
-
return 1;
|
|
1742
|
-
}
|
|
1743
|
-
return;
|
|
1744
|
-
};
|
|
1745
|
-
const trigger = async (name, options = {}) => {
|
|
1746
|
-
let isValid;
|
|
1747
|
-
let validationResult;
|
|
1748
|
-
const fieldNames = convertToArrayPayload(name);
|
|
1749
|
-
_updateIsValidating(true);
|
|
1750
|
-
if (_options.resolver) {
|
|
1751
|
-
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
|
1752
|
-
isValid = isEmptyObject(errors);
|
|
1753
|
-
validationResult = name ? !fieldNames.some((name2) => get(errors, name2)) : isValid;
|
|
1754
|
-
} else if (name) {
|
|
1755
|
-
validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
|
|
1756
|
-
const field = get(_fields, fieldName);
|
|
1757
|
-
return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
|
|
1758
|
-
}))).every(Boolean);
|
|
1759
|
-
!(!validationResult && !_formState.isValid) && _updateValid();
|
|
1760
|
-
} else {
|
|
1761
|
-
validationResult = isValid = await executeBuiltInValidation(_fields);
|
|
1762
|
-
}
|
|
1763
|
-
_subjects.state.next({
|
|
1764
|
-
...!isString(name) || _proxyFormState.isValid && isValid !== _formState.isValid ? {} : { name },
|
|
1765
|
-
..._options.resolver || !name ? { isValid } : {},
|
|
1766
|
-
errors: _formState.errors,
|
|
1767
|
-
isValidating: false
|
|
1768
|
-
});
|
|
1769
|
-
options.shouldFocus && !validationResult && iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
|
|
1770
|
-
return validationResult;
|
|
1771
|
-
};
|
|
1772
|
-
const getValues = (fieldNames) => {
|
|
1773
|
-
const values = {
|
|
1774
|
-
..._defaultValues,
|
|
1775
|
-
..._state.mount ? _formValues : {}
|
|
1776
|
-
};
|
|
1777
|
-
return isUndefined(fieldNames) ? values : isString(fieldNames) ? get(values, fieldNames) : fieldNames.map((name) => get(values, name));
|
|
1778
|
-
};
|
|
1779
|
-
const getFieldState = (name, formState) => ({
|
|
1780
|
-
invalid: !!get((formState || _formState).errors, name),
|
|
1781
|
-
isDirty: !!get((formState || _formState).dirtyFields, name),
|
|
1782
|
-
isTouched: !!get((formState || _formState).touchedFields, name),
|
|
1783
|
-
error: get((formState || _formState).errors, name)
|
|
1784
|
-
});
|
|
1785
|
-
const clearErrors = (name) => {
|
|
1786
|
-
name && convertToArrayPayload(name).forEach((inputName) => unset(_formState.errors, inputName));
|
|
1787
|
-
_subjects.state.next({
|
|
1788
|
-
errors: name ? _formState.errors : {}
|
|
1789
|
-
});
|
|
1790
|
-
};
|
|
1791
|
-
const setError = (name, error, options) => {
|
|
1792
|
-
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
|
|
1793
|
-
set(_formState.errors, name, {
|
|
1794
|
-
...error,
|
|
1795
|
-
ref
|
|
1796
|
-
});
|
|
1797
|
-
_subjects.state.next({
|
|
1798
|
-
name,
|
|
1799
|
-
errors: _formState.errors,
|
|
1800
|
-
isValid: false
|
|
1801
|
-
});
|
|
1802
|
-
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
|
1803
|
-
};
|
|
1804
|
-
const watch = (name, defaultValue) => isFunction(name) ? _subjects.values.subscribe({
|
|
1805
|
-
next: (payload) => name(_getWatch(void 0, defaultValue), payload)
|
|
1806
|
-
}) : _getWatch(name, defaultValue, true);
|
|
1807
|
-
const unregister = (name, options = {}) => {
|
|
1808
|
-
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
|
|
1809
|
-
_names.mount.delete(fieldName);
|
|
1810
|
-
_names.array.delete(fieldName);
|
|
1811
|
-
if (!options.keepValue) {
|
|
1812
|
-
unset(_fields, fieldName);
|
|
1813
|
-
unset(_formValues, fieldName);
|
|
1814
|
-
}
|
|
1815
|
-
!options.keepError && unset(_formState.errors, fieldName);
|
|
1816
|
-
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
|
1817
|
-
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
|
1818
|
-
!_options.shouldUnregister && !options.keepDefaultValue && unset(_defaultValues, fieldName);
|
|
1819
|
-
}
|
|
1820
|
-
_subjects.values.next({
|
|
1821
|
-
values: { ..._formValues }
|
|
1822
|
-
});
|
|
1823
|
-
_subjects.state.next({
|
|
1824
|
-
..._formState,
|
|
1825
|
-
...!options.keepDirty ? {} : { isDirty: _getDirty() }
|
|
1826
|
-
});
|
|
1827
|
-
!options.keepIsValid && _updateValid();
|
|
1828
|
-
};
|
|
1829
|
-
const _updateDisabledField = ({ disabled, name, field, fields, value }) => {
|
|
1830
|
-
if (isBoolean(disabled)) {
|
|
1831
|
-
const inputValue = disabled ? void 0 : isUndefined(value) ? getFieldValue(field ? field._f : get(fields, name)._f) : value;
|
|
1832
|
-
set(_formValues, name, inputValue);
|
|
1833
|
-
updateTouchAndDirty(name, inputValue, false, false, true);
|
|
1834
|
-
}
|
|
1835
|
-
};
|
|
1836
|
-
const register = (name, options = {}) => {
|
|
1837
|
-
let field = get(_fields, name);
|
|
1838
|
-
const disabledIsDefined = isBoolean(options.disabled);
|
|
1839
|
-
set(_fields, name, {
|
|
1840
|
-
...field || {},
|
|
1841
|
-
_f: {
|
|
1842
|
-
...field && field._f ? field._f : { ref: { name } },
|
|
1843
|
-
name,
|
|
1844
|
-
mount: true,
|
|
1845
|
-
...options
|
|
1846
|
-
}
|
|
1847
|
-
});
|
|
1848
|
-
_names.mount.add(name);
|
|
1849
|
-
if (field) {
|
|
1850
|
-
_updateDisabledField({
|
|
1851
|
-
field,
|
|
1852
|
-
disabled: options.disabled,
|
|
1853
|
-
name
|
|
1854
|
-
});
|
|
1855
|
-
} else {
|
|
1856
|
-
updateValidAndValue(name, true, options.value);
|
|
1857
|
-
}
|
|
1858
|
-
return {
|
|
1859
|
-
...disabledIsDefined ? { disabled: options.disabled } : {},
|
|
1860
|
-
..._options.progressive ? {
|
|
1861
|
-
required: !!options.required,
|
|
1862
|
-
min: getRuleValue(options.min),
|
|
1863
|
-
max: getRuleValue(options.max),
|
|
1864
|
-
minLength: getRuleValue(options.minLength),
|
|
1865
|
-
maxLength: getRuleValue(options.maxLength),
|
|
1866
|
-
pattern: getRuleValue(options.pattern)
|
|
1867
|
-
} : {},
|
|
1868
|
-
name,
|
|
1869
|
-
onChange,
|
|
1870
|
-
onBlur: onChange,
|
|
1871
|
-
ref: (ref) => {
|
|
1872
|
-
if (ref) {
|
|
1873
|
-
register(name, options);
|
|
1874
|
-
field = get(_fields, name);
|
|
1875
|
-
const fieldRef = isUndefined(ref.value) ? ref.querySelectorAll ? ref.querySelectorAll("input,select,textarea")[0] || ref : ref : ref;
|
|
1876
|
-
const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
|
|
1877
|
-
const refs = field._f.refs || [];
|
|
1878
|
-
if (radioOrCheckbox ? refs.find((option) => option === fieldRef) : fieldRef === field._f.ref) {
|
|
1879
|
-
return;
|
|
1880
|
-
}
|
|
1881
|
-
set(_fields, name, {
|
|
1882
|
-
_f: {
|
|
1883
|
-
...field._f,
|
|
1884
|
-
...radioOrCheckbox ? {
|
|
1885
|
-
refs: [
|
|
1886
|
-
...refs.filter(live),
|
|
1887
|
-
fieldRef,
|
|
1888
|
-
...Array.isArray(get(_defaultValues, name)) ? [{}] : []
|
|
1889
|
-
],
|
|
1890
|
-
ref: { type: fieldRef.type, name }
|
|
1891
|
-
} : { ref: fieldRef }
|
|
1892
|
-
}
|
|
1893
|
-
});
|
|
1894
|
-
updateValidAndValue(name, false, void 0, fieldRef);
|
|
1895
|
-
} else {
|
|
1896
|
-
field = get(_fields, name, {});
|
|
1897
|
-
if (field._f) {
|
|
1898
|
-
field._f.mount = false;
|
|
1899
|
-
}
|
|
1900
|
-
(_options.shouldUnregister || options.shouldUnregister) && !(isNameInFieldArray(_names.array, name) && _state.action) && _names.unMount.add(name);
|
|
1901
|
-
}
|
|
1902
|
-
}
|
|
1903
|
-
};
|
|
1904
|
-
};
|
|
1905
|
-
const _focusError = () => _options.shouldFocusError && iterateFieldsByAction(_fields, _focusInput, _names.mount);
|
|
1906
|
-
const _disableForm = (disabled) => {
|
|
1907
|
-
if (isBoolean(disabled)) {
|
|
1908
|
-
_subjects.state.next({ disabled });
|
|
1909
|
-
iterateFieldsByAction(_fields, (ref, name) => {
|
|
1910
|
-
let requiredDisabledState = disabled;
|
|
1911
|
-
const currentField = get(_fields, name);
|
|
1912
|
-
if (currentField && isBoolean(currentField._f.disabled)) {
|
|
1913
|
-
requiredDisabledState || (requiredDisabledState = currentField._f.disabled);
|
|
1914
|
-
}
|
|
1915
|
-
ref.disabled = requiredDisabledState;
|
|
1916
|
-
}, 0, false);
|
|
1917
|
-
}
|
|
1918
|
-
};
|
|
1919
|
-
const handleSubmit = (onValid, onInvalid) => async (e2) => {
|
|
1920
|
-
if (e2) {
|
|
1921
|
-
e2.preventDefault && e2.preventDefault();
|
|
1922
|
-
e2.persist && e2.persist();
|
|
1923
|
-
}
|
|
1924
|
-
let fieldValues = cloneObject(_formValues);
|
|
1925
|
-
_subjects.state.next({
|
|
1926
|
-
isSubmitting: true
|
|
1927
|
-
});
|
|
1928
|
-
if (_options.resolver) {
|
|
1929
|
-
const { errors, values } = await _executeSchema();
|
|
1930
|
-
_formState.errors = errors;
|
|
1931
|
-
fieldValues = values;
|
|
1932
|
-
} else {
|
|
1933
|
-
await executeBuiltInValidation(_fields);
|
|
1934
|
-
}
|
|
1935
|
-
unset(_formState.errors, "root");
|
|
1936
|
-
if (isEmptyObject(_formState.errors)) {
|
|
1937
|
-
_subjects.state.next({
|
|
1938
|
-
errors: {}
|
|
1939
|
-
});
|
|
1940
|
-
await onValid(fieldValues, e2);
|
|
1941
|
-
} else {
|
|
1942
|
-
if (onInvalid) {
|
|
1943
|
-
await onInvalid({ ..._formState.errors }, e2);
|
|
1944
|
-
}
|
|
1945
|
-
_focusError();
|
|
1946
|
-
setTimeout(_focusError);
|
|
1947
|
-
}
|
|
1948
|
-
_subjects.state.next({
|
|
1949
|
-
isSubmitted: true,
|
|
1950
|
-
isSubmitting: false,
|
|
1951
|
-
isSubmitSuccessful: isEmptyObject(_formState.errors),
|
|
1952
|
-
submitCount: _formState.submitCount + 1,
|
|
1953
|
-
errors: _formState.errors
|
|
1954
|
-
});
|
|
1955
|
-
};
|
|
1956
|
-
const resetField = (name, options = {}) => {
|
|
1957
|
-
if (get(_fields, name)) {
|
|
1958
|
-
if (isUndefined(options.defaultValue)) {
|
|
1959
|
-
setValue(name, get(_defaultValues, name));
|
|
1960
|
-
} else {
|
|
1961
|
-
setValue(name, options.defaultValue);
|
|
1962
|
-
set(_defaultValues, name, options.defaultValue);
|
|
1963
|
-
}
|
|
1964
|
-
if (!options.keepTouched) {
|
|
1965
|
-
unset(_formState.touchedFields, name);
|
|
1966
|
-
}
|
|
1967
|
-
if (!options.keepDirty) {
|
|
1968
|
-
unset(_formState.dirtyFields, name);
|
|
1969
|
-
_formState.isDirty = options.defaultValue ? _getDirty(name, get(_defaultValues, name)) : _getDirty();
|
|
1970
|
-
}
|
|
1971
|
-
if (!options.keepError) {
|
|
1972
|
-
unset(_formState.errors, name);
|
|
1973
|
-
_proxyFormState.isValid && _updateValid();
|
|
1974
|
-
}
|
|
1975
|
-
_subjects.state.next({ ..._formState });
|
|
1976
|
-
}
|
|
1977
|
-
};
|
|
1978
|
-
const _reset = (formValues, keepStateOptions = {}) => {
|
|
1979
|
-
const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
|
|
1980
|
-
const cloneUpdatedValues = cloneObject(updatedValues);
|
|
1981
|
-
const values = formValues && !isEmptyObject(formValues) ? cloneUpdatedValues : _defaultValues;
|
|
1982
|
-
if (!keepStateOptions.keepDefaultValues) {
|
|
1983
|
-
_defaultValues = updatedValues;
|
|
1984
|
-
}
|
|
1985
|
-
if (!keepStateOptions.keepValues) {
|
|
1986
|
-
if (keepStateOptions.keepDirtyValues || shouldCaptureDirtyFields) {
|
|
1987
|
-
for (const fieldName of _names.mount) {
|
|
1988
|
-
get(_formState.dirtyFields, fieldName) ? set(values, fieldName, get(_formValues, fieldName)) : setValue(fieldName, get(values, fieldName));
|
|
1989
|
-
}
|
|
1990
|
-
} else {
|
|
1991
|
-
if (isWeb && isUndefined(formValues)) {
|
|
1992
|
-
for (const name of _names.mount) {
|
|
1993
|
-
const field = get(_fields, name);
|
|
1994
|
-
if (field && field._f) {
|
|
1995
|
-
const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
|
|
1996
|
-
if (isHTMLElement(fieldReference)) {
|
|
1997
|
-
const form = fieldReference.closest("form");
|
|
1998
|
-
if (form) {
|
|
1999
|
-
form.reset();
|
|
2000
|
-
break;
|
|
2001
|
-
}
|
|
2002
|
-
}
|
|
2003
|
-
}
|
|
2004
|
-
}
|
|
2005
|
-
}
|
|
2006
|
-
_fields = {};
|
|
2007
|
-
}
|
|
2008
|
-
_formValues = props.shouldUnregister ? keepStateOptions.keepDefaultValues ? cloneObject(_defaultValues) : {} : cloneObject(values);
|
|
2009
|
-
_subjects.array.next({
|
|
2010
|
-
values: { ...values }
|
|
2011
|
-
});
|
|
2012
|
-
_subjects.values.next({
|
|
2013
|
-
values: { ...values }
|
|
2014
|
-
});
|
|
2015
|
-
}
|
|
2016
|
-
_names = {
|
|
2017
|
-
mount: /* @__PURE__ */ new Set(),
|
|
2018
|
-
unMount: /* @__PURE__ */ new Set(),
|
|
2019
|
-
array: /* @__PURE__ */ new Set(),
|
|
2020
|
-
watch: /* @__PURE__ */ new Set(),
|
|
2021
|
-
watchAll: false,
|
|
2022
|
-
focus: ""
|
|
2023
|
-
};
|
|
2024
|
-
!_state.mount && flushRootRender();
|
|
2025
|
-
_state.mount = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
|
|
2026
|
-
_state.watch = !!props.shouldUnregister;
|
|
2027
|
-
_subjects.state.next({
|
|
2028
|
-
submitCount: keepStateOptions.keepSubmitCount ? _formState.submitCount : 0,
|
|
2029
|
-
isDirty: keepStateOptions.keepDirty ? _formState.isDirty : !!(keepStateOptions.keepDefaultValues && !deepEqual(formValues, _defaultValues)),
|
|
2030
|
-
isSubmitted: keepStateOptions.keepIsSubmitted ? _formState.isSubmitted : false,
|
|
2031
|
-
dirtyFields: keepStateOptions.keepDirtyValues ? _formState.dirtyFields : keepStateOptions.keepDefaultValues && formValues ? getDirtyFields(_defaultValues, formValues) : {},
|
|
2032
|
-
touchedFields: keepStateOptions.keepTouched ? _formState.touchedFields : {},
|
|
2033
|
-
errors: keepStateOptions.keepErrors ? _formState.errors : {},
|
|
2034
|
-
isSubmitSuccessful: keepStateOptions.keepIsSubmitSuccessful ? _formState.isSubmitSuccessful : false,
|
|
2035
|
-
isSubmitting: false
|
|
2036
|
-
});
|
|
2037
|
-
};
|
|
2038
|
-
const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues) ? formValues(_formValues) : formValues, keepStateOptions);
|
|
2039
|
-
const setFocus = (name, options = {}) => {
|
|
2040
|
-
const field = get(_fields, name);
|
|
2041
|
-
const fieldReference = field && field._f;
|
|
2042
|
-
if (fieldReference) {
|
|
2043
|
-
const fieldRef = fieldReference.refs ? fieldReference.refs[0] : fieldReference.ref;
|
|
2044
|
-
if (fieldRef.focus) {
|
|
2045
|
-
fieldRef.focus();
|
|
2046
|
-
options.shouldSelect && fieldRef.select();
|
|
2047
|
-
}
|
|
2048
|
-
}
|
|
2049
|
-
};
|
|
2050
|
-
const _updateFormState = (updatedFormState) => {
|
|
2051
|
-
_formState = {
|
|
2052
|
-
..._formState,
|
|
2053
|
-
...updatedFormState
|
|
2054
|
-
};
|
|
2055
|
-
};
|
|
2056
|
-
const _resetDefaultValues = () => isFunction(_options.defaultValues) && _options.defaultValues().then((values) => {
|
|
2057
|
-
reset(values, _options.resetOptions);
|
|
2058
|
-
_subjects.state.next({
|
|
2059
|
-
isLoading: false
|
|
2060
|
-
});
|
|
2061
|
-
});
|
|
2062
|
-
return {
|
|
2063
|
-
control: {
|
|
2064
|
-
register,
|
|
2065
|
-
unregister,
|
|
2066
|
-
getFieldState,
|
|
2067
|
-
handleSubmit,
|
|
2068
|
-
setError,
|
|
2069
|
-
_executeSchema,
|
|
2070
|
-
_getWatch,
|
|
2071
|
-
_getDirty,
|
|
2072
|
-
_updateValid,
|
|
2073
|
-
_removeUnmounted,
|
|
2074
|
-
_updateFieldArray,
|
|
2075
|
-
_updateDisabledField,
|
|
2076
|
-
_getFieldArray,
|
|
2077
|
-
_reset,
|
|
2078
|
-
_resetDefaultValues,
|
|
2079
|
-
_updateFormState,
|
|
2080
|
-
_disableForm,
|
|
2081
|
-
_subjects,
|
|
2082
|
-
_proxyFormState,
|
|
2083
|
-
_setErrors,
|
|
2084
|
-
get _fields() {
|
|
2085
|
-
return _fields;
|
|
2086
|
-
},
|
|
2087
|
-
get _formValues() {
|
|
2088
|
-
return _formValues;
|
|
2089
|
-
},
|
|
2090
|
-
get _state() {
|
|
2091
|
-
return _state;
|
|
2092
|
-
},
|
|
2093
|
-
set _state(value) {
|
|
2094
|
-
_state = value;
|
|
2095
|
-
},
|
|
2096
|
-
get _defaultValues() {
|
|
2097
|
-
return _defaultValues;
|
|
2098
|
-
},
|
|
2099
|
-
get _names() {
|
|
2100
|
-
return _names;
|
|
2101
|
-
},
|
|
2102
|
-
set _names(value) {
|
|
2103
|
-
_names = value;
|
|
2104
|
-
},
|
|
2105
|
-
get _formState() {
|
|
2106
|
-
return _formState;
|
|
2107
|
-
},
|
|
2108
|
-
set _formState(value) {
|
|
2109
|
-
_formState = value;
|
|
2110
|
-
},
|
|
2111
|
-
get _options() {
|
|
2112
|
-
return _options;
|
|
2113
|
-
},
|
|
2114
|
-
set _options(value) {
|
|
2115
|
-
_options = {
|
|
2116
|
-
..._options,
|
|
2117
|
-
...value
|
|
2118
|
-
};
|
|
2119
|
-
}
|
|
2120
|
-
},
|
|
2121
|
-
trigger,
|
|
2122
|
-
register,
|
|
2123
|
-
handleSubmit,
|
|
2124
|
-
watch,
|
|
2125
|
-
setValue,
|
|
2126
|
-
getValues,
|
|
2127
|
-
reset,
|
|
2128
|
-
resetField,
|
|
2129
|
-
clearErrors,
|
|
2130
|
-
unregister,
|
|
2131
|
-
setError,
|
|
2132
|
-
setFocus,
|
|
2133
|
-
getFieldState
|
|
2134
|
-
};
|
|
2135
|
-
}
|
|
2136
|
-
function useForm(props = {}) {
|
|
2137
|
-
const _formControl = React__default.default.useRef();
|
|
2138
|
-
const _values = React__default.default.useRef();
|
|
2139
|
-
const [formState, updateFormState] = React__default.default.useState({
|
|
2140
|
-
isDirty: false,
|
|
2141
|
-
isValidating: false,
|
|
2142
|
-
isLoading: isFunction(props.defaultValues),
|
|
2143
|
-
isSubmitted: false,
|
|
2144
|
-
isSubmitting: false,
|
|
2145
|
-
isSubmitSuccessful: false,
|
|
2146
|
-
isValid: false,
|
|
2147
|
-
submitCount: 0,
|
|
2148
|
-
dirtyFields: {},
|
|
2149
|
-
touchedFields: {},
|
|
2150
|
-
errors: props.errors || {},
|
|
2151
|
-
disabled: false,
|
|
2152
|
-
defaultValues: isFunction(props.defaultValues) ? void 0 : props.defaultValues
|
|
2153
|
-
});
|
|
2154
|
-
if (!_formControl.current) {
|
|
2155
|
-
_formControl.current = {
|
|
2156
|
-
...createFormControl(props, () => updateFormState((formState2) => ({ ...formState2 }))),
|
|
2157
|
-
formState
|
|
2158
|
-
};
|
|
2159
|
-
}
|
|
2160
|
-
const control = _formControl.current.control;
|
|
2161
|
-
control._options = props;
|
|
2162
|
-
useSubscribe({
|
|
2163
|
-
subject: control._subjects.state,
|
|
2164
|
-
next: (value) => {
|
|
2165
|
-
if (shouldRenderFormState(value, control._proxyFormState, control._updateFormState, true)) {
|
|
2166
|
-
updateFormState({ ...control._formState });
|
|
2167
|
-
}
|
|
2168
|
-
}
|
|
2169
|
-
});
|
|
2170
|
-
React__default.default.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
|
|
2171
|
-
React__default.default.useEffect(() => {
|
|
2172
|
-
if (control._proxyFormState.isDirty) {
|
|
2173
|
-
const isDirty = control._getDirty();
|
|
2174
|
-
if (isDirty !== formState.isDirty) {
|
|
2175
|
-
control._subjects.state.next({
|
|
2176
|
-
isDirty
|
|
2177
|
-
});
|
|
2178
|
-
}
|
|
2179
|
-
}
|
|
2180
|
-
}, [control, formState.isDirty]);
|
|
2181
|
-
React__default.default.useEffect(() => {
|
|
2182
|
-
if (props.values && !deepEqual(props.values, _values.current)) {
|
|
2183
|
-
control._reset(props.values, control._options.resetOptions);
|
|
2184
|
-
_values.current = props.values;
|
|
2185
|
-
updateFormState((state) => ({ ...state }));
|
|
2186
|
-
} else {
|
|
2187
|
-
control._resetDefaultValues();
|
|
2188
|
-
}
|
|
2189
|
-
}, [props.values, control]);
|
|
2190
|
-
React__default.default.useEffect(() => {
|
|
2191
|
-
if (props.errors) {
|
|
2192
|
-
control._setErrors(props.errors);
|
|
2193
|
-
}
|
|
2194
|
-
}, [props.errors, control]);
|
|
2195
|
-
React__default.default.useEffect(() => {
|
|
2196
|
-
if (!control._state.mount) {
|
|
2197
|
-
control._updateValid();
|
|
2198
|
-
control._state.mount = true;
|
|
2199
|
-
}
|
|
2200
|
-
if (control._state.watch) {
|
|
2201
|
-
control._state.watch = false;
|
|
2202
|
-
control._subjects.state.next({ ...control._formState });
|
|
2203
|
-
}
|
|
2204
|
-
control._removeUnmounted();
|
|
2205
|
-
});
|
|
2206
|
-
_formControl.current.formState = getProxyFormState(formState, control);
|
|
2207
|
-
return _formControl.current;
|
|
2208
|
-
}
|
|
2209
|
-
var t$1 = function(t2, n2, e2) {
|
|
2210
|
-
if (t2 && "reportValidity" in t2) {
|
|
2211
|
-
var i2 = get(e2, n2);
|
|
2212
|
-
t2.setCustomValidity(i2 && i2.message || ""), t2.reportValidity();
|
|
2213
|
-
}
|
|
2214
|
-
}, n$1 = function(r, n2) {
|
|
2215
|
-
var e2 = function(e3) {
|
|
2216
|
-
var i3 = n2.fields[e3];
|
|
2217
|
-
i3 && i3.ref && "reportValidity" in i3.ref ? t$1(i3.ref, e3, r) : i3.refs && i3.refs.forEach(function(n3) {
|
|
2218
|
-
return t$1(n3, e3, r);
|
|
2219
|
-
});
|
|
2220
|
-
};
|
|
2221
|
-
for (var i2 in n2.fields) e2(i2);
|
|
2222
|
-
}, e = function(r) {
|
|
2223
|
-
return r instanceof Date;
|
|
2224
|
-
}, i = function(r) {
|
|
2225
|
-
return null == r;
|
|
2226
|
-
}, a = function(r) {
|
|
2227
|
-
return "object" == typeof r;
|
|
2228
|
-
}, o = function(r) {
|
|
2229
|
-
return !i(r) && !Array.isArray(r) && a(r) && !e(r);
|
|
2230
|
-
}, f = function(r) {
|
|
2231
|
-
return /^\w*$/.test(r);
|
|
2232
|
-
}, s = function(r, t2, n2) {
|
|
2233
|
-
for (var e2 = -1, i2 = f(t2) ? [t2] : function(r2) {
|
|
2234
|
-
return t3 = r2.replace(/["|']|\]/g, "").split(/\.|\[/), Array.isArray(t3) ? t3.filter(Boolean) : [];
|
|
2235
|
-
var t3;
|
|
2236
|
-
}(t2), a2 = i2.length, s2 = a2 - 1; ++e2 < a2; ) {
|
|
2237
|
-
var u2 = i2[e2], c2 = n2;
|
|
2238
|
-
if (e2 !== s2) {
|
|
2239
|
-
var l = r[u2];
|
|
2240
|
-
c2 = o(l) || Array.isArray(l) ? l : isNaN(+i2[e2 + 1]) ? {} : [];
|
|
2241
|
-
}
|
|
2242
|
-
r[u2] = c2, r = r[u2];
|
|
2243
|
-
}
|
|
2244
|
-
return r;
|
|
2245
|
-
}, u = function(t2, e2) {
|
|
2246
|
-
e2.shouldUseNativeValidation && n$1(t2, e2);
|
|
2247
|
-
var i2 = {};
|
|
2248
|
-
for (var a2 in t2) {
|
|
2249
|
-
var o2 = get(e2.fields, a2), f2 = Object.assign(t2[a2] || {}, { ref: o2 && o2.ref });
|
|
2250
|
-
if (c(e2.names || Object.keys(t2), a2)) {
|
|
2251
|
-
var u2 = Object.assign({}, get(i2, a2));
|
|
2252
|
-
s(u2, "root", f2), s(i2, a2, u2);
|
|
2253
|
-
} else s(i2, a2, f2);
|
|
2254
|
-
}
|
|
2255
|
-
return i2;
|
|
2256
|
-
}, c = function(r, t2) {
|
|
2257
|
-
return r.some(function(r2) {
|
|
2258
|
-
return r2.startsWith(t2 + ".");
|
|
2259
|
-
});
|
|
2260
|
-
};
|
|
2261
|
-
var n = function(e2, o2) {
|
|
2262
|
-
for (var n2 = {}; e2.length; ) {
|
|
2263
|
-
var t2 = e2[0], s2 = t2.code, i2 = t2.message, a2 = t2.path.join(".");
|
|
2264
|
-
if (!n2[a2]) if ("unionErrors" in t2) {
|
|
2265
|
-
var u2 = t2.unionErrors[0].errors[0];
|
|
2266
|
-
n2[a2] = { message: u2.message, type: u2.code };
|
|
2267
|
-
} else n2[a2] = { message: i2, type: s2 };
|
|
2268
|
-
if ("unionErrors" in t2 && t2.unionErrors.forEach(function(r) {
|
|
2269
|
-
return r.errors.forEach(function(r2) {
|
|
2270
|
-
return e2.push(r2);
|
|
2271
|
-
});
|
|
2272
|
-
}), o2) {
|
|
2273
|
-
var c2 = n2[a2].types, f2 = c2 && c2[t2.code];
|
|
2274
|
-
n2[a2] = appendErrors(a2, o2, n2, s2, f2 ? [].concat(f2, t2.message) : t2.message);
|
|
2275
|
-
}
|
|
2276
|
-
e2.shift();
|
|
2277
|
-
}
|
|
2278
|
-
return n2;
|
|
2279
|
-
}, t = function(r, t2, s2) {
|
|
2280
|
-
return void 0 === s2 && (s2 = {}), function(i2, a2, u$1) {
|
|
2281
|
-
try {
|
|
2282
|
-
return Promise.resolve(function(o2, n2) {
|
|
2283
|
-
try {
|
|
2284
|
-
var a3 = Promise.resolve(r["sync" === s2.mode ? "parse" : "parseAsync"](i2, t2)).then(function(r2) {
|
|
2285
|
-
return u$1.shouldUseNativeValidation && n$1({}, u$1), { errors: {}, values: s2.raw ? i2 : r2 };
|
|
2286
|
-
});
|
|
2287
|
-
} catch (r2) {
|
|
2288
|
-
return n2(r2);
|
|
2289
|
-
}
|
|
2290
|
-
return a3 && a3.then ? a3.then(void 0, n2) : a3;
|
|
2291
|
-
}(0, function(r2) {
|
|
2292
|
-
if (function(r3) {
|
|
2293
|
-
return null != r3.errors;
|
|
2294
|
-
}(r2)) return { values: {}, errors: u(n(r2.errors, !u$1.shouldUseNativeValidation && "all" === u$1.criteriaMode), u$1) };
|
|
2295
|
-
throw r2;
|
|
2296
|
-
}));
|
|
2297
|
-
} catch (r2) {
|
|
2298
|
-
return Promise.reject(r2);
|
|
2299
|
-
}
|
|
2300
|
-
};
|
|
2301
|
-
};
|
|
2302
|
-
const couponSchema = z__default.default.object({
|
|
2303
|
-
name: z__default.default.string().min(1, "Name is required"),
|
|
2304
|
-
description: z__default.default.string().min(1, "Description is required"),
|
|
2305
|
-
type: z__default.default.literal("coupon"),
|
|
2306
|
-
code: z__default.default.string().min(1, "Coupon code is required"),
|
|
2307
|
-
discount_type: z__default.default.enum(["percentage", "fixed"]),
|
|
2308
|
-
discount_value: z__default.default.number().positive("Discount must be positive"),
|
|
2309
|
-
currency_code: z__default.default.string().optional(),
|
|
2310
|
-
starts_at: z__default.default.string().min(1, "Start date is required"),
|
|
2311
|
-
ends_at: z__default.default.string().min(1, "End date is required"),
|
|
2312
|
-
allocation: z__default.default.enum(["each", "total"]).optional(),
|
|
2313
|
-
target_type: z__default.default.enum(["order", "items"]).optional()
|
|
2314
|
-
}).superRefine((data, ctx) => {
|
|
2315
|
-
if (new Date(data.ends_at) < new Date(data.starts_at)) {
|
|
2316
|
-
ctx.addIssue({
|
|
2317
|
-
code: z__default.default.ZodIssueCode.custom,
|
|
2318
|
-
path: ["ends_at"],
|
|
2319
|
-
message: "End date must be after start date"
|
|
2320
|
-
});
|
|
2321
|
-
}
|
|
2322
|
-
if (data.discount_type === "fixed" && !data.currency_code) {
|
|
2323
|
-
ctx.addIssue({
|
|
2324
|
-
code: z__default.default.ZodIssueCode.custom,
|
|
2325
|
-
path: ["currency_code"],
|
|
2326
|
-
message: "Currency is required for fixed discount"
|
|
2327
|
-
});
|
|
2328
|
-
}
|
|
2329
|
-
});
|
|
2330
|
-
const CouponForm = ({
|
|
2331
|
-
initialData,
|
|
2332
|
-
onSubmit,
|
|
2333
|
-
onCancel,
|
|
2334
|
-
disabled = false
|
|
2335
|
-
}) => {
|
|
2336
|
-
const {
|
|
2337
|
-
register,
|
|
2338
|
-
control,
|
|
2339
|
-
handleSubmit,
|
|
2340
|
-
watch,
|
|
2341
|
-
setValue,
|
|
2342
|
-
formState: { errors }
|
|
2343
|
-
} = useForm({
|
|
2344
|
-
resolver: t(couponSchema),
|
|
2345
|
-
defaultValues: {
|
|
2346
|
-
name: (initialData == null ? void 0 : initialData.name) ?? "",
|
|
2347
|
-
description: (initialData == null ? void 0 : initialData.description) ?? "",
|
|
2348
|
-
type: "coupon",
|
|
2349
|
-
code: (initialData == null ? void 0 : initialData.code) ?? "",
|
|
2350
|
-
discount_type: (initialData == null ? void 0 : initialData.discount_type) ?? "percentage",
|
|
2351
|
-
discount_value: (initialData == null ? void 0 : initialData.discount_value) ?? 0,
|
|
2352
|
-
currency_code: (initialData == null ? void 0 : initialData.currency_code) ?? "",
|
|
2353
|
-
starts_at: (initialData == null ? void 0 : initialData.starts_at) ?? dayjs__default.default().startOf("day").format("YYYY-MM-DDTHH:mm"),
|
|
2354
|
-
ends_at: (initialData == null ? void 0 : initialData.ends_at) ?? dayjs__default.default().endOf("day").format("YYYY-MM-DDTHH:mm"),
|
|
2355
|
-
allocation: (initialData == null ? void 0 : initialData.allocation) ?? "total",
|
|
2356
|
-
target_type: (initialData == null ? void 0 : initialData.target_type) ?? "order"
|
|
2357
|
-
}
|
|
2358
|
-
});
|
|
2359
|
-
const discountType = watch("discount_type");
|
|
2360
|
-
const startsAt = watch("starts_at");
|
|
2361
|
-
const endsAt = watch("ends_at");
|
|
2362
|
-
const handleDateTimeChange = (field, type, value) => {
|
|
2363
|
-
const currentValue = watch(field);
|
|
2364
|
-
if (type === "date") {
|
|
2365
|
-
const time = currentValue ? dayjs__default.default(currentValue).format("HH:mm") : field === "starts_at" ? "00:00" : "23:59";
|
|
2366
|
-
setValue(field, `${value}T${time}`);
|
|
2367
|
-
} else {
|
|
2368
|
-
const date = currentValue ? dayjs__default.default(currentValue).format("YYYY-MM-DD") : dayjs__default.default().format("YYYY-MM-DD");
|
|
2369
|
-
setValue(field, `${date}T${value}`);
|
|
2370
|
-
}
|
|
2371
|
-
};
|
|
2372
|
-
const onFormSubmit = (data) => {
|
|
2373
|
-
onSubmit(data);
|
|
2374
|
-
};
|
|
2375
|
-
const onFormError = () => {
|
|
2376
|
-
const errorMessages = Object.values(errors).map((err) => err == null ? void 0 : err.message).filter(Boolean).join(", ");
|
|
2377
|
-
ui.toast.error("Invalid coupon data", {
|
|
2378
|
-
description: errorMessages
|
|
2379
|
-
});
|
|
2380
|
-
};
|
|
2381
|
-
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { children: [
|
|
2382
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
|
|
2383
|
-
/* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-xl font-semibold", children: "Coupon" }),
|
|
2384
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "transparent", onClick: onCancel, children: "Cancel" })
|
|
2385
|
-
] }),
|
|
2386
|
-
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
2387
|
-
"form",
|
|
2388
|
-
{
|
|
2389
|
-
onSubmit: handleSubmit(onFormSubmit, onFormError),
|
|
2390
|
-
className: "space-y-6 my-8",
|
|
2391
|
-
children: [
|
|
2392
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2393
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Name" }),
|
|
2394
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...register("name"), disabled }),
|
|
2395
|
-
errors.name && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.name.message })
|
|
2396
|
-
] }),
|
|
2397
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2398
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Description" }),
|
|
2399
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Textarea, { ...register("description"), disabled }),
|
|
2400
|
-
errors.description && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.description.message })
|
|
2401
|
-
] }),
|
|
2402
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
|
|
2403
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2404
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Start Date" }),
|
|
2405
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
|
|
2406
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2407
|
-
ui.Input,
|
|
2408
|
-
{
|
|
2409
|
-
type: "date",
|
|
2410
|
-
value: startsAt ? dayjs__default.default(startsAt).format("YYYY-MM-DD") : "",
|
|
2411
|
-
onChange: (e2) => handleDateTimeChange("starts_at", "date", e2.target.value),
|
|
2412
|
-
disabled,
|
|
2413
|
-
className: "flex-1"
|
|
2414
|
-
}
|
|
2415
|
-
),
|
|
2416
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2417
|
-
ui.Input,
|
|
2418
|
-
{
|
|
2419
|
-
type: "time",
|
|
2420
|
-
value: startsAt ? dayjs__default.default(startsAt).format("HH:mm") : "",
|
|
2421
|
-
onChange: (e2) => handleDateTimeChange("starts_at", "time", e2.target.value),
|
|
2422
|
-
disabled,
|
|
2423
|
-
className: "flex-1"
|
|
2424
|
-
}
|
|
2425
|
-
)
|
|
2426
|
-
] }),
|
|
2427
|
-
errors.starts_at && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.starts_at.message })
|
|
2428
|
-
] }),
|
|
2429
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2430
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "End Date" }),
|
|
2431
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
|
|
2432
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2433
|
-
ui.Input,
|
|
2434
|
-
{
|
|
2435
|
-
type: "date",
|
|
2436
|
-
value: endsAt ? dayjs__default.default(endsAt).format("YYYY-MM-DD") : "",
|
|
2437
|
-
onChange: (e2) => handleDateTimeChange("ends_at", "date", e2.target.value),
|
|
2438
|
-
disabled,
|
|
2439
|
-
className: "flex-1"
|
|
2440
|
-
}
|
|
2441
|
-
),
|
|
2442
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2443
|
-
ui.Input,
|
|
2444
|
-
{
|
|
2445
|
-
type: "time",
|
|
2446
|
-
value: endsAt ? dayjs__default.default(endsAt).format("HH:mm") : "",
|
|
2447
|
-
onChange: (e2) => handleDateTimeChange("ends_at", "time", e2.target.value),
|
|
2448
|
-
disabled,
|
|
2449
|
-
className: "flex-1"
|
|
2450
|
-
}
|
|
2451
|
-
)
|
|
2452
|
-
] }),
|
|
2453
|
-
errors.ends_at && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.ends_at.message })
|
|
2454
|
-
] })
|
|
2455
|
-
] }),
|
|
2456
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
|
|
2457
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2458
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Coupon Code" }),
|
|
2459
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...register("code"), disabled }),
|
|
2460
|
-
errors.code && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.code.message })
|
|
2461
|
-
] }),
|
|
2462
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2463
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Discount Type" }),
|
|
2464
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2465
|
-
Controller,
|
|
2466
|
-
{
|
|
2467
|
-
name: "discount_type",
|
|
2468
|
-
control,
|
|
2469
|
-
render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2470
|
-
ui.Select,
|
|
2471
|
-
{
|
|
2472
|
-
value: field.value,
|
|
2473
|
-
onValueChange: field.onChange,
|
|
2474
|
-
disabled,
|
|
2475
|
-
children: [
|
|
2476
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "Select discount type" }) }),
|
|
2477
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Select.Content, { children: [
|
|
2478
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "percentage", children: "Percentage" }),
|
|
2479
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "fixed", children: "Fixed Amount" })
|
|
2480
|
-
] })
|
|
2481
|
-
]
|
|
2482
|
-
}
|
|
2483
|
-
)
|
|
2484
|
-
}
|
|
2485
|
-
)
|
|
2486
|
-
] })
|
|
2487
|
-
] }),
|
|
2488
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
|
|
2489
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2490
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Discount Value" }),
|
|
2491
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2492
|
-
Controller,
|
|
2493
|
-
{
|
|
2494
|
-
name: "discount_value",
|
|
2495
|
-
control,
|
|
2496
|
-
render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
2497
|
-
ui.Input,
|
|
2498
|
-
{
|
|
2499
|
-
type: "number",
|
|
2500
|
-
value: field.value,
|
|
2501
|
-
min: 0,
|
|
2502
|
-
step: field.value % 1 === 0 ? 1 : 0.01,
|
|
2503
|
-
onChange: (e2) => field.onChange(Number(e2.target.value)),
|
|
2504
|
-
disabled
|
|
2505
|
-
}
|
|
2506
|
-
)
|
|
2507
|
-
}
|
|
2508
|
-
),
|
|
2509
|
-
errors.discount_value && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.discount_value.message })
|
|
2510
|
-
] }),
|
|
2511
|
-
discountType === "fixed" && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2512
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Currency Code" }),
|
|
2513
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...register("currency_code"), disabled }),
|
|
2514
|
-
errors.currency_code && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.currency_code.message })
|
|
2515
|
-
] })
|
|
2516
|
-
] }),
|
|
2517
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
|
|
2518
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2519
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Allocation" }),
|
|
2520
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2521
|
-
Controller,
|
|
2522
|
-
{
|
|
2523
|
-
name: "allocation",
|
|
2524
|
-
control,
|
|
2525
|
-
render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2526
|
-
ui.Select,
|
|
2527
|
-
{
|
|
2528
|
-
value: field.value,
|
|
2529
|
-
onValueChange: field.onChange,
|
|
2530
|
-
disabled,
|
|
2531
|
-
children: [
|
|
2532
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "Select allocation" }) }),
|
|
2533
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Select.Content, { children: [
|
|
2534
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "total", children: "Order Total" }),
|
|
2535
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "each", children: "Each Item" })
|
|
2536
|
-
] })
|
|
2537
|
-
]
|
|
2538
|
-
}
|
|
2539
|
-
)
|
|
2540
|
-
}
|
|
2541
|
-
)
|
|
2542
|
-
] }),
|
|
2543
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2544
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Target Type" }),
|
|
2545
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2546
|
-
Controller,
|
|
2547
|
-
{
|
|
2548
|
-
name: "target_type",
|
|
2549
|
-
control,
|
|
2550
|
-
render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2551
|
-
ui.Select,
|
|
2552
|
-
{
|
|
2553
|
-
value: field.value,
|
|
2554
|
-
onValueChange: field.onChange,
|
|
2555
|
-
disabled,
|
|
2556
|
-
children: [
|
|
2557
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "Select target" }) }),
|
|
2558
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Select.Content, { children: [
|
|
2559
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "order", children: "Order" }),
|
|
2560
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "items", children: "Items" })
|
|
2561
|
-
] })
|
|
2562
|
-
]
|
|
2563
|
-
}
|
|
2564
|
-
)
|
|
2565
|
-
}
|
|
2566
|
-
)
|
|
2567
|
-
] })
|
|
2568
|
-
] }),
|
|
2569
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
|
|
2570
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "secondary", type: "button", onClick: onCancel, children: "Cancel" }),
|
|
2571
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "submit", disabled, children: "Save Coupon" })
|
|
2572
|
-
] })
|
|
2573
|
-
]
|
|
2574
|
-
}
|
|
2575
|
-
)
|
|
2576
|
-
] });
|
|
2577
|
-
};
|
|
2578
|
-
const useCouponById = (id) => {
|
|
2579
|
-
const [data, setData] = React.useState(null);
|
|
2580
|
-
const [isLoading, setIsLoading] = React.useState(true);
|
|
2581
|
-
const [error, setError] = React.useState(null);
|
|
2582
|
-
const fetchCoupon = async () => {
|
|
2583
|
-
if (!id) {
|
|
2584
|
-
return;
|
|
2585
|
-
}
|
|
2586
|
-
setIsLoading(true);
|
|
2587
|
-
setError(null);
|
|
2588
|
-
try {
|
|
2589
|
-
const response = await axios__default.default.get(`/admin/coupons/${id}`);
|
|
2590
|
-
setData(response.data);
|
|
2591
|
-
} catch (err) {
|
|
2592
|
-
setError(err instanceof Error ? err : new Error("Failed to fetch coupon"));
|
|
2593
|
-
} finally {
|
|
2594
|
-
setIsLoading(false);
|
|
2595
|
-
}
|
|
2596
|
-
};
|
|
2597
|
-
React.useEffect(() => {
|
|
2598
|
-
fetchCoupon();
|
|
2599
|
-
}, [id]);
|
|
2600
|
-
return {
|
|
2601
|
-
data,
|
|
2602
|
-
isLoading,
|
|
2603
|
-
error,
|
|
2604
|
-
refetch: fetchCoupon
|
|
2605
|
-
};
|
|
2606
|
-
};
|
|
2607
|
-
const MarkdownEditor = ({
|
|
2608
|
-
label,
|
|
2609
|
-
value,
|
|
2610
|
-
onChange,
|
|
2611
|
-
placeholder,
|
|
2612
|
-
helpText,
|
|
2613
|
-
rows = 10,
|
|
2614
|
-
showPreview = true
|
|
2615
|
-
}) => {
|
|
2616
|
-
const [activeTab, setActiveTab] = React.useState("write");
|
|
2617
|
-
const insertMarkdown = (before, after = "") => {
|
|
2618
|
-
const textarea = document.getElementById(
|
|
2619
|
-
`markdown-${label}`
|
|
2620
|
-
);
|
|
2621
|
-
if (!textarea) return;
|
|
2622
|
-
const start = textarea.selectionStart;
|
|
2623
|
-
const end = textarea.selectionEnd;
|
|
2624
|
-
const selectedText = value.substring(start, end);
|
|
2625
|
-
const newText = value.substring(0, start) + before + selectedText + after + value.substring(end);
|
|
2626
|
-
onChange(newText);
|
|
2627
|
-
setTimeout(() => {
|
|
2628
|
-
textarea.focus();
|
|
2629
|
-
textarea.setSelectionRange(
|
|
2630
|
-
start + before.length,
|
|
2631
|
-
start + before.length + selectedText.length
|
|
2632
|
-
);
|
|
2633
|
-
}, 0);
|
|
2634
|
-
};
|
|
2635
|
-
const renderMarkdownPreview = (markdown) => {
|
|
2636
|
-
let html = markdown;
|
|
2637
|
-
html = html.replace(/^### (.*$)/gim, "<h3>$1</h3>");
|
|
2638
|
-
html = html.replace(/^## (.*$)/gim, "<h2>$1</h2>");
|
|
2639
|
-
html = html.replace(/^# (.*$)/gim, "<h1>$1</h1>");
|
|
2640
|
-
html = html.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>");
|
|
2641
|
-
html = html.replace(/__(.+?)__/g, "<strong>$1</strong>");
|
|
2642
|
-
html = html.replace(/\*(.+?)\*/g, "<em>$1</em>");
|
|
2643
|
-
html = html.replace(/_(.+?)_/g, "<em>$1</em>");
|
|
2644
|
-
html = html.replace(
|
|
2645
|
-
/```(\w+)?\n([\s\S]+?)```/g,
|
|
2646
|
-
'<pre class="bg-ui-bg-subtle p-4 rounded-lg overflow-x-auto"><code class="language-$1">$2</code></pre>'
|
|
2647
|
-
);
|
|
2648
|
-
html = html.replace(
|
|
2649
|
-
/`([^`]+)`/g,
|
|
2650
|
-
'<code class="bg-ui-bg-subtle px-1 py-0.5 rounded text-sm">$1</code>'
|
|
2651
|
-
);
|
|
2652
|
-
html = html.replace(
|
|
2653
|
-
/\[([^\]]+)\]\(([^)]+)\)/g,
|
|
2654
|
-
'<a href="$2" class="text-ui-fg-interactive underline">$1</a>'
|
|
2655
|
-
);
|
|
2656
|
-
html = html.replace(
|
|
2657
|
-
/!\[([^\]]*)\]\(([^)]+)\)/g,
|
|
2658
|
-
'<img src="$2" alt="$1" class="max-w-full h-auto rounded-lg my-2" />'
|
|
2659
|
-
);
|
|
2660
|
-
html = html.replace(/^\* (.+)$/gim, "<li>$1</li>");
|
|
2661
|
-
html = html.replace(/^\- (.+)$/gim, "<li>$1</li>");
|
|
2662
|
-
html = html.replace(
|
|
2663
|
-
/(<li>.*<\/li>)/s,
|
|
2664
|
-
"<ul class='list-disc ml-6'>$1</ul>"
|
|
2665
|
-
);
|
|
2666
|
-
html = html.replace(/\n\n/g, "</p><p>");
|
|
2667
|
-
html = `<p>${html}</p>`;
|
|
2668
|
-
return html;
|
|
2669
|
-
};
|
|
2670
|
-
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2671
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
|
|
2672
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: `markdown-${label}`, className: "font-medium", children: label }),
|
|
2673
|
-
helpText && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1 text-xs text-ui-fg-subtle", children: [
|
|
2674
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.InformationCircle, { className: "h-4 w-4" }),
|
|
2675
|
-
/* @__PURE__ */ jsxRuntime.jsx("span", { children: helpText })
|
|
2676
|
-
] })
|
|
2677
|
-
] }),
|
|
2678
|
-
showPreview ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Tabs, { value: activeTab, onValueChange: (v) => setActiveTab(v), children: [
|
|
2679
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Tabs.List, { children: [
|
|
2680
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Trigger, { value: "write", children: "Write" }),
|
|
2681
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Trigger, { value: "preview", children: "Preview" })
|
|
2682
|
-
] }),
|
|
2683
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Content, { value: "write", className: "mt-4", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2684
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-wrap gap-2 border-b pb-2", children: [
|
|
2685
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2686
|
-
ui.Button,
|
|
2687
|
-
{
|
|
2688
|
-
type: "button",
|
|
2689
|
-
variant: "secondary",
|
|
2690
|
-
size: "small",
|
|
2691
|
-
onClick: () => insertMarkdown("**", "**"),
|
|
2692
|
-
title: "Bold",
|
|
2693
|
-
children: /* @__PURE__ */ jsxRuntime.jsx("strong", { children: "B" })
|
|
2694
|
-
}
|
|
2695
|
-
),
|
|
2696
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2697
|
-
ui.Button,
|
|
2698
|
-
{
|
|
2699
|
-
type: "button",
|
|
2700
|
-
variant: "secondary",
|
|
2701
|
-
size: "small",
|
|
2702
|
-
onClick: () => insertMarkdown("*", "*"),
|
|
2703
|
-
title: "Italic",
|
|
2704
|
-
children: /* @__PURE__ */ jsxRuntime.jsx("em", { children: "I" })
|
|
2705
|
-
}
|
|
2706
|
-
),
|
|
2707
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2708
|
-
ui.Button,
|
|
2709
|
-
{
|
|
2710
|
-
type: "button",
|
|
2711
|
-
variant: "secondary",
|
|
2712
|
-
size: "small",
|
|
2713
|
-
onClick: () => insertMarkdown("`", "`"),
|
|
2714
|
-
title: "Inline Code",
|
|
2715
|
-
children: /* @__PURE__ */ jsxRuntime.jsx(icons.CommandLine, { className: "h-4 w-4" })
|
|
2716
|
-
}
|
|
2717
|
-
),
|
|
2718
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2719
|
-
ui.Button,
|
|
2720
|
-
{
|
|
2721
|
-
type: "button",
|
|
2722
|
-
variant: "secondary",
|
|
2723
|
-
size: "small",
|
|
2724
|
-
onClick: () => insertMarkdown("## "),
|
|
2725
|
-
title: "Heading",
|
|
2726
|
-
children: "H2"
|
|
2727
|
-
}
|
|
2728
|
-
),
|
|
2729
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2730
|
-
ui.Button,
|
|
2731
|
-
{
|
|
2732
|
-
type: "button",
|
|
2733
|
-
variant: "secondary",
|
|
2734
|
-
size: "small",
|
|
2735
|
-
onClick: () => insertMarkdown("### "),
|
|
2736
|
-
title: "Heading",
|
|
2737
|
-
children: "H3"
|
|
2738
|
-
}
|
|
2739
|
-
),
|
|
2740
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2741
|
-
ui.Button,
|
|
2742
|
-
{
|
|
2743
|
-
type: "button",
|
|
2744
|
-
variant: "secondary",
|
|
2745
|
-
size: "small",
|
|
2746
|
-
onClick: () => insertMarkdown("[", "](url)"),
|
|
2747
|
-
title: "Link",
|
|
2748
|
-
children: "Link"
|
|
2749
|
-
}
|
|
2750
|
-
),
|
|
2751
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2752
|
-
ui.Button,
|
|
2753
|
-
{
|
|
2754
|
-
type: "button",
|
|
2755
|
-
variant: "secondary",
|
|
2756
|
-
size: "small",
|
|
2757
|
-
onClick: () => insertMarkdown(""),
|
|
2758
|
-
title: "Image",
|
|
2759
|
-
children: "Image"
|
|
2760
|
-
}
|
|
2761
|
-
),
|
|
2762
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2763
|
-
ui.Button,
|
|
2764
|
-
{
|
|
2765
|
-
type: "button",
|
|
2766
|
-
variant: "secondary",
|
|
2767
|
-
size: "small",
|
|
2768
|
-
onClick: () => insertMarkdown("```\n", "\n```"),
|
|
2769
|
-
title: "Code Block",
|
|
2770
|
-
children: "Code"
|
|
2771
|
-
}
|
|
2772
|
-
),
|
|
2773
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2774
|
-
ui.Button,
|
|
2775
|
-
{
|
|
2776
|
-
type: "button",
|
|
2777
|
-
variant: "secondary",
|
|
2778
|
-
size: "small",
|
|
2779
|
-
onClick: () => insertMarkdown("- "),
|
|
2780
|
-
title: "List",
|
|
2781
|
-
children: "List"
|
|
2782
|
-
}
|
|
2783
|
-
)
|
|
2784
|
-
] }),
|
|
2785
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2786
|
-
ui.Textarea,
|
|
2787
|
-
{
|
|
2788
|
-
id: `markdown-${label}`,
|
|
2789
|
-
value,
|
|
2790
|
-
onChange: (e2) => onChange(e2.target.value),
|
|
2791
|
-
placeholder,
|
|
2792
|
-
rows,
|
|
2793
|
-
className: "font-mono text-sm"
|
|
2794
|
-
}
|
|
2795
|
-
)
|
|
2796
|
-
] }) }),
|
|
2797
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Content, { value: "preview", className: "mt-4", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
2798
|
-
"div",
|
|
2799
|
-
{
|
|
2800
|
-
className: ui.clx(
|
|
2801
|
-
"min-h-[200px] rounded-lg border border-ui-border-base bg-ui-bg-subtle p-4",
|
|
2802
|
-
"prose prose-sm max-w-none"
|
|
2803
|
-
),
|
|
2804
|
-
dangerouslySetInnerHTML: {
|
|
2805
|
-
__html: value ? renderMarkdownPreview(value) : '<p class="text-ui-fg-subtle">Nothing to preview</p>'
|
|
2806
|
-
}
|
|
2807
|
-
}
|
|
2808
|
-
) })
|
|
2809
|
-
] }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
2810
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-wrap gap-2 border-b pb-2", children: [
|
|
2811
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2812
|
-
ui.Button,
|
|
2813
|
-
{
|
|
2814
|
-
type: "button",
|
|
2815
|
-
variant: "secondary",
|
|
2816
|
-
size: "small",
|
|
2817
|
-
onClick: () => insertMarkdown("**", "**"),
|
|
2818
|
-
title: "Bold",
|
|
2819
|
-
children: /* @__PURE__ */ jsxRuntime.jsx("strong", { children: "B" })
|
|
2820
|
-
}
|
|
2821
|
-
),
|
|
2822
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2823
|
-
ui.Button,
|
|
2824
|
-
{
|
|
2825
|
-
type: "button",
|
|
2826
|
-
variant: "secondary",
|
|
2827
|
-
size: "small",
|
|
2828
|
-
onClick: () => insertMarkdown("*", "*"),
|
|
2829
|
-
title: "Italic",
|
|
2830
|
-
children: /* @__PURE__ */ jsxRuntime.jsx("em", { children: "I" })
|
|
2831
|
-
}
|
|
2832
|
-
),
|
|
2833
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2834
|
-
ui.Button,
|
|
2835
|
-
{
|
|
2836
|
-
type: "button",
|
|
2837
|
-
variant: "secondary",
|
|
2838
|
-
size: "small",
|
|
2839
|
-
onClick: () => insertMarkdown("`", "`"),
|
|
2840
|
-
title: "Inline Code",
|
|
2841
|
-
children: /* @__PURE__ */ jsxRuntime.jsx(icons.CommandLine, { className: "h-4 w-4" })
|
|
2842
|
-
}
|
|
2843
|
-
),
|
|
2844
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2845
|
-
ui.Button,
|
|
2846
|
-
{
|
|
2847
|
-
type: "button",
|
|
2848
|
-
variant: "secondary",
|
|
2849
|
-
size: "small",
|
|
2850
|
-
onClick: () => insertMarkdown("```\n", "\n```"),
|
|
2851
|
-
title: "Code Block",
|
|
2852
|
-
children: "Code"
|
|
2853
|
-
}
|
|
2854
|
-
)
|
|
2855
|
-
] }),
|
|
2856
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2857
|
-
ui.Textarea,
|
|
2858
|
-
{
|
|
2859
|
-
id: `markdown-${label}`,
|
|
2860
|
-
value,
|
|
2861
|
-
onChange: (e2) => onChange(e2.target.value),
|
|
2862
|
-
placeholder,
|
|
2863
|
-
rows,
|
|
2864
|
-
className: "font-mono text-sm"
|
|
2865
|
-
}
|
|
2866
|
-
)
|
|
2867
|
-
] }),
|
|
2868
|
-
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-xs text-ui-fg-subtle", children: "Supports Markdown formatting: **bold**, *italic*, `code`, ```code blocks```, [links](url), , and lists" })
|
|
2869
|
-
] });
|
|
2870
|
-
};
|
|
2871
|
-
const CampaignImageUploader = ({
|
|
2872
|
-
campaignId,
|
|
2873
|
-
imageType,
|
|
2874
|
-
currentImageUrl,
|
|
2875
|
-
onClose,
|
|
2876
|
-
onSuccess
|
|
2877
|
-
}) => {
|
|
2878
|
-
const [displayImage, setDisplayImage] = React.useState(currentImageUrl || null);
|
|
2879
|
-
const [isUploading, setIsUploading] = React.useState(false);
|
|
2880
|
-
const [isDragging, setIsDragging] = React.useState(false);
|
|
2881
|
-
const [error, setError] = React.useState(null);
|
|
2882
|
-
const [previewUrl, setPreviewUrl] = React.useState(null);
|
|
2883
|
-
const isThumbnail = imageType === "thumbnail";
|
|
2884
|
-
const maxSize = 5;
|
|
2885
|
-
const allowedTypes = ["image/jpeg", "image/png", "image/gif", "image/webp"];
|
|
2886
|
-
const formatFileTypes = () => {
|
|
2887
|
-
return "JPEG, PNG, GIF, or WebP";
|
|
2888
|
-
};
|
|
2889
|
-
const validateFile = (file) => {
|
|
2890
|
-
if (!allowedTypes.includes(file.type)) {
|
|
2891
|
-
setError(`Please upload a valid image file (${formatFileTypes()})`);
|
|
2892
|
-
return false;
|
|
2893
|
-
}
|
|
2894
|
-
if (file.size > maxSize * 1024 * 1024) {
|
|
2895
|
-
setError(`File size must be less than ${maxSize}MB`);
|
|
2896
|
-
return false;
|
|
2897
|
-
}
|
|
2898
|
-
return true;
|
|
2899
|
-
};
|
|
2900
|
-
const handleFileUpload = async (file) => {
|
|
2901
|
-
var _a, _b;
|
|
2902
|
-
if (!validateFile(file)) return;
|
|
2903
|
-
setError(null);
|
|
2904
|
-
const reader = new FileReader();
|
|
2905
|
-
reader.onloadend = () => {
|
|
2906
|
-
setPreviewUrl(reader.result);
|
|
2907
|
-
};
|
|
2908
|
-
reader.readAsDataURL(file);
|
|
2909
|
-
setIsUploading(true);
|
|
2910
|
-
const formData = new FormData();
|
|
2911
|
-
formData.append("file", file);
|
|
2912
|
-
try {
|
|
2913
|
-
const response = await fetch(
|
|
2914
|
-
`/admin/campaigns/${campaignId}/${imageType}`,
|
|
2915
|
-
{
|
|
2916
|
-
method: "POST",
|
|
2917
|
-
body: formData,
|
|
2918
|
-
credentials: "include"
|
|
2919
|
-
}
|
|
2920
|
-
);
|
|
2921
|
-
if (response.ok) {
|
|
2922
|
-
const result = await response.json();
|
|
2923
|
-
const newImageUrl = imageType === "image" ? (_a = result.campaign_detail) == null ? void 0 : _a.image_url : (_b = result.campaign_detail) == null ? void 0 : _b.thumbnail_url;
|
|
2924
|
-
setDisplayImage(newImageUrl);
|
|
2925
|
-
setPreviewUrl(null);
|
|
2926
|
-
onSuccess == null ? void 0 : onSuccess();
|
|
2927
|
-
setTimeout(() => {
|
|
2928
|
-
onClose();
|
|
2929
|
-
}, 1e3);
|
|
2930
|
-
} else {
|
|
2931
|
-
const errorData = await response.json();
|
|
2932
|
-
setError(errorData.error || `Failed to upload ${imageType}`);
|
|
2933
|
-
setPreviewUrl(null);
|
|
2934
|
-
}
|
|
2935
|
-
} catch (err) {
|
|
2936
|
-
setError(`Error uploading ${imageType}`);
|
|
2937
|
-
setPreviewUrl(null);
|
|
2938
|
-
console.error(`Error uploading ${imageType}:`, err);
|
|
2939
|
-
} finally {
|
|
2940
|
-
setIsUploading(false);
|
|
2941
|
-
}
|
|
2942
|
-
};
|
|
2943
|
-
const handleDelete = async () => {
|
|
2944
|
-
if (!confirm(`Are you sure you want to delete the ${imageType}?`)) return;
|
|
2945
|
-
setIsUploading(true);
|
|
2946
|
-
setError(null);
|
|
2947
|
-
try {
|
|
2948
|
-
const response = await fetch(
|
|
2949
|
-
`/admin/campaigns/${campaignId}/${imageType}`,
|
|
2950
|
-
{
|
|
2951
|
-
method: "DELETE",
|
|
2952
|
-
credentials: "include"
|
|
2953
|
-
}
|
|
2954
|
-
);
|
|
2955
|
-
if (response.ok) {
|
|
2956
|
-
setDisplayImage(null);
|
|
2957
|
-
setPreviewUrl(null);
|
|
2958
|
-
onSuccess == null ? void 0 : onSuccess();
|
|
2959
|
-
setTimeout(() => {
|
|
2960
|
-
onClose();
|
|
2961
|
-
}, 500);
|
|
2962
|
-
} else {
|
|
2963
|
-
const errorData = await response.json();
|
|
2964
|
-
setError(errorData.error || `Failed to delete ${imageType}`);
|
|
2965
|
-
}
|
|
2966
|
-
} catch (err) {
|
|
2967
|
-
setError(`Error deleting ${imageType}`);
|
|
2968
|
-
console.error(`Error deleting ${imageType}:`, err);
|
|
2969
|
-
} finally {
|
|
2970
|
-
setIsUploading(false);
|
|
2971
|
-
}
|
|
2972
|
-
};
|
|
2973
|
-
const handleDragEnter = React.useCallback((e2) => {
|
|
2974
|
-
e2.preventDefault();
|
|
2975
|
-
e2.stopPropagation();
|
|
2976
|
-
setIsDragging(true);
|
|
2977
|
-
}, []);
|
|
2978
|
-
const handleDragLeave = React.useCallback((e2) => {
|
|
2979
|
-
e2.preventDefault();
|
|
2980
|
-
e2.stopPropagation();
|
|
2981
|
-
setIsDragging(false);
|
|
2982
|
-
}, []);
|
|
2983
|
-
const handleDragOver = React.useCallback((e2) => {
|
|
2984
|
-
e2.preventDefault();
|
|
2985
|
-
e2.stopPropagation();
|
|
2986
|
-
}, []);
|
|
2987
|
-
const handleDrop = React.useCallback((e2) => {
|
|
2988
|
-
var _a;
|
|
2989
|
-
e2.preventDefault();
|
|
2990
|
-
e2.stopPropagation();
|
|
2991
|
-
setIsDragging(false);
|
|
2992
|
-
const file = (_a = e2.dataTransfer.files) == null ? void 0 : _a[0];
|
|
2993
|
-
if (file) {
|
|
2994
|
-
handleFileUpload(file);
|
|
2995
|
-
}
|
|
2996
|
-
}, []);
|
|
2997
|
-
const imageToDisplay = previewUrl || displayImage;
|
|
2998
|
-
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "fixed inset-0 z-50 flex items-center justify-center bg-black/50", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full max-w-2xl rounded-lg bg-ui-bg-base p-6", children: [
|
|
2999
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mb-6 flex items-center justify-between", children: [
|
|
3000
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
3001
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Heading, { level: "h2", children: [
|
|
3002
|
-
"Upload Campaign ",
|
|
3003
|
-
isThumbnail ? "Thumbnail" : "Image"
|
|
3004
|
-
] }),
|
|
3005
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-ui-fg-subtle", children: isThumbnail ? "Thumbnail for campaign listing" : "Main campaign image" })
|
|
3006
|
-
] }),
|
|
3007
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "secondary", size: "small", onClick: onClose, children: /* @__PURE__ */ jsxRuntime.jsx(icons.X, {}) })
|
|
3008
|
-
] }),
|
|
3009
|
-
error && /* @__PURE__ */ jsxRuntime.jsx(ui.Alert, { variant: "error", dismissible: true, className: "mb-4", children: error }),
|
|
3010
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
|
|
3011
|
-
imageToDisplay ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
|
|
3012
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "relative overflow-hidden rounded-lg border border-ui-border-base bg-ui-bg-subtle", children: [
|
|
3013
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3014
|
-
"img",
|
|
3015
|
-
{
|
|
3016
|
-
src: imageToDisplay,
|
|
3017
|
-
alt: `Campaign ${imageType}`,
|
|
3018
|
-
className: ui.clx(
|
|
3019
|
-
"w-full object-contain",
|
|
3020
|
-
isThumbnail ? "h-32" : "h-64"
|
|
3021
|
-
)
|
|
3022
|
-
}
|
|
3023
|
-
),
|
|
3024
|
-
isUploading && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "absolute inset-0 flex items-center justify-center bg-black/50", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-white", children: "Uploading..." }) })
|
|
3025
|
-
] }),
|
|
3026
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
|
|
3027
|
-
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
3028
|
-
ui.Button,
|
|
3029
|
-
{
|
|
3030
|
-
variant: "secondary",
|
|
3031
|
-
disabled: isUploading,
|
|
3032
|
-
onClick: () => {
|
|
3033
|
-
var _a;
|
|
3034
|
-
return (_a = document.getElementById(`${imageType}-file-input`)) == null ? void 0 : _a.click();
|
|
3035
|
-
},
|
|
3036
|
-
children: [
|
|
3037
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.CloudArrowUp, { className: "mr-2" }),
|
|
3038
|
-
"Replace"
|
|
3039
|
-
]
|
|
3040
|
-
}
|
|
3041
|
-
),
|
|
3042
|
-
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
3043
|
-
ui.Button,
|
|
3044
|
-
{
|
|
3045
|
-
variant: "danger",
|
|
3046
|
-
disabled: isUploading,
|
|
3047
|
-
onClick: handleDelete,
|
|
3048
|
-
children: [
|
|
3049
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "mr-2" }),
|
|
3050
|
-
"Delete"
|
|
3051
|
-
]
|
|
3052
|
-
}
|
|
3053
|
-
)
|
|
3054
|
-
] })
|
|
3055
|
-
] }) : /* @__PURE__ */ jsxRuntime.jsxs(
|
|
3056
|
-
"div",
|
|
3057
|
-
{
|
|
3058
|
-
className: ui.clx(
|
|
3059
|
-
"flex flex-col items-center justify-center rounded-lg border-2 border-dashed transition-colors",
|
|
3060
|
-
isThumbnail ? "h-48" : "h-64",
|
|
3061
|
-
isDragging ? "border-ui-border-interactive bg-ui-bg-highlight" : "border-ui-border-base bg-ui-bg-subtle",
|
|
3062
|
-
!isUploading && "cursor-pointer"
|
|
3063
|
-
),
|
|
3064
|
-
onDragEnter: handleDragEnter,
|
|
3065
|
-
onDragLeave: handleDragLeave,
|
|
3066
|
-
onDragOver: handleDragOver,
|
|
3067
|
-
onDrop: handleDrop,
|
|
3068
|
-
onClick: () => {
|
|
3069
|
-
var _a;
|
|
3070
|
-
return !isUploading && ((_a = document.getElementById(`${imageType}-file-input`)) == null ? void 0 : _a.click());
|
|
3071
|
-
},
|
|
3072
|
-
children: [
|
|
3073
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.PhotoSolid, { className: "mb-4 h-12 w-12 text-ui-fg-subtle" }),
|
|
3074
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "mb-2 text-lg font-medium", children: isDragging ? `Drop ${imageType} here` : `Upload ${isThumbnail ? "Thumbnail" : "Image"}` }),
|
|
3075
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "mb-4 text-center text-ui-fg-subtle", children: "Drag and drop an image here, or click to select" }),
|
|
3076
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { className: "text-sm text-ui-fg-subtle", children: [
|
|
3077
|
-
formatFileTypes(),
|
|
3078
|
-
" • Max ",
|
|
3079
|
-
maxSize,
|
|
3080
|
-
"MB"
|
|
3081
|
-
] }),
|
|
3082
|
-
isThumbnail && /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "mt-2 text-xs text-ui-fg-subtle", children: "Recommended: 16:9 aspect ratio, minimum 400x225px" }),
|
|
3083
|
-
!isThumbnail && /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "mt-2 text-xs text-ui-fg-subtle", children: "Recommended: High resolution, minimum 1200x600px" }),
|
|
3084
|
-
isUploading && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "mt-4", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { children: "Uploading..." }) })
|
|
3085
|
-
]
|
|
3086
|
-
}
|
|
3087
|
-
),
|
|
3088
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3089
|
-
"input",
|
|
3090
|
-
{
|
|
3091
|
-
id: `${imageType}-file-input`,
|
|
3092
|
-
type: "file",
|
|
3093
|
-
accept: allowedTypes.join(","),
|
|
3094
|
-
onChange: (e2) => {
|
|
3095
|
-
var _a;
|
|
3096
|
-
const file = (_a = e2.target.files) == null ? void 0 : _a[0];
|
|
3097
|
-
if (file) handleFileUpload(file);
|
|
3098
|
-
e2.target.value = "";
|
|
3099
|
-
},
|
|
3100
|
-
className: "hidden"
|
|
3101
|
-
}
|
|
3102
|
-
)
|
|
3103
|
-
] }),
|
|
3104
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mt-6 flex items-center justify-between border-t pt-4", children: [
|
|
3105
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Text, { className: "text-sm text-ui-fg-subtle", children: isThumbnail ? "Thumbnail will be displayed in campaign listings" : "Main image for the campaign detail page" }),
|
|
3106
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "secondary", onClick: onClose, children: "Close" })
|
|
3107
|
-
] })
|
|
3108
|
-
] }) });
|
|
3109
|
-
};
|
|
3110
|
-
const CampaignDetailForm = ({
|
|
3111
|
-
campaignId,
|
|
3112
|
-
campaignName
|
|
3113
|
-
}) => {
|
|
3114
|
-
const [campaignDetail, setCampaignDetail] = React.useState(
|
|
3115
|
-
null
|
|
3116
|
-
);
|
|
3117
|
-
const [isLoading, setIsLoading] = React.useState(true);
|
|
3118
|
-
const [isSaving, setIsSaving] = React.useState(false);
|
|
3119
|
-
const [showImageUploader, setShowImageUploader] = React.useState(null);
|
|
3120
|
-
const [formData, setFormData] = React.useState({
|
|
3121
|
-
detail_content: "",
|
|
3122
|
-
terms_and_conditions: "",
|
|
3123
|
-
meta_title: "",
|
|
3124
|
-
meta_description: "",
|
|
3125
|
-
meta_keywords: "",
|
|
3126
|
-
link_url: "",
|
|
3127
|
-
link_text: "",
|
|
3128
|
-
display_order: 0
|
|
3129
|
-
});
|
|
3130
|
-
React.useEffect(() => {
|
|
3131
|
-
fetchCampaignDetail();
|
|
3132
|
-
}, [campaignId]);
|
|
3133
|
-
const fetchCampaignDetail = async () => {
|
|
3134
|
-
setIsLoading(true);
|
|
3135
|
-
try {
|
|
3136
|
-
const response = await fetch(`/admin/campaigns/${campaignId}/detail`, {
|
|
3137
|
-
credentials: "include"
|
|
3138
|
-
});
|
|
3139
|
-
if (response.ok) {
|
|
3140
|
-
const data = await response.json();
|
|
3141
|
-
if (data.campaign_detail) {
|
|
3142
|
-
console.log("Campaign detail loaded:", {
|
|
3143
|
-
image_url: data.campaign_detail.image_url,
|
|
3144
|
-
thumbnail_url: data.campaign_detail.thumbnail_url,
|
|
3145
|
-
decoded_image: data.campaign_detail.image_url ? decodeURIComponent(data.campaign_detail.image_url) : null,
|
|
3146
|
-
decoded_thumbnail: data.campaign_detail.thumbnail_url ? decodeURIComponent(data.campaign_detail.thumbnail_url) : null
|
|
3147
|
-
});
|
|
3148
|
-
setCampaignDetail(data.campaign_detail);
|
|
3149
|
-
setFormData({
|
|
3150
|
-
detail_content: data.campaign_detail.detail_content || "",
|
|
3151
|
-
terms_and_conditions: data.campaign_detail.terms_and_conditions || "",
|
|
3152
|
-
meta_title: data.campaign_detail.meta_title || "",
|
|
3153
|
-
meta_description: data.campaign_detail.meta_description || "",
|
|
3154
|
-
meta_keywords: data.campaign_detail.meta_keywords || "",
|
|
3155
|
-
link_url: data.campaign_detail.link_url || "",
|
|
3156
|
-
link_text: data.campaign_detail.link_text || "",
|
|
3157
|
-
display_order: data.campaign_detail.display_order || 0
|
|
3158
|
-
});
|
|
3159
|
-
}
|
|
3160
|
-
} else if (response.status !== 404) {
|
|
3161
|
-
console.error("Failed to fetch campaign detail");
|
|
3162
|
-
}
|
|
3163
|
-
} catch (error) {
|
|
3164
|
-
console.error("Error fetching campaign detail:", error);
|
|
3165
|
-
} finally {
|
|
3166
|
-
setIsLoading(false);
|
|
3167
|
-
}
|
|
3168
|
-
};
|
|
3169
|
-
const handleSave = async () => {
|
|
3170
|
-
setIsSaving(true);
|
|
3171
|
-
try {
|
|
3172
|
-
const response = await fetch(`/admin/campaigns/${campaignId}/detail`, {
|
|
3173
|
-
method: "POST",
|
|
3174
|
-
headers: {
|
|
3175
|
-
"Content-Type": "application/json"
|
|
3176
|
-
},
|
|
3177
|
-
body: JSON.stringify(formData),
|
|
3178
|
-
credentials: "include"
|
|
3179
|
-
});
|
|
3180
|
-
if (response.ok) {
|
|
3181
|
-
const data = await response.json();
|
|
3182
|
-
setCampaignDetail(data.campaign_detail);
|
|
3183
|
-
ui.toast.success("Campaign details saved successfully");
|
|
3184
|
-
} else {
|
|
3185
|
-
const errorData = await response.json();
|
|
3186
|
-
ui.toast.error(errorData.error || "Failed to save campaign details");
|
|
3187
|
-
}
|
|
3188
|
-
} catch (error) {
|
|
3189
|
-
console.error("Error saving campaign detail:", error);
|
|
3190
|
-
ui.toast.error("Error saving campaign details");
|
|
3191
|
-
} finally {
|
|
3192
|
-
setIsSaving(false);
|
|
3193
|
-
}
|
|
3194
|
-
};
|
|
3195
|
-
if (isLoading) {
|
|
3196
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "py-8 text-center", children: "Loading campaign details..." }) });
|
|
3197
|
-
}
|
|
3198
|
-
return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
3199
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { className: "space-y-6", children: [
|
|
3200
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
|
|
3201
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
3202
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h2", children: "Campaign Details" }),
|
|
3203
|
-
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm text-ui-fg-subtle", children: campaignName })
|
|
3204
|
-
] }),
|
|
3205
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: handleSave, isLoading: isSaving, children: "Save Details" })
|
|
3206
|
-
] }),
|
|
3207
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
|
|
3208
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h3", children: "Images" }),
|
|
3209
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
|
|
3210
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
3211
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Main Campaign Image" }),
|
|
3212
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3213
|
-
"div",
|
|
3214
|
-
{
|
|
3215
|
-
className: ui.clx(
|
|
3216
|
-
"relative flex h-64 items-center justify-center overflow-hidden rounded-lg border-2 border-dashed",
|
|
3217
|
-
(campaignDetail == null ? void 0 : campaignDetail.image_url) ? "border-ui-border-base" : "border-ui-border-base bg-ui-bg-subtle"
|
|
3218
|
-
),
|
|
3219
|
-
children: (campaignDetail == null ? void 0 : campaignDetail.image_url) ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
3220
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3221
|
-
"img",
|
|
3222
|
-
{
|
|
3223
|
-
src: campaignDetail.image_url,
|
|
3224
|
-
alt: "Campaign",
|
|
3225
|
-
className: "h-full w-full object-cover",
|
|
3226
|
-
onError: (e2) => {
|
|
3227
|
-
console.error(
|
|
3228
|
-
"Failed to load image:",
|
|
3229
|
-
campaignDetail.image_url
|
|
3230
|
-
);
|
|
3231
|
-
e2.currentTarget.style.display = "none";
|
|
3232
|
-
}
|
|
3233
|
-
}
|
|
3234
|
-
),
|
|
3235
|
-
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
3236
|
-
ui.Button,
|
|
3237
|
-
{
|
|
3238
|
-
variant: "secondary",
|
|
3239
|
-
size: "small",
|
|
3240
|
-
className: "absolute bottom-2 right-2",
|
|
3241
|
-
onClick: () => setShowImageUploader("image"),
|
|
3242
|
-
children: [
|
|
3243
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, { className: "mr-1" }),
|
|
3244
|
-
"Change"
|
|
3245
|
-
]
|
|
3246
|
-
}
|
|
3247
|
-
)
|
|
3248
|
-
] }) : /* @__PURE__ */ jsxRuntime.jsxs(
|
|
3249
|
-
ui.Button,
|
|
3250
|
-
{
|
|
3251
|
-
variant: "secondary",
|
|
3252
|
-
onClick: () => setShowImageUploader("image"),
|
|
3253
|
-
children: [
|
|
3254
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.PhotoSolid, { className: "mr-2" }),
|
|
3255
|
-
"Upload Image"
|
|
3256
|
-
]
|
|
3257
|
-
}
|
|
3258
|
-
)
|
|
3259
|
-
}
|
|
3260
|
-
)
|
|
3261
|
-
] }),
|
|
3262
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
3263
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Thumbnail" }),
|
|
3264
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3265
|
-
"div",
|
|
3266
|
-
{
|
|
3267
|
-
className: ui.clx(
|
|
3268
|
-
"relative flex h-64 items-center justify-center overflow-hidden rounded-lg border-2 border-dashed",
|
|
3269
|
-
(campaignDetail == null ? void 0 : campaignDetail.thumbnail_url) ? "border-ui-border-base" : "border-ui-border-base bg-ui-bg-subtle"
|
|
3270
|
-
),
|
|
3271
|
-
children: (campaignDetail == null ? void 0 : campaignDetail.thumbnail_url) ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
3272
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3273
|
-
"img",
|
|
3274
|
-
{
|
|
3275
|
-
src: campaignDetail.thumbnail_url,
|
|
3276
|
-
alt: "Thumbnail",
|
|
3277
|
-
className: "h-full w-full object-cover",
|
|
3278
|
-
onError: (e2) => {
|
|
3279
|
-
console.error(
|
|
3280
|
-
"Failed to load thumbnail:",
|
|
3281
|
-
campaignDetail.thumbnail_url
|
|
3282
|
-
);
|
|
3283
|
-
e2.currentTarget.style.display = "none";
|
|
3284
|
-
}
|
|
3285
|
-
}
|
|
3286
|
-
),
|
|
3287
|
-
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
3288
|
-
ui.Button,
|
|
3289
|
-
{
|
|
3290
|
-
variant: "secondary",
|
|
3291
|
-
size: "small",
|
|
3292
|
-
className: "absolute bottom-2 right-2",
|
|
3293
|
-
onClick: () => setShowImageUploader("thumbnail"),
|
|
3294
|
-
children: [
|
|
3295
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, { className: "mr-1" }),
|
|
3296
|
-
"Change"
|
|
3297
|
-
]
|
|
3298
|
-
}
|
|
3299
|
-
)
|
|
3300
|
-
] }) : /* @__PURE__ */ jsxRuntime.jsxs(
|
|
3301
|
-
ui.Button,
|
|
3302
|
-
{
|
|
3303
|
-
variant: "secondary",
|
|
3304
|
-
onClick: () => setShowImageUploader("thumbnail"),
|
|
3305
|
-
children: [
|
|
3306
|
-
/* @__PURE__ */ jsxRuntime.jsx(icons.PhotoSolid, { className: "mr-2" }),
|
|
3307
|
-
"Upload Thumbnail"
|
|
3308
|
-
]
|
|
3309
|
-
}
|
|
3310
|
-
)
|
|
3311
|
-
}
|
|
3312
|
-
)
|
|
3313
|
-
] })
|
|
3314
|
-
] })
|
|
3315
|
-
] }),
|
|
3316
|
-
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "space-y-4", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3317
|
-
MarkdownEditor,
|
|
3318
|
-
{
|
|
3319
|
-
label: "Campaign Detail Content",
|
|
3320
|
-
value: formData.detail_content,
|
|
3321
|
-
onChange: (value) => setFormData({ ...formData, detail_content: value }),
|
|
3322
|
-
placeholder: "Enter campaign details in Markdown format. You can include images, links, code blocks, and more...",
|
|
3323
|
-
helpText: "Supports Markdown and code syntax",
|
|
3324
|
-
rows: 12,
|
|
3325
|
-
showPreview: true
|
|
3326
|
-
}
|
|
3327
|
-
) }),
|
|
3328
|
-
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "space-y-4", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3329
|
-
MarkdownEditor,
|
|
3330
|
-
{
|
|
3331
|
-
label: "Terms and Conditions",
|
|
3332
|
-
value: formData.terms_and_conditions,
|
|
3333
|
-
onChange: (value) => setFormData({ ...formData, terms_and_conditions: value }),
|
|
3334
|
-
placeholder: "Enter terms and conditions for this campaign...",
|
|
3335
|
-
helpText: "Optional - Campaign specific terms",
|
|
3336
|
-
rows: 8,
|
|
3337
|
-
showPreview: true
|
|
3338
|
-
}
|
|
3339
|
-
) }),
|
|
3340
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
|
|
3341
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h3", children: "Call to Action Link" }),
|
|
3342
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
|
|
3343
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
3344
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "link-url", children: "Link URL" }),
|
|
3345
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3346
|
-
ui.Input,
|
|
3347
|
-
{
|
|
3348
|
-
id: "link-url",
|
|
3349
|
-
type: "url",
|
|
3350
|
-
value: formData.link_url,
|
|
3351
|
-
onChange: (e2) => setFormData({ ...formData, link_url: e2.target.value }),
|
|
3352
|
-
placeholder: "https://example.com/campaign"
|
|
3353
|
-
}
|
|
3354
|
-
)
|
|
3355
|
-
] }),
|
|
3356
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
3357
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "link-text", children: "Link Text" }),
|
|
3358
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3359
|
-
ui.Input,
|
|
3360
|
-
{
|
|
3361
|
-
id: "link-text",
|
|
3362
|
-
value: formData.link_text,
|
|
3363
|
-
onChange: (e2) => setFormData({ ...formData, link_text: e2.target.value }),
|
|
3364
|
-
placeholder: "Shop Now"
|
|
3365
|
-
}
|
|
3366
|
-
)
|
|
3367
|
-
] })
|
|
3368
|
-
] })
|
|
3369
|
-
] }),
|
|
3370
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
|
|
3371
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { level: "h3", children: "SEO & Metadata" }),
|
|
3372
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
|
|
3373
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
3374
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "meta-title", children: "Meta Title" }),
|
|
3375
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3376
|
-
ui.Input,
|
|
3377
|
-
{
|
|
3378
|
-
id: "meta-title",
|
|
3379
|
-
value: formData.meta_title,
|
|
3380
|
-
onChange: (e2) => setFormData({ ...formData, meta_title: e2.target.value }),
|
|
3381
|
-
placeholder: "Campaign meta title for SEO"
|
|
3382
|
-
}
|
|
3383
|
-
)
|
|
3384
|
-
] }),
|
|
3385
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
3386
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "meta-description", children: "Meta Description" }),
|
|
3387
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3388
|
-
ui.Input,
|
|
3389
|
-
{
|
|
3390
|
-
id: "meta-description",
|
|
3391
|
-
value: formData.meta_description,
|
|
3392
|
-
onChange: (e2) => setFormData({
|
|
3393
|
-
...formData,
|
|
3394
|
-
meta_description: e2.target.value
|
|
3395
|
-
}),
|
|
3396
|
-
placeholder: "Campaign meta description for SEO"
|
|
3397
|
-
}
|
|
3398
|
-
)
|
|
3399
|
-
] }),
|
|
3400
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
3401
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "meta-keywords", children: "Meta Keywords" }),
|
|
3402
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3403
|
-
ui.Input,
|
|
3404
|
-
{
|
|
3405
|
-
id: "meta-keywords",
|
|
3406
|
-
value: formData.meta_keywords,
|
|
3407
|
-
onChange: (e2) => setFormData({
|
|
3408
|
-
...formData,
|
|
3409
|
-
meta_keywords: e2.target.value
|
|
3410
|
-
}),
|
|
3411
|
-
placeholder: "keyword1, keyword2, keyword3"
|
|
3412
|
-
}
|
|
3413
|
-
),
|
|
3414
|
-
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-xs text-ui-fg-subtle", children: "Comma-separated keywords for SEO" })
|
|
3415
|
-
] }),
|
|
3416
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-2", children: [
|
|
3417
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { htmlFor: "display-order", children: "Display Order" }),
|
|
3418
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3419
|
-
ui.Input,
|
|
3420
|
-
{
|
|
3421
|
-
id: "display-order",
|
|
3422
|
-
type: "number",
|
|
3423
|
-
value: formData.display_order,
|
|
3424
|
-
onChange: (e2) => setFormData({
|
|
3425
|
-
...formData,
|
|
3426
|
-
display_order: parseInt(e2.target.value) || 0
|
|
3427
|
-
}),
|
|
3428
|
-
placeholder: "0"
|
|
3429
|
-
}
|
|
3430
|
-
),
|
|
3431
|
-
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-xs text-ui-fg-subtle", children: "Lower numbers appear first in listings" })
|
|
3432
|
-
] })
|
|
3433
|
-
] })
|
|
3434
|
-
] }),
|
|
3435
|
-
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex justify-end border-t pt-4", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: handleSave, isLoading: isSaving, children: "Save Campaign Details" }) })
|
|
3436
|
-
] }),
|
|
3437
|
-
showImageUploader && /* @__PURE__ */ jsxRuntime.jsx(
|
|
3438
|
-
CampaignImageUploader,
|
|
3439
|
-
{
|
|
3440
|
-
campaignId,
|
|
3441
|
-
imageType: showImageUploader,
|
|
3442
|
-
currentImageUrl: showImageUploader === "image" ? campaignDetail == null ? void 0 : campaignDetail.image_url : campaignDetail == null ? void 0 : campaignDetail.thumbnail_url,
|
|
3443
|
-
onClose: () => setShowImageUploader(null),
|
|
3444
|
-
onSuccess: fetchCampaignDetail
|
|
3445
|
-
}
|
|
3446
|
-
)
|
|
3447
|
-
] });
|
|
3448
|
-
};
|
|
3449
|
-
const CouponDetail = () => {
|
|
3450
|
-
var _a, _b, _c, _d, _e, _f;
|
|
3451
|
-
const { id } = reactRouterDom.useParams();
|
|
3452
|
-
const navigate = reactRouterDom.useNavigate();
|
|
3453
|
-
const [isEditing, setIsEditing] = React.useState(false);
|
|
3454
|
-
const { data, isLoading, refetch } = useCouponById(id ?? "");
|
|
3455
|
-
if (isLoading) {
|
|
3456
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "flex items-center justify-center h-screen", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "animate-spin h-10 w-10 border-4 border-primary rounded-full border-t-transparent" }) });
|
|
3457
|
-
}
|
|
3458
|
-
if (!(data == null ? void 0 : data.campaign)) {
|
|
3459
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { children: /* @__PURE__ */ jsxRuntime.jsx("p", { children: "Coupon not found" }) });
|
|
3460
|
-
}
|
|
3461
|
-
const campaign = data.campaign;
|
|
3462
|
-
const promotion = (_a = campaign.promotions) == null ? void 0 : _a[0];
|
|
3463
|
-
const initialValues = {
|
|
3464
|
-
name: campaign.name ?? "",
|
|
3465
|
-
description: campaign.description ?? "",
|
|
3466
|
-
type: "coupon",
|
|
3467
|
-
code: (promotion == null ? void 0 : promotion.code) ?? "",
|
|
3468
|
-
discount_type: ((_b = promotion == null ? void 0 : promotion.application_method) == null ? void 0 : _b.type) ?? "percentage",
|
|
3469
|
-
discount_value: Number(((_c = promotion == null ? void 0 : promotion.application_method) == null ? void 0 : _c.value) ?? 0),
|
|
3470
|
-
currency_code: ((_d = promotion == null ? void 0 : promotion.application_method) == null ? void 0 : _d.currency_code) ?? void 0,
|
|
3471
|
-
starts_at: campaign.starts_at ? dayjs__default.default(campaign.starts_at).format("YYYY-MM-DDTHH:mm") : void 0,
|
|
3472
|
-
ends_at: campaign.ends_at ? dayjs__default.default(campaign.ends_at).format("YYYY-MM-DDTHH:mm") : void 0,
|
|
3473
|
-
allocation: ((_e = promotion == null ? void 0 : promotion.application_method) == null ? void 0 : _e.allocation) ?? "total",
|
|
3474
|
-
target_type: ((_f = promotion == null ? void 0 : promotion.application_method) == null ? void 0 : _f.target_type) ?? "order"
|
|
3475
|
-
};
|
|
3476
|
-
const handleSubmit = async (formData) => {
|
|
3477
|
-
var _a2, _b2;
|
|
3478
|
-
if (!id) {
|
|
3479
|
-
return;
|
|
3480
|
-
}
|
|
3481
|
-
try {
|
|
3482
|
-
await axios__default.default.put(`/admin/coupons/${id}`, {
|
|
3483
|
-
...formData,
|
|
3484
|
-
starts_at: new Date(formData.starts_at).toUTCString(),
|
|
3485
|
-
ends_at: new Date(formData.ends_at).toUTCString(),
|
|
3486
|
-
currency_code: formData.discount_type === "fixed" ? formData.currency_code : void 0
|
|
3487
|
-
});
|
|
3488
|
-
ui.toast.success("Coupon updated successfully");
|
|
3489
|
-
setIsEditing(false);
|
|
3490
|
-
refetch();
|
|
3491
|
-
} catch (error) {
|
|
3492
|
-
let message = "Failed to update coupon";
|
|
3493
|
-
if (axios__default.default.isAxiosError(error)) {
|
|
3494
|
-
message = ((_b2 = (_a2 = error.response) == null ? void 0 : _a2.data) == null ? void 0 : _b2.message) ?? message;
|
|
3495
|
-
} else if (error instanceof Error) {
|
|
3496
|
-
message = error.message;
|
|
3497
|
-
}
|
|
3498
|
-
ui.toast.error("Failed to update coupon", {
|
|
3499
|
-
description: message
|
|
3500
|
-
});
|
|
3501
|
-
}
|
|
3502
|
-
};
|
|
3503
|
-
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { children: [
|
|
3504
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mb-6 flex items-center justify-between", children: [
|
|
3505
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
3506
|
-
/* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-2xl font-semibold mb-1", children: campaign.name }),
|
|
3507
|
-
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-ui-fg-subtle", children: "Manage coupon configuration and content" })
|
|
3508
|
-
] }),
|
|
3509
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
|
|
3510
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "secondary", onClick: () => navigate("/coupons"), children: "Back to Coupons" }),
|
|
3511
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: () => setIsEditing((prev) => !prev), children: isEditing ? "View Mode" : "Edit Mode" })
|
|
3512
|
-
] })
|
|
3513
|
-
] }),
|
|
3514
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Tabs, { defaultValue: "settings", children: [
|
|
3515
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Tabs.List, { children: [
|
|
3516
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Trigger, { value: "settings", children: "Coupon Settings" }),
|
|
3517
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Trigger, { value: "details", children: "Coupon Details" })
|
|
3518
|
-
] }),
|
|
3519
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Content, { value: "settings", className: "mt-6", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3520
|
-
CouponForm,
|
|
3521
|
-
{
|
|
3522
|
-
initialData: initialValues,
|
|
3523
|
-
onSubmit: handleSubmit,
|
|
3524
|
-
onCancel: () => navigate("/coupons"),
|
|
3525
|
-
disabled: !isEditing
|
|
3526
|
-
}
|
|
3527
|
-
) }),
|
|
3528
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Content, { value: "details", className: "mt-6", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3529
|
-
CampaignDetailForm,
|
|
3530
|
-
{
|
|
3531
|
-
campaignId: campaign.id,
|
|
3532
|
-
campaignName: campaign.name ?? ""
|
|
3533
|
-
}
|
|
3534
|
-
) })
|
|
3535
|
-
] })
|
|
3536
|
-
] });
|
|
3537
|
-
};
|
|
3538
|
-
const config$1 = adminSdk.defineRouteConfig({
|
|
3539
|
-
label: "Coupon Detail",
|
|
3540
|
-
icon: icons.Tag
|
|
3541
|
-
});
|
|
3542
|
-
const CouponCreate = () => {
|
|
3543
|
-
const navigate = reactRouterDom.useNavigate();
|
|
3544
|
-
const handleSubmit = async (formData) => {
|
|
3545
|
-
var _a, _b;
|
|
3546
|
-
try {
|
|
3547
|
-
await axios__default.default.post("/admin/coupons", {
|
|
3548
|
-
...formData,
|
|
3549
|
-
starts_at: new Date(formData.starts_at).toUTCString(),
|
|
3550
|
-
ends_at: new Date(formData.ends_at).toUTCString(),
|
|
3551
|
-
currency_code: formData.discount_type === "fixed" ? formData.currency_code : void 0
|
|
3552
|
-
});
|
|
3553
|
-
ui.toast.success("Coupon created successfully");
|
|
3554
|
-
navigate("/coupons");
|
|
3555
|
-
} catch (error) {
|
|
3556
|
-
let message = "Failed to create coupon";
|
|
3557
|
-
if (axios__default.default.isAxiosError(error)) {
|
|
3558
|
-
message = ((_b = (_a = error.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message) ?? message;
|
|
3559
|
-
} else if (error instanceof Error) {
|
|
3560
|
-
message = error.message;
|
|
3561
|
-
}
|
|
3562
|
-
ui.toast.error("Failed to create coupon", {
|
|
3563
|
-
description: message
|
|
3564
|
-
});
|
|
3565
|
-
}
|
|
3566
|
-
};
|
|
3567
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
3568
|
-
CouponForm,
|
|
3569
|
-
{
|
|
3570
|
-
onSubmit: handleSubmit,
|
|
3571
|
-
onCancel: () => navigate("/coupons")
|
|
3572
|
-
}
|
|
3573
|
-
);
|
|
3574
|
-
};
|
|
3575
|
-
const sdk = new Medusa__default.default({
|
|
3576
|
-
baseUrl: "/",
|
|
3577
|
-
debug: false,
|
|
3578
|
-
auth: {
|
|
3579
|
-
type: "session"
|
|
3580
|
-
}
|
|
3581
|
-
});
|
|
3582
|
-
const columnHelper = ui.createDataTableColumnHelper();
|
|
3583
|
-
const columns = [
|
|
3584
|
-
columnHelper.accessor("title", {
|
|
3585
|
-
header: "Title",
|
|
3586
|
-
cell: (info) => /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "large", className: "py-2", children: info.getValue() })
|
|
3587
|
-
}),
|
|
3588
|
-
columnHelper.accessor("description", {
|
|
3589
|
-
header: "Description",
|
|
3590
|
-
cell: (info) => /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "large", className: "py-2", children: info.getValue() })
|
|
3591
|
-
})
|
|
3592
|
-
];
|
|
3593
|
-
const ProductSelector = ({
|
|
3594
|
-
selectedProductIds,
|
|
3595
|
-
onSelectProduct
|
|
3596
|
-
}) => {
|
|
3597
|
-
const [search, setSearch] = React.useState("");
|
|
3598
|
-
const debouncedSearchHandler = lodash.debounce((value) => {
|
|
3599
|
-
setSearch(value);
|
|
3600
|
-
}, 500);
|
|
3601
|
-
const [products, setProducts] = React.useState(null);
|
|
3602
|
-
const [isLoading, setIsLoading] = React.useState(true);
|
|
3603
|
-
React.useEffect(() => {
|
|
3604
|
-
const fetchProducts = async () => {
|
|
3605
|
-
setIsLoading(true);
|
|
3606
|
-
try {
|
|
3607
|
-
const result = await sdk.admin.product.list({
|
|
3608
|
-
q: search,
|
|
3609
|
-
limit: 100
|
|
3610
|
-
});
|
|
3611
|
-
setProducts(result);
|
|
3612
|
-
} catch (error) {
|
|
3613
|
-
console.error("Failed to fetch products:", error);
|
|
3614
|
-
} finally {
|
|
3615
|
-
setIsLoading(false);
|
|
3616
|
-
}
|
|
3617
|
-
};
|
|
3618
|
-
fetchProducts();
|
|
3619
|
-
}, [selectedProductIds, search]);
|
|
3620
|
-
const selectableProducts = React.useMemo(() => {
|
|
3621
|
-
var _a;
|
|
3622
|
-
return (_a = products == null ? void 0 : products.products) == null ? void 0 : _a.filter(
|
|
3623
|
-
(product) => !selectedProductIds.includes(product.id)
|
|
3624
|
-
);
|
|
3625
|
-
}, [products == null ? void 0 : products.products, selectedProductIds]);
|
|
3626
|
-
const table = ui.useDataTable({
|
|
3627
|
-
data: selectableProducts || [],
|
|
3628
|
-
columns,
|
|
3629
|
-
getRowId: (product) => product.id,
|
|
3630
|
-
onRowClick: (_, row) => {
|
|
3631
|
-
onSelectProduct == null ? void 0 : onSelectProduct(row.original);
|
|
3632
|
-
}
|
|
3633
|
-
});
|
|
3634
|
-
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { children: [
|
|
3635
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3636
|
-
ui.Input,
|
|
3637
|
-
{
|
|
3638
|
-
className: "text-lg py-2",
|
|
3639
|
-
placeholder: "Search products...",
|
|
3640
|
-
onChange: (e2) => debouncedSearchHandler(e2.target.value)
|
|
3641
|
-
}
|
|
3642
|
-
),
|
|
3643
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.DataTable, { instance: table, children: /* @__PURE__ */ jsxRuntime.jsx(ui.DataTable.Table, {}) })
|
|
3644
|
-
] });
|
|
3645
|
-
};
|
|
3646
|
-
const customCampaignSchema = z__default.default.object({
|
|
3647
|
-
name: z__default.default.string().min(1, "Name is required"),
|
|
3648
|
-
description: z__default.default.string().min(1, "Description is required"),
|
|
3649
|
-
type: z__default.default.literal("flash-sale"),
|
|
3650
|
-
starts_at: z__default.default.string().min(1, "Start date is required"),
|
|
3651
|
-
ends_at: z__default.default.string().min(1, "End date is required")
|
|
3652
|
-
});
|
|
3653
|
-
const campaignProductSchema = z__default.default.object({
|
|
3654
|
-
product: z__default.default.object({
|
|
3655
|
-
id: z__default.default.string(),
|
|
3656
|
-
title: z__default.default.string()
|
|
3657
|
-
}),
|
|
3658
|
-
discountType: z__default.default.enum([
|
|
3659
|
-
"percentage"
|
|
3660
|
-
// , "fixed"
|
|
3661
|
-
]),
|
|
3662
|
-
discountValue: z__default.default.number().min(1),
|
|
3663
|
-
limit: z__default.default.number().min(1),
|
|
3664
|
-
maxQty: z__default.default.number().min(1)
|
|
3665
|
-
});
|
|
3666
|
-
const campaignDataSchema = customCampaignSchema.extend({
|
|
3667
|
-
products: z__default.default.array(campaignProductSchema).min(1, "At least one product is required")
|
|
3668
|
-
}).refine(
|
|
3669
|
-
(data) => new Date(data.starts_at) < new Date(data.ends_at),
|
|
3670
|
-
"End date must be after start date"
|
|
3671
|
-
);
|
|
3672
|
-
const FlashSaleForm = ({
|
|
3673
|
-
initialData,
|
|
3674
|
-
initialProducts,
|
|
3675
|
-
onSubmit,
|
|
3676
|
-
onCancel,
|
|
3677
|
-
disabled = false
|
|
3678
|
-
}) => {
|
|
3679
|
-
var _a;
|
|
3680
|
-
const {
|
|
3681
|
-
control,
|
|
3682
|
-
register,
|
|
3683
|
-
handleSubmit,
|
|
3684
|
-
watch,
|
|
3685
|
-
setValue,
|
|
3686
|
-
formState: { errors }
|
|
3687
|
-
} = useForm({
|
|
3688
|
-
resolver: t(campaignDataSchema),
|
|
3689
|
-
defaultValues: {
|
|
3690
|
-
name: (initialData == null ? void 0 : initialData.name) || "",
|
|
3691
|
-
description: (initialData == null ? void 0 : initialData.description) || "",
|
|
3692
|
-
type: "flash-sale",
|
|
3693
|
-
starts_at: (initialData == null ? void 0 : initialData.starts_at) || "",
|
|
3694
|
-
ends_at: (initialData == null ? void 0 : initialData.ends_at) || "",
|
|
3695
|
-
products: initialProducts ? Array.from(initialProducts.values()) : []
|
|
3696
|
-
}
|
|
3697
|
-
});
|
|
3698
|
-
const { fields, append, remove } = useFieldArray({
|
|
3699
|
-
control,
|
|
3700
|
-
name: "products"
|
|
3701
|
-
});
|
|
3702
|
-
const [openProductModal, setOpenProductModal] = React.useState(false);
|
|
3703
|
-
const startsAt = watch("starts_at");
|
|
3704
|
-
const endsAt = watch("ends_at");
|
|
3705
|
-
const handleDateTimeChange = (field, type, value) => {
|
|
3706
|
-
const currentValue = watch(field);
|
|
3707
|
-
if (type === "date") {
|
|
3708
|
-
const time = currentValue ? dayjs__default.default(currentValue).format("HH:mm") : field === "starts_at" ? "00:00" : "23:59";
|
|
3709
|
-
setValue(field, `${value}T${time}`);
|
|
3710
|
-
} else {
|
|
3711
|
-
const date = currentValue ? dayjs__default.default(currentValue).format("YYYY-MM-DD") : dayjs__default.default().format("YYYY-MM-DD");
|
|
3712
|
-
setValue(field, `${date}T${value}`);
|
|
3713
|
-
}
|
|
3714
|
-
};
|
|
3715
|
-
const onFormSubmit = (data) => {
|
|
3716
|
-
onSubmit(data);
|
|
3717
|
-
};
|
|
3718
|
-
const onFormError = () => {
|
|
3719
|
-
const errorMessages = Object.entries(errors).map(([key, value]) => {
|
|
3720
|
-
var _a2;
|
|
3721
|
-
if (key === "products" && Array.isArray(value)) {
|
|
3722
|
-
return value.map(
|
|
3723
|
-
(item, index) => item ? `Product ${index + 1}: ${Object.values(item).join(", ")}` : ""
|
|
3724
|
-
).filter(Boolean).join(", ");
|
|
3725
|
-
}
|
|
3726
|
-
return (value == null ? void 0 : value.message) || ((_a2 = value == null ? void 0 : value.root) == null ? void 0 : _a2.message);
|
|
3727
|
-
}).filter(Boolean).join(", ");
|
|
3728
|
-
ui.toast.error("Invalid data", {
|
|
3729
|
-
description: errorMessages
|
|
3730
|
-
});
|
|
3731
|
-
};
|
|
3732
|
-
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { children: [
|
|
3733
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
|
|
3734
|
-
/* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-xl font-semibold", children: "Flash sale campaign" }),
|
|
3735
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "transparent", onClick: onCancel, children: "Cancel" })
|
|
3736
|
-
] }),
|
|
3737
|
-
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
3738
|
-
"form",
|
|
3739
|
-
{
|
|
3740
|
-
onSubmit: handleSubmit(onFormSubmit, onFormError),
|
|
3741
|
-
className: "space-y-4 my-8",
|
|
3742
|
-
children: [
|
|
3743
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
3744
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Name" }),
|
|
3745
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...register("name"), disabled }),
|
|
3746
|
-
errors.name && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.name.message })
|
|
3747
|
-
] }),
|
|
3748
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
3749
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Description" }),
|
|
3750
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Textarea, { ...register("description"), disabled }),
|
|
3751
|
-
errors.description && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.description.message })
|
|
3752
|
-
] }),
|
|
3753
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
|
|
3754
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
3755
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Start Date" }),
|
|
3756
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
|
|
3757
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3758
|
-
ui.Input,
|
|
3759
|
-
{
|
|
3760
|
-
type: "date",
|
|
3761
|
-
value: startsAt ? dayjs__default.default(startsAt).format("YYYY-MM-DD") : "",
|
|
3762
|
-
onChange: (e2) => handleDateTimeChange("starts_at", "date", e2.target.value),
|
|
3763
|
-
disabled,
|
|
3764
|
-
className: "flex-1"
|
|
3765
|
-
}
|
|
3766
|
-
),
|
|
3767
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3768
|
-
ui.Input,
|
|
3769
|
-
{
|
|
3770
|
-
type: "time",
|
|
3771
|
-
value: startsAt ? dayjs__default.default(startsAt).format("HH:mm") : "",
|
|
3772
|
-
onChange: (e2) => handleDateTimeChange("starts_at", "time", e2.target.value),
|
|
3773
|
-
disabled,
|
|
3774
|
-
className: "w-32"
|
|
3775
|
-
}
|
|
3776
|
-
)
|
|
3777
|
-
] }),
|
|
3778
|
-
errors.starts_at && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.starts_at.message })
|
|
3779
|
-
] }),
|
|
3780
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
3781
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "End Date" }),
|
|
3782
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
|
|
3783
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3784
|
-
ui.Input,
|
|
3785
|
-
{
|
|
3786
|
-
type: "date",
|
|
3787
|
-
value: endsAt ? dayjs__default.default(endsAt).format("YYYY-MM-DD") : "",
|
|
3788
|
-
onChange: (e2) => handleDateTimeChange("ends_at", "date", e2.target.value),
|
|
3789
|
-
disabled,
|
|
3790
|
-
className: "flex-1"
|
|
3791
|
-
}
|
|
3792
|
-
),
|
|
3793
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3794
|
-
ui.Input,
|
|
3795
|
-
{
|
|
3796
|
-
type: "time",
|
|
3797
|
-
value: endsAt ? dayjs__default.default(endsAt).format("HH:mm") : "",
|
|
3798
|
-
onChange: (e2) => handleDateTimeChange("ends_at", "time", e2.target.value),
|
|
3799
|
-
disabled,
|
|
3800
|
-
className: "w-32"
|
|
3801
|
-
}
|
|
3802
|
-
)
|
|
3803
|
-
] }),
|
|
3804
|
-
errors.ends_at && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm mt-1", children: errors.ends_at.message })
|
|
3805
|
-
] })
|
|
3806
|
-
] }),
|
|
3807
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-between items-center", children: [
|
|
3808
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Label, { children: "Products" }),
|
|
3809
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
3810
|
-
ui.Button,
|
|
3811
|
-
{
|
|
3812
|
-
type: "button",
|
|
3813
|
-
variant: "secondary",
|
|
3814
|
-
onClick: () => setOpenProductModal(true),
|
|
3815
|
-
disabled,
|
|
3816
|
-
children: "Add Product"
|
|
3817
|
-
}
|
|
3818
|
-
)
|
|
3819
|
-
] }),
|
|
3820
|
-
((_a = errors.products) == null ? void 0 : _a.root) && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-red-500 text-sm", children: errors.products.root.message }),
|
|
3821
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Table, { children: [
|
|
3822
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.Header, { children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
|
|
3823
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "Product" }),
|
|
3824
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "Discount Type" }),
|
|
3825
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "Discount Value" }),
|
|
3826
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "Limit" }),
|
|
3827
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "Max Qty per Order" }),
|
|
3828
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.HeaderCell, { children: "Actions" })
|
|
3829
|
-
] }) }),
|
|
3830
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.Body, { children: fields.map((field, index) => /* @__PURE__ */ jsxRuntime.jsxs(ui.Table.Row, { children: [
|
|
3831
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: field.product.title }),
|
|
3832
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3833
|
-
Controller,
|
|
3834
|
-
{
|
|
3835
|
-
name: `products.${index}.discountType`,
|
|
3836
|
-
control,
|
|
3837
|
-
render: ({ field: field2 }) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
3838
|
-
ui.Select,
|
|
3839
|
-
{
|
|
3840
|
-
value: field2.value,
|
|
3841
|
-
onValueChange: field2.onChange,
|
|
3842
|
-
disabled,
|
|
3843
|
-
children: [
|
|
3844
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Trigger, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Value, { placeholder: "Select discount type" }) }),
|
|
3845
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Select.Content, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Select.Item, { value: "percentage", children: "Percentage" }) })
|
|
3846
|
-
]
|
|
3847
|
-
}
|
|
3848
|
-
)
|
|
3849
|
-
}
|
|
3850
|
-
) }),
|
|
3851
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3852
|
-
Controller,
|
|
3853
|
-
{
|
|
3854
|
-
name: `products.${index}.discountValue`,
|
|
3855
|
-
control,
|
|
3856
|
-
render: ({ field: field2 }) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
3857
|
-
ui.Input,
|
|
3858
|
-
{
|
|
3859
|
-
type: "number",
|
|
3860
|
-
value: field2.value,
|
|
3861
|
-
onChange: (e2) => field2.onChange(Number(e2.target.value)),
|
|
3862
|
-
disabled
|
|
3863
|
-
}
|
|
3864
|
-
)
|
|
3865
|
-
}
|
|
3866
|
-
) }),
|
|
3867
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3868
|
-
Controller,
|
|
3869
|
-
{
|
|
3870
|
-
name: `products.${index}.limit`,
|
|
3871
|
-
control,
|
|
3872
|
-
render: ({ field: field2 }) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
3873
|
-
ui.Input,
|
|
3874
|
-
{
|
|
3875
|
-
type: "number",
|
|
3876
|
-
value: field2.value,
|
|
3877
|
-
onChange: (e2) => field2.onChange(Number(e2.target.value)),
|
|
3878
|
-
disabled
|
|
3879
|
-
}
|
|
3880
|
-
)
|
|
3881
|
-
}
|
|
3882
|
-
) }),
|
|
3883
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3884
|
-
Controller,
|
|
3885
|
-
{
|
|
3886
|
-
name: `products.${index}.maxQty`,
|
|
3887
|
-
control,
|
|
3888
|
-
render: ({ field: field2 }) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
3889
|
-
ui.Input,
|
|
3890
|
-
{
|
|
3891
|
-
type: "number",
|
|
3892
|
-
value: field2.value,
|
|
3893
|
-
onChange: (e2) => field2.onChange(Number(e2.target.value)),
|
|
3894
|
-
disabled
|
|
3895
|
-
}
|
|
3896
|
-
)
|
|
3897
|
-
}
|
|
3898
|
-
) }),
|
|
3899
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Table.Cell, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3900
|
-
ui.Button,
|
|
3901
|
-
{
|
|
3902
|
-
type: "button",
|
|
3903
|
-
variant: "danger",
|
|
3904
|
-
onClick: () => remove(index),
|
|
3905
|
-
disabled,
|
|
3906
|
-
children: /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, {})
|
|
3907
|
-
}
|
|
3908
|
-
) })
|
|
3909
|
-
] }, field.id)) })
|
|
3910
|
-
] }),
|
|
3911
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { type: "submit", disabled, children: "Save" })
|
|
3912
|
-
]
|
|
3913
|
-
}
|
|
3914
|
-
),
|
|
3915
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.FocusModal, { open: openProductModal, onOpenChange: setOpenProductModal, children: /* @__PURE__ */ jsxRuntime.jsxs(ui.FocusModal.Content, { children: [
|
|
3916
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.FocusModal.Header, { title: "Add Product" }),
|
|
3917
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.FocusModal.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
3918
|
-
ProductSelector,
|
|
3919
|
-
{
|
|
3920
|
-
selectedProductIds: fields.map((f2) => f2.product.id),
|
|
3921
|
-
onSelectProduct: (product) => {
|
|
3922
|
-
append({
|
|
3923
|
-
product,
|
|
3924
|
-
discountType: "percentage",
|
|
3925
|
-
discountValue: 10,
|
|
3926
|
-
limit: 10,
|
|
3927
|
-
maxQty: 1
|
|
3928
|
-
});
|
|
3929
|
-
setOpenProductModal(false);
|
|
3930
|
-
}
|
|
3931
|
-
}
|
|
3932
|
-
) })
|
|
3933
|
-
] }) })
|
|
3934
|
-
] });
|
|
3935
|
-
};
|
|
3936
|
-
const useFlashSaleById = (id) => {
|
|
3937
|
-
const [data, setData] = React.useState(null);
|
|
3938
|
-
const [isLoading, setIsLoading] = React.useState(true);
|
|
3939
|
-
const [error, setError] = React.useState(null);
|
|
3940
|
-
const fetchFlashSale = async () => {
|
|
3941
|
-
if (!id) {
|
|
3942
|
-
setIsLoading(false);
|
|
3943
|
-
return;
|
|
3944
|
-
}
|
|
3945
|
-
setIsLoading(true);
|
|
3946
|
-
setError(null);
|
|
3947
|
-
try {
|
|
3948
|
-
const response = await axios__default.default.get(`/admin/flash-sales/${id}`);
|
|
3949
|
-
setData(response.data);
|
|
3950
|
-
} catch (err) {
|
|
3951
|
-
setError(
|
|
3952
|
-
err instanceof Error ? err : new Error("Failed to fetch flash sale")
|
|
3953
|
-
);
|
|
3954
|
-
} finally {
|
|
3955
|
-
setIsLoading(false);
|
|
3956
|
-
}
|
|
3957
|
-
};
|
|
3958
|
-
React.useEffect(() => {
|
|
3959
|
-
fetchFlashSale();
|
|
3960
|
-
}, [id]);
|
|
3961
|
-
return {
|
|
3962
|
-
data,
|
|
3963
|
-
isLoading,
|
|
3964
|
-
error,
|
|
3965
|
-
refetch: fetchFlashSale
|
|
3966
|
-
};
|
|
3967
|
-
};
|
|
3968
|
-
const FlashSaleDetail = () => {
|
|
3969
|
-
const { id } = reactRouterDom.useParams();
|
|
3970
|
-
const navigate = reactRouterDom.useNavigate();
|
|
3971
|
-
const { data, isLoading, refetch } = useFlashSaleById(id || "");
|
|
3972
|
-
const [isEditing, setIsEditing] = React.useState(false);
|
|
3973
|
-
async function handleSubmit(campaignData2) {
|
|
3974
|
-
var _a;
|
|
3975
|
-
try {
|
|
3976
|
-
await axios__default.default.put(`/admin/flash-sales/${id}`, {
|
|
3977
|
-
...campaignData2,
|
|
3978
|
-
starts_at: new Date(campaignData2.starts_at).toUTCString(),
|
|
3979
|
-
ends_at: new Date(campaignData2.ends_at).toUTCString()
|
|
3980
|
-
});
|
|
3981
|
-
ui.toast.success("Flash sale updated successfully");
|
|
3982
|
-
refetch();
|
|
3983
|
-
navigate("/flash-sales");
|
|
3984
|
-
} catch (error) {
|
|
3985
|
-
let message;
|
|
3986
|
-
if (axios__default.default.isAxiosError(error)) {
|
|
3987
|
-
console.log(error);
|
|
3988
|
-
message = (_a = error.response) == null ? void 0 : _a.data.message;
|
|
3989
|
-
} else if (error instanceof Error) {
|
|
3990
|
-
message = error.message;
|
|
3991
|
-
} else {
|
|
3992
|
-
message = "Failed to update flash sale";
|
|
3993
|
-
}
|
|
3994
|
-
ui.toast.error("Failed to update flash sale", {
|
|
3995
|
-
description: message
|
|
3996
|
-
});
|
|
3997
|
-
}
|
|
3998
|
-
}
|
|
3999
|
-
if (isLoading) {
|
|
4000
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { className: "flex items-center justify-center h-screen", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "animate-spin h-10 w-10 border-4 border-primary rounded-full border-t-transparent" }) });
|
|
4001
|
-
}
|
|
4002
|
-
if (!data) {
|
|
4003
|
-
return /* @__PURE__ */ jsxRuntime.jsx(ui.Container, { children: /* @__PURE__ */ jsxRuntime.jsx("p", { children: "Flash sale not found" }) });
|
|
4004
|
-
}
|
|
4005
|
-
const campaignData = {
|
|
4006
|
-
name: data.name || "",
|
|
4007
|
-
description: data.description || "",
|
|
4008
|
-
type: "flash-sale",
|
|
4009
|
-
starts_at: data.starts_at,
|
|
4010
|
-
ends_at: data.ends_at
|
|
4011
|
-
};
|
|
4012
|
-
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Container, { children: [
|
|
4013
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "mb-6 flex items-center justify-between", children: [
|
|
4014
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
4015
|
-
/* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-2xl font-semibold mb-1", children: data.name }),
|
|
4016
|
-
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-ui-fg-subtle", children: "Manage campaign settings and content" })
|
|
4017
|
-
] }),
|
|
4018
|
-
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-2", children: [
|
|
4019
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { variant: "secondary", onClick: () => navigate("/flash-sales"), children: "Back to Campaigns" }),
|
|
4020
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: () => setIsEditing((prev) => !prev), children: isEditing ? "View Mode" : "Edit Mode" })
|
|
4021
|
-
] })
|
|
4022
|
-
] }),
|
|
4023
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Tabs, { defaultValue: "settings", children: [
|
|
4024
|
-
/* @__PURE__ */ jsxRuntime.jsxs(ui.Tabs.List, { children: [
|
|
4025
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Trigger, { value: "settings", children: "Campaign Settings" }),
|
|
4026
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Trigger, { value: "details", children: "Campaign Details" })
|
|
4027
|
-
] }),
|
|
4028
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Content, { value: "settings", className: "mt-6", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
4029
|
-
FlashSaleForm,
|
|
4030
|
-
{
|
|
4031
|
-
initialData: campaignData,
|
|
4032
|
-
initialProducts: new Map(
|
|
4033
|
-
data.products.map((product) => [product.product.id, product])
|
|
4034
|
-
),
|
|
4035
|
-
onSubmit: handleSubmit,
|
|
4036
|
-
onCancel: () => navigate("/flash-sales"),
|
|
4037
|
-
disabled: !isEditing
|
|
4038
|
-
}
|
|
4039
|
-
) }),
|
|
4040
|
-
/* @__PURE__ */ jsxRuntime.jsx(ui.Tabs.Content, { value: "details", className: "mt-6", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
4041
|
-
CampaignDetailForm,
|
|
4042
|
-
{
|
|
4043
|
-
campaignId: id || "",
|
|
4044
|
-
campaignName: data.name || ""
|
|
4045
|
-
}
|
|
4046
|
-
) })
|
|
4047
|
-
] })
|
|
4048
|
-
] });
|
|
4049
|
-
};
|
|
4050
|
-
const config = adminSdk.defineRouteConfig({
|
|
4051
|
-
label: "Flash Sale Detail",
|
|
4052
|
-
icon: icons.Sparkles
|
|
4053
|
-
});
|
|
4054
|
-
const FlashSaleCreate = () => {
|
|
4055
|
-
const navigate = reactRouterDom.useNavigate();
|
|
4056
|
-
async function handleSubmit(campaignData) {
|
|
4057
|
-
var _a;
|
|
4058
|
-
try {
|
|
4059
|
-
await axios__default.default.post("/admin/flash-sales", {
|
|
4060
|
-
...campaignData,
|
|
4061
|
-
starts_at: new Date(campaignData.starts_at).toUTCString(),
|
|
4062
|
-
ends_at: new Date(campaignData.ends_at).toUTCString()
|
|
4063
|
-
});
|
|
4064
|
-
ui.toast.success("Flash sale created successfully");
|
|
4065
|
-
navigate("/flash-sales");
|
|
4066
|
-
} catch (error) {
|
|
4067
|
-
let message;
|
|
4068
|
-
if (axios__default.default.isAxiosError(error)) {
|
|
4069
|
-
console.log(error);
|
|
4070
|
-
message = (_a = error.response) == null ? void 0 : _a.data.message;
|
|
4071
|
-
} else if (error instanceof Error) {
|
|
4072
|
-
message = error.message;
|
|
4073
|
-
} else {
|
|
4074
|
-
message = "Failed to create flash sale";
|
|
4075
|
-
}
|
|
4076
|
-
ui.toast.error("Failed to create flash sale", {
|
|
4077
|
-
description: message
|
|
4078
|
-
});
|
|
4079
|
-
}
|
|
4080
|
-
}
|
|
4081
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
4082
|
-
FlashSaleForm,
|
|
4083
|
-
{
|
|
4084
|
-
onSubmit: handleSubmit,
|
|
4085
|
-
onCancel: () => navigate("/flash-sales")
|
|
4086
|
-
}
|
|
4087
|
-
);
|
|
4088
|
-
};
|
|
4089
|
-
const widgetModule = { widgets: [
|
|
4090
|
-
{
|
|
4091
|
-
Component: CampaignDetailWidget,
|
|
4092
|
-
zone: ["promotion.details.side.after"]
|
|
4093
|
-
}
|
|
4094
|
-
] };
|
|
4095
|
-
const routeModule = {
|
|
4096
|
-
routes: [
|
|
4097
|
-
{
|
|
4098
|
-
Component: Coupons,
|
|
4099
|
-
path: "/coupons"
|
|
4100
|
-
},
|
|
4101
|
-
{
|
|
4102
|
-
Component: FlashSale,
|
|
4103
|
-
path: "/flash-sales"
|
|
4104
|
-
},
|
|
4105
|
-
{
|
|
4106
|
-
Component: CouponDetail,
|
|
4107
|
-
path: "/coupons/:id"
|
|
4108
|
-
},
|
|
4109
|
-
{
|
|
4110
|
-
Component: CouponCreate,
|
|
4111
|
-
path: "/coupons/create"
|
|
4112
|
-
},
|
|
4113
|
-
{
|
|
4114
|
-
Component: FlashSaleDetail,
|
|
4115
|
-
path: "/flash-sales/:id"
|
|
4116
|
-
},
|
|
4117
|
-
{
|
|
4118
|
-
Component: FlashSaleCreate,
|
|
4119
|
-
path: "/flash-sales/create"
|
|
4120
|
-
}
|
|
4121
|
-
]
|
|
4122
|
-
};
|
|
4123
|
-
const menuItemModule = {
|
|
4124
|
-
menuItems: [
|
|
4125
|
-
{
|
|
4126
|
-
label: config$3.label,
|
|
4127
|
-
icon: config$3.icon,
|
|
4128
|
-
path: "/coupons",
|
|
4129
|
-
nested: void 0
|
|
4130
|
-
},
|
|
4131
|
-
{
|
|
4132
|
-
label: config$2.label,
|
|
4133
|
-
icon: config$2.icon,
|
|
4134
|
-
path: "/flash-sales",
|
|
4135
|
-
nested: void 0
|
|
4136
|
-
},
|
|
4137
|
-
{
|
|
4138
|
-
label: config$1.label,
|
|
4139
|
-
icon: config$1.icon,
|
|
4140
|
-
path: "/coupons/:id",
|
|
4141
|
-
nested: void 0
|
|
4142
|
-
},
|
|
4143
|
-
{
|
|
4144
|
-
label: config.label,
|
|
4145
|
-
icon: config.icon,
|
|
4146
|
-
path: "/flash-sales/:id",
|
|
4147
|
-
nested: void 0
|
|
4148
|
-
}
|
|
4149
|
-
]
|
|
4150
|
-
};
|
|
4151
|
-
const formModule = { customFields: {} };
|
|
4152
|
-
const displayModule = {
|
|
4153
|
-
displays: {}
|
|
4154
|
-
};
|
|
4155
|
-
const i18nModule = { resources: {} };
|
|
4156
|
-
const plugin = {
|
|
4157
|
-
widgetModule,
|
|
4158
|
-
routeModule,
|
|
4159
|
-
menuItemModule,
|
|
4160
|
-
formModule,
|
|
4161
|
-
displayModule,
|
|
4162
|
-
i18nModule
|
|
4163
|
-
};
|
|
4164
|
-
module.exports = plugin;
|