@terreno/admin-frontend 0.7.1 → 0.8.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/dist/AdminFieldRenderer.d.ts +2 -0
- package/dist/AdminFieldRenderer.js +96 -3
- package/dist/AdminFieldRenderer.js.map +1 -1
- package/dist/AdminModelForm.d.ts +11 -0
- package/dist/AdminModelForm.js +95 -24
- package/dist/AdminModelForm.js.map +1 -1
- package/dist/AdminModelList.d.ts +4 -0
- package/dist/AdminModelList.js +10 -4
- package/dist/AdminModelList.js.map +1 -1
- package/dist/AdminModelTable.js +33 -33
- package/dist/AdminModelTable.js.map +1 -1
- package/dist/AdminNestedArrayField.d.ts +26 -0
- package/dist/AdminNestedArrayField.js +118 -0
- package/dist/AdminNestedArrayField.js.map +1 -0
- package/dist/CheckboxListEditor.d.ts +15 -0
- package/dist/CheckboxListEditor.js +21 -0
- package/dist/CheckboxListEditor.js.map +1 -0
- package/dist/ConsentFormEditor.d.ts +12 -0
- package/dist/ConsentFormEditor.js +299 -0
- package/dist/ConsentFormEditor.js.map +1 -0
- package/dist/ConsentFormList.d.ts +9 -0
- package/dist/ConsentFormList.js +70 -0
- package/dist/ConsentFormList.js.map +1 -0
- package/dist/ConsentResponseViewer.d.ts +8 -0
- package/dist/ConsentResponseViewer.js +61 -0
- package/dist/ConsentResponseViewer.js.map +1 -0
- package/dist/LocaleContentEditor.d.ts +10 -0
- package/dist/LocaleContentEditor.js +48 -0
- package/dist/LocaleContentEditor.js.map +1 -0
- package/dist/generateConsentPdf.d.ts +26 -0
- package/dist/generateConsentPdf.js +210 -0
- package/dist/generateConsentPdf.js.map +1 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +4 -0
- package/dist/types.js.map +1 -1
- package/dist/useAdminApi.js +2 -1
- package/dist/useAdminApi.js.map +1 -1
- package/package.json +3 -2
- package/src/AdminFieldRenderer.tsx +190 -2
- package/src/AdminModelForm.tsx +134 -40
- package/src/AdminModelList.tsx +35 -19
- package/src/AdminModelTable.tsx +40 -36
- package/src/AdminNestedArrayField.tsx +228 -0
- package/src/CheckboxListEditor.tsx +107 -0
- package/src/ConsentFormEditor.tsx +677 -0
- package/src/ConsentFormList.tsx +164 -0
- package/src/ConsentResponseViewer.tsx +228 -0
- package/src/LocaleContentEditor.tsx +151 -0
- package/src/generateConsentPdf.ts +264 -0
- package/src/index.tsx +7 -0
- package/src/types.ts +4 -0
- package/src/useAdminApi.ts +2 -1
package/src/AdminModelForm.tsx
CHANGED
|
@@ -14,12 +14,75 @@ interface AdminModelFormProps {
|
|
|
14
14
|
modelName: string;
|
|
15
15
|
mode: "create" | "edit";
|
|
16
16
|
itemId?: string;
|
|
17
|
+
footerContent?: React.ReactNode;
|
|
18
|
+
transformPayload?: (params: {mode: "create" | "edit"; payload: Record<string, any>}) => Promise<
|
|
19
|
+
Record<string, any>
|
|
20
|
+
> | Record<string, any>;
|
|
21
|
+
onSaveSuccess?: (params: {
|
|
22
|
+
mode: "create" | "edit";
|
|
23
|
+
payload: Record<string, any>;
|
|
24
|
+
result: any;
|
|
25
|
+
itemId?: string;
|
|
26
|
+
}) => Promise<void> | void;
|
|
17
27
|
}
|
|
18
28
|
|
|
19
29
|
const getEditableFields = (
|
|
20
|
-
fields: Record<string, AdminFieldConfig
|
|
30
|
+
fields: Record<string, AdminFieldConfig>,
|
|
31
|
+
fieldOrder?: string[]
|
|
21
32
|
): [string, AdminFieldConfig][] => {
|
|
22
|
-
|
|
33
|
+
const entries = Object.entries(fields).filter(([key]) => !SYSTEM_FIELDS.has(key));
|
|
34
|
+
if (!fieldOrder || fieldOrder.length === 0) {
|
|
35
|
+
return entries;
|
|
36
|
+
}
|
|
37
|
+
const entryMap = new Map(entries);
|
|
38
|
+
const ordered: [string, AdminFieldConfig][] = [];
|
|
39
|
+
for (const key of fieldOrder) {
|
|
40
|
+
const config = entryMap.get(key);
|
|
41
|
+
if (config) {
|
|
42
|
+
ordered.push([key, config]);
|
|
43
|
+
entryMap.delete(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// Append any remaining fields not in fieldOrder
|
|
47
|
+
for (const [key, config] of entryMap) {
|
|
48
|
+
ordered.push([key, config]);
|
|
49
|
+
}
|
|
50
|
+
return ordered;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const getFieldDefault = (fieldConfig: AdminFieldConfig): any => {
|
|
54
|
+
if (fieldConfig.default !== undefined) {
|
|
55
|
+
return fieldConfig.default;
|
|
56
|
+
}
|
|
57
|
+
if (fieldConfig.type === "boolean") {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
if (fieldConfig.type === "number") {
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
return "";
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const sanitizePayloadValue = (value: any): any => {
|
|
67
|
+
if (value === null) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
if (Array.isArray(value)) {
|
|
71
|
+
return value
|
|
72
|
+
.map((item) => sanitizePayloadValue(item))
|
|
73
|
+
.filter((item) => item !== undefined);
|
|
74
|
+
}
|
|
75
|
+
if (value && typeof value === "object") {
|
|
76
|
+
const nextValue: Record<string, any> = {};
|
|
77
|
+
for (const [key, childValue] of Object.entries(value)) {
|
|
78
|
+
const sanitizedChild = sanitizePayloadValue(childValue);
|
|
79
|
+
if (sanitizedChild !== undefined) {
|
|
80
|
+
nextValue[key] = sanitizedChild;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return nextValue;
|
|
84
|
+
}
|
|
85
|
+
return value;
|
|
23
86
|
};
|
|
24
87
|
|
|
25
88
|
const DeleteButton: React.FC<{loading: boolean; onDelete: () => void}> = ({loading, onDelete}) => (
|
|
@@ -93,6 +156,9 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
93
156
|
modelName,
|
|
94
157
|
mode,
|
|
95
158
|
itemId,
|
|
159
|
+
footerContent,
|
|
160
|
+
transformPayload,
|
|
161
|
+
onSaveSuccess,
|
|
96
162
|
}) => {
|
|
97
163
|
const {config, isLoading: isConfigLoading} = useAdminConfig(api, baseUrl);
|
|
98
164
|
const [formState, setFormState] = useState<Record<string, any>>({});
|
|
@@ -107,15 +173,6 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
107
173
|
|
|
108
174
|
const toast = useToast();
|
|
109
175
|
|
|
110
|
-
useEffect(() => {
|
|
111
|
-
if (!modelConfig) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
const title =
|
|
115
|
-
mode === "create" ? `Create ${modelConfig.displayName}` : `Edit ${modelConfig.displayName}`;
|
|
116
|
-
navigation.setOptions({title});
|
|
117
|
-
}, [navigation, mode, modelConfig]);
|
|
118
|
-
|
|
119
176
|
const {useReadQuery, useCreateMutation, useUpdateMutation, useDeleteMutation} = useAdminApi(
|
|
120
177
|
api,
|
|
121
178
|
modelConfig?.routePath ?? "",
|
|
@@ -130,12 +187,17 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
130
187
|
const [updateItem, {isLoading: isUpdating}] = useUpdateMutation();
|
|
131
188
|
const [deleteItem, {isLoading: isDeleting}] = useDeleteMutation();
|
|
132
189
|
|
|
190
|
+
// Initialize form state from fetched item data in edit mode
|
|
133
191
|
useEffect(() => {
|
|
134
192
|
if (mode === "edit" && itemData && !isInitialized) {
|
|
135
193
|
const initial: Record<string, any> = {};
|
|
136
194
|
if (modelConfig) {
|
|
137
|
-
for (const [key] of getEditableFields(
|
|
138
|
-
|
|
195
|
+
for (const [key, fieldConfig] of getEditableFields(
|
|
196
|
+
modelConfig.fields,
|
|
197
|
+
modelConfig.fieldOrder
|
|
198
|
+
)) {
|
|
199
|
+
const raw = itemData[key];
|
|
200
|
+
initial[key] = raw ?? getFieldDefault(fieldConfig);
|
|
139
201
|
}
|
|
140
202
|
}
|
|
141
203
|
setFormState(initial);
|
|
@@ -143,11 +205,15 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
143
205
|
}
|
|
144
206
|
}, [mode, itemData, modelConfig, isInitialized]);
|
|
145
207
|
|
|
208
|
+
// Initialize form state with defaults in create mode
|
|
146
209
|
useEffect(() => {
|
|
147
210
|
if (mode === "create" && modelConfig && !isInitialized) {
|
|
148
211
|
const initial: Record<string, any> = {};
|
|
149
|
-
for (const [key, fieldConfig] of getEditableFields(
|
|
150
|
-
|
|
212
|
+
for (const [key, fieldConfig] of getEditableFields(
|
|
213
|
+
modelConfig.fields,
|
|
214
|
+
modelConfig.fieldOrder
|
|
215
|
+
)) {
|
|
216
|
+
initial[key] = getFieldDefault(fieldConfig);
|
|
151
217
|
}
|
|
152
218
|
setFormState(initial);
|
|
153
219
|
setIsInitialized(true);
|
|
@@ -168,7 +234,10 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
168
234
|
return false;
|
|
169
235
|
}
|
|
170
236
|
const newErrors: Record<string, string> = {};
|
|
171
|
-
for (const [key, fieldConfig] of getEditableFields(
|
|
237
|
+
for (const [key, fieldConfig] of getEditableFields(
|
|
238
|
+
modelConfig.fields,
|
|
239
|
+
modelConfig.fieldOrder
|
|
240
|
+
)) {
|
|
172
241
|
if (fieldConfig.required && (formState[key] == null || formState[key] === "")) {
|
|
173
242
|
newErrors[key] = `${key} is required`;
|
|
174
243
|
}
|
|
@@ -182,16 +251,35 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
182
251
|
return;
|
|
183
252
|
}
|
|
184
253
|
try {
|
|
254
|
+
const sanitizedPayload = sanitizePayloadValue(formState) as Record<string, any>;
|
|
255
|
+
const payload = transformPayload
|
|
256
|
+
? await transformPayload({mode, payload: sanitizedPayload})
|
|
257
|
+
: sanitizedPayload;
|
|
258
|
+
let result: any;
|
|
185
259
|
if (mode === "create") {
|
|
186
|
-
await createItem(
|
|
260
|
+
result = await createItem(payload).unwrap();
|
|
187
261
|
} else if (itemId) {
|
|
188
|
-
await updateItem({body:
|
|
262
|
+
result = await updateItem({body: payload, id: itemId}).unwrap();
|
|
263
|
+
}
|
|
264
|
+
if (onSaveSuccess) {
|
|
265
|
+
await onSaveSuccess({itemId, mode, payload, result});
|
|
189
266
|
}
|
|
190
267
|
router.back();
|
|
191
268
|
} catch (err) {
|
|
192
269
|
toast.catch(err, `Failed to ${mode === "create" ? "create" : "update"} ${modelName}`);
|
|
193
270
|
}
|
|
194
|
-
}, [
|
|
271
|
+
}, [
|
|
272
|
+
mode,
|
|
273
|
+
formState,
|
|
274
|
+
itemId,
|
|
275
|
+
createItem,
|
|
276
|
+
updateItem,
|
|
277
|
+
validate,
|
|
278
|
+
toast,
|
|
279
|
+
modelName,
|
|
280
|
+
transformPayload,
|
|
281
|
+
onSaveSuccess,
|
|
282
|
+
]);
|
|
195
283
|
|
|
196
284
|
const handleDelete = useCallback(async () => {
|
|
197
285
|
if (!itemId) {
|
|
@@ -205,6 +293,29 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
205
293
|
}
|
|
206
294
|
}, [itemId, deleteItem, toast, modelName]);
|
|
207
295
|
|
|
296
|
+
const isSaving = isCreating || isUpdating;
|
|
297
|
+
|
|
298
|
+
// Set header action buttons (save/delete)
|
|
299
|
+
useEffect(() => {
|
|
300
|
+
if (!modelConfig) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
navigation.setOptions({
|
|
304
|
+
headerRight: () => (
|
|
305
|
+
<Box alignItems="center" direction="row" gap={2} justifyContent="center" marginRight={3}>
|
|
306
|
+
{mode === "edit" && <DeleteButton loading={isDeleting} onDelete={handleDelete} />}
|
|
307
|
+
<Button
|
|
308
|
+
loading={isSaving}
|
|
309
|
+
onClick={handleSave}
|
|
310
|
+
testID="admin-save-button"
|
|
311
|
+
text={mode === "create" ? "Create" : "Save"}
|
|
312
|
+
variant="primary"
|
|
313
|
+
/>
|
|
314
|
+
</Box>
|
|
315
|
+
),
|
|
316
|
+
});
|
|
317
|
+
}, [navigation, modelConfig, mode, isSaving, isDeleting, handleSave, handleDelete]);
|
|
318
|
+
|
|
208
319
|
if (isConfigLoading || !modelConfig) {
|
|
209
320
|
return (
|
|
210
321
|
<Page maxWidth="100%">
|
|
@@ -225,32 +336,13 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
225
336
|
);
|
|
226
337
|
}
|
|
227
338
|
|
|
228
|
-
const editableFields = getEditableFields(modelConfig.fields);
|
|
229
|
-
const isSaving = isCreating || isUpdating;
|
|
230
|
-
const saveLabel = mode === "create" ? "Create" : "Save";
|
|
339
|
+
const editableFields = getEditableFields(modelConfig.fields, modelConfig.fieldOrder);
|
|
231
340
|
|
|
232
341
|
const modelConfigs =
|
|
233
342
|
config?.models.map((m: AdminModelConfig) => ({name: m.name, routePath: m.routePath})) ?? [];
|
|
234
343
|
|
|
235
344
|
return (
|
|
236
|
-
<Page
|
|
237
|
-
footer={
|
|
238
|
-
<Box direction="row" gap={2} justifyContent="between" padding={2}>
|
|
239
|
-
<Box>
|
|
240
|
-
{mode === "edit" && <DeleteButton loading={isDeleting} onDelete={handleDelete} />}
|
|
241
|
-
</Box>
|
|
242
|
-
<Button
|
|
243
|
-
loading={isSaving}
|
|
244
|
-
onClick={handleSave}
|
|
245
|
-
testID="admin-save-button"
|
|
246
|
-
text={saveLabel}
|
|
247
|
-
variant="primary"
|
|
248
|
-
/>
|
|
249
|
-
</Box>
|
|
250
|
-
}
|
|
251
|
-
maxWidth="100%"
|
|
252
|
-
scroll
|
|
253
|
-
>
|
|
345
|
+
<Page maxWidth="100%" scroll>
|
|
254
346
|
<Box gap={3} padding={4}>
|
|
255
347
|
{editableFields.map(([fieldKey, fieldConfig]) => (
|
|
256
348
|
<AdminFieldRenderer
|
|
@@ -262,10 +354,12 @@ export const AdminModelForm: React.FC<AdminModelFormProps> = ({
|
|
|
262
354
|
key={fieldKey}
|
|
263
355
|
modelConfigs={modelConfigs}
|
|
264
356
|
onChange={(value: any) => handleFieldChange(fieldKey, value)}
|
|
357
|
+
parentFormState={formState}
|
|
265
358
|
value={formState[fieldKey]}
|
|
266
359
|
/>
|
|
267
360
|
))}
|
|
268
361
|
{editableFields.length === 0 && <EmptyFields />}
|
|
362
|
+
{footerContent}
|
|
269
363
|
</Box>
|
|
270
364
|
</Page>
|
|
271
365
|
);
|
package/src/AdminModelList.tsx
CHANGED
|
@@ -2,7 +2,7 @@ import type {Api} from "@reduxjs/toolkit/query/react";
|
|
|
2
2
|
import {Box, Card, Heading, Page, Spinner, Text} from "@terreno/ui";
|
|
3
3
|
import {router} from "expo-router";
|
|
4
4
|
import React, {useCallback} from "react";
|
|
5
|
-
import type {AdminModelConfig} from "./types";
|
|
5
|
+
import type {AdminCustomScreen, AdminModelConfig} from "./types";
|
|
6
6
|
import {useAdminConfig} from "./useAdminConfig";
|
|
7
7
|
|
|
8
8
|
interface AdminModelListProps {
|
|
@@ -10,6 +10,8 @@ interface AdminModelListProps {
|
|
|
10
10
|
api: Api<any, any, any, any>;
|
|
11
11
|
/** Path to navigate to for the configuration screen. When provided, a Configuration card is shown. */
|
|
12
12
|
configurationPath?: string;
|
|
13
|
+
/** Additional custom screens to display as cards. Merged with any custom screens from the backend config. */
|
|
14
|
+
customScreens?: AdminCustomScreen[];
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
const ScriptsCard: React.FC<{count: number; onPress: () => void}> = ({count, onPress}) => (
|
|
@@ -69,6 +71,26 @@ const ModelCard: React.FC<{model: AdminModelConfig; onPress: (name: string) => v
|
|
|
69
71
|
);
|
|
70
72
|
};
|
|
71
73
|
|
|
74
|
+
const CustomScreenCard: React.FC<{screen: AdminCustomScreen; onPress: () => void}> = ({
|
|
75
|
+
screen,
|
|
76
|
+
onPress,
|
|
77
|
+
}) => (
|
|
78
|
+
<Card padding={4} testID={`admin-custom-screen-card-${screen.name}`}>
|
|
79
|
+
<Box
|
|
80
|
+
accessibilityHint={`Navigate to ${screen.displayName}`}
|
|
81
|
+
accessibilityLabel={screen.displayName}
|
|
82
|
+
gap={2}
|
|
83
|
+
onClick={onPress}
|
|
84
|
+
width={240}
|
|
85
|
+
>
|
|
86
|
+
<Heading size="md">{screen.displayName}</Heading>
|
|
87
|
+
<Text color="secondaryDark" size="sm">
|
|
88
|
+
{screen.description ?? "Custom screen"}
|
|
89
|
+
</Text>
|
|
90
|
+
</Box>
|
|
91
|
+
</Card>
|
|
92
|
+
);
|
|
93
|
+
|
|
72
94
|
/**
|
|
73
95
|
* Admin panel entry screen that displays all available models as clickable cards.
|
|
74
96
|
*
|
|
@@ -79,6 +101,7 @@ const ModelCard: React.FC<{model: AdminModelConfig; onPress: (name: string) => v
|
|
|
79
101
|
* @param props - Component props
|
|
80
102
|
* @param props.baseUrl - Base URL for admin routes (e.g., "/admin")
|
|
81
103
|
* @param props.api - RTK Query API instance for making authenticated requests
|
|
104
|
+
* @param props.customScreens - Additional custom screens to display as cards
|
|
82
105
|
*
|
|
83
106
|
* @example
|
|
84
107
|
* ```typescript
|
|
@@ -97,6 +120,7 @@ export const AdminModelList: React.FC<AdminModelListProps> = ({
|
|
|
97
120
|
baseUrl,
|
|
98
121
|
api,
|
|
99
122
|
configurationPath,
|
|
123
|
+
customScreens: propCustomScreens,
|
|
100
124
|
}) => {
|
|
101
125
|
const {config, isLoading, error} = useAdminConfig(api, baseUrl);
|
|
102
126
|
|
|
@@ -127,30 +151,22 @@ export const AdminModelList: React.FC<AdminModelListProps> = ({
|
|
|
127
151
|
);
|
|
128
152
|
}
|
|
129
153
|
|
|
130
|
-
const
|
|
154
|
+
const backendScreens = config.customScreens ?? [];
|
|
155
|
+
const allCustomScreens = [...backendScreens, ...(propCustomScreens ?? [])];
|
|
131
156
|
const scripts = config.scripts ?? [];
|
|
132
|
-
const hasToolCards = scripts.length > 0 || !!configurationPath;
|
|
157
|
+
const hasToolCards = allCustomScreens.length > 0 || scripts.length > 0 || !!configurationPath;
|
|
133
158
|
|
|
134
159
|
return (
|
|
135
160
|
<Page maxWidth="100%" scroll title="Admin">
|
|
136
161
|
<Box gap={4} padding={4}>
|
|
137
|
-
{
|
|
162
|
+
{hasToolCards && (
|
|
138
163
|
<Box direction="row" gap={4} wrap>
|
|
139
|
-
{
|
|
140
|
-
<
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
onClick={() => handlePress(screen.name)}
|
|
146
|
-
width={240}
|
|
147
|
-
>
|
|
148
|
-
<Heading size="md">{screen.displayName}</Heading>
|
|
149
|
-
<Text color="secondaryDark" size="sm">
|
|
150
|
-
Singleton config
|
|
151
|
-
</Text>
|
|
152
|
-
</Box>
|
|
153
|
-
</Card>
|
|
164
|
+
{allCustomScreens.map((screen) => (
|
|
165
|
+
<CustomScreenCard
|
|
166
|
+
key={screen.name}
|
|
167
|
+
onPress={() => handlePress(screen.name)}
|
|
168
|
+
screen={screen}
|
|
169
|
+
/>
|
|
154
170
|
))}
|
|
155
171
|
{scripts.length > 0 && (
|
|
156
172
|
<ScriptsCard count={scripts.length} onPress={() => handlePress("__scripts")} />
|
package/src/AdminModelTable.tsx
CHANGED
|
@@ -10,12 +10,12 @@ import {
|
|
|
10
10
|
IconButton,
|
|
11
11
|
Link,
|
|
12
12
|
Page,
|
|
13
|
+
printDateAndTime,
|
|
13
14
|
Spinner,
|
|
14
15
|
Text,
|
|
15
16
|
} from "@terreno/ui";
|
|
16
17
|
import {router, useNavigation} from "expo-router";
|
|
17
18
|
import startCase from "lodash/startCase";
|
|
18
|
-
import {DateTime} from "luxon";
|
|
19
19
|
import React, {useCallback, useEffect, useMemo, useState} from "react";
|
|
20
20
|
import type {AdminFieldConfig, AdminModelConfig} from "./types";
|
|
21
21
|
import {useAdminApi} from "./useAdminApi";
|
|
@@ -31,28 +31,34 @@ interface AdminModelTableProps {
|
|
|
31
31
|
const ACTIONS_COLUMN_TYPE = "adminActions";
|
|
32
32
|
const LINK_COLUMN_TYPE = "adminLink";
|
|
33
33
|
const DEFAULT_LIMIT = 20;
|
|
34
|
+
const DATE_FIELD_NAMES = new Set(["created", "updated", "deleted"]);
|
|
34
35
|
|
|
35
|
-
const
|
|
36
|
-
if (fieldConfig
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
const getColumnType = (fieldKey: string, fieldConfig?: AdminFieldConfig): string => {
|
|
37
|
+
if (fieldConfig) {
|
|
38
|
+
if (fieldConfig.type === "boolean") {
|
|
39
|
+
return "boolean";
|
|
40
|
+
}
|
|
41
|
+
if (fieldConfig.type === "number") {
|
|
42
|
+
return "number";
|
|
43
|
+
}
|
|
44
|
+
if (fieldConfig.type === "date" || fieldConfig.type === "datetime") {
|
|
45
|
+
return "date";
|
|
46
|
+
}
|
|
41
47
|
}
|
|
42
|
-
if (
|
|
48
|
+
if (DATE_FIELD_NAMES.has(fieldKey)) {
|
|
43
49
|
return "date";
|
|
44
50
|
}
|
|
45
51
|
return "text";
|
|
46
52
|
};
|
|
47
53
|
|
|
48
|
-
const getColumnWidth = (fieldKey: string,
|
|
49
|
-
if (
|
|
54
|
+
const getColumnWidth = (fieldKey: string, columnType: string): number => {
|
|
55
|
+
if (columnType === "boolean") {
|
|
50
56
|
return 100;
|
|
51
57
|
}
|
|
52
|
-
if (
|
|
58
|
+
if (columnType === "number") {
|
|
53
59
|
return 120;
|
|
54
60
|
}
|
|
55
|
-
if (
|
|
61
|
+
if (columnType === "date") {
|
|
56
62
|
return 180;
|
|
57
63
|
}
|
|
58
64
|
if (fieldKey === "_id") {
|
|
@@ -61,16 +67,18 @@ const getColumnWidth = (fieldKey: string, fieldConfig: AdminFieldConfig): number
|
|
|
61
67
|
return 200;
|
|
62
68
|
};
|
|
63
69
|
|
|
64
|
-
const formatCellValue = (value: any,
|
|
70
|
+
const formatCellValue = (value: any, columnType: string): string => {
|
|
65
71
|
if (value == null) {
|
|
66
72
|
return "";
|
|
67
73
|
}
|
|
68
|
-
if (
|
|
74
|
+
if (columnType === "boolean") {
|
|
69
75
|
return value ? "\u2713" : "";
|
|
70
76
|
}
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
if (columnType === "date") {
|
|
78
|
+
return printDateAndTime(String(value), {defaultValue: String(value)});
|
|
79
|
+
}
|
|
80
|
+
if (Array.isArray(value)) {
|
|
81
|
+
return `${value.length} item${value.length === 1 ? "" : "s"}`;
|
|
74
82
|
}
|
|
75
83
|
if (typeof value === "object") {
|
|
76
84
|
return value._id ?? JSON.stringify(value);
|
|
@@ -201,12 +209,14 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
201
209
|
}
|
|
202
210
|
navigation.setOptions({
|
|
203
211
|
headerRight: () => (
|
|
204
|
-
<
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
212
|
+
<Box alignItems="center" justifyContent="center" marginRight={3}>
|
|
213
|
+
<Button
|
|
214
|
+
onClick={() => router.push(`${baseUrl}/${modelName}/create` as any)}
|
|
215
|
+
testID="admin-create-button"
|
|
216
|
+
text="Create"
|
|
217
|
+
variant="primary"
|
|
218
|
+
/>
|
|
219
|
+
</Box>
|
|
210
220
|
),
|
|
211
221
|
title: modelConfig.displayName,
|
|
212
222
|
});
|
|
@@ -263,15 +273,12 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
263
273
|
const dataColumns: DataTableColumn[] = displayFields.map((fieldKey, index) => {
|
|
264
274
|
const fieldConfig = modelConfig.fields[fieldKey];
|
|
265
275
|
const isFirst = index === 0;
|
|
276
|
+
const columnType = getColumnType(fieldKey, fieldConfig);
|
|
266
277
|
return {
|
|
267
|
-
columnType: isFirst
|
|
268
|
-
? LINK_COLUMN_TYPE
|
|
269
|
-
: fieldConfig
|
|
270
|
-
? mapFieldTypeToColumnType(fieldConfig)
|
|
271
|
-
: "text",
|
|
278
|
+
columnType: isFirst ? LINK_COLUMN_TYPE : columnType,
|
|
272
279
|
sortable: true,
|
|
273
280
|
title: startCase(fieldKey),
|
|
274
|
-
width:
|
|
281
|
+
width: getColumnWidth(fieldKey, columnType),
|
|
275
282
|
};
|
|
276
283
|
});
|
|
277
284
|
|
|
@@ -289,23 +296,20 @@ export const AdminModelTable: React.FC<AdminModelTableProps> = ({
|
|
|
289
296
|
const fieldCells = displayFields.map((fieldKey, index) => {
|
|
290
297
|
const fieldConfig = modelConfig.fields[fieldKey];
|
|
291
298
|
const isFirst = index === 0;
|
|
299
|
+
const columnType = getColumnType(fieldKey, fieldConfig);
|
|
300
|
+
const formatted = formatCellValue(item[fieldKey], columnType);
|
|
292
301
|
|
|
293
302
|
if (isFirst) {
|
|
294
|
-
const displayText = fieldConfig
|
|
295
|
-
? formatCellValue(item[fieldKey], fieldConfig)
|
|
296
|
-
: String(item[fieldKey] ?? "");
|
|
297
303
|
return {
|
|
298
304
|
value: {
|
|
299
305
|
href: `${baseUrl}/${modelName}/${item._id}`,
|
|
300
|
-
text:
|
|
306
|
+
text: formatted,
|
|
301
307
|
},
|
|
302
308
|
};
|
|
303
309
|
}
|
|
304
310
|
|
|
305
311
|
return {
|
|
306
|
-
value:
|
|
307
|
-
? formatCellValue(item[fieldKey], fieldConfig)
|
|
308
|
-
: String(item[fieldKey] ?? ""),
|
|
312
|
+
value: formatted,
|
|
309
313
|
};
|
|
310
314
|
});
|
|
311
315
|
|