admin_lsac 1.2.8 → 1.2.9
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/App.js +3 -3
- package/dist/cjs/App.js +3 -3
- package/dist/cjs/components/Page.js +23 -8
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/utils/websockets.js +22 -0
- package/dist/components/Page.js +23 -8
- package/dist/esm/App.js +3 -3
- package/dist/esm/components/Page.js +23 -8
- package/dist/esm/index.js +3 -3
- package/dist/esm/utils/websockets.js +15 -0
- package/dist/index.js +2 -2
- package/dist/utils/websockets.js +22 -0
- package/package.json +2 -1
- package/src/App.tsx +50 -110
- package/src/components/GenericForm.tsx +308 -304
- package/src/components/Page.tsx +243 -223
- package/src/index.tsx +20 -20
- package/src/pages/event.ts +93 -93
- package/src/pages/page.ts +336 -336
- package/src/schema/schema.config.ts +8 -8
- package/src/schema/schema.ts +63 -63
- package/src/utils/websockets.tsx +17 -0
|
@@ -1,304 +1,308 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Box,
|
|
3
|
-
Button,
|
|
4
|
-
FormControl,
|
|
5
|
-
FormErrorMessage,
|
|
6
|
-
FormLabel,
|
|
7
|
-
Input,
|
|
8
|
-
Select,
|
|
9
|
-
} from "@chakra-ui/react";
|
|
10
|
-
import { Field, Form, Formik, FieldArray, getIn } from "formik";
|
|
11
|
-
import {
|
|
12
|
-
Action,
|
|
13
|
-
FormField,
|
|
14
|
-
SimpleField,
|
|
15
|
-
DropdownField,
|
|
16
|
-
RowAction,
|
|
17
|
-
} from "../schema/schema";
|
|
18
|
-
import * as Yup from "yup";
|
|
19
|
-
import Swal from "sweetalert2";
|
|
20
|
-
import { useEffect, useState } from "react";
|
|
21
|
-
|
|
22
|
-
interface
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
);
|
|
47
|
-
} else {
|
|
48
|
-
acc[field.name] = Yup.array().of(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
))
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
formfield.element,
|
|
147
|
-
`${fieldName}.${index}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
fields
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
else
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const [
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if (onClose) onClose();
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Box,
|
|
3
|
+
Button,
|
|
4
|
+
FormControl,
|
|
5
|
+
FormErrorMessage,
|
|
6
|
+
FormLabel,
|
|
7
|
+
Input,
|
|
8
|
+
Select,
|
|
9
|
+
} from "@chakra-ui/react";
|
|
10
|
+
import { Field, Form, Formik, FieldArray, getIn } from "formik";
|
|
11
|
+
import {
|
|
12
|
+
Action,
|
|
13
|
+
FormField,
|
|
14
|
+
SimpleField,
|
|
15
|
+
DropdownField,
|
|
16
|
+
RowAction,
|
|
17
|
+
} from "../schema/schema";
|
|
18
|
+
import * as Yup from "yup";
|
|
19
|
+
import Swal from "sweetalert2";
|
|
20
|
+
import { useEffect, useState } from "react";
|
|
21
|
+
|
|
22
|
+
interface RowActionWithInitialise extends RowAction {
|
|
23
|
+
initialise: (obj: any) => any;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface GenericFormProps {
|
|
27
|
+
action: Action | RowAction;
|
|
28
|
+
onClose?: () => void;
|
|
29
|
+
rowId?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function parseValidation(fields: FormField[] | undefined) {
|
|
33
|
+
if (!fields) {
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return fields.reduce((acc: any, field) => {
|
|
38
|
+
if (
|
|
39
|
+
field.type !== "dropdown" &&
|
|
40
|
+
field.type !== "object" &&
|
|
41
|
+
field.type !== "array"
|
|
42
|
+
) {
|
|
43
|
+
acc[field.name] = field.validationSchema;
|
|
44
|
+
} else if (field.type === "array") {
|
|
45
|
+
if (field.element.type === "dropdown") {
|
|
46
|
+
acc[field.name] = Yup.array().of(Yup.string().required());
|
|
47
|
+
} else if (field.element.type === "object") {
|
|
48
|
+
acc[field.name] = Yup.array().of(
|
|
49
|
+
Yup.object(parseValidation(field.element.fields))
|
|
50
|
+
);
|
|
51
|
+
} else {
|
|
52
|
+
acc[field.name] = Yup.array().of(field.element.validationSchema);
|
|
53
|
+
}
|
|
54
|
+
} else if (field.type === "object") {
|
|
55
|
+
acc[field.name] = Yup.object(parseValidation(field.fields));
|
|
56
|
+
}
|
|
57
|
+
if (field.type === "dropdown") {
|
|
58
|
+
acc[field.name] = Yup.string().required();
|
|
59
|
+
}
|
|
60
|
+
return acc;
|
|
61
|
+
}, {});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function generateSingleField(
|
|
65
|
+
formfield: SimpleField | DropdownField,
|
|
66
|
+
fieldName: string,
|
|
67
|
+
key: any
|
|
68
|
+
) {
|
|
69
|
+
return (
|
|
70
|
+
<Field name={fieldName} key={key}>
|
|
71
|
+
{({ field, form }: any) => (
|
|
72
|
+
<FormControl
|
|
73
|
+
isInvalid={
|
|
74
|
+
getIn(form.touched, fieldName) && getIn(form.errors, fieldName)
|
|
75
|
+
}
|
|
76
|
+
>
|
|
77
|
+
<FormLabel marginTop={2}>{formfield.label}</FormLabel>
|
|
78
|
+
|
|
79
|
+
{formfield.type === "string" || formfield.type === "number" ? (
|
|
80
|
+
<Input
|
|
81
|
+
{...field}
|
|
82
|
+
type={formfield.type === "number" ? "number" : "text"}
|
|
83
|
+
placeholder=""
|
|
84
|
+
/>
|
|
85
|
+
) : formfield.type === "file" ? (
|
|
86
|
+
<Input
|
|
87
|
+
type="file"
|
|
88
|
+
name={fieldName}
|
|
89
|
+
onChange={(event) => {
|
|
90
|
+
if (event.currentTarget.files != null) {
|
|
91
|
+
form.setFieldValue(fieldName, event.currentTarget.files[0]);
|
|
92
|
+
}
|
|
93
|
+
}}
|
|
94
|
+
/>
|
|
95
|
+
) : (
|
|
96
|
+
<Select {...field} defaultValue="">
|
|
97
|
+
<option disabled={true} value="">
|
|
98
|
+
Select an option!
|
|
99
|
+
</option>
|
|
100
|
+
{(formfield as DropdownField).options?.map((option, key) => (
|
|
101
|
+
<option key={key} value={`${option.value}`}>
|
|
102
|
+
{option.display}
|
|
103
|
+
</option>
|
|
104
|
+
))}
|
|
105
|
+
</Select>
|
|
106
|
+
)}
|
|
107
|
+
|
|
108
|
+
<FormErrorMessage>{getIn(form.errors, fieldName)}</FormErrorMessage>
|
|
109
|
+
</FormControl>
|
|
110
|
+
)}
|
|
111
|
+
</Field>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function parseFields(
|
|
116
|
+
fields: FormField[] | undefined,
|
|
117
|
+
previous?: string | undefined
|
|
118
|
+
) {
|
|
119
|
+
if (!fields) {
|
|
120
|
+
return [];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const computedFields = fields.map((formfield, key) => {
|
|
124
|
+
let fieldName: string =
|
|
125
|
+
previous !== undefined ? previous + "." + formfield.name : formfield.name;
|
|
126
|
+
|
|
127
|
+
if (formfield.type === "object") {
|
|
128
|
+
const objectFields = parseFields(formfield.fields, fieldName);
|
|
129
|
+
return <FormControl key={fieldName}>{objectFields}</FormControl>;
|
|
130
|
+
} else if (formfield.type === "array") {
|
|
131
|
+
return (
|
|
132
|
+
<FieldArray name={fieldName}>
|
|
133
|
+
{({ remove, push, form }) => (
|
|
134
|
+
<div>
|
|
135
|
+
<FormLabel marginTop={2}>{formfield.label}</FormLabel>
|
|
136
|
+
{getIn(form.values, fieldName).length > 0 &&
|
|
137
|
+
getIn(form.values, fieldName).map(
|
|
138
|
+
(element: any, index: number) => (
|
|
139
|
+
<Box paddingLeft={10}>
|
|
140
|
+
<h4>
|
|
141
|
+
<b>{`${fieldName}.${index}`}</b>
|
|
142
|
+
</h4>
|
|
143
|
+
<Box paddingLeft={10}>
|
|
144
|
+
{formfield.element.type === "object"
|
|
145
|
+
? parseFields(
|
|
146
|
+
formfield.element.fields,
|
|
147
|
+
`${fieldName}.${index}`
|
|
148
|
+
)
|
|
149
|
+
: generateSingleField(
|
|
150
|
+
formfield.element,
|
|
151
|
+
`${fieldName}.${index}`,
|
|
152
|
+
key
|
|
153
|
+
)}
|
|
154
|
+
</Box>
|
|
155
|
+
<Button
|
|
156
|
+
type="button"
|
|
157
|
+
colorScheme="red"
|
|
158
|
+
marginY={2}
|
|
159
|
+
onClick={() => remove(index)}
|
|
160
|
+
>
|
|
161
|
+
Remove {`${fieldName}.${index}`}
|
|
162
|
+
</Button>
|
|
163
|
+
</Box>
|
|
164
|
+
)
|
|
165
|
+
)}
|
|
166
|
+
|
|
167
|
+
<Button
|
|
168
|
+
type="button"
|
|
169
|
+
marginY={2}
|
|
170
|
+
colorScheme="blue"
|
|
171
|
+
onClick={() => {
|
|
172
|
+
let value = undefined;
|
|
173
|
+
|
|
174
|
+
if (formfield.element.type === "object") {
|
|
175
|
+
value = getCustomInitialValues(formfield.element.fields);
|
|
176
|
+
} else {
|
|
177
|
+
value = getCustomInitialValues([formfield.element])[0];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
push(value);
|
|
181
|
+
}}
|
|
182
|
+
>
|
|
183
|
+
Add {fieldName}
|
|
184
|
+
</Button>
|
|
185
|
+
</div>
|
|
186
|
+
)}
|
|
187
|
+
</FieldArray>
|
|
188
|
+
);
|
|
189
|
+
} else {
|
|
190
|
+
return generateSingleField(formfield, fieldName, key);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return computedFields;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function getCustomInitialValues(fields: FormField[] | undefined) {
|
|
198
|
+
let obj: any = {};
|
|
199
|
+
|
|
200
|
+
if (!fields) {
|
|
201
|
+
return obj;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
fields.forEach((field) => {
|
|
205
|
+
if (field.type === "array") obj[field.name] = [];
|
|
206
|
+
else if (field.type === "object")
|
|
207
|
+
obj[field.name] = getCustomInitialValues(field.fields);
|
|
208
|
+
else if (field.type === "string") obj[field.name] = "";
|
|
209
|
+
else if (field.type === "number") obj[field.name] = 0;
|
|
210
|
+
else obj[field.name] = null;
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
return obj;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function getInitialValues(action: Action | RowAction, rowId: number | undefined) {
|
|
217
|
+
let obj: any = {};
|
|
218
|
+
if ((action as RowAction).initialise) {
|
|
219
|
+
obj = await (action as RowActionWithInitialise).initialise(rowId);
|
|
220
|
+
}
|
|
221
|
+
const customInitialValues = getCustomInitialValues(action.fields);
|
|
222
|
+
return Object.assign({}, customInitialValues, obj);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function setOptions(fields: FormField[] | undefined) {
|
|
226
|
+
if (!fields) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const promises = fields.map(async (field) => {
|
|
231
|
+
if (field.type === "dropdown") field.options = await field.getOptions();
|
|
232
|
+
else if (field.type === "array") await setOptions([field.element]);
|
|
233
|
+
else if (field.type === "object") await setOptions(field.fields);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
await Promise.all(promises);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function GenericForm({ action, onClose, rowId }: GenericFormProps) {
|
|
240
|
+
const [formFields, setFormFields] = useState<any[]>(
|
|
241
|
+
parseFields(action.fields)
|
|
242
|
+
);
|
|
243
|
+
const [initialValues, setInitialValues] = useState<any>(getCustomInitialValues(action.fields));
|
|
244
|
+
const [validationSchema, setValidationSchema] = useState<any>(parseValidation(action.fields));
|
|
245
|
+
|
|
246
|
+
useEffect(() => {
|
|
247
|
+
setOptions(action.fields).then(() => {
|
|
248
|
+
setFormFields(parseFields(action.fields));
|
|
249
|
+
});
|
|
250
|
+
}, []);
|
|
251
|
+
|
|
252
|
+
const handleFormReset = async () => {
|
|
253
|
+
const newInitialValues = await getInitialValues(action, rowId);
|
|
254
|
+
console.log(newInitialValues)
|
|
255
|
+
setInitialValues(newInitialValues);
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
if (!action.fields) return <></>;
|
|
259
|
+
return (
|
|
260
|
+
<>
|
|
261
|
+
<Formik
|
|
262
|
+
enableReinitialize={true}
|
|
263
|
+
initialValues={initialValues}
|
|
264
|
+
validationSchema={Yup.object(validationSchema)}
|
|
265
|
+
onReset={handleFormReset}
|
|
266
|
+
onSubmit={async (values) => {
|
|
267
|
+
try {
|
|
268
|
+
let res;
|
|
269
|
+
if (rowId) res = await action.onSubmit(values, rowId);
|
|
270
|
+
else res = await action.onSubmit(values);
|
|
271
|
+
|
|
272
|
+
if (onClose) onClose();
|
|
273
|
+
|
|
274
|
+
Swal.fire("Ok");
|
|
275
|
+
} catch (e: any) {
|
|
276
|
+
if (onClose) onClose();
|
|
277
|
+
const err = e.request.response;
|
|
278
|
+
|
|
279
|
+
Swal.fire({
|
|
280
|
+
text: "Error: " + err,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}}
|
|
284
|
+
>
|
|
285
|
+
{({ isSubmitting }) => (
|
|
286
|
+
<Form>
|
|
287
|
+
{formFields}
|
|
288
|
+
<Button
|
|
289
|
+
mt={4}
|
|
290
|
+
colorScheme="teal"
|
|
291
|
+
isLoading={isSubmitting}
|
|
292
|
+
type="submit"
|
|
293
|
+
>
|
|
294
|
+
Submit
|
|
295
|
+
</Button>
|
|
296
|
+
<Button
|
|
297
|
+
mt={4}
|
|
298
|
+
colorScheme="teal"
|
|
299
|
+
type="reset"
|
|
300
|
+
>
|
|
301
|
+
Initialise Form
|
|
302
|
+
</Button>
|
|
303
|
+
</Form>
|
|
304
|
+
)}
|
|
305
|
+
</Formik>
|
|
306
|
+
</>
|
|
307
|
+
);
|
|
308
|
+
}
|