admin_lsac 1.0.1 → 1.1.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 CHANGED
@@ -1,46 +1,127 @@
1
- # Getting Started with Create React App
1
+ # Generic Admin Panel
2
+
3
+ For configuring your own admin panel, follow the next instructions.
4
+
5
+ ### `npm install admin_lsac`
2
6
 
3
- This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4
-
5
- ## Available Scripts
6
-
7
- In the project directory, you can run:
8
-
9
- ### `npm start`
10
-
11
- Runs the app in the development mode.\
12
- Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13
-
14
- The page will reload if you make edits.\
15
- You will also see any lint errors in the console.
16
-
17
- ### `npm test`
18
-
19
- Launches the test runner in the interactive watch mode.\
20
- See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21
-
22
- ### `npm run build`
23
-
24
- Builds the app for production to the `build` folder.\
25
- It correctly bundles React in production mode and optimizes the build for the best performance.
26
-
27
- The build is minified and the filenames include the hashes.\
28
- Your app is ready to be deployed!
29
-
30
- See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31
-
32
- ### `npm run eject`
33
-
34
- **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35
-
36
- If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37
-
38
- Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39
-
40
- You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
7
+ This command will download and install the specified package and its dependencies into your project's node_modules directory, making it available for use in your project.
8
+
9
+ ### `npm install export-to-csv@1.2.1`
10
+
11
+ Export-to-csv package latest version is not working properly, so make sure you have the last functional version of this package install.
12
+
13
+ ### `import AdminPanel from 'admin_lsac'`
14
+
15
+ You can import this component in the root file of your project (i.e index.js) or in App.js. If your project is TypeScript-based, you need to write the following line as well:
16
+
17
+ ### `import PageSchema from 'admin_lsac'`
18
+
19
+ You need to create an array of schemas to pass it as props to the component.
20
+
21
+ ### `<AdminPanel pages={your_array} />`
22
+
23
+ ## What is a schema?
24
+
25
+ ```
26
+ export interface PageSchema {
27
+ name: string;
28
+ actions: Action[];
29
+ rowActions: RowAction[];
30
+ getRequest: () => Promise<any[]>;
31
+ tableFields: TableField[];
32
+ }
33
+
34
+ export interface TableField {
35
+ name: string;
36
+ access: (obj: any) => string | number | React.ReactElement;
37
+ }
38
+
39
+ export interface SimpleField {
40
+ name: string;
41
+ label: string;
42
+ type: "string" | "number" | "file";
43
+ validationSchema: any;
44
+ }
45
+
46
+ export interface Option {
47
+ value: string | number | boolean;
48
+ display: string;
49
+ }
50
+
51
+ export interface DropdownField {
52
+ name: string;
53
+ label: string;
54
+ type: "dropdown";
55
+ options?: Option[] | undefined;
56
+ getOptions: () => Promise<Option[]>;
57
+ }
58
+
59
+ export interface ObjectField {
60
+ name: string;
61
+ label: string;
62
+ type: "object";
63
+ fields: FormField[];
64
+ }
65
+
66
+ export interface ArrayField {
67
+ name: string;
68
+ label: string;
69
+ type: "array";
70
+ element: Exclude<FormField, ArrayField>;
71
+ }
72
+
73
+ export type FormField = SimpleField | DropdownField | ObjectField | ArrayField;
74
+
75
+ export interface Action {
76
+ name: string;
77
+ fields?: FormField[];
78
+ onSubmit: (obj1: any, obj2?: any) => Promise<any>;
79
+ }
80
+
81
+ export interface RowAction {
82
+ name: string;
83
+ fields?: FormField[];
84
+ initialise?: (obj: any) => any;
85
+ onSubmit: (obj1: any, obj2?: any) => Promise<any>;
86
+ }
87
+ ```
88
+
89
+ A page schema is based on these interfaces, which can be imported into your files. A page schema describes how one table of your admin panel would need to be structured to accomplish what the user needs regarding an entity. (i.e. users, teams, companies, quizzes)
90
+
91
+ #### In JavaScript, an array of schemas would be defined as follows:
92
+
93
+ ```
94
+ const your_array = [
95
+ {
96
+ // schema 1
97
+ },
98
+ {
99
+ // schema 2
100
+ },
101
+ ...
102
+ ];
103
+ ```
104
+
105
+ #### In TypeScript, an array of schemas would be defined as follows:
106
+
107
+ ```
108
+ const your_array: PageSchema[] = [
109
+ {
110
+ // schema 1
111
+ },
112
+ {
113
+ // schema 2
114
+ },
115
+ ...
116
+ ];
117
+ ```
118
+
119
+ ## Other Specifications
120
+
121
+ Due to some corrupt dependencies, you need a Node version >= 20.5.0 to use this package in a Vite app.
122
+
123
+ For every entity you describe through a page schema, getRequest and onSubmit functions will use routes created for the entity. `getRequest` function will make a GET request, while `onSumbit` will probably make a POST, PATCH or DELETE request.
41
124
 
42
125
  ## Learn More
43
126
 
44
- You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45
-
46
- To learn React, check out the [React documentation](https://reactjs.org/).
127
+ For more details, especially about page schemas, contact the owner. :)
package/dist/App.d.ts ADDED
@@ -0,0 +1,79 @@
1
+ import * as React from "react";
2
+
3
+
4
+ declare module "admin_lsac" {
5
+ export interface TableField {
6
+ name: string;
7
+ access: (obj: any) => string | number | React.ReactElement;
8
+ }
9
+
10
+ export interface SimpleField {
11
+ name: string;
12
+ label: string;
13
+ type: "string" | "number" | "file";
14
+ validationSchema: any;
15
+ }
16
+
17
+ export interface Option {
18
+ value: string | number | boolean;
19
+ display: string;
20
+ }
21
+
22
+ export interface DropdownField {
23
+ name: string;
24
+ label: string;
25
+ type: "dropdown";
26
+ options?: Option[] | undefined;
27
+ getOptions: () => Promise<Option[]>;
28
+ }
29
+
30
+ export interface ObjectField {
31
+ name: string;
32
+ label: string;
33
+ type: "object";
34
+ fields: FormField[];
35
+ }
36
+
37
+ export interface ArrayField {
38
+ name: string;
39
+ label: string;
40
+ type: "array";
41
+ element: Exclude<FormField, ArrayField>;
42
+ }
43
+
44
+ export type FormField = SimpleField | DropdownField | ObjectField | ArrayField;
45
+
46
+ export interface Action {
47
+ name: string;
48
+ fields?: FormField[];
49
+ onSubmit: (obj1: any, obj2?: any) => Promise<any>;
50
+ }
51
+
52
+ export interface RowAction {
53
+ name: string;
54
+ fields?: FormField[];
55
+ initialise?: (obj: any) => any;
56
+ onSubmit: (obj1: any, obj2?: any) => Promise<any>;
57
+ }
58
+
59
+ export interface PageSchema {
60
+ name: string;
61
+ actions: Action[];
62
+ rowActions: RowAction[];
63
+ getRequest: () => Promise<any[]>;
64
+ tableFields: TableField[];
65
+ }
66
+
67
+ interface AdminPanelProps {
68
+ pages: PageSchema[];
69
+ }
70
+
71
+ export function AdminPanel(props: AdminPanelProps): React.ReactElement;
72
+
73
+ interface PageProps {
74
+ pages: PageSchema[];
75
+ selectedPage: PageSchema;
76
+ }
77
+
78
+ export function Page(props: PageProps): React.ReactElement;
79
+ }
package/dist/App.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var jsx_runtime_1 = require("react/jsx-runtime");
4
+ var react_1 = require("@chakra-ui/react");
5
+ var react_router_dom_1 = require("react-router-dom");
6
+ var Page_1 = require("./components/Page");
7
+ var schema_1 = require("./schema/schema");
8
+ function AdminPanel(_a) {
9
+ var pages = _a.pages;
10
+ return ((0, jsx_runtime_1.jsx)(react_1.ChakraProvider, { children: (0, jsx_runtime_1.jsx)(react_router_dom_1.BrowserRouter, { children: (0, jsx_runtime_1.jsxs)(react_router_dom_1.Routes, { children: [pages.map(function (page) { return ((0, jsx_runtime_1.jsx)(react_router_dom_1.Route, { path: (0, schema_1.pageToUrl)(page), element: (0, jsx_runtime_1.jsx)(Page_1.Page, { pages: pages, selectedPage: page }) })); }), pages[0] ?
11
+ (0, jsx_runtime_1.jsx)(react_router_dom_1.Route, { path: "*", element: (0, jsx_runtime_1.jsx)(Page_1.Page, { pages: pages, selectedPage: pages[0] }) })
12
+ : null] }) }) }));
13
+ }
14
+ exports.default = AdminPanel;
@@ -0,0 +1,273 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
+ return new (P || (P = Promise))(function (resolve, reject) {
39
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
43
+ });
44
+ };
45
+ var __generator = (this && this.__generator) || function (thisArg, body) {
46
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
+ function verb(n) { return function (v) { return step([n, v]); }; }
49
+ function step(op) {
50
+ if (f) throw new TypeError("Generator is already executing.");
51
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
52
+ 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;
53
+ if (y = 0, t) op = [op[0] & 2, t.value];
54
+ switch (op[0]) {
55
+ case 0: case 1: t = op; break;
56
+ case 4: _.label++; return { value: op[1], done: false };
57
+ case 5: _.label++; y = op[1]; op = [0]; continue;
58
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
+ default:
60
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
+ if (t[2]) _.ops.pop();
65
+ _.trys.pop(); continue;
66
+ }
67
+ op = body.call(thisArg, _);
68
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
+ }
71
+ };
72
+ var __importDefault = (this && this.__importDefault) || function (mod) {
73
+ return (mod && mod.__esModule) ? mod : { "default": mod };
74
+ };
75
+ Object.defineProperty(exports, "__esModule", { value: true });
76
+ exports.GenericForm = void 0;
77
+ var jsx_runtime_1 = require("react/jsx-runtime");
78
+ var react_1 = require("@chakra-ui/react");
79
+ var formik_1 = require("formik");
80
+ var Yup = __importStar(require("yup"));
81
+ var sweetalert2_1 = __importDefault(require("sweetalert2"));
82
+ var react_2 = require("react");
83
+ function parseValidation(fields) {
84
+ if (!fields) {
85
+ return {};
86
+ }
87
+ return fields.reduce(function (acc, field) {
88
+ if (field.type !== "dropdown" &&
89
+ field.type !== "object" &&
90
+ field.type !== "array") {
91
+ acc[field.name] = field.validationSchema;
92
+ }
93
+ else if (field.type === "array") {
94
+ if (field.element.type === "dropdown") {
95
+ acc[field.name] = Yup.array().of(Yup.string().required());
96
+ }
97
+ else if (field.element.type === "object") {
98
+ acc[field.name] = Yup.array().of(Yup.object(parseValidation(field.element.fields)));
99
+ }
100
+ else {
101
+ acc[field.name] = Yup.array().of(field.element.validationSchema);
102
+ }
103
+ }
104
+ else if (field.type === "object") {
105
+ acc[field.name] = Yup.object(parseValidation(field.fields));
106
+ }
107
+ if (field.type === "dropdown") {
108
+ acc[field.name] = Yup.string().required();
109
+ }
110
+ return acc;
111
+ }, {});
112
+ }
113
+ function generateSingleField(formfield, fieldName, key) {
114
+ return ((0, jsx_runtime_1.jsx)(formik_1.Field, { name: fieldName, children: function (_a) {
115
+ var _b;
116
+ var field = _a.field, form = _a.form;
117
+ return ((0, jsx_runtime_1.jsxs)(react_1.FormControl, { isInvalid: (0, formik_1.getIn)(form.touched, fieldName) && (0, formik_1.getIn)(form.errors, fieldName), children: [(0, jsx_runtime_1.jsx)(react_1.FormLabel, { marginTop: 2, children: formfield.label }), formfield.type === "string" || formfield.type === "number" ? ((0, jsx_runtime_1.jsx)(react_1.Input, __assign({}, field, { type: formfield.type === "number" ? "number" : "text", placeholder: "" }))) : formfield.type === "file" ? ((0, jsx_runtime_1.jsx)(react_1.Input, { type: "file", name: fieldName, onChange: function (event) {
118
+ if (event.currentTarget.files != null) {
119
+ form.setFieldValue(fieldName, event.currentTarget.files[0]);
120
+ }
121
+ } })) : ((0, jsx_runtime_1.jsxs)(react_1.Select, __assign({}, field, { defaultValue: "", children: [(0, jsx_runtime_1.jsx)("option", { disabled: true, value: "", children: "Select an option!" }), (_b = formfield.options) === null || _b === void 0 ? void 0 : _b.map(function (option, key) { return ((0, jsx_runtime_1.jsx)("option", { value: "".concat(option.value), children: option.display }, key)); })] }))), (0, jsx_runtime_1.jsx)(react_1.FormErrorMessage, { children: (0, formik_1.getIn)(form.errors, fieldName) })] }));
122
+ } }, key));
123
+ }
124
+ function parseFields(fields, previous) {
125
+ if (!fields) {
126
+ return [];
127
+ }
128
+ var computedFields = fields.map(function (formfield, key) {
129
+ var fieldName = previous !== undefined ? previous + "." + formfield.name : formfield.name;
130
+ if (formfield.type === "object") {
131
+ var objectFields = parseFields(formfield.fields, fieldName);
132
+ return (0, jsx_runtime_1.jsx)(react_1.FormControl, { children: objectFields }, fieldName);
133
+ }
134
+ else if (formfield.type === "array") {
135
+ return ((0, jsx_runtime_1.jsx)(formik_1.FieldArray, { name: fieldName, children: function (_a) {
136
+ var remove = _a.remove, push = _a.push, form = _a.form;
137
+ return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(react_1.FormLabel, { marginTop: 2, children: formfield.label }), (0, formik_1.getIn)(form.values, fieldName).length > 0 &&
138
+ (0, formik_1.getIn)(form.values, fieldName).map(function (element, index) { return ((0, jsx_runtime_1.jsxs)(react_1.Box, { paddingLeft: 10, children: [(0, jsx_runtime_1.jsx)("h4", { children: (0, jsx_runtime_1.jsx)("b", { children: "".concat(fieldName, ".").concat(index) }) }), (0, jsx_runtime_1.jsx)(react_1.Box, { paddingLeft: 10, children: formfield.element.type === "object"
139
+ ? parseFields(formfield.element.fields, "".concat(fieldName, ".").concat(index))
140
+ : generateSingleField(formfield.element, "".concat(fieldName, ".").concat(index), key) }), (0, jsx_runtime_1.jsxs)(react_1.Button, { type: "button", colorScheme: "red", marginY: 2, onClick: function () { return remove(index); }, children: ["Remove ", "".concat(fieldName, ".").concat(index)] })] })); }), (0, jsx_runtime_1.jsxs)(react_1.Button, { type: "button", marginY: 2, colorScheme: "blue", onClick: function () {
141
+ var value = undefined;
142
+ if (formfield.element.type === "object") {
143
+ value = getInitialValues(formfield.element.fields);
144
+ }
145
+ else {
146
+ value = getInitialValues([formfield.element])[0];
147
+ }
148
+ push(value);
149
+ }, children: ["Add ", fieldName] })] }));
150
+ } }));
151
+ }
152
+ else {
153
+ return generateSingleField(formfield, fieldName, key);
154
+ }
155
+ });
156
+ return computedFields;
157
+ }
158
+ function getInitialValues(fields) {
159
+ var obj = {};
160
+ if (!fields) {
161
+ return obj;
162
+ }
163
+ fields.forEach(function (field) {
164
+ if (field.type === "array")
165
+ obj[field.name] = [];
166
+ else if (field.type === "object")
167
+ obj[field.name] = getInitialValues(field.fields);
168
+ else if (field.type === "string")
169
+ obj[field.name] = "";
170
+ else if (field.type === "number")
171
+ obj[field.name] = 0;
172
+ else
173
+ obj[field.name] = null;
174
+ });
175
+ return obj;
176
+ }
177
+ function setOptions(fields) {
178
+ return __awaiter(this, void 0, void 0, function () {
179
+ var promises;
180
+ var _this = this;
181
+ return __generator(this, function (_a) {
182
+ switch (_a.label) {
183
+ case 0:
184
+ if (!fields) {
185
+ return [2 /*return*/];
186
+ }
187
+ promises = fields.map(function (field) { return __awaiter(_this, void 0, void 0, function () {
188
+ var _a;
189
+ return __generator(this, function (_b) {
190
+ switch (_b.label) {
191
+ case 0:
192
+ if (!(field.type === "dropdown")) return [3 /*break*/, 2];
193
+ _a = field;
194
+ return [4 /*yield*/, field.getOptions()];
195
+ case 1:
196
+ _a.options = _b.sent();
197
+ return [3 /*break*/, 6];
198
+ case 2:
199
+ if (!(field.type === "array")) return [3 /*break*/, 4];
200
+ return [4 /*yield*/, setOptions([field.element])];
201
+ case 3:
202
+ _b.sent();
203
+ return [3 /*break*/, 6];
204
+ case 4:
205
+ if (!(field.type === "object")) return [3 /*break*/, 6];
206
+ return [4 /*yield*/, setOptions(field.fields)];
207
+ case 5:
208
+ _b.sent();
209
+ _b.label = 6;
210
+ case 6: return [2 /*return*/];
211
+ }
212
+ });
213
+ }); });
214
+ return [4 /*yield*/, Promise.all(promises)];
215
+ case 1:
216
+ _a.sent();
217
+ return [2 /*return*/];
218
+ }
219
+ });
220
+ });
221
+ }
222
+ function GenericForm(_a) {
223
+ var _this = this;
224
+ var action = _a.action, onClose = _a.onClose, rowId = _a.rowId;
225
+ var _b = (0, react_2.useState)(parseFields(action.fields)), formFields = _b[0], setFormFields = _b[1];
226
+ var initialValues = (0, react_2.useState)(getInitialValues(action.fields))[0];
227
+ var validationSchema = (0, react_2.useState)(parseValidation(action.fields))[0];
228
+ (0, react_2.useEffect)(function () {
229
+ setOptions(action.fields).then(function () {
230
+ setFormFields(parseFields(action.fields));
231
+ });
232
+ }, []);
233
+ if (!action.fields)
234
+ return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {});
235
+ return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(formik_1.Formik, { initialValues: initialValues, validationSchema: Yup.object(validationSchema), onSubmit: function (values) { return __awaiter(_this, void 0, void 0, function () {
236
+ var res, e_1, err;
237
+ return __generator(this, function (_a) {
238
+ switch (_a.label) {
239
+ case 0:
240
+ _a.trys.push([0, 5, , 6]);
241
+ res = void 0;
242
+ if (!rowId) return [3 /*break*/, 2];
243
+ return [4 /*yield*/, action.onSubmit(values, rowId)];
244
+ case 1:
245
+ res = _a.sent();
246
+ return [3 /*break*/, 4];
247
+ case 2: return [4 /*yield*/, action.onSubmit(values)];
248
+ case 3:
249
+ res = _a.sent();
250
+ _a.label = 4;
251
+ case 4:
252
+ if (onClose)
253
+ onClose();
254
+ sweetalert2_1.default.fire("Ok");
255
+ return [3 /*break*/, 6];
256
+ case 5:
257
+ e_1 = _a.sent();
258
+ if (onClose)
259
+ onClose();
260
+ err = e_1.request.response;
261
+ sweetalert2_1.default.fire({
262
+ text: "Error: " + err,
263
+ });
264
+ return [3 /*break*/, 6];
265
+ case 6: return [2 /*return*/];
266
+ }
267
+ });
268
+ }); }, children: function (_a) {
269
+ var isSubmitting = _a.isSubmitting;
270
+ return ((0, jsx_runtime_1.jsxs)(formik_1.Form, { children: [formFields, (0, jsx_runtime_1.jsx)(react_1.Button, { mt: 4, colorScheme: "teal", isLoading: isSubmitting, type: "submit", children: "Submit" })] }));
271
+ } }) }));
272
+ }
273
+ exports.GenericForm = GenericForm;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Page = void 0;
7
+ var jsx_runtime_1 = require("react/jsx-runtime");
8
+ var react_1 = require("react");
9
+ var react_2 = require("@chakra-ui/react");
10
+ var schema_1 = require("../schema/schema");
11
+ var react_3 = require("@chakra-ui/react");
12
+ var GenericForm_1 = require("./GenericForm");
13
+ var react_router_1 = require("react-router");
14
+ var react_4 = require("@emotion/react");
15
+ var styles_1 = require("@mui/material/styles");
16
+ var material_react_table_1 = require("material-react-table");
17
+ var export_to_csv_1 = require("export-to-csv");
18
+ var sweetalert2_1 = __importDefault(require("sweetalert2"));
19
+ var MenuItem_1 = __importDefault(require("@mui/material/MenuItem"));
20
+ function Header(_a) {
21
+ var pages = _a.pages, data = _a.data;
22
+ var nav = (0, react_router_1.useNavigate)();
23
+ return ((0, jsx_runtime_1.jsx)(react_3.Box, { background: "blue.50", padding: 1, children: (0, jsx_runtime_1.jsxs)(react_3.Stack, { margin: 3, direction: "row", justifyContent: "space-between", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(react_3.Stack, { margin: 4, gap: 10, direction: "row", alignItems: "center", children: pages.map(function (p) { return ((0, jsx_runtime_1.jsx)(react_3.Box, { as: "a", href: (0, schema_1.pageToUrl)(p), children: p.name })); }) }), (0, jsx_runtime_1.jsx)(react_3.Stack, { direction: "row", children: (0, jsx_runtime_1.jsx)(react_3.Button, { colorScheme: "red", onClick: function () {
24
+ var filename = prompt("Enter filename:");
25
+ if (filename)
26
+ exportToCSV(data, filename);
27
+ }, children: "Export Data" }) })] }) }));
28
+ }
29
+ function exportToCSV(data, filename) {
30
+ var formatImageData = function (obj) {
31
+ var formattedObj = {};
32
+ for (var key in obj) {
33
+ if (typeof obj[key] !== "object") {
34
+ formattedObj[key] = obj[key];
35
+ }
36
+ }
37
+ return formattedObj;
38
+ };
39
+ var formattedData = data.map(formatImageData);
40
+ var csvConfig = (0, export_to_csv_1.mkConfig)({
41
+ useKeysAsHeaders: true,
42
+ filename: filename,
43
+ });
44
+ if (formattedData.length > 0) {
45
+ var csv = (0, export_to_csv_1.generateCsv)(csvConfig)(formattedData);
46
+ (0, export_to_csv_1.download)(csvConfig)(csv);
47
+ }
48
+ else {
49
+ sweetalert2_1.default.fire({
50
+ title: "Error while exporting CSV!",
51
+ text: "There is no data to export.",
52
+ });
53
+ }
54
+ }
55
+ function Page(_a) {
56
+ var _b;
57
+ var pages = _a.pages, selectedPage = _a.selectedPage;
58
+ var _c = (0, react_3.useDisclosure)(), isOpen = _c.isOpen, onOpen = _c.onOpen, onClose = _c.onClose;
59
+ var _d = (0, react_1.useState)({}), selectedAction = _d[0], setSelectedAction = _d[1];
60
+ var _e = (0, react_1.useState)([]), data = _e[0], setData = _e[1];
61
+ var _f = (0, react_1.useState)([]), displayData = _f[0], setDisplayData = _f[1];
62
+ var _g = (0, react_1.useState)(), rowId = _g[0], setRowId = _g[1];
63
+ (0, react_1.useEffect)(function () {
64
+ selectedPage.getRequest().then(function (resArray) {
65
+ setData(resArray);
66
+ });
67
+ }, []);
68
+ (0, react_1.useEffect)(function () {
69
+ setDisplayData(data.map(function (table_data) {
70
+ return selectedPage.tableFields.map(function (field) { return field.access(table_data); });
71
+ }));
72
+ }, [data]);
73
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Header, { pages: pages, data: data }), (0, jsx_runtime_1.jsx)(react_4.ThemeProvider, { theme: (0, styles_1.createTheme)(), children: (0, jsx_runtime_1.jsx)(material_react_table_1.MaterialReactTable, { columns: selectedPage.tableFields.map(function (field, index) { return ({
74
+ accessorFn: function (row) { return row[index]; },
75
+ header: field.name,
76
+ }); }), data: displayData, muiTableContainerProps: { sx: { maxHeight: "70vh" } }, enableColumnResizing: true, enableColumnPinning: true, enableGlobalFilterModes: true, enablePagination: true, paginationDisplayMode: "pages", enableRowVirtualization: true, rowVirtualizerOptions: { overscan: 20 }, renderTopToolbarCustomActions: function () { return ((0, jsx_runtime_1.jsx)(react_2.ChakraProvider, { children: (0, jsx_runtime_1.jsx)(react_3.Stack, { margin: 0, padding: 3, direction: "row", children: selectedPage.actions.map(function (action) { return ((0, jsx_runtime_1.jsx)(react_3.Button, { colorScheme: "blue", onClick: function () {
77
+ setSelectedAction(action);
78
+ onOpen();
79
+ }, children: action.name })); }) }) })); }, enableRowActions: true, renderRowActionMenuItems: function (obj) {
80
+ return selectedPage.rowActions.map(function (rowAction) { return ((0, jsx_runtime_1.jsx)(MenuItem_1.default, { onClick: function () {
81
+ setSelectedAction(rowAction);
82
+ setRowId(obj.row.original[0]);
83
+ onOpen();
84
+ var rowActionsMenu = document.querySelector(".MuiPaper-root.MuiPopover-paper.MuiMenu-paper");
85
+ if (rowActionsMenu) {
86
+ rowActionsMenu.remove();
87
+ }
88
+ }, children: rowAction.name + " " + obj.row.original[0] })); });
89
+ } }) }), (0, jsx_runtime_1.jsxs)(react_3.Modal, { size: "6xl", isOpen: isOpen, onClose: onClose, children: [(0, jsx_runtime_1.jsx)(react_3.ModalOverlay, {}), (0, jsx_runtime_1.jsxs)(react_3.ModalContent, { children: [(0, jsx_runtime_1.jsx)(react_3.ModalHeader, { children: selectedAction.name }), (0, jsx_runtime_1.jsx)(react_3.ModalCloseButton, {}), (0, jsx_runtime_1.jsxs)(react_3.ModalBody, { children: [selectedAction.fields && ((_b = selectedAction.fields) === null || _b === void 0 ? void 0 : _b.length) > 0 ? ((0, jsx_runtime_1.jsx)(GenericForm_1.GenericForm, { action: selectedAction, onClose: onClose, rowId: rowId })) : ((0, jsx_runtime_1.jsx)(react_3.Button, { colorScheme: "blue", mr: 3, onClick: function () {
90
+ selectedAction.onSubmit(rowId);
91
+ onClose();
92
+ window.location.reload();
93
+ }, children: "Confirm Action" })), (0, jsx_runtime_1.jsx)(react_3.Button, { colorScheme: "red", mr: 3, onClick: onClose, children: "Close" })] })] })] })] }));
94
+ }
95
+ exports.Page = Page;
@@ -0,0 +1,12 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { ChakraProvider } from "@chakra-ui/react";
3
+ import { BrowserRouter, Route, Routes } from "react-router-dom";
4
+ import { Page } from "./components/Page";
5
+ import { pageToUrl } from "./schema/schema";
6
+ function AdminPanel(_a) {
7
+ var pages = _a.pages;
8
+ return (_jsx(ChakraProvider, { children: _jsx(BrowserRouter, { children: _jsxs(Routes, { children: [pages.map(function (page) { return (_jsx(Route, { path: pageToUrl(page), element: _jsx(Page, { pages: pages, selectedPage: page }) })); }), pages[0] ?
9
+ _jsx(Route, { path: "*", element: _jsx(Page, { pages: pages, selectedPage: pages[0] }) })
10
+ : null] }) }) }));
11
+ }
12
+ export default AdminPanel;