admin_lsac 1.1.4 → 1.2.2
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 +2 -2
- package/dist/App.d.ts +3 -1
- package/dist/App.js +38 -4
- package/dist/cjs/App.js +48 -0
- package/dist/cjs/components/GenericForm.js +269 -0
- package/dist/cjs/components/Page.js +106 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/pages/event.js +110 -0
- package/dist/cjs/pages/page.js +343 -0
- package/dist/cjs/reportWebVitals.js +34 -0
- package/dist/cjs/schema/schema.config.js +9 -0
- package/dist/cjs/schema/schema.js +7 -0
- package/dist/cjs/setupTests.js +7 -0
- package/dist/components/GenericForm.js +17 -21
- package/dist/components/Page.js +22 -11
- package/dist/esm/App.js +27 -12
- package/dist/esm/components/GenericForm.js +243 -243
- package/dist/esm/components/Page.js +99 -99
- package/dist/esm/index.js +13 -13
- package/dist/esm/pages/event.js +104 -104
- package/dist/esm/pages/page.js +321 -0
- package/dist/esm/reportWebVitals.js +13 -13
- package/dist/esm/schema/schema.config.js +6 -4
- package/dist/esm/schema/schema.js +3 -3
- package/dist/esm/setupTests.js +5 -5
- package/dist/index.js +1 -1
- package/dist/pages/event.js +1 -1
- package/dist/pages/page.js +343 -0
- package/dist/reportWebVitals.js +1 -5
- package/dist/schema/schema.config.js +3 -1
- package/package.json +1 -1
- package/src/App.tsx +22 -17
- package/src/components/Page.tsx +6 -4
- package/src/index.tsx +20 -20
- package/src/pages/page.ts +291 -0
- package/src/schema/schema.config.ts +2 -0
|
@@ -1,243 +1,243 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
49
|
-
import { Box, Button, FormControl, FormErrorMessage, FormLabel, Input, Select, } from "@chakra-ui/react";
|
|
50
|
-
import { Field, Form, Formik, FieldArray, getIn } from "formik";
|
|
51
|
-
import * as Yup from "yup";
|
|
52
|
-
import Swal from "sweetalert2";
|
|
53
|
-
import { useEffect, useState } from "react";
|
|
54
|
-
function parseValidation(fields) {
|
|
55
|
-
if (!fields) {
|
|
56
|
-
return {};
|
|
57
|
-
}
|
|
58
|
-
return fields.reduce(function (acc, field) {
|
|
59
|
-
if (field.type !== "dropdown" &&
|
|
60
|
-
field.type !== "object" &&
|
|
61
|
-
field.type !== "array") {
|
|
62
|
-
acc[field.name] = field.validationSchema;
|
|
63
|
-
}
|
|
64
|
-
else if (field.type === "array") {
|
|
65
|
-
if (field.element.type === "dropdown") {
|
|
66
|
-
acc[field.name] = Yup.array().of(Yup.string().required());
|
|
67
|
-
}
|
|
68
|
-
else if (field.element.type === "object") {
|
|
69
|
-
acc[field.name] = Yup.array().of(Yup.object(parseValidation(field.element.fields)));
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
acc[field.name] = Yup.array().of(field.element.validationSchema);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
else if (field.type === "object") {
|
|
76
|
-
acc[field.name] = Yup.object(parseValidation(field.fields));
|
|
77
|
-
}
|
|
78
|
-
if (field.type === "dropdown") {
|
|
79
|
-
acc[field.name] = Yup.string().required();
|
|
80
|
-
}
|
|
81
|
-
return acc;
|
|
82
|
-
}, {});
|
|
83
|
-
}
|
|
84
|
-
function generateSingleField(formfield, fieldName, key) {
|
|
85
|
-
return (_jsx(Field, __assign({ name: fieldName }, { children: function (_a) {
|
|
86
|
-
var _b;
|
|
87
|
-
var field = _a.field, form = _a.form;
|
|
88
|
-
return (_jsxs(FormControl, __assign({ isInvalid: getIn(form.touched, fieldName) && getIn(form.errors, fieldName) }, { children: [_jsx(FormLabel, __assign({ marginTop: 2 }, { children: formfield.label })), formfield.type === "string" || formfield.type === "number" ? (_jsx(Input, __assign({}, field, { type: formfield.type === "number" ? "number" : "text", placeholder: "" }))) : formfield.type === "file" ? (_jsx(Input, { type: "file", name: fieldName, onChange: function (event) {
|
|
89
|
-
if (event.currentTarget.files != null) {
|
|
90
|
-
form.setFieldValue(fieldName, event.currentTarget.files[0]);
|
|
91
|
-
}
|
|
92
|
-
} })) : (_jsxs(Select, __assign({}, field, { defaultValue: "" }, { children: [_jsx("option", __assign({ disabled: true, value: "" }, { children: "Select an option!" })), (_b = formfield.options) === null || _b === void 0 ? void 0 : _b.map(function (option, key) { return (_jsx("option", __assign({ value: "".concat(option.value) }, { children: option.display }), key)); })] }))), _jsx(FormErrorMessage, { children: getIn(form.errors, fieldName) })] })));
|
|
93
|
-
} }), key));
|
|
94
|
-
}
|
|
95
|
-
function parseFields(fields, previous) {
|
|
96
|
-
if (!fields) {
|
|
97
|
-
return [];
|
|
98
|
-
}
|
|
99
|
-
var computedFields = fields.map(function (formfield, key) {
|
|
100
|
-
var fieldName = previous !== undefined ? previous + "." + formfield.name : formfield.name;
|
|
101
|
-
if (formfield.type === "object") {
|
|
102
|
-
var objectFields = parseFields(formfield.fields, fieldName);
|
|
103
|
-
return _jsx(FormControl, { children: objectFields }, fieldName);
|
|
104
|
-
}
|
|
105
|
-
else if (formfield.type === "array") {
|
|
106
|
-
return (_jsx(FieldArray, __assign({ name: fieldName }, { children: function (_a) {
|
|
107
|
-
var remove = _a.remove, push = _a.push, form = _a.form;
|
|
108
|
-
return (_jsxs("div", { children: [_jsx(FormLabel, __assign({ marginTop: 2 }, { children: formfield.label })), getIn(form.values, fieldName).length > 0 &&
|
|
109
|
-
getIn(form.values, fieldName).map(function (element, index) { return (_jsxs(Box, __assign({ paddingLeft: 10 }, { children: [_jsx("h4", { children: _jsx("b", { children: "".concat(fieldName, ".").concat(index) }) }), _jsx(Box, __assign({ paddingLeft: 10 }, { children: formfield.element.type === "object"
|
|
110
|
-
? parseFields(formfield.element.fields, "".concat(fieldName, ".").concat(index))
|
|
111
|
-
: generateSingleField(formfield.element, "".concat(fieldName, ".").concat(index), key) })), _jsxs(Button, __assign({ type: "button", colorScheme: "red", marginY: 2, onClick: function () { return remove(index); } }, { children: ["Remove ", "".concat(fieldName, ".").concat(index)] }))] }))); }), _jsxs(Button, __assign({ type: "button", marginY: 2, colorScheme: "blue", onClick: function () {
|
|
112
|
-
var value = undefined;
|
|
113
|
-
if (formfield.element.type === "object") {
|
|
114
|
-
value = getInitialValues(formfield.element.fields);
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
value = getInitialValues([formfield.element])[0];
|
|
118
|
-
}
|
|
119
|
-
push(value);
|
|
120
|
-
} }, { children: ["Add ", fieldName] }))] }));
|
|
121
|
-
} })));
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
return generateSingleField(formfield, fieldName, key);
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
return computedFields;
|
|
128
|
-
}
|
|
129
|
-
function getInitialValues(fields) {
|
|
130
|
-
var obj = {};
|
|
131
|
-
if (!fields) {
|
|
132
|
-
return obj;
|
|
133
|
-
}
|
|
134
|
-
fields.forEach(function (field) {
|
|
135
|
-
if (field.type === "array")
|
|
136
|
-
obj[field.name] = [];
|
|
137
|
-
else if (field.type === "object")
|
|
138
|
-
obj[field.name] = getInitialValues(field.fields);
|
|
139
|
-
else if (field.type === "string")
|
|
140
|
-
obj[field.name] = "";
|
|
141
|
-
else if (field.type === "number")
|
|
142
|
-
obj[field.name] = 0;
|
|
143
|
-
else
|
|
144
|
-
obj[field.name] = null;
|
|
145
|
-
});
|
|
146
|
-
return obj;
|
|
147
|
-
}
|
|
148
|
-
function setOptions(fields) {
|
|
149
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
150
|
-
var promises;
|
|
151
|
-
var _this = this;
|
|
152
|
-
return __generator(this, function (_a) {
|
|
153
|
-
switch (_a.label) {
|
|
154
|
-
case 0:
|
|
155
|
-
if (!fields) {
|
|
156
|
-
return [2 /*return*/];
|
|
157
|
-
}
|
|
158
|
-
promises = fields.map(function (field) { return __awaiter(_this, void 0, void 0, function () {
|
|
159
|
-
var _a;
|
|
160
|
-
return __generator(this, function (_b) {
|
|
161
|
-
switch (_b.label) {
|
|
162
|
-
case 0:
|
|
163
|
-
if (!(field.type === "dropdown")) return [3 /*break*/, 2];
|
|
164
|
-
_a = field;
|
|
165
|
-
return [4 /*yield*/, field.getOptions()];
|
|
166
|
-
case 1:
|
|
167
|
-
_a.options = _b.sent();
|
|
168
|
-
return [3 /*break*/, 6];
|
|
169
|
-
case 2:
|
|
170
|
-
if (!(field.type === "array")) return [3 /*break*/, 4];
|
|
171
|
-
return [4 /*yield*/, setOptions([field.element])];
|
|
172
|
-
case 3:
|
|
173
|
-
_b.sent();
|
|
174
|
-
return [3 /*break*/, 6];
|
|
175
|
-
case 4:
|
|
176
|
-
if (!(field.type === "object")) return [3 /*break*/, 6];
|
|
177
|
-
return [4 /*yield*/, setOptions(field.fields)];
|
|
178
|
-
case 5:
|
|
179
|
-
_b.sent();
|
|
180
|
-
_b.label = 6;
|
|
181
|
-
case 6: return [2 /*return*/];
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
}); });
|
|
185
|
-
return [4 /*yield*/, Promise.all(promises)];
|
|
186
|
-
case 1:
|
|
187
|
-
_a.sent();
|
|
188
|
-
return [2 /*return*/];
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
export function GenericForm(_a) {
|
|
194
|
-
var _this = this;
|
|
195
|
-
var action = _a.action, onClose = _a.onClose, rowId = _a.rowId;
|
|
196
|
-
var _b = useState(parseFields(action.fields)), formFields = _b[0], setFormFields = _b[1];
|
|
197
|
-
var initialValues = useState(getInitialValues(action.fields))[0];
|
|
198
|
-
var validationSchema = useState(parseValidation(action.fields))[0];
|
|
199
|
-
useEffect(function () {
|
|
200
|
-
setOptions(action.fields).then(function () {
|
|
201
|
-
setFormFields(parseFields(action.fields));
|
|
202
|
-
});
|
|
203
|
-
}, []);
|
|
204
|
-
if (!action.fields)
|
|
205
|
-
return _jsx(_Fragment, {});
|
|
206
|
-
return (_jsx(_Fragment, { children: _jsx(Formik, __assign({ initialValues: initialValues, validationSchema: Yup.object(validationSchema), onSubmit: function (values) { return __awaiter(_this, void 0, void 0, function () {
|
|
207
|
-
var res, e_1, err;
|
|
208
|
-
return __generator(this, function (_a) {
|
|
209
|
-
switch (_a.label) {
|
|
210
|
-
case 0:
|
|
211
|
-
_a.trys.push([0, 5, , 6]);
|
|
212
|
-
res = void 0;
|
|
213
|
-
if (!rowId) return [3 /*break*/, 2];
|
|
214
|
-
return [4 /*yield*/, action.onSubmit(values, rowId)];
|
|
215
|
-
case 1:
|
|
216
|
-
res = _a.sent();
|
|
217
|
-
return [3 /*break*/, 4];
|
|
218
|
-
case 2: return [4 /*yield*/, action.onSubmit(values)];
|
|
219
|
-
case 3:
|
|
220
|
-
res = _a.sent();
|
|
221
|
-
_a.label = 4;
|
|
222
|
-
case 4:
|
|
223
|
-
if (onClose)
|
|
224
|
-
onClose();
|
|
225
|
-
Swal.fire("Ok");
|
|
226
|
-
return [3 /*break*/, 6];
|
|
227
|
-
case 5:
|
|
228
|
-
e_1 = _a.sent();
|
|
229
|
-
if (onClose)
|
|
230
|
-
onClose();
|
|
231
|
-
err = e_1.request.response;
|
|
232
|
-
Swal.fire({
|
|
233
|
-
text: "Error: " + err,
|
|
234
|
-
});
|
|
235
|
-
return [3 /*break*/, 6];
|
|
236
|
-
case 6: return [2 /*return*/];
|
|
237
|
-
}
|
|
238
|
-
});
|
|
239
|
-
}); } }, { children: function (_a) {
|
|
240
|
-
var isSubmitting = _a.isSubmitting;
|
|
241
|
-
return (_jsxs(Form, { children: [formFields, _jsx(Button, __assign({ mt: 4, colorScheme: "teal", isLoading: isSubmitting, type: "submit" }, { children: "Submit" }))] }));
|
|
242
|
-
} })) }));
|
|
243
|
-
}
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
+
function step(op) {
|
|
26
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
+
while (_) try {
|
|
28
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
+
switch (op[0]) {
|
|
31
|
+
case 0: case 1: t = op; break;
|
|
32
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
+
default:
|
|
36
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
+
if (t[2]) _.ops.pop();
|
|
41
|
+
_.trys.pop(); continue;
|
|
42
|
+
}
|
|
43
|
+
op = body.call(thisArg, _);
|
|
44
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
49
|
+
import { Box, Button, FormControl, FormErrorMessage, FormLabel, Input, Select, } from "@chakra-ui/react";
|
|
50
|
+
import { Field, Form, Formik, FieldArray, getIn } from "formik";
|
|
51
|
+
import * as Yup from "yup";
|
|
52
|
+
import Swal from "sweetalert2";
|
|
53
|
+
import { useEffect, useState } from "react";
|
|
54
|
+
function parseValidation(fields) {
|
|
55
|
+
if (!fields) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
return fields.reduce(function (acc, field) {
|
|
59
|
+
if (field.type !== "dropdown" &&
|
|
60
|
+
field.type !== "object" &&
|
|
61
|
+
field.type !== "array") {
|
|
62
|
+
acc[field.name] = field.validationSchema;
|
|
63
|
+
}
|
|
64
|
+
else if (field.type === "array") {
|
|
65
|
+
if (field.element.type === "dropdown") {
|
|
66
|
+
acc[field.name] = Yup.array().of(Yup.string().required());
|
|
67
|
+
}
|
|
68
|
+
else if (field.element.type === "object") {
|
|
69
|
+
acc[field.name] = Yup.array().of(Yup.object(parseValidation(field.element.fields)));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
acc[field.name] = Yup.array().of(field.element.validationSchema);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else if (field.type === "object") {
|
|
76
|
+
acc[field.name] = Yup.object(parseValidation(field.fields));
|
|
77
|
+
}
|
|
78
|
+
if (field.type === "dropdown") {
|
|
79
|
+
acc[field.name] = Yup.string().required();
|
|
80
|
+
}
|
|
81
|
+
return acc;
|
|
82
|
+
}, {});
|
|
83
|
+
}
|
|
84
|
+
function generateSingleField(formfield, fieldName, key) {
|
|
85
|
+
return (_jsx(Field, __assign({ name: fieldName }, { children: function (_a) {
|
|
86
|
+
var _b;
|
|
87
|
+
var field = _a.field, form = _a.form;
|
|
88
|
+
return (_jsxs(FormControl, __assign({ isInvalid: getIn(form.touched, fieldName) && getIn(form.errors, fieldName) }, { children: [_jsx(FormLabel, __assign({ marginTop: 2 }, { children: formfield.label }), void 0), formfield.type === "string" || formfield.type === "number" ? (_jsx(Input, __assign({}, field, { type: formfield.type === "number" ? "number" : "text", placeholder: "" }), void 0)) : formfield.type === "file" ? (_jsx(Input, { type: "file", name: fieldName, onChange: function (event) {
|
|
89
|
+
if (event.currentTarget.files != null) {
|
|
90
|
+
form.setFieldValue(fieldName, event.currentTarget.files[0]);
|
|
91
|
+
}
|
|
92
|
+
} }, void 0)) : (_jsxs(Select, __assign({}, field, { defaultValue: "" }, { children: [_jsx("option", __assign({ disabled: true, value: "" }, { children: "Select an option!" }), void 0), (_b = formfield.options) === null || _b === void 0 ? void 0 : _b.map(function (option, key) { return (_jsx("option", __assign({ value: "".concat(option.value) }, { children: option.display }), key)); })] }), void 0)), _jsx(FormErrorMessage, { children: getIn(form.errors, fieldName) }, void 0)] }), void 0));
|
|
93
|
+
} }), key));
|
|
94
|
+
}
|
|
95
|
+
function parseFields(fields, previous) {
|
|
96
|
+
if (!fields) {
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
var computedFields = fields.map(function (formfield, key) {
|
|
100
|
+
var fieldName = previous !== undefined ? previous + "." + formfield.name : formfield.name;
|
|
101
|
+
if (formfield.type === "object") {
|
|
102
|
+
var objectFields = parseFields(formfield.fields, fieldName);
|
|
103
|
+
return _jsx(FormControl, { children: objectFields }, fieldName);
|
|
104
|
+
}
|
|
105
|
+
else if (formfield.type === "array") {
|
|
106
|
+
return (_jsx(FieldArray, __assign({ name: fieldName }, { children: function (_a) {
|
|
107
|
+
var remove = _a.remove, push = _a.push, form = _a.form;
|
|
108
|
+
return (_jsxs("div", { children: [_jsx(FormLabel, __assign({ marginTop: 2 }, { children: formfield.label }), void 0), getIn(form.values, fieldName).length > 0 &&
|
|
109
|
+
getIn(form.values, fieldName).map(function (element, index) { return (_jsxs(Box, __assign({ paddingLeft: 10 }, { children: [_jsx("h4", { children: _jsx("b", { children: "".concat(fieldName, ".").concat(index) }, void 0) }, void 0), _jsx(Box, __assign({ paddingLeft: 10 }, { children: formfield.element.type === "object"
|
|
110
|
+
? parseFields(formfield.element.fields, "".concat(fieldName, ".").concat(index))
|
|
111
|
+
: generateSingleField(formfield.element, "".concat(fieldName, ".").concat(index), key) }), void 0), _jsxs(Button, __assign({ type: "button", colorScheme: "red", marginY: 2, onClick: function () { return remove(index); } }, { children: ["Remove ", "".concat(fieldName, ".").concat(index)] }), void 0)] }), void 0)); }), _jsxs(Button, __assign({ type: "button", marginY: 2, colorScheme: "blue", onClick: function () {
|
|
112
|
+
var value = undefined;
|
|
113
|
+
if (formfield.element.type === "object") {
|
|
114
|
+
value = getInitialValues(formfield.element.fields);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
value = getInitialValues([formfield.element])[0];
|
|
118
|
+
}
|
|
119
|
+
push(value);
|
|
120
|
+
} }, { children: ["Add ", fieldName] }), void 0)] }, void 0));
|
|
121
|
+
} }), void 0));
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
return generateSingleField(formfield, fieldName, key);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
return computedFields;
|
|
128
|
+
}
|
|
129
|
+
function getInitialValues(fields) {
|
|
130
|
+
var obj = {};
|
|
131
|
+
if (!fields) {
|
|
132
|
+
return obj;
|
|
133
|
+
}
|
|
134
|
+
fields.forEach(function (field) {
|
|
135
|
+
if (field.type === "array")
|
|
136
|
+
obj[field.name] = [];
|
|
137
|
+
else if (field.type === "object")
|
|
138
|
+
obj[field.name] = getInitialValues(field.fields);
|
|
139
|
+
else if (field.type === "string")
|
|
140
|
+
obj[field.name] = "";
|
|
141
|
+
else if (field.type === "number")
|
|
142
|
+
obj[field.name] = 0;
|
|
143
|
+
else
|
|
144
|
+
obj[field.name] = null;
|
|
145
|
+
});
|
|
146
|
+
return obj;
|
|
147
|
+
}
|
|
148
|
+
function setOptions(fields) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
150
|
+
var promises;
|
|
151
|
+
var _this = this;
|
|
152
|
+
return __generator(this, function (_a) {
|
|
153
|
+
switch (_a.label) {
|
|
154
|
+
case 0:
|
|
155
|
+
if (!fields) {
|
|
156
|
+
return [2 /*return*/];
|
|
157
|
+
}
|
|
158
|
+
promises = fields.map(function (field) { return __awaiter(_this, void 0, void 0, function () {
|
|
159
|
+
var _a;
|
|
160
|
+
return __generator(this, function (_b) {
|
|
161
|
+
switch (_b.label) {
|
|
162
|
+
case 0:
|
|
163
|
+
if (!(field.type === "dropdown")) return [3 /*break*/, 2];
|
|
164
|
+
_a = field;
|
|
165
|
+
return [4 /*yield*/, field.getOptions()];
|
|
166
|
+
case 1:
|
|
167
|
+
_a.options = _b.sent();
|
|
168
|
+
return [3 /*break*/, 6];
|
|
169
|
+
case 2:
|
|
170
|
+
if (!(field.type === "array")) return [3 /*break*/, 4];
|
|
171
|
+
return [4 /*yield*/, setOptions([field.element])];
|
|
172
|
+
case 3:
|
|
173
|
+
_b.sent();
|
|
174
|
+
return [3 /*break*/, 6];
|
|
175
|
+
case 4:
|
|
176
|
+
if (!(field.type === "object")) return [3 /*break*/, 6];
|
|
177
|
+
return [4 /*yield*/, setOptions(field.fields)];
|
|
178
|
+
case 5:
|
|
179
|
+
_b.sent();
|
|
180
|
+
_b.label = 6;
|
|
181
|
+
case 6: return [2 /*return*/];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}); });
|
|
185
|
+
return [4 /*yield*/, Promise.all(promises)];
|
|
186
|
+
case 1:
|
|
187
|
+
_a.sent();
|
|
188
|
+
return [2 /*return*/];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
export function GenericForm(_a) {
|
|
194
|
+
var _this = this;
|
|
195
|
+
var action = _a.action, onClose = _a.onClose, rowId = _a.rowId;
|
|
196
|
+
var _b = useState(parseFields(action.fields)), formFields = _b[0], setFormFields = _b[1];
|
|
197
|
+
var initialValues = useState(getInitialValues(action.fields))[0];
|
|
198
|
+
var validationSchema = useState(parseValidation(action.fields))[0];
|
|
199
|
+
useEffect(function () {
|
|
200
|
+
setOptions(action.fields).then(function () {
|
|
201
|
+
setFormFields(parseFields(action.fields));
|
|
202
|
+
});
|
|
203
|
+
}, []);
|
|
204
|
+
if (!action.fields)
|
|
205
|
+
return _jsx(_Fragment, {}, void 0);
|
|
206
|
+
return (_jsx(_Fragment, { children: _jsx(Formik, __assign({ initialValues: initialValues, validationSchema: Yup.object(validationSchema), onSubmit: function (values) { return __awaiter(_this, void 0, void 0, function () {
|
|
207
|
+
var res, e_1, err;
|
|
208
|
+
return __generator(this, function (_a) {
|
|
209
|
+
switch (_a.label) {
|
|
210
|
+
case 0:
|
|
211
|
+
_a.trys.push([0, 5, , 6]);
|
|
212
|
+
res = void 0;
|
|
213
|
+
if (!rowId) return [3 /*break*/, 2];
|
|
214
|
+
return [4 /*yield*/, action.onSubmit(values, rowId)];
|
|
215
|
+
case 1:
|
|
216
|
+
res = _a.sent();
|
|
217
|
+
return [3 /*break*/, 4];
|
|
218
|
+
case 2: return [4 /*yield*/, action.onSubmit(values)];
|
|
219
|
+
case 3:
|
|
220
|
+
res = _a.sent();
|
|
221
|
+
_a.label = 4;
|
|
222
|
+
case 4:
|
|
223
|
+
if (onClose)
|
|
224
|
+
onClose();
|
|
225
|
+
Swal.fire("Ok");
|
|
226
|
+
return [3 /*break*/, 6];
|
|
227
|
+
case 5:
|
|
228
|
+
e_1 = _a.sent();
|
|
229
|
+
if (onClose)
|
|
230
|
+
onClose();
|
|
231
|
+
err = e_1.request.response;
|
|
232
|
+
Swal.fire({
|
|
233
|
+
text: "Error: " + err,
|
|
234
|
+
});
|
|
235
|
+
return [3 /*break*/, 6];
|
|
236
|
+
case 6: return [2 /*return*/];
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}); } }, { children: function (_a) {
|
|
240
|
+
var isSubmitting = _a.isSubmitting;
|
|
241
|
+
return (_jsxs(Form, { children: [formFields, _jsx(Button, __assign({ mt: 4, colorScheme: "teal", isLoading: isSubmitting, type: "submit" }, { children: "Submit" }), void 0)] }, void 0));
|
|
242
|
+
} }), void 0) }, void 0));
|
|
243
|
+
}
|