admin_lsac 1.2.8 → 1.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,223 +1,243 @@
1
- import { useEffect, useState } from "react";
2
- import { ChakraProvider } from "@chakra-ui/react";
3
- import { PageSchema, Action } from "../schema/schema";
4
- import {
5
- Button,
6
- Modal,
7
- ModalHeader,
8
- ModalBody,
9
- useDisclosure,
10
- ModalOverlay,
11
- ModalContent,
12
- ModalCloseButton,
13
- Box,
14
- Stack,
15
- } from "@chakra-ui/react";
16
- import { GenericForm } from "./GenericForm";
17
- import { ThemeProvider } from "@emotion/react";
18
- import { createTheme } from "@mui/material/styles";
19
- import { MaterialReactTable } from "material-react-table";
20
- import { mkConfig, generateCsv, download } from "export-to-csv";
21
- import Swal from "sweetalert2";
22
- import MenuItem from "@mui/material/MenuItem";
23
-
24
- export function pageToUrl(page: PageSchema): string {
25
- return `${page.name.toLowerCase().replace(" ", "_")}`;
26
- }
27
-
28
- function Header({
29
- pages,
30
- data,
31
- basePath,
32
- }: {
33
- pages: PageSchema[];
34
- data: any[];
35
- basePath: string;
36
- }) {
37
- return (
38
- <Box background="blue.50" padding={1}>
39
- <Stack
40
- margin={3}
41
- direction="row"
42
- justifyContent="space-between"
43
- alignItems="center"
44
- >
45
- <Stack margin={4} gap={10} direction="row" alignItems="center">
46
- {pages.map((p) => (
47
- <Box as="a" href={`${basePath}/${pageToUrl(p)}`}>
48
- {p.name}
49
- </Box>
50
- ))}
51
- </Stack>
52
- <Stack direction="row">
53
- <Button
54
- colorScheme="red"
55
- onClick={() => {
56
- const filename = prompt("Enter filename:");
57
- if (filename) exportToCSV(data, filename);
58
- }}
59
- >
60
- Export Data
61
- </Button>
62
- </Stack>
63
- </Stack>
64
- </Box>
65
- );
66
- }
67
-
68
- function exportToCSV(data: any[], filename: string) {
69
- const formatImageData = (obj: any) => {
70
- const formattedObj: any = {};
71
- for (const key in obj) {
72
- if (typeof obj[key] !== "object") {
73
- formattedObj[key] = obj[key];
74
- } else if (typeof obj[key] === "object" && obj[key] != null) {
75
- Object.keys(obj[key]).forEach((k: any) => {
76
- formattedObj[`${key}.${k}`] = obj[key][k];
77
- });
78
- }
79
- }
80
- return formattedObj;
81
- };
82
-
83
- const formattedData = data.map(formatImageData);
84
-
85
- const csvConfig = mkConfig({
86
- useKeysAsHeaders: true,
87
- filename: filename,
88
- });
89
-
90
- if (formattedData.length > 0) {
91
- const csv = generateCsv(csvConfig)(formattedData);
92
- download(csvConfig)(csv);
93
- } else {
94
- Swal.fire({
95
- title: "Error while exporting CSV!",
96
- text: "There is no data to export.",
97
- });
98
- }
99
- }
100
-
101
- export function AdminPage({
102
- pages,
103
- selectedPage,
104
- basePath,
105
- }: {
106
- pages: PageSchema[];
107
- selectedPage: PageSchema;
108
- basePath: string;
109
- }) {
110
- const { isOpen, onOpen, onClose } = useDisclosure();
111
- const [selectedAction, setSelectedAction] = useState({} as Action);
112
- const [data, setData] = useState<any[]>([]);
113
- const [displayData, setDisplayData] = useState<any[][]>([]);
114
- const [rowId, setRowId] = useState<number>();
115
-
116
- useEffect(() => {
117
- selectedPage.getRequest().then((resArray) => {
118
- setData(resArray);
119
- });
120
- }, []);
121
-
122
- useEffect(() => {
123
- setDisplayData(
124
- data.map((table_data) =>
125
- selectedPage.tableFields.map((field) => field.access(table_data))
126
- )
127
- );
128
- }, [data]);
129
-
130
- return (
131
- <>
132
- <Header pages={pages} data={data} basePath={basePath} />
133
-
134
- <ThemeProvider theme={createTheme()}>
135
- <MaterialReactTable
136
- columns={selectedPage.tableFields.map((field, index) => ({
137
- accessorFn: (row: any) => row[index],
138
- header: field.name,
139
- }))}
140
- data={displayData}
141
- muiTableContainerProps={{ sx: { maxHeight: "70vh" } }}
142
- enableColumnResizing
143
- enableColumnPinning
144
- enableGlobalFilterModes
145
- enablePagination={true}
146
- paginationDisplayMode="pages"
147
- enableRowVirtualization
148
- rowVirtualizerOptions={{ overscan: 20 }}
149
- renderTopToolbarCustomActions={() => (
150
- <ChakraProvider>
151
- <Stack margin={0} padding={3} direction="row">
152
- {selectedPage.actions.map((action) => (
153
- <Button
154
- colorScheme="blue"
155
- onClick={() => {
156
- setSelectedAction(action);
157
- onOpen();
158
- }}
159
- >
160
- {action.name}
161
- </Button>
162
- ))}
163
- </Stack>
164
- </ChakraProvider>
165
- )}
166
- enableRowActions
167
- renderRowActionMenuItems={(obj: any) => {
168
- return selectedPage.rowActions.map((rowAction) => (
169
- <MenuItem
170
- onClick={() => {
171
- setSelectedAction(rowAction);
172
- setRowId(obj.row.original[0]);
173
- onOpen();
174
-
175
- const rowActionsMenu = document.querySelector(
176
- ".MuiPaper-root.MuiPopover-paper.MuiMenu-paper"
177
- );
178
- if (rowActionsMenu) {
179
- rowActionsMenu.remove();
180
- }
181
- }}
182
- >
183
- {rowAction.name + " " + obj.row.original[0]}
184
- </MenuItem>
185
- ));
186
- }}
187
- />
188
- </ThemeProvider>
189
-
190
- <Modal size={"6xl"} isOpen={isOpen} onClose={onClose}>
191
- <ModalOverlay />
192
- <ModalContent>
193
- <ModalHeader>{selectedAction.name}</ModalHeader>
194
- <ModalCloseButton />
195
- <ModalBody>
196
- {selectedAction.fields && selectedAction.fields?.length > 0 ? (
197
- <GenericForm
198
- action={selectedAction}
199
- onClose={onClose}
200
- rowId={rowId}
201
- />
202
- ) : (
203
- <Button
204
- colorScheme="blue"
205
- mr={3}
206
- onClick={() => {
207
- selectedAction.onSubmit(rowId);
208
- onClose();
209
- window.location.reload();
210
- }}
211
- >
212
- Confirm Action
213
- </Button>
214
- )}
215
- <Button colorScheme="red" mr={3} onClick={onClose}>
216
- Close
217
- </Button>
218
- </ModalBody>
219
- </ModalContent>
220
- </Modal>
221
- </>
222
- );
223
- }
1
+ import { useEffect, useState } from "react";
2
+ import { ChakraProvider } from "@chakra-ui/react";
3
+ import { PageSchema, Action } from "../schema/schema";
4
+ import {
5
+ Button,
6
+ Modal,
7
+ ModalHeader,
8
+ ModalBody,
9
+ useDisclosure,
10
+ ModalOverlay,
11
+ ModalContent,
12
+ ModalCloseButton,
13
+ Box,
14
+ Stack,
15
+ } from "@chakra-ui/react";
16
+ import { GenericForm } from "./GenericForm";
17
+ import { ThemeProvider } from "@emotion/react";
18
+ import { createTheme } from "@mui/material/styles";
19
+ import { MaterialReactTable } from "material-react-table";
20
+ import { mkConfig, generateCsv, download } from "export-to-csv";
21
+ import Swal from "sweetalert2";
22
+ import MenuItem from "@mui/material/MenuItem";
23
+ import { io } from "socket.io-client";
24
+ import { newSocket } from "../utils/websockets";
25
+
26
+ export function pageToUrl(page: PageSchema): string {
27
+ return `${page.name.toLowerCase().replace(" ", "_")}`;
28
+ }
29
+
30
+ function Header({
31
+ pages,
32
+ data,
33
+ basePath,
34
+ }: {
35
+ pages: PageSchema[];
36
+ data: any[];
37
+ basePath: string;
38
+ }) {
39
+ return (
40
+ <Box background="blue.50" padding={1}>
41
+ <Stack
42
+ margin={3}
43
+ direction="row"
44
+ justifyContent="space-between"
45
+ alignItems="center"
46
+ >
47
+ <Stack margin={4} gap={10} direction="row" alignItems="center">
48
+ {pages.map((p) => (
49
+ <Box as="a" href={`${basePath}/${pageToUrl(p)}`}>
50
+ {p.name}
51
+ </Box>
52
+ ))}
53
+ </Stack>
54
+ <Stack direction="row">
55
+ <Button
56
+ colorScheme="red"
57
+ onClick={() => {
58
+ const filename = prompt("Enter filename:");
59
+ if (filename) exportToCSV(data, filename);
60
+ }}
61
+ >
62
+ Export Data
63
+ </Button>
64
+ </Stack>
65
+ </Stack>
66
+ </Box>
67
+ );
68
+ }
69
+
70
+ function exportToCSV(data: any[], filename: string) {
71
+ const formatImageData = (obj: any) => {
72
+ const formattedObj: any = {};
73
+ for (const key in obj) {
74
+ if (typeof obj[key] !== "object") {
75
+ formattedObj[key] = obj[key];
76
+ } else if (typeof obj[key] === "object" && obj[key] != null) {
77
+ Object.keys(obj[key]).forEach((k: any) => {
78
+ formattedObj[`${key}.${k}`] = obj[key][k];
79
+ });
80
+ }
81
+ }
82
+ return formattedObj;
83
+ };
84
+
85
+ const formattedData = data.map(formatImageData);
86
+
87
+ const csvConfig = mkConfig({
88
+ useKeysAsHeaders: true,
89
+ filename: filename,
90
+ });
91
+
92
+ if (formattedData.length > 0) {
93
+ const csv = generateCsv(csvConfig)(formattedData);
94
+ download(csvConfig)(csv);
95
+ } else {
96
+ Swal.fire({
97
+ title: "Error while exporting CSV!",
98
+ text: "There is no data to export.",
99
+ });
100
+ }
101
+ }
102
+
103
+ export function AdminPage({
104
+ pages,
105
+ selectedPage,
106
+ basePath,
107
+ apiURL=''
108
+ }: {
109
+ pages: PageSchema[];
110
+ selectedPage: PageSchema;
111
+ basePath: string;
112
+ apiURL?: string;
113
+ }) {
114
+ const { isOpen, onOpen, onClose } = useDisclosure();
115
+ const [selectedAction, setSelectedAction] = useState({} as Action);
116
+ const [data, setData] = useState<any[]>([]);
117
+ const [displayData, setDisplayData] = useState<any[][]>([]);
118
+ const [rowId, setRowId] = useState<number>();
119
+ const [refetch, setRefetch] = useState<boolean>(false);
120
+ const [socket, setSocket] = useState(io);
121
+ newSocket(setSocket, apiURL);
122
+
123
+ useEffect(() => {
124
+ selectedPage.getRequest().then((resArray) => {
125
+ setData(resArray);
126
+ setRefetch(false);
127
+ });
128
+ }, [refetch]); // Do not remove 'refetch' from here !!!
129
+
130
+ useEffect(() => {
131
+ socket.on("updateData", (message) => {
132
+ console.log("Message emitted from socket: " + message);
133
+ setRefetch(true);
134
+ const rowActionsMenu = document.querySelector(
135
+ ".MuiPaper-root.MuiPopover-paper.MuiMenu-paper"
136
+ );
137
+ if (rowActionsMenu) {
138
+ rowActionsMenu.remove();
139
+ }
140
+ });
141
+ }, [socket, selectedPage]);
142
+
143
+ useEffect(() => {
144
+ setDisplayData(
145
+ data.map((table_data) =>
146
+ selectedPage.tableFields.map((field) => field.access(table_data))
147
+ )
148
+ );
149
+ }, [data]);
150
+
151
+ return (
152
+ <>
153
+ <Header pages={pages} data={data} basePath={basePath} />
154
+
155
+ <ThemeProvider theme={createTheme()}>
156
+ <MaterialReactTable
157
+ columns={selectedPage.tableFields.map((field, index) => ({
158
+ accessorFn: (row: any) => row[index],
159
+ header: field.name,
160
+ }))}
161
+ data={displayData}
162
+ muiTableContainerProps={{ sx: { maxHeight: "70vh" } }}
163
+ enableColumnResizing
164
+ enableColumnPinning
165
+ enableGlobalFilterModes
166
+ enablePagination={true}
167
+ paginationDisplayMode="pages"
168
+ enableRowVirtualization
169
+ rowVirtualizerOptions={{ overscan: 20 }}
170
+ renderTopToolbarCustomActions={() => (
171
+ <ChakraProvider>
172
+ <Stack margin={0} padding={3} direction="row">
173
+ {selectedPage.actions.map((action) => (
174
+ <Button
175
+ colorScheme="blue"
176
+ onClick={() => {
177
+ setSelectedAction(action);
178
+ onOpen();
179
+ }}
180
+ >
181
+ {action.name}
182
+ </Button>
183
+ ))}
184
+ </Stack>
185
+ </ChakraProvider>
186
+ )}
187
+ enableRowActions
188
+ renderRowActionMenuItems={(obj: any) => {
189
+ return selectedPage.rowActions.map((rowAction) => (
190
+ <MenuItem
191
+ onClick={() => {
192
+ setSelectedAction(rowAction);
193
+ setRowId(obj.row.original[0]);
194
+ onOpen();
195
+
196
+ const rowActionsMenu = document.querySelector(
197
+ ".MuiPaper-root.MuiPopover-paper.MuiMenu-paper"
198
+ );
199
+ if (rowActionsMenu) {
200
+ rowActionsMenu.remove();
201
+ }
202
+ }}
203
+ >
204
+ {rowAction.name + " " + obj.row.original[0]}
205
+ </MenuItem>
206
+ ));
207
+ }}
208
+ />
209
+ </ThemeProvider>
210
+
211
+ <Modal size={"6xl"} isOpen={isOpen} onClose={onClose}>
212
+ <ModalOverlay />
213
+ <ModalContent>
214
+ <ModalHeader>{selectedAction.name}</ModalHeader>
215
+ <ModalCloseButton />
216
+ <ModalBody>
217
+ {selectedAction.fields && selectedAction.fields?.length > 0 ? (
218
+ <GenericForm
219
+ action={selectedAction}
220
+ onClose={onClose}
221
+ rowId={rowId}
222
+ />
223
+ ) : (
224
+ <Button
225
+ colorScheme="blue"
226
+ mr={3}
227
+ onClick={() => {
228
+ selectedAction.onSubmit(rowId);
229
+ onClose();
230
+ }}
231
+ >
232
+ Confirm Action
233
+ </Button>
234
+ )}
235
+ <Button colorScheme="red" mr={3} onClick={onClose}>
236
+ Close
237
+ </Button>
238
+ </ModalBody>
239
+ </ModalContent>
240
+ </Modal>
241
+ </>
242
+ );
243
+ }
package/src/index.tsx CHANGED
@@ -1,20 +1,20 @@
1
- import React from "react";
2
- import ReactDOM from "react-dom/client";
3
- import "./index.css";
4
- import AdminPanel from "./App";
5
- import reportWebVitals from "./reportWebVitals";
6
- import { event } from "./pages/event";
7
-
8
- const root = ReactDOM.createRoot(
9
- document.getElementById("root") as HTMLElement
10
- );
11
- root.render(
12
- <React.StrictMode>
13
- <AdminPanel pages={[event]} basePath="" />
14
- </React.StrictMode>
15
- );
16
-
17
- // If you want to start measuring performance in your app, pass a function
18
- // to log results (for example: reportWebVitals(console.log))
19
- // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
20
- reportWebVitals();
1
+ import React from "react";
2
+ import ReactDOM from "react-dom/client";
3
+ import "./index.css";
4
+ import App from "./App";
5
+ import reportWebVitals from "./reportWebVitals";
6
+ import { pages } from "./schema/schema.config";
7
+
8
+ const root = ReactDOM.createRoot(
9
+ document.getElementById("root") as HTMLElement
10
+ );
11
+ root.render(
12
+ <React.StrictMode>
13
+ <App pages={pages} basePath="" />
14
+ </React.StrictMode>
15
+ );
16
+
17
+ // If you want to start measuring performance in your app, pass a function
18
+ // to log results (for example: reportWebVitals(console.log))
19
+ // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
20
+ reportWebVitals();