admin_lsac 2.0.21 → 2.0.23

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
@@ -33,7 +33,7 @@ Import the component from above where you want to write your schemas! (See below
33
33
  <AdminPage
34
34
  pages={your_array}
35
35
  selectedPage={page} // 'page' param from above
36
- basePath={your_url_to_admin_panel}
36
+ basePath={your_url_to_admin_panel} // use '', NOT '/' if at root
37
37
  apiURL={your_url_to_backend} // optional, for websockets
38
38
  />
39
39
  }
@@ -46,7 +46,7 @@ Import the component from above where you want to write your schemas! (See below
46
46
  <AdminPage
47
47
  pages={your_array}
48
48
  selectedPage={your_array[0]}
49
- basePath={your_url_to_admin_panel}
49
+ basePath={your_url_to_admin_panel} // use '', NOT '/' if at root
50
50
  apiURL={your_url_to_backend} // optional, for websockets
51
51
  />
52
52
  }
@@ -1,7 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useEffect, useState, useRef } from 'react';
3
- import { ChakraProvider } from '@chakra-ui/react';
4
- import { Button, Modal, ModalHeader, ModalBody, useDisclosure, ModalOverlay, ModalContent, ModalCloseButton, Box, Stack, } from '@chakra-ui/react';
2
+ import { useEffect, useState } from 'react';
3
+ import { Button, Modal, ModalHeader, ModalBody, ModalOverlay, ModalContent, ModalCloseButton, useDisclosure, Box, Stack, ChakraProvider, } from '@chakra-ui/react';
5
4
  import { GenericForm } from './GenericForm';
6
5
  import { ThemeProvider } from '@emotion/react';
7
6
  import { createTheme } from '@mui/material/styles';
@@ -66,28 +65,21 @@ export function AdminPage({ pages, selectedPage, basePath, apiURL = '', }) {
66
65
  });
67
66
  }, [socket, selectedPage]);
68
67
  useEffect(() => {
69
- setDisplayData(data.map((table_data) => selectedPage.tableFields.map((field) => field.access(table_data))));
68
+ setDisplayData(data.map((row) => selectedPage.tableFields.map((field) => field.access(row))));
70
69
  }, [data]);
71
- const headerRef = useRef(null);
72
- const [tableHeight, setTableHeight] = useState('80vh');
73
- useEffect(() => {
74
- const updateTableHeight = () => {
75
- if (headerRef.current) {
76
- const headerHeight = headerRef.current.clientHeight;
77
- setTableHeight(`calc(88vh - ${headerHeight}px)`);
78
- }
79
- };
80
- updateTableHeight();
81
- }, []);
82
- return (_jsxs(ChakraProvider, { children: [_jsx(Box, { ref: headerRef, children: _jsx(Header, { pages: pages, data: [selectedPage.tableFields.map((field) => field.name), ...displayData], basePath: basePath }) }), _jsx(ThemeProvider, { theme: createTheme(), children: _jsx(MaterialReactTable, { columns: selectedPage.tableFields.map((field, index) => ({
70
+ return (_jsxs(ChakraProvider, { children: [_jsx(Box, { children: _jsx(Header, { pages: pages, data: [selectedPage.tableFields.map((field) => field.name), ...displayData], basePath: basePath }) }), _jsx(ThemeProvider, { theme: createTheme(), children: _jsx(MaterialReactTable, { columns: selectedPage.tableFields.map((field, colIndex) => ({
83
71
  accessorFn: (row) => {
84
- if (typeof row[index] === 'boolean') {
85
- row[index] = String(row[index]);
86
- }
87
- return row[index];
72
+ const value = row[colIndex];
73
+ return typeof value === 'boolean' ? String(value) : value;
88
74
  },
89
75
  header: field.name,
90
- })), data: displayData, muiTableContainerProps: { sx: { height: tableHeight } }, enableColumnResizing: true, enableColumnPinning: true, enableGlobalFilterModes: true, enablePagination: false, paginationDisplayMode: "pages", enableRowVirtualization: true, rowVirtualizerOptions: { overscan: 20 }, renderTopToolbarCustomActions: () => (_jsx(ChakraProvider, { children: _jsx(Stack, { margin: 0, padding: 2, direction: "row", wrap: "wrap", children: selectedPage.actions.map((action, index) => (_jsx(Button, { colorScheme: "blue", onClick: () => {
76
+ })), data: displayData, muiTableContainerProps: {
77
+ sx: {
78
+ maxHeight: 'none',
79
+ overflowY: 'visible',
80
+ overflowX: 'auto',
81
+ },
82
+ }, enableColumnResizing: true, enableColumnPinning: true, enableGlobalFilterModes: true, enablePagination: false, paginationDisplayMode: "pages", enableRowVirtualization: true, rowVirtualizerOptions: { overscan: 20 }, renderTopToolbarCustomActions: () => (_jsx(ChakraProvider, { children: _jsx(Stack, { margin: 0, padding: 2, direction: "row", wrap: "wrap", children: selectedPage.actions.map((action, index) => (_jsx(Button, { colorScheme: "blue", onClick: () => {
91
83
  setSelectedAction(action);
92
84
  onOpen();
93
85
  }, children: action.name }, index))) }) })), enableRowActions: true, renderRowActionMenuItems: (obj) => {
@@ -4,33 +4,6 @@ import { Field, Form, Formik, FieldArray, getIn } from 'formik';
4
4
  import * as Yup from 'yup';
5
5
  import Swal from 'sweetalert2';
6
6
  import { useEffect, useState } from 'react';
7
- function parseValidation(fields) {
8
- if (!fields) {
9
- return {};
10
- }
11
- return fields.reduce((acc, field) => {
12
- if (field.type !== 'object' && field.type !== 'array') {
13
- acc[field.name] = field.validationSchema ? field.validationSchema : '';
14
- }
15
- else if (field.type === 'array') {
16
- if (field.element.type === 'dropdown') {
17
- acc[field.name] = field.element.validationSchema
18
- ? Yup.array().of(field.element.validationSchema)
19
- : '';
20
- }
21
- else if (field.element.type === 'object') {
22
- acc[field.name] = Yup.array().of(Yup.object(parseValidation(field.element.fields)));
23
- }
24
- else {
25
- acc[field.name] = Yup.array().of(field.element.validationSchema);
26
- }
27
- }
28
- else if (field.type === 'object') {
29
- acc[field.name] = Yup.object(parseValidation(field.fields));
30
- }
31
- return acc;
32
- }, {});
33
- }
34
7
  function generateSingleField(formfield, fieldName, key) {
35
8
  return (_jsx(Field, { name: fieldName, children: ({ field, form }) => (_jsxs(FormControl, { isInvalid: getIn(form.touched, fieldName) && getIn(form.errors, fieldName), children: [_jsx(FormLabel, { marginTop: 2, children: formfield.label }), formfield.type === 'string' ||
36
9
  formfield.type === 'number' ||
@@ -56,19 +29,19 @@ function parseFields(fields, previous) {
56
29
  return _jsx(FormControl, { children: objectFields }, key);
57
30
  }
58
31
  else if (formfield.type === 'array') {
59
- return (_jsx(FieldArray, { name: fieldName, children: ({ remove, push, form }) => (_jsxs("div", { children: [_jsx(FormLabel, { marginTop: 2, children: formfield.label }), getIn(form.values, fieldName).length > 0 &&
32
+ return (_jsx(FieldArray, { name: fieldName, children: ({ pop, push, form }) => (_jsxs("div", { children: [_jsx(FormLabel, { marginTop: 2, children: formfield.label }), getIn(form.values, fieldName).length > 0 &&
60
33
  getIn(form.values, fieldName).map((_, index) => (_jsxs(Box, { paddingLeft: 10, children: [_jsx("h4", { children: _jsx("b", { children: `${fieldName}.${index}` }) }), _jsx(Box, { paddingLeft: 10, children: formfield.element.type === 'object'
61
34
  ? parseFields(formfield.element.fields, `${fieldName}.${index}`)
62
- : generateSingleField(formfield.element, `${fieldName}.${index}`, key) }), _jsxs(Button, { type: "button", colorScheme: "red", marginY: 2, onClick: () => remove(index), children: ["Remove ", `${fieldName}.${index}`] })] }, index))), _jsxs(Button, { type: "button", marginY: 2, colorScheme: "blue", onClick: () => {
63
- let value = undefined;
35
+ : generateSingleField(formfield.element, `${fieldName}.${index}`, key) })] }, index))), _jsxs(Button, { type: "button", marginY: 4, colorScheme: "blue", onClick: () => {
36
+ let value;
64
37
  if (formfield.element.type === 'object') {
65
- value = getCustomInitialValues(formfield.element.fields);
38
+ value = getDefaultValues(formfield.element.fields);
66
39
  }
67
40
  else {
68
- value = getCustomInitialValues([formfield.element])[0];
41
+ value = getDefaultValues([formfield.element])[0];
69
42
  }
70
43
  push(value);
71
- }, children: ["Add ", fieldName] })] })) }, key));
44
+ }, children: ["Add ", fieldName] }), getIn(form.values, fieldName).length > 0 && (_jsxs(Button, { type: "button", colorScheme: "red", marginY: 4, ml: 4, onClick: () => pop(), children: ["Remove last ", fieldName] }))] })) }, key));
72
45
  }
73
46
  else {
74
47
  return generateSingleField(formfield, fieldName, key);
@@ -76,14 +49,41 @@ function parseFields(fields, previous) {
76
49
  });
77
50
  return computedFields;
78
51
  }
79
- function getCustomInitialValues(fields) {
52
+ function parseValidation(fields) {
53
+ if (!fields) {
54
+ return {};
55
+ }
56
+ return fields.reduce((acc, field) => {
57
+ if (field.type !== 'object' && field.type !== 'array') {
58
+ acc[field.name] = field.validationSchema ? field.validationSchema : '';
59
+ }
60
+ else if (field.type === 'array') {
61
+ if (field.element.type !== 'object') {
62
+ acc[field.name] = Yup.isSchema(field.element.validationSchema)
63
+ ? Yup.array().of(field.element.validationSchema)
64
+ : '';
65
+ }
66
+ else if (field.element.type === 'object') {
67
+ acc[field.name] = Yup.array().of(Yup.object(parseValidation(field.element.fields)));
68
+ }
69
+ }
70
+ else if (field.type === 'object') {
71
+ acc[field.name] = Yup.object(parseValidation(field.fields));
72
+ }
73
+ return acc;
74
+ }, {});
75
+ }
76
+ function getDefaultValues(fields) {
80
77
  const obj = {};
81
78
  if (!fields) {
82
79
  return obj;
83
80
  }
84
81
  fields.forEach((field) => {
85
82
  if (field.type === 'object') {
86
- obj[field.name] = getCustomInitialValues(field.fields);
83
+ obj[field.name] = getDefaultValues(field.fields);
84
+ }
85
+ else if (field.type === 'array') {
86
+ obj[field.name] = [];
87
87
  }
88
88
  else {
89
89
  obj[field.name] = undefined;
@@ -91,12 +91,12 @@ function getCustomInitialValues(fields) {
91
91
  });
92
92
  return obj;
93
93
  }
94
- async function getInitialValues(action, rowId) {
94
+ async function getValuesFromInitialise(action, rowId) {
95
95
  let obj = {};
96
96
  if (action.initialise) {
97
97
  obj = await action.initialise(rowId);
98
98
  }
99
- const customInitialValues = getCustomInitialValues(action.fields);
99
+ const customInitialValues = getDefaultValues(action.fields);
100
100
  return Object.assign({}, customInitialValues, obj);
101
101
  }
102
102
  async function setOptions(fields) {
@@ -118,7 +118,7 @@ async function setOptions(fields) {
118
118
  }
119
119
  export function GenericForm({ action, onClose, rowId, rowObject }) {
120
120
  const [formFields, setFormFields] = useState(parseFields(action.fields));
121
- const [initialValues, setInitialValues] = useState(getCustomInitialValues(action.fields));
121
+ const [initialValues, setInitialValues] = useState(getDefaultValues(action.fields));
122
122
  const [validationSchema, _] = useState(parseValidation(action.fields));
123
123
  useEffect(() => {
124
124
  setOptions(action.fields).then(() => {
@@ -126,7 +126,7 @@ export function GenericForm({ action, onClose, rowId, rowObject }) {
126
126
  });
127
127
  }, []);
128
128
  const handleFormReset = async () => {
129
- const newInitialValues = await getInitialValues(action, rowId);
129
+ const newInitialValues = await getValuesFromInitialise(action, rowId);
130
130
  setInitialValues(newInitialValues);
131
131
  };
132
132
  return (_jsx(_Fragment, { children: _jsx(Formik, { enableReinitialize: true, initialValues: initialValues, validationSchema: Yup.object(validationSchema), onReset: handleFormReset, onSubmit: async (values) => {
@@ -6,7 +6,7 @@ export interface SimpleField {
6
6
  name: string;
7
7
  label: string;
8
8
  type: 'string' | 'number' | 'file' | 'date' | 'datetime-local' | 'time';
9
- validationSchema: any;
9
+ validationSchema?: any;
10
10
  }
11
11
  export interface Option {
12
12
  value: string | number | boolean;
@@ -48,6 +48,6 @@ export interface PageSchema {
48
48
  name: string;
49
49
  actions: Action[];
50
50
  rowActions: RowAction[];
51
- getRequest: () => Promise<any[]>;
51
+ getRequest: (filter?: any) => Promise<any[]>;
52
52
  tableFields: TableField[];
53
53
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "admin_lsac",
3
3
  "private": false,
4
- "version": "2.0.21",
4
+ "version": "2.0.23",
5
5
  "description": "Generic Admin Panel created for storing events' data",
6
6
  "scripts": {
7
7
  "dev": "vite",