admin_lsac 2.0.21 → 2.0.22

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.
@@ -4,33 +4,6 @@ import { Field, Form, Formik, FieldArray, getIn } from 'formik';
4
4
  import * as Yup from 'yup';
5
5
  import Swal from 'sweetalert2';
6
6
  import { useEffect, useState } from 'react';
7
- function parseValidation(fields) {
8
- if (!fields) {
9
- return {};
10
- }
11
- return fields.reduce((acc, field) => {
12
- if (field.type !== 'object' && field.type !== 'array') {
13
- acc[field.name] = field.validationSchema ? field.validationSchema : '';
14
- }
15
- else if (field.type === 'array') {
16
- if (field.element.type === 'dropdown') {
17
- acc[field.name] = field.element.validationSchema
18
- ? Yup.array().of(field.element.validationSchema)
19
- : '';
20
- }
21
- else if (field.element.type === 'object') {
22
- acc[field.name] = Yup.array().of(Yup.object(parseValidation(field.element.fields)));
23
- }
24
- else {
25
- acc[field.name] = Yup.array().of(field.element.validationSchema);
26
- }
27
- }
28
- else if (field.type === 'object') {
29
- acc[field.name] = Yup.object(parseValidation(field.fields));
30
- }
31
- return acc;
32
- }, {});
33
- }
34
7
  function generateSingleField(formfield, fieldName, key) {
35
8
  return (_jsx(Field, { name: fieldName, children: ({ field, form }) => (_jsxs(FormControl, { isInvalid: getIn(form.touched, fieldName) && getIn(form.errors, fieldName), children: [_jsx(FormLabel, { marginTop: 2, children: formfield.label }), formfield.type === 'string' ||
36
9
  formfield.type === 'number' ||
@@ -56,19 +29,19 @@ function parseFields(fields, previous) {
56
29
  return _jsx(FormControl, { children: objectFields }, key);
57
30
  }
58
31
  else if (formfield.type === 'array') {
59
- return (_jsx(FieldArray, { name: fieldName, children: ({ remove, push, form }) => (_jsxs("div", { children: [_jsx(FormLabel, { marginTop: 2, children: formfield.label }), getIn(form.values, fieldName).length > 0 &&
32
+ return (_jsx(FieldArray, { name: fieldName, children: ({ pop, push, form }) => (_jsxs("div", { children: [_jsx(FormLabel, { marginTop: 2, children: formfield.label }), getIn(form.values, fieldName).length > 0 &&
60
33
  getIn(form.values, fieldName).map((_, index) => (_jsxs(Box, { paddingLeft: 10, children: [_jsx("h4", { children: _jsx("b", { children: `${fieldName}.${index}` }) }), _jsx(Box, { paddingLeft: 10, children: formfield.element.type === 'object'
61
34
  ? parseFields(formfield.element.fields, `${fieldName}.${index}`)
62
- : generateSingleField(formfield.element, `${fieldName}.${index}`, key) }), _jsxs(Button, { type: "button", colorScheme: "red", marginY: 2, onClick: () => remove(index), children: ["Remove ", `${fieldName}.${index}`] })] }, index))), _jsxs(Button, { type: "button", marginY: 2, colorScheme: "blue", onClick: () => {
63
- let value = undefined;
35
+ : generateSingleField(formfield.element, `${fieldName}.${index}`, key) })] }, index))), _jsxs(Button, { type: "button", marginY: 4, colorScheme: "blue", onClick: () => {
36
+ let value;
64
37
  if (formfield.element.type === 'object') {
65
- value = getCustomInitialValues(formfield.element.fields);
38
+ value = getDefaultValues(formfield.element.fields);
66
39
  }
67
40
  else {
68
- value = getCustomInitialValues([formfield.element])[0];
41
+ value = getDefaultValues([formfield.element])[0];
69
42
  }
70
43
  push(value);
71
- }, children: ["Add ", fieldName] })] })) }, key));
44
+ }, children: ["Add ", fieldName] }), getIn(form.values, fieldName).length > 0 && (_jsxs(Button, { type: "button", colorScheme: "red", marginY: 4, ml: 4, onClick: () => pop(), children: ["Remove last ", fieldName] }))] })) }, key));
72
45
  }
73
46
  else {
74
47
  return generateSingleField(formfield, fieldName, key);
@@ -76,14 +49,41 @@ function parseFields(fields, previous) {
76
49
  });
77
50
  return computedFields;
78
51
  }
79
- function getCustomInitialValues(fields) {
52
+ function parseValidation(fields) {
53
+ if (!fields) {
54
+ return {};
55
+ }
56
+ return fields.reduce((acc, field) => {
57
+ if (field.type !== 'object' && field.type !== 'array') {
58
+ acc[field.name] = field.validationSchema ? field.validationSchema : '';
59
+ }
60
+ else if (field.type === 'array') {
61
+ if (field.element.type !== 'object') {
62
+ acc[field.name] = Yup.isSchema(field.element.validationSchema)
63
+ ? Yup.array().of(field.element.validationSchema)
64
+ : '';
65
+ }
66
+ else if (field.element.type === 'object') {
67
+ acc[field.name] = Yup.array().of(Yup.object(parseValidation(field.element.fields)));
68
+ }
69
+ }
70
+ else if (field.type === 'object') {
71
+ acc[field.name] = Yup.object(parseValidation(field.fields));
72
+ }
73
+ return acc;
74
+ }, {});
75
+ }
76
+ function getDefaultValues(fields) {
80
77
  const obj = {};
81
78
  if (!fields) {
82
79
  return obj;
83
80
  }
84
81
  fields.forEach((field) => {
85
82
  if (field.type === 'object') {
86
- obj[field.name] = getCustomInitialValues(field.fields);
83
+ obj[field.name] = getDefaultValues(field.fields);
84
+ }
85
+ else if (field.type === 'array') {
86
+ obj[field.name] = [];
87
87
  }
88
88
  else {
89
89
  obj[field.name] = undefined;
@@ -91,12 +91,12 @@ function getCustomInitialValues(fields) {
91
91
  });
92
92
  return obj;
93
93
  }
94
- async function getInitialValues(action, rowId) {
94
+ async function getValuesFromInitialise(action, rowId) {
95
95
  let obj = {};
96
96
  if (action.initialise) {
97
97
  obj = await action.initialise(rowId);
98
98
  }
99
- const customInitialValues = getCustomInitialValues(action.fields);
99
+ const customInitialValues = getDefaultValues(action.fields);
100
100
  return Object.assign({}, customInitialValues, obj);
101
101
  }
102
102
  async function setOptions(fields) {
@@ -118,7 +118,7 @@ async function setOptions(fields) {
118
118
  }
119
119
  export function GenericForm({ action, onClose, rowId, rowObject }) {
120
120
  const [formFields, setFormFields] = useState(parseFields(action.fields));
121
- const [initialValues, setInitialValues] = useState(getCustomInitialValues(action.fields));
121
+ const [initialValues, setInitialValues] = useState(getDefaultValues(action.fields));
122
122
  const [validationSchema, _] = useState(parseValidation(action.fields));
123
123
  useEffect(() => {
124
124
  setOptions(action.fields).then(() => {
@@ -126,7 +126,7 @@ export function GenericForm({ action, onClose, rowId, rowObject }) {
126
126
  });
127
127
  }, []);
128
128
  const handleFormReset = async () => {
129
- const newInitialValues = await getInitialValues(action, rowId);
129
+ const newInitialValues = await getValuesFromInitialise(action, rowId);
130
130
  setInitialValues(newInitialValues);
131
131
  };
132
132
  return (_jsx(_Fragment, { children: _jsx(Formik, { enableReinitialize: true, initialValues: initialValues, validationSchema: Yup.object(validationSchema), onReset: handleFormReset, onSubmit: async (values) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "admin_lsac",
3
3
  "private": false,
4
- "version": "2.0.21",
4
+ "version": "2.0.22",
5
5
  "description": "Generic Admin Panel created for storing events' data",
6
6
  "scripts": {
7
7
  "dev": "vite",