@stamprally/admin-ui 0.12.0 → 0.13.0
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/README.md +4 -0
- package/dist/index.cjs +77 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +78 -32
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -25,9 +25,13 @@ const editor = useAdminRallyEditor(config, { onChange: saveDraft });
|
|
|
25
25
|
editor.addSpot({ name: { en: "Museum" }, conditions: [{ type: "qr", secretToken: "" }] });
|
|
26
26
|
editor.duplicateReward("reward-1");
|
|
27
27
|
editor.reorderSpots(2, 0);
|
|
28
|
+
editor.reorderRewards(1, 0);
|
|
28
29
|
editor.undo();
|
|
29
30
|
```
|
|
30
31
|
|
|
31
32
|
Use `canUndo`/`canRedo` to control history buttons. `resetConfig(newConfig)` replaces
|
|
32
33
|
the current draft when an external CMS update arrives. `updateLocalizedField` accepts
|
|
33
34
|
paths such as `title`, `description`, `spots.0.name`, or `spots.spot-1.name`.
|
|
35
|
+
Updates use functional React state transitions, so consecutive updates are not
|
|
36
|
+
lost. External configuration changes replace a clean draft automatically; call
|
|
37
|
+
`resetConfig(newConfig)` to explicitly accept a conflicting external draft.
|
package/dist/index.cjs
CHANGED
|
@@ -26,12 +26,22 @@ function localizedValue(value, locale, nextValue) {
|
|
|
26
26
|
}
|
|
27
27
|
function useAdminRallyEditor(initialConfig, options = {}) {
|
|
28
28
|
const initialRef = react.useRef(initialConfig);
|
|
29
|
+
const previousInitialRef = react.useRef(initialConfig);
|
|
29
30
|
const [history, setHistory] = react.useState({
|
|
30
31
|
past: [],
|
|
31
32
|
present: initialConfig,
|
|
32
33
|
future: []
|
|
33
34
|
});
|
|
34
35
|
const config = history.present;
|
|
36
|
+
react.useEffect(() => {
|
|
37
|
+
if (previousInitialRef.current === initialConfig) return;
|
|
38
|
+
previousInitialRef.current = initialConfig;
|
|
39
|
+
const isDirty = JSON.stringify(history.present) !== JSON.stringify(initialRef.current);
|
|
40
|
+
if (!isDirty) {
|
|
41
|
+
initialRef.current = initialConfig;
|
|
42
|
+
setHistory({ past: [], present: initialConfig, future: [] });
|
|
43
|
+
}
|
|
44
|
+
}, [history.present, initialConfig]);
|
|
35
45
|
const commit = react.useCallback(
|
|
36
46
|
(value) => {
|
|
37
47
|
setHistory((current) => {
|
|
@@ -68,6 +78,7 @@ function useAdminRallyEditor(initialConfig, options = {}) {
|
|
|
68
78
|
spots: current.spots.filter((spot) => spot.id !== spotId).map((spot, index) => ({ ...spot, orderIndex: index }))
|
|
69
79
|
}));
|
|
70
80
|
const reorderSpots = (fromIndex, toIndex) => commit((current) => ({ ...current, spots: moveTo(current.spots, fromIndex, toIndex) }));
|
|
81
|
+
const reorderRewards = (fromIndex, toIndex) => commit((current) => ({ ...current, rewards: moveTo(current.rewards, fromIndex, toIndex) }));
|
|
71
82
|
const duplicateSpot = (spotId) => commit((current) => {
|
|
72
83
|
const source = current.spots.find((spot) => spot.id === spotId);
|
|
73
84
|
if (source === void 0) return current;
|
|
@@ -166,6 +177,7 @@ function useAdminRallyEditor(initialConfig, options = {}) {
|
|
|
166
177
|
});
|
|
167
178
|
const resetConfig = (newConfig) => {
|
|
168
179
|
initialRef.current = newConfig;
|
|
180
|
+
previousInitialRef.current = newConfig;
|
|
169
181
|
setHistory({ past: [], present: newConfig, future: [] });
|
|
170
182
|
options.onChange?.(newConfig);
|
|
171
183
|
};
|
|
@@ -178,6 +190,7 @@ function useAdminRallyEditor(initialConfig, options = {}) {
|
|
|
178
190
|
addSpot,
|
|
179
191
|
removeSpot,
|
|
180
192
|
reorderSpots: (fromIndex, toIndex) => reorderSpots(fromIndex, toIndex),
|
|
193
|
+
reorderRewards,
|
|
181
194
|
duplicateSpot,
|
|
182
195
|
addReward,
|
|
183
196
|
removeReward,
|
|
@@ -198,9 +211,13 @@ function useSpotEditor(spotId, options = {}) {
|
|
|
198
211
|
const [config, setConfig] = react.useState(
|
|
199
212
|
options.config ?? options.initialConfig
|
|
200
213
|
);
|
|
201
|
-
const commit = (
|
|
202
|
-
setConfig(
|
|
203
|
-
|
|
214
|
+
const commit = (updateConfig) => {
|
|
215
|
+
setConfig((current) => {
|
|
216
|
+
if (current === void 0) return current;
|
|
217
|
+
const next = updateConfig(current);
|
|
218
|
+
options.onChange?.(next);
|
|
219
|
+
return next;
|
|
220
|
+
});
|
|
204
221
|
};
|
|
205
222
|
const spot = config?.spots.find((item) => item.id === spotId);
|
|
206
223
|
return {
|
|
@@ -209,14 +226,17 @@ function useSpotEditor(spotId, options = {}) {
|
|
|
209
226
|
setConfig,
|
|
210
227
|
update: (patch) => {
|
|
211
228
|
if (config === void 0 || spot === void 0) return;
|
|
212
|
-
commit({
|
|
213
|
-
...
|
|
214
|
-
spots:
|
|
215
|
-
});
|
|
229
|
+
commit((current) => ({
|
|
230
|
+
...current,
|
|
231
|
+
spots: current.spots.map((item) => item.id === spotId ? { ...item, ...patch } : item)
|
|
232
|
+
}));
|
|
216
233
|
},
|
|
217
234
|
remove: () => {
|
|
218
235
|
if (config === void 0 || spot === void 0) return;
|
|
219
|
-
commit(
|
|
236
|
+
commit((current) => ({
|
|
237
|
+
...current,
|
|
238
|
+
spots: current.spots.filter((item) => item.id !== spotId)
|
|
239
|
+
}));
|
|
220
240
|
}
|
|
221
241
|
};
|
|
222
242
|
}
|
|
@@ -225,9 +245,13 @@ function useRewardEditor(rewardId, options = {}) {
|
|
|
225
245
|
options.config ?? options.initialConfig
|
|
226
246
|
);
|
|
227
247
|
const reward = config?.rewards.find((item) => item.id === rewardId);
|
|
228
|
-
const commit = (
|
|
229
|
-
setConfig(
|
|
230
|
-
|
|
248
|
+
const commit = (updateConfig) => {
|
|
249
|
+
setConfig((current) => {
|
|
250
|
+
if (current === void 0) return current;
|
|
251
|
+
const next = updateConfig(current);
|
|
252
|
+
options.onChange?.(next);
|
|
253
|
+
return next;
|
|
254
|
+
});
|
|
231
255
|
};
|
|
232
256
|
return {
|
|
233
257
|
config,
|
|
@@ -235,16 +259,19 @@ function useRewardEditor(rewardId, options = {}) {
|
|
|
235
259
|
setConfig,
|
|
236
260
|
update: (patch) => {
|
|
237
261
|
if (config === void 0 || reward === void 0) return;
|
|
238
|
-
commit({
|
|
239
|
-
...
|
|
240
|
-
rewards:
|
|
262
|
+
commit((current) => ({
|
|
263
|
+
...current,
|
|
264
|
+
rewards: current.rewards.map(
|
|
241
265
|
(item) => item.id === rewardId ? { ...item, ...patch } : item
|
|
242
266
|
)
|
|
243
|
-
});
|
|
267
|
+
}));
|
|
244
268
|
},
|
|
245
269
|
remove: () => {
|
|
246
270
|
if (config === void 0 || reward === void 0) return;
|
|
247
|
-
commit(
|
|
271
|
+
commit((current) => ({
|
|
272
|
+
...current,
|
|
273
|
+
rewards: current.rewards.filter((item) => item.id !== rewardId)
|
|
274
|
+
}));
|
|
248
275
|
}
|
|
249
276
|
};
|
|
250
277
|
}
|
|
@@ -1114,21 +1141,40 @@ function AdminRallyEditor({ config, onChange, locale, dictionary }) {
|
|
|
1114
1141
|
children: field("addReward", "Add reward")
|
|
1115
1142
|
}
|
|
1116
1143
|
),
|
|
1117
|
-
/* @__PURE__ */ jsxRuntime.jsx("div", { children: config.rewards.map((reward) => /* @__PURE__ */ jsxRuntime.
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1144
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { children: config.rewards.map((reward, index) => /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
1145
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1146
|
+
RewardItemForm,
|
|
1147
|
+
{
|
|
1148
|
+
reward,
|
|
1149
|
+
locale: activeLocale,
|
|
1150
|
+
...dictionary === void 0 ? {} : { dictionary },
|
|
1151
|
+
onChange: (next) => update({
|
|
1152
|
+
rewards: config.rewards.map(
|
|
1153
|
+
(current) => current.id === reward.id ? next : current
|
|
1154
|
+
)
|
|
1155
|
+
}),
|
|
1156
|
+
onRemove: () => update({ rewards: config.rewards.filter((current) => current.id !== reward.id) })
|
|
1157
|
+
}
|
|
1158
|
+
),
|
|
1159
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1160
|
+
"button",
|
|
1161
|
+
{
|
|
1162
|
+
type: "button",
|
|
1163
|
+
disabled: index === 0,
|
|
1164
|
+
onClick: () => update({ rewards: move(config.rewards, index, -1) }),
|
|
1165
|
+
children: field("moveUp", "Move up")
|
|
1166
|
+
}
|
|
1167
|
+
),
|
|
1168
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1169
|
+
"button",
|
|
1170
|
+
{
|
|
1171
|
+
type: "button",
|
|
1172
|
+
disabled: index === config.rewards.length - 1,
|
|
1173
|
+
onClick: () => update({ rewards: move(config.rewards, index, 1) }),
|
|
1174
|
+
children: field("moveDown", "Move down")
|
|
1175
|
+
}
|
|
1176
|
+
)
|
|
1177
|
+
] }, reward.id)) })
|
|
1132
1178
|
] });
|
|
1133
1179
|
}
|
|
1134
1180
|
var RallyEditor = AdminRallyEditor;
|