admin_lsac 1.2.7 → 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 CHANGED
@@ -5,8 +5,8 @@ var react_1 = require("@chakra-ui/react");
5
5
  var react_router_dom_1 = require("react-router-dom");
6
6
  var Page_1 = require("./components/Page");
7
7
  var Page_2 = require("./components/Page");
8
- function AdminPanel(_a) {
8
+ function App(_a) {
9
9
  var pages = _a.pages, basePath = _a.basePath;
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: "".concat(basePath, "/").concat((0, Page_2.pageToUrl)(page)), element: (0, jsx_runtime_1.jsx)(Page_1.AdminPage, { pages: pages, selectedPage: page, basePath: basePath }) })); }), pages[0] ? ((0, jsx_runtime_1.jsx)(react_router_dom_1.Route, { path: "".concat(basePath, "/*"), element: (0, jsx_runtime_1.jsx)(Page_1.AdminPage, { pages: pages, selectedPage: pages[0], basePath: basePath }) })) : null] }) }) }));
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: "".concat(basePath, "/").concat((0, Page_2.pageToUrl)(page)), element: (0, jsx_runtime_1.jsx)(Page_1.AdminPage, { pages: pages, selectedPage: page, basePath: basePath, apiURL: "http://localhost:3001" }) })); }), pages[0] ? ((0, jsx_runtime_1.jsx)(react_router_dom_1.Route, { path: "".concat(basePath, "/*"), element: (0, jsx_runtime_1.jsx)(Page_1.AdminPage, { pages: pages, selectedPage: pages[0], basePath: basePath, apiURL: "http://localhost:3001" }) })) : null] }) }) }));
11
11
  }
12
- exports.default = AdminPanel;
12
+ exports.default = App;
package/dist/cjs/App.js CHANGED
@@ -5,8 +5,8 @@ var react_1 = require("@chakra-ui/react");
5
5
  var react_router_dom_1 = require("react-router-dom");
6
6
  var Page_1 = require("./components/Page");
7
7
  var Page_2 = require("./components/Page");
8
- function AdminPanel(_a) {
8
+ function App(_a) {
9
9
  var pages = _a.pages, basePath = _a.basePath;
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: "".concat(basePath, "/").concat((0, Page_2.pageToUrl)(page)), element: (0, jsx_runtime_1.jsx)(Page_1.AdminPage, { pages: pages, selectedPage: page, basePath: basePath }) })); }), pages[0] ? ((0, jsx_runtime_1.jsx)(react_router_dom_1.Route, { path: "".concat(basePath, "/*"), element: (0, jsx_runtime_1.jsx)(Page_1.AdminPage, { pages: pages, selectedPage: pages[0], basePath: basePath }) })) : null] }) }) }));
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: "".concat(basePath, "/").concat((0, Page_2.pageToUrl)(page)), element: (0, jsx_runtime_1.jsx)(Page_1.AdminPage, { pages: pages, selectedPage: page, basePath: basePath, apiURL: "http://localhost:3001" }) })); }), pages[0] ? ((0, jsx_runtime_1.jsx)(react_router_dom_1.Route, { path: "".concat(basePath, "/*"), element: (0, jsx_runtime_1.jsx)(Page_1.AdminPage, { pages: pages, selectedPage: pages[0], basePath: basePath, apiURL: "http://localhost:3001" }) })) : null] }) }) }));
11
11
  }
12
- exports.default = AdminPanel;
12
+ exports.default = App;
@@ -140,10 +140,10 @@ function parseFields(fields, previous) {
140
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
141
  var value = undefined;
142
142
  if (formfield.element.type === "object") {
143
- value = getInitialValues(formfield.element.fields);
143
+ value = getCustomInitialValues(formfield.element.fields);
144
144
  }
145
145
  else {
146
- value = getInitialValues([formfield.element])[0];
146
+ value = getCustomInitialValues([formfield.element])[0];
147
147
  }
148
148
  push(value);
149
149
  }, children: ["Add ", fieldName] })] }));
@@ -155,7 +155,7 @@ function parseFields(fields, previous) {
155
155
  });
156
156
  return computedFields;
157
157
  }
158
- function getInitialValues(fields) {
158
+ function getCustomInitialValues(fields) {
159
159
  var obj = {};
160
160
  if (!fields) {
161
161
  return obj;
@@ -164,7 +164,7 @@ function getInitialValues(fields) {
164
164
  if (field.type === "array")
165
165
  obj[field.name] = [];
166
166
  else if (field.type === "object")
167
- obj[field.name] = getInitialValues(field.fields);
167
+ obj[field.name] = getCustomInitialValues(field.fields);
168
168
  else if (field.type === "string")
169
169
  obj[field.name] = "";
170
170
  else if (field.type === "number")
@@ -174,6 +174,25 @@ function getInitialValues(fields) {
174
174
  });
175
175
  return obj;
176
176
  }
177
+ function getInitialValues(action, rowId) {
178
+ return __awaiter(this, void 0, void 0, function () {
179
+ var obj, customInitialValues;
180
+ return __generator(this, function (_a) {
181
+ switch (_a.label) {
182
+ case 0:
183
+ obj = {};
184
+ if (!action.initialise) return [3 /*break*/, 2];
185
+ return [4 /*yield*/, action.initialise(rowId)];
186
+ case 1:
187
+ obj = _a.sent();
188
+ _a.label = 2;
189
+ case 2:
190
+ customInitialValues = getCustomInitialValues(action.fields);
191
+ return [2 /*return*/, Object.assign({}, customInitialValues, obj)];
192
+ }
193
+ });
194
+ });
195
+ }
177
196
  function setOptions(fields) {
178
197
  return __awaiter(this, void 0, void 0, function () {
179
198
  var promises;
@@ -223,16 +242,29 @@ function GenericForm(_a) {
223
242
  var _this = this;
224
243
  var action = _a.action, onClose = _a.onClose, rowId = _a.rowId;
225
244
  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];
245
+ var _c = (0, react_2.useState)(getCustomInitialValues(action.fields)), initialValues = _c[0], setInitialValues = _c[1];
246
+ var _d = (0, react_2.useState)(parseValidation(action.fields)), validationSchema = _d[0], setValidationSchema = _d[1];
228
247
  (0, react_2.useEffect)(function () {
229
248
  setOptions(action.fields).then(function () {
230
249
  setFormFields(parseFields(action.fields));
231
250
  });
232
251
  }, []);
252
+ var handleFormReset = function () { return __awaiter(_this, void 0, void 0, function () {
253
+ var newInitialValues;
254
+ return __generator(this, function (_a) {
255
+ switch (_a.label) {
256
+ case 0: return [4 /*yield*/, getInitialValues(action, rowId)];
257
+ case 1:
258
+ newInitialValues = _a.sent();
259
+ console.log(newInitialValues);
260
+ setInitialValues(newInitialValues);
261
+ return [2 /*return*/];
262
+ }
263
+ });
264
+ }); };
233
265
  if (!action.fields)
234
266
  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 () {
267
+ return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(formik_1.Formik, { enableReinitialize: true, initialValues: initialValues, validationSchema: Yup.object(validationSchema), onReset: handleFormReset, onSubmit: function (values) { return __awaiter(_this, void 0, void 0, function () {
236
268
  var res, e_1, err;
237
269
  return __generator(this, function (_a) {
238
270
  switch (_a.label) {
@@ -267,7 +299,7 @@ function GenericForm(_a) {
267
299
  });
268
300
  }); }, children: function (_a) {
269
301
  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" })] }));
302
+ 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" }), (0, jsx_runtime_1.jsx)(react_1.Button, { mt: 4, colorScheme: "teal", type: "reset", children: "Initialise Form" })] }));
271
303
  } }) }));
272
304
  }
273
305
  exports.GenericForm = GenericForm;
@@ -15,6 +15,8 @@ var material_react_table_1 = require("material-react-table");
15
15
  var export_to_csv_1 = require("export-to-csv");
16
16
  var sweetalert2_1 = __importDefault(require("sweetalert2"));
17
17
  var MenuItem_1 = __importDefault(require("@mui/material/MenuItem"));
18
+ var socket_io_client_1 = require("socket.io-client");
19
+ var websockets_1 = require("../utils/websockets");
18
20
  function pageToUrl(page) {
19
21
  return "".concat(page.name.toLowerCase().replace(" ", "_"));
20
22
  }
@@ -63,17 +65,31 @@ function exportToCSV(data, filename) {
63
65
  }
64
66
  function AdminPage(_a) {
65
67
  var _b;
66
- var pages = _a.pages, selectedPage = _a.selectedPage, basePath = _a.basePath;
67
- var _c = (0, react_3.useDisclosure)(), isOpen = _c.isOpen, onOpen = _c.onOpen, onClose = _c.onClose;
68
- var _d = (0, react_1.useState)({}), selectedAction = _d[0], setSelectedAction = _d[1];
69
- var _e = (0, react_1.useState)([]), data = _e[0], setData = _e[1];
70
- var _f = (0, react_1.useState)([]), displayData = _f[0], setDisplayData = _f[1];
71
- var _g = (0, react_1.useState)(), rowId = _g[0], setRowId = _g[1];
68
+ var pages = _a.pages, selectedPage = _a.selectedPage, basePath = _a.basePath, _c = _a.apiURL, apiURL = _c === void 0 ? '' : _c;
69
+ var _d = (0, react_3.useDisclosure)(), isOpen = _d.isOpen, onOpen = _d.onOpen, onClose = _d.onClose;
70
+ var _e = (0, react_1.useState)({}), selectedAction = _e[0], setSelectedAction = _e[1];
71
+ var _f = (0, react_1.useState)([]), data = _f[0], setData = _f[1];
72
+ var _g = (0, react_1.useState)([]), displayData = _g[0], setDisplayData = _g[1];
73
+ var _h = (0, react_1.useState)(), rowId = _h[0], setRowId = _h[1];
74
+ var _j = (0, react_1.useState)(false), refetch = _j[0], setRefetch = _j[1];
75
+ var _k = (0, react_1.useState)(socket_io_client_1.io), socket = _k[0], setSocket = _k[1];
76
+ (0, websockets_1.newSocket)(setSocket, apiURL);
72
77
  (0, react_1.useEffect)(function () {
73
78
  selectedPage.getRequest().then(function (resArray) {
74
79
  setData(resArray);
80
+ setRefetch(false);
75
81
  });
76
- }, []);
82
+ }, [refetch]); // Do not remove 'refetch' from here !!!
83
+ (0, react_1.useEffect)(function () {
84
+ socket.on("updateData", function (message) {
85
+ console.log("Message emitted from socket: " + message);
86
+ setRefetch(true);
87
+ var rowActionsMenu = document.querySelector(".MuiPaper-root.MuiPopover-paper.MuiMenu-paper");
88
+ if (rowActionsMenu) {
89
+ rowActionsMenu.remove();
90
+ }
91
+ });
92
+ }, [socket, selectedPage]);
77
93
  (0, react_1.useEffect)(function () {
78
94
  setDisplayData(data.map(function (table_data) {
79
95
  return selectedPage.tableFields.map(function (field) { return field.access(table_data); });
@@ -98,7 +114,6 @@ function AdminPage(_a) {
98
114
  } }) }), (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 () {
99
115
  selectedAction.onSubmit(rowId);
100
116
  onClose();
101
- window.location.reload();
102
117
  }, children: "Confirm Action" })), (0, jsx_runtime_1.jsx)(react_3.Button, { colorScheme: "red", mr: 3, onClick: onClose, children: "Close" })] })] })] })] }));
103
118
  }
104
119
  exports.AdminPage = AdminPage;
package/dist/cjs/index.js CHANGED
@@ -9,9 +9,9 @@ var client_1 = __importDefault(require("react-dom/client"));
9
9
  require("./index.css");
10
10
  var App_1 = __importDefault(require("./App"));
11
11
  var reportWebVitals_1 = __importDefault(require("./reportWebVitals"));
12
- var event_1 = require("./pages/event");
12
+ var schema_config_1 = require("./schema/schema.config");
13
13
  var root = client_1.default.createRoot(document.getElementById("root"));
14
- root.render((0, jsx_runtime_1.jsx)(react_1.default.StrictMode, { children: (0, jsx_runtime_1.jsx)(App_1.default, { pages: [event_1.event], basePath: "" }) }));
14
+ root.render((0, jsx_runtime_1.jsx)(react_1.default.StrictMode, { children: (0, jsx_runtime_1.jsx)(App_1.default, { pages: schema_config_1.pages, basePath: "" }) }));
15
15
  // If you want to start measuring performance in your app, pass a function
16
16
  // to log results (for example: reportWebVitals(console.log))
17
17
  // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
@@ -45,7 +45,31 @@ var domain = "http://localhost:3001";
45
45
  exports.event = {
46
46
  name: "Events",
47
47
  actions: [],
48
- rowActions: [],
48
+ rowActions: [
49
+ // {
50
+ // name: "Delete Event",
51
+ // fields: [
52
+ // {
53
+ // name: "input",
54
+ // label: "Mock Field",
55
+ // type: "string",
56
+ // validationSchema: "",
57
+ // },
58
+ // ],
59
+ // initialise: async (eventId: number) => {
60
+ // const res = await axios.get(domain + "/events/" + eventId);
61
+ // let obj: any = {};
62
+ // obj.input = res.data.name;
63
+ // return obj;
64
+ // },
65
+ // onSubmit: async (formValues: any, eventId: number) => {
66
+ // const res = await axios.delete(
67
+ // domain + "/events/" + eventId
68
+ // );
69
+ // return res.data;
70
+ // },
71
+ // }
72
+ ],
49
73
  getRequest: function () { return __awaiter(void 0, void 0, void 0, function () {
50
74
  return __generator(this, function (_a) {
51
75
  // const res = await axios.get(domain + "/events");
@@ -0,0 +1,22 @@
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.newSocket = void 0;
7
+ var socket_io_client_1 = __importDefault(require("socket.io-client"));
8
+ var react_1 = require("react");
9
+ var Socket = function (setSocket, apiURL) {
10
+ (0, react_1.useEffect)(function () {
11
+ var socketRef = (0, socket_io_client_1.default)(apiURL, {
12
+ transports: ["websocket"],
13
+ });
14
+ setSocket(socketRef);
15
+ return function () {
16
+ socketRef.removeAllListeners();
17
+ socketRef.disconnect();
18
+ };
19
+ }, []);
20
+ };
21
+ var newSocket = function (setSocket, apiURL) { return Socket(setSocket, apiURL); };
22
+ exports.newSocket = newSocket;
@@ -140,10 +140,10 @@ function parseFields(fields, previous) {
140
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
141
  var value = undefined;
142
142
  if (formfield.element.type === "object") {
143
- value = getInitialValues(formfield.element.fields);
143
+ value = getCustomInitialValues(formfield.element.fields);
144
144
  }
145
145
  else {
146
- value = getInitialValues([formfield.element])[0];
146
+ value = getCustomInitialValues([formfield.element])[0];
147
147
  }
148
148
  push(value);
149
149
  }, children: ["Add ", fieldName] })] }));
@@ -155,7 +155,7 @@ function parseFields(fields, previous) {
155
155
  });
156
156
  return computedFields;
157
157
  }
158
- function getInitialValues(fields) {
158
+ function getCustomInitialValues(fields) {
159
159
  var obj = {};
160
160
  if (!fields) {
161
161
  return obj;
@@ -164,7 +164,7 @@ function getInitialValues(fields) {
164
164
  if (field.type === "array")
165
165
  obj[field.name] = [];
166
166
  else if (field.type === "object")
167
- obj[field.name] = getInitialValues(field.fields);
167
+ obj[field.name] = getCustomInitialValues(field.fields);
168
168
  else if (field.type === "string")
169
169
  obj[field.name] = "";
170
170
  else if (field.type === "number")
@@ -174,6 +174,25 @@ function getInitialValues(fields) {
174
174
  });
175
175
  return obj;
176
176
  }
177
+ function getInitialValues(action, rowId) {
178
+ return __awaiter(this, void 0, void 0, function () {
179
+ var obj, customInitialValues;
180
+ return __generator(this, function (_a) {
181
+ switch (_a.label) {
182
+ case 0:
183
+ obj = {};
184
+ if (!action.initialise) return [3 /*break*/, 2];
185
+ return [4 /*yield*/, action.initialise(rowId)];
186
+ case 1:
187
+ obj = _a.sent();
188
+ _a.label = 2;
189
+ case 2:
190
+ customInitialValues = getCustomInitialValues(action.fields);
191
+ return [2 /*return*/, Object.assign({}, customInitialValues, obj)];
192
+ }
193
+ });
194
+ });
195
+ }
177
196
  function setOptions(fields) {
178
197
  return __awaiter(this, void 0, void 0, function () {
179
198
  var promises;
@@ -223,16 +242,29 @@ function GenericForm(_a) {
223
242
  var _this = this;
224
243
  var action = _a.action, onClose = _a.onClose, rowId = _a.rowId;
225
244
  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];
245
+ var _c = (0, react_2.useState)(getCustomInitialValues(action.fields)), initialValues = _c[0], setInitialValues = _c[1];
246
+ var _d = (0, react_2.useState)(parseValidation(action.fields)), validationSchema = _d[0], setValidationSchema = _d[1];
228
247
  (0, react_2.useEffect)(function () {
229
248
  setOptions(action.fields).then(function () {
230
249
  setFormFields(parseFields(action.fields));
231
250
  });
232
251
  }, []);
252
+ var handleFormReset = function () { return __awaiter(_this, void 0, void 0, function () {
253
+ var newInitialValues;
254
+ return __generator(this, function (_a) {
255
+ switch (_a.label) {
256
+ case 0: return [4 /*yield*/, getInitialValues(action, rowId)];
257
+ case 1:
258
+ newInitialValues = _a.sent();
259
+ console.log(newInitialValues);
260
+ setInitialValues(newInitialValues);
261
+ return [2 /*return*/];
262
+ }
263
+ });
264
+ }); };
233
265
  if (!action.fields)
234
266
  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 () {
267
+ return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(formik_1.Formik, { enableReinitialize: true, initialValues: initialValues, validationSchema: Yup.object(validationSchema), onReset: handleFormReset, onSubmit: function (values) { return __awaiter(_this, void 0, void 0, function () {
236
268
  var res, e_1, err;
237
269
  return __generator(this, function (_a) {
238
270
  switch (_a.label) {
@@ -267,7 +299,7 @@ function GenericForm(_a) {
267
299
  });
268
300
  }); }, children: function (_a) {
269
301
  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" })] }));
302
+ 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" }), (0, jsx_runtime_1.jsx)(react_1.Button, { mt: 4, colorScheme: "teal", type: "reset", children: "Initialise Form" })] }));
271
303
  } }) }));
272
304
  }
273
305
  exports.GenericForm = GenericForm;
@@ -15,6 +15,8 @@ var material_react_table_1 = require("material-react-table");
15
15
  var export_to_csv_1 = require("export-to-csv");
16
16
  var sweetalert2_1 = __importDefault(require("sweetalert2"));
17
17
  var MenuItem_1 = __importDefault(require("@mui/material/MenuItem"));
18
+ var socket_io_client_1 = require("socket.io-client");
19
+ var websockets_1 = require("../utils/websockets");
18
20
  function pageToUrl(page) {
19
21
  return "".concat(page.name.toLowerCase().replace(" ", "_"));
20
22
  }
@@ -63,17 +65,31 @@ function exportToCSV(data, filename) {
63
65
  }
64
66
  function AdminPage(_a) {
65
67
  var _b;
66
- var pages = _a.pages, selectedPage = _a.selectedPage, basePath = _a.basePath;
67
- var _c = (0, react_3.useDisclosure)(), isOpen = _c.isOpen, onOpen = _c.onOpen, onClose = _c.onClose;
68
- var _d = (0, react_1.useState)({}), selectedAction = _d[0], setSelectedAction = _d[1];
69
- var _e = (0, react_1.useState)([]), data = _e[0], setData = _e[1];
70
- var _f = (0, react_1.useState)([]), displayData = _f[0], setDisplayData = _f[1];
71
- var _g = (0, react_1.useState)(), rowId = _g[0], setRowId = _g[1];
68
+ var pages = _a.pages, selectedPage = _a.selectedPage, basePath = _a.basePath, _c = _a.apiURL, apiURL = _c === void 0 ? '' : _c;
69
+ var _d = (0, react_3.useDisclosure)(), isOpen = _d.isOpen, onOpen = _d.onOpen, onClose = _d.onClose;
70
+ var _e = (0, react_1.useState)({}), selectedAction = _e[0], setSelectedAction = _e[1];
71
+ var _f = (0, react_1.useState)([]), data = _f[0], setData = _f[1];
72
+ var _g = (0, react_1.useState)([]), displayData = _g[0], setDisplayData = _g[1];
73
+ var _h = (0, react_1.useState)(), rowId = _h[0], setRowId = _h[1];
74
+ var _j = (0, react_1.useState)(false), refetch = _j[0], setRefetch = _j[1];
75
+ var _k = (0, react_1.useState)(socket_io_client_1.io), socket = _k[0], setSocket = _k[1];
76
+ (0, websockets_1.newSocket)(setSocket, apiURL);
72
77
  (0, react_1.useEffect)(function () {
73
78
  selectedPage.getRequest().then(function (resArray) {
74
79
  setData(resArray);
80
+ setRefetch(false);
75
81
  });
76
- }, []);
82
+ }, [refetch]); // Do not remove 'refetch' from here !!!
83
+ (0, react_1.useEffect)(function () {
84
+ socket.on("updateData", function (message) {
85
+ console.log("Message emitted from socket: " + message);
86
+ setRefetch(true);
87
+ var rowActionsMenu = document.querySelector(".MuiPaper-root.MuiPopover-paper.MuiMenu-paper");
88
+ if (rowActionsMenu) {
89
+ rowActionsMenu.remove();
90
+ }
91
+ });
92
+ }, [socket, selectedPage]);
77
93
  (0, react_1.useEffect)(function () {
78
94
  setDisplayData(data.map(function (table_data) {
79
95
  return selectedPage.tableFields.map(function (field) { return field.access(table_data); });
@@ -98,7 +114,6 @@ function AdminPage(_a) {
98
114
  } }) }), (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 () {
99
115
  selectedAction.onSubmit(rowId);
100
116
  onClose();
101
- window.location.reload();
102
117
  }, children: "Confirm Action" })), (0, jsx_runtime_1.jsx)(react_3.Button, { colorScheme: "red", mr: 3, onClick: onClose, children: "Close" })] })] })] })] }));
103
118
  }
104
119
  exports.AdminPage = AdminPage;
package/dist/esm/App.js CHANGED
@@ -3,8 +3,8 @@ import { ChakraProvider } from "@chakra-ui/react";
3
3
  import { BrowserRouter, Route, Routes } from "react-router-dom";
4
4
  import { AdminPage } from "./components/Page";
5
5
  import { pageToUrl } from "./components/Page";
6
- function AdminPanel(_a) {
6
+ function App(_a) {
7
7
  var pages = _a.pages, basePath = _a.basePath;
8
- return (_jsx(ChakraProvider, { children: _jsx(BrowserRouter, { children: _jsxs(Routes, { children: [pages.map(function (page) { return (_jsx(Route, { path: "".concat(basePath, "/").concat(pageToUrl(page)), element: _jsx(AdminPage, { pages: pages, selectedPage: page, basePath: basePath }) })); }), pages[0] ? (_jsx(Route, { path: "".concat(basePath, "/*"), element: _jsx(AdminPage, { pages: pages, selectedPage: pages[0], basePath: basePath }) })) : null] }) }) }));
8
+ return (_jsx(ChakraProvider, { children: _jsx(BrowserRouter, { children: _jsxs(Routes, { children: [pages.map(function (page) { return (_jsx(Route, { path: "".concat(basePath, "/").concat(pageToUrl(page)), element: _jsx(AdminPage, { pages: pages, selectedPage: page, basePath: basePath, apiURL: "http://localhost:3001" }) })); }), pages[0] ? (_jsx(Route, { path: "".concat(basePath, "/*"), element: _jsx(AdminPage, { pages: pages, selectedPage: pages[0], basePath: basePath, apiURL: "http://localhost:3001" }) })) : null] }) }) }));
9
9
  }
10
- export default AdminPanel;
10
+ export default App;
@@ -111,10 +111,10 @@ function parseFields(fields, previous) {
111
111
  : generateSingleField(formfield.element, "".concat(fieldName, ".").concat(index), key) }), _jsxs(Button, { type: "button", colorScheme: "red", marginY: 2, onClick: function () { return remove(index); }, children: ["Remove ", "".concat(fieldName, ".").concat(index)] })] })); }), _jsxs(Button, { type: "button", marginY: 2, colorScheme: "blue", onClick: function () {
112
112
  var value = undefined;
113
113
  if (formfield.element.type === "object") {
114
- value = getInitialValues(formfield.element.fields);
114
+ value = getCustomInitialValues(formfield.element.fields);
115
115
  }
116
116
  else {
117
- value = getInitialValues([formfield.element])[0];
117
+ value = getCustomInitialValues([formfield.element])[0];
118
118
  }
119
119
  push(value);
120
120
  }, children: ["Add ", fieldName] })] }));
@@ -126,7 +126,7 @@ function parseFields(fields, previous) {
126
126
  });
127
127
  return computedFields;
128
128
  }
129
- function getInitialValues(fields) {
129
+ function getCustomInitialValues(fields) {
130
130
  var obj = {};
131
131
  if (!fields) {
132
132
  return obj;
@@ -135,7 +135,7 @@ function getInitialValues(fields) {
135
135
  if (field.type === "array")
136
136
  obj[field.name] = [];
137
137
  else if (field.type === "object")
138
- obj[field.name] = getInitialValues(field.fields);
138
+ obj[field.name] = getCustomInitialValues(field.fields);
139
139
  else if (field.type === "string")
140
140
  obj[field.name] = "";
141
141
  else if (field.type === "number")
@@ -145,6 +145,25 @@ function getInitialValues(fields) {
145
145
  });
146
146
  return obj;
147
147
  }
148
+ function getInitialValues(action, rowId) {
149
+ return __awaiter(this, void 0, void 0, function () {
150
+ var obj, customInitialValues;
151
+ return __generator(this, function (_a) {
152
+ switch (_a.label) {
153
+ case 0:
154
+ obj = {};
155
+ if (!action.initialise) return [3 /*break*/, 2];
156
+ return [4 /*yield*/, action.initialise(rowId)];
157
+ case 1:
158
+ obj = _a.sent();
159
+ _a.label = 2;
160
+ case 2:
161
+ customInitialValues = getCustomInitialValues(action.fields);
162
+ return [2 /*return*/, Object.assign({}, customInitialValues, obj)];
163
+ }
164
+ });
165
+ });
166
+ }
148
167
  function setOptions(fields) {
149
168
  return __awaiter(this, void 0, void 0, function () {
150
169
  var promises;
@@ -194,16 +213,29 @@ export function GenericForm(_a) {
194
213
  var _this = this;
195
214
  var action = _a.action, onClose = _a.onClose, rowId = _a.rowId;
196
215
  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];
216
+ var _c = useState(getCustomInitialValues(action.fields)), initialValues = _c[0], setInitialValues = _c[1];
217
+ var _d = useState(parseValidation(action.fields)), validationSchema = _d[0], setValidationSchema = _d[1];
199
218
  useEffect(function () {
200
219
  setOptions(action.fields).then(function () {
201
220
  setFormFields(parseFields(action.fields));
202
221
  });
203
222
  }, []);
223
+ var handleFormReset = function () { return __awaiter(_this, void 0, void 0, function () {
224
+ var newInitialValues;
225
+ return __generator(this, function (_a) {
226
+ switch (_a.label) {
227
+ case 0: return [4 /*yield*/, getInitialValues(action, rowId)];
228
+ case 1:
229
+ newInitialValues = _a.sent();
230
+ console.log(newInitialValues);
231
+ setInitialValues(newInitialValues);
232
+ return [2 /*return*/];
233
+ }
234
+ });
235
+ }); };
204
236
  if (!action.fields)
205
237
  return _jsx(_Fragment, {});
206
- return (_jsx(_Fragment, { children: _jsx(Formik, { initialValues: initialValues, validationSchema: Yup.object(validationSchema), onSubmit: function (values) { return __awaiter(_this, void 0, void 0, function () {
238
+ return (_jsx(_Fragment, { children: _jsx(Formik, { enableReinitialize: true, initialValues: initialValues, validationSchema: Yup.object(validationSchema), onReset: handleFormReset, onSubmit: function (values) { return __awaiter(_this, void 0, void 0, function () {
207
239
  var res, e_1, err;
208
240
  return __generator(this, function (_a) {
209
241
  switch (_a.label) {
@@ -238,6 +270,6 @@ export function GenericForm(_a) {
238
270
  });
239
271
  }); }, children: function (_a) {
240
272
  var isSubmitting = _a.isSubmitting;
241
- return (_jsxs(Form, { children: [formFields, _jsx(Button, { mt: 4, colorScheme: "teal", isLoading: isSubmitting, type: "submit", children: "Submit" })] }));
273
+ return (_jsxs(Form, { children: [formFields, _jsx(Button, { mt: 4, colorScheme: "teal", isLoading: isSubmitting, type: "submit", children: "Submit" }), _jsx(Button, { mt: 4, colorScheme: "teal", type: "reset", children: "Initialise Form" })] }));
242
274
  } }) }));
243
275
  }
@@ -9,6 +9,8 @@ import { MaterialReactTable } from "material-react-table";
9
9
  import { mkConfig, generateCsv, download } from "export-to-csv";
10
10
  import Swal from "sweetalert2";
11
11
  import MenuItem from "@mui/material/MenuItem";
12
+ import { io } from "socket.io-client";
13
+ import { newSocket } from "../utils/websockets";
12
14
  export function pageToUrl(page) {
13
15
  return "".concat(page.name.toLowerCase().replace(" ", "_"));
14
16
  }
@@ -56,17 +58,31 @@ function exportToCSV(data, filename) {
56
58
  }
57
59
  export function AdminPage(_a) {
58
60
  var _b;
59
- var pages = _a.pages, selectedPage = _a.selectedPage, basePath = _a.basePath;
60
- var _c = useDisclosure(), isOpen = _c.isOpen, onOpen = _c.onOpen, onClose = _c.onClose;
61
- var _d = useState({}), selectedAction = _d[0], setSelectedAction = _d[1];
62
- var _e = useState([]), data = _e[0], setData = _e[1];
63
- var _f = useState([]), displayData = _f[0], setDisplayData = _f[1];
64
- var _g = useState(), rowId = _g[0], setRowId = _g[1];
61
+ var pages = _a.pages, selectedPage = _a.selectedPage, basePath = _a.basePath, _c = _a.apiURL, apiURL = _c === void 0 ? '' : _c;
62
+ var _d = useDisclosure(), isOpen = _d.isOpen, onOpen = _d.onOpen, onClose = _d.onClose;
63
+ var _e = useState({}), selectedAction = _e[0], setSelectedAction = _e[1];
64
+ var _f = useState([]), data = _f[0], setData = _f[1];
65
+ var _g = useState([]), displayData = _g[0], setDisplayData = _g[1];
66
+ var _h = useState(), rowId = _h[0], setRowId = _h[1];
67
+ var _j = useState(false), refetch = _j[0], setRefetch = _j[1];
68
+ var _k = useState(io), socket = _k[0], setSocket = _k[1];
69
+ newSocket(setSocket, apiURL);
65
70
  useEffect(function () {
66
71
  selectedPage.getRequest().then(function (resArray) {
67
72
  setData(resArray);
73
+ setRefetch(false);
68
74
  });
69
- }, []);
75
+ }, [refetch]); // Do not remove 'refetch' from here !!!
76
+ useEffect(function () {
77
+ socket.on("updateData", function (message) {
78
+ console.log("Message emitted from socket: " + message);
79
+ setRefetch(true);
80
+ var rowActionsMenu = document.querySelector(".MuiPaper-root.MuiPopover-paper.MuiMenu-paper");
81
+ if (rowActionsMenu) {
82
+ rowActionsMenu.remove();
83
+ }
84
+ });
85
+ }, [socket, selectedPage]);
70
86
  useEffect(function () {
71
87
  setDisplayData(data.map(function (table_data) {
72
88
  return selectedPage.tableFields.map(function (field) { return field.access(table_data); });
@@ -91,6 +107,5 @@ export function AdminPage(_a) {
91
107
  } }) }), _jsxs(Modal, { size: "6xl", isOpen: isOpen, onClose: onClose, children: [_jsx(ModalOverlay, {}), _jsxs(ModalContent, { children: [_jsx(ModalHeader, { children: selectedAction.name }), _jsx(ModalCloseButton, {}), _jsxs(ModalBody, { children: [selectedAction.fields && ((_b = selectedAction.fields) === null || _b === void 0 ? void 0 : _b.length) > 0 ? (_jsx(GenericForm, { action: selectedAction, onClose: onClose, rowId: rowId })) : (_jsx(Button, { colorScheme: "blue", mr: 3, onClick: function () {
92
108
  selectedAction.onSubmit(rowId);
93
109
  onClose();
94
- window.location.reload();
95
110
  }, children: "Confirm Action" })), _jsx(Button, { colorScheme: "red", mr: 3, onClick: onClose, children: "Close" })] })] })] })] }));
96
111
  }
package/dist/esm/index.js CHANGED
@@ -2,11 +2,11 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import React from "react";
3
3
  import ReactDOM from "react-dom/client";
4
4
  import "./index.css";
5
- import AdminPanel from "./App";
5
+ import App from "./App";
6
6
  import reportWebVitals from "./reportWebVitals";
7
- import { event } from "./pages/event";
7
+ import { pages } from "./schema/schema.config";
8
8
  var root = ReactDOM.createRoot(document.getElementById("root"));
9
- root.render(_jsx(React.StrictMode, { children: _jsx(AdminPanel, { pages: [event], basePath: "" }) }));
9
+ root.render(_jsx(React.StrictMode, { children: _jsx(App, { pages: pages, basePath: "" }) }));
10
10
  // If you want to start measuring performance in your app, pass a function
11
11
  // to log results (for example: reportWebVitals(console.log))
12
12
  // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
@@ -39,7 +39,31 @@ var domain = "http://localhost:3001";
39
39
  export var event = {
40
40
  name: "Events",
41
41
  actions: [],
42
- rowActions: [],
42
+ rowActions: [
43
+ // {
44
+ // name: "Delete Event",
45
+ // fields: [
46
+ // {
47
+ // name: "input",
48
+ // label: "Mock Field",
49
+ // type: "string",
50
+ // validationSchema: "",
51
+ // },
52
+ // ],
53
+ // initialise: async (eventId: number) => {
54
+ // const res = await axios.get(domain + "/events/" + eventId);
55
+ // let obj: any = {};
56
+ // obj.input = res.data.name;
57
+ // return obj;
58
+ // },
59
+ // onSubmit: async (formValues: any, eventId: number) => {
60
+ // const res = await axios.delete(
61
+ // domain + "/events/" + eventId
62
+ // );
63
+ // return res.data;
64
+ // },
65
+ // }
66
+ ],
43
67
  getRequest: function () { return __awaiter(void 0, void 0, void 0, function () {
44
68
  return __generator(this, function (_a) {
45
69
  // const res = await axios.get(domain + "/events");