admin_lsac 2.0.20 → 2.0.22

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
@@ -61,7 +61,7 @@ You will need to create an array of schemas for your own admin panel.
61
61
  ```
62
62
  export interface TableField {
63
63
  name: string;
64
- access: (obj: any) => Promise<any>;
64
+ access: (obj: any) => any;
65
65
  }
66
66
 
67
67
  export interface SimpleField {
package/admin_lsac.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  declare module 'admin_lsac' {
2
2
  export interface TableField {
3
3
  name: string;
4
- access: (obj: any) => Promise<any>;
4
+ access: (obj: any) => any;
5
5
  }
6
6
  export interface SimpleField {
7
7
  name: string;
@@ -1,7 +1,7 @@
1
1
  declare module 'admin_lsac' {
2
2
  export interface TableField {
3
3
  name: string;
4
- access: (obj: any) => Promise<any>;
4
+ access: (obj: any) => any;
5
5
  }
6
6
  export interface SimpleField {
7
7
  name: string;
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useEffect, useState } from 'react';
2
+ import { useEffect, useState, useRef } from 'react';
3
3
  import { ChakraProvider } from '@chakra-ui/react';
4
4
  import { Button, Modal, ModalHeader, ModalBody, useDisclosure, ModalOverlay, ModalContent, ModalCloseButton, Box, Stack, } from '@chakra-ui/react';
5
5
  import { GenericForm } from './GenericForm';
@@ -17,8 +17,9 @@ export function pageToUrl(page) {
17
17
  function Header({ pages, data, basePath }) {
18
18
  return (_jsx(Box, { background: "blue.50", padding: 1, children: _jsxs(Stack, { margin: 3, direction: "row", justifyContent: "space-between", alignItems: "center", children: [_jsx(Stack, { margin: 4, gap: 10, direction: "row", alignItems: "center", wrap: "wrap", children: pages.map((p, index) => (_jsx(Box, { as: "a", href: `${basePath}/${pageToUrl(p)}`, children: p.name }, index))) }), _jsx(Stack, { direction: "row", children: _jsx(Button, { colorScheme: "red", onClick: () => {
19
19
  const filename = prompt('Enter filename: (without file extension)');
20
- if (filename)
20
+ if (filename) {
21
21
  exportToCSV(data, filename);
22
+ }
22
23
  }, children: "Export Data" }) })] }) }));
23
24
  }
24
25
  function exportToCSV(data, filename) {
@@ -67,7 +68,18 @@ export function AdminPage({ pages, selectedPage, basePath, apiURL = '', }) {
67
68
  useEffect(() => {
68
69
  setDisplayData(data.map((table_data) => selectedPage.tableFields.map((field) => field.access(table_data))));
69
70
  }, [data]);
70
- return (_jsxs(ChakraProvider, { 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) => ({
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) => ({
71
83
  accessorFn: (row) => {
72
84
  if (typeof row[index] === 'boolean') {
73
85
  row[index] = String(row[index]);
@@ -75,7 +87,7 @@ export function AdminPage({ pages, selectedPage, basePath, apiURL = '', }) {
75
87
  return row[index];
76
88
  },
77
89
  header: field.name,
78
- })), data: displayData, muiTableContainerProps: { sx: { height: '80vh' } }, 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: () => {
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: () => {
79
91
  setSelectedAction(action);
80
92
  onOpen();
81
93
  }, children: action.name }, index))) }) })), enableRowActions: true, renderRowActionMenuItems: (obj) => {
@@ -4,44 +4,12 @@ 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' ||
37
10
  formfield.type === 'date' ||
38
11
  formfield.type === 'datetime-local' ||
39
- formfield.type === 'time' ? (_jsx(Input, { ...field, value: field.value ?? '', type: formfield.type === 'number' ||
40
- formfield.type === 'date' ||
41
- formfield.type === 'datetime-local' ||
42
- formfield.type === 'time'
43
- ? formfield.type
44
- : 'text' })) : formfield.type === 'file' ? (_jsx(Input, { type: "file", name: fieldName, onChange: (event) => {
12
+ formfield.type === 'time' ? (_jsx(Input, { ...field, value: field.value ?? '', type: formfield.type !== 'string' ? formfield.type : 'text' })) : formfield.type === 'file' ? (_jsx(Input, { type: "file", name: fieldName, onChange: (event) => {
45
13
  if (event.currentTarget.files != null) {
46
14
  form.setFieldValue(fieldName, event.currentTarget.files[0]);
47
15
  }
@@ -61,19 +29,19 @@ function parseFields(fields, previous) {
61
29
  return _jsx(FormControl, { children: objectFields }, key);
62
30
  }
63
31
  else if (formfield.type === 'array') {
64
- 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 &&
65
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'
66
34
  ? parseFields(formfield.element.fields, `${fieldName}.${index}`)
67
- : 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: () => {
68
- let value = undefined;
35
+ : generateSingleField(formfield.element, `${fieldName}.${index}`, key) })] }, index))), _jsxs(Button, { type: "button", marginY: 4, colorScheme: "blue", onClick: () => {
36
+ let value;
69
37
  if (formfield.element.type === 'object') {
70
- value = getCustomInitialValues(formfield.element.fields);
38
+ value = getDefaultValues(formfield.element.fields);
71
39
  }
72
40
  else {
73
- value = getCustomInitialValues([formfield.element])[0];
41
+ value = getDefaultValues([formfield.element])[0];
74
42
  }
75
43
  push(value);
76
- }, 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));
77
45
  }
78
46
  else {
79
47
  return generateSingleField(formfield, fieldName, key);
@@ -81,25 +49,54 @@ function parseFields(fields, previous) {
81
49
  });
82
50
  return computedFields;
83
51
  }
84
- 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) {
85
77
  const obj = {};
86
78
  if (!fields) {
87
79
  return obj;
88
80
  }
89
81
  fields.forEach((field) => {
90
- if (field.type === 'object')
91
- obj[field.name] = getCustomInitialValues(field.fields);
92
- else
82
+ if (field.type === 'object') {
83
+ obj[field.name] = getDefaultValues(field.fields);
84
+ }
85
+ else if (field.type === 'array') {
86
+ obj[field.name] = [];
87
+ }
88
+ else {
93
89
  obj[field.name] = undefined;
90
+ }
94
91
  });
95
92
  return obj;
96
93
  }
97
- async function getInitialValues(action, rowId) {
94
+ async function getValuesFromInitialise(action, rowId) {
98
95
  let obj = {};
99
96
  if (action.initialise) {
100
97
  obj = await action.initialise(rowId);
101
98
  }
102
- const customInitialValues = getCustomInitialValues(action.fields);
99
+ const customInitialValues = getDefaultValues(action.fields);
103
100
  return Object.assign({}, customInitialValues, obj);
104
101
  }
105
102
  async function setOptions(fields) {
@@ -107,18 +104,21 @@ async function setOptions(fields) {
107
104
  return;
108
105
  }
109
106
  const promises = fields.map(async (field) => {
110
- if (field.type === 'dropdown')
107
+ if (field.type === 'dropdown') {
111
108
  field.options = field.options || (field.getOptions && (await field.getOptions()));
112
- else if (field.type === 'array')
109
+ }
110
+ else if (field.type === 'array') {
113
111
  await setOptions([field.element]);
114
- else if (field.type === 'object')
112
+ }
113
+ else if (field.type === 'object') {
115
114
  await setOptions(field.fields);
115
+ }
116
116
  });
117
117
  await Promise.all(promises);
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,25 +126,30 @@ 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) => {
133
133
  try {
134
- if (rowObject !== undefined)
134
+ if (rowObject !== undefined) {
135
135
  await action.onSubmit(values, rowId, rowObject);
136
- else if (rowId !== undefined)
136
+ }
137
+ else if (rowId !== undefined) {
137
138
  await action.onSubmit(values, rowId);
138
- else
139
+ }
140
+ else {
139
141
  await action.onSubmit(values);
140
- if (onClose)
142
+ }
143
+ if (onClose) {
141
144
  onClose();
145
+ }
142
146
  Swal.fire('Action/RowAction executed successfully');
143
147
  }
144
148
  catch (e) {
145
- if (onClose)
146
- onClose();
147
149
  const err = e.message;
150
+ if (onClose) {
151
+ onClose();
152
+ }
148
153
  Swal.fire({
149
154
  text: 'Error: ' + err,
150
155
  });
@@ -1,6 +1,6 @@
1
1
  export interface TableField {
2
2
  name: string;
3
- access: (obj: any) => Promise<any>;
3
+ access: (obj: any) => any;
4
4
  }
5
5
  export interface SimpleField {
6
6
  name: string;
@@ -9,7 +9,7 @@ export const event = {
9
9
  name: 'Delete Event - Variant 1', // without form
10
10
  onSubmit: async (eventId) => {
11
11
  // onSubmit has only param, the row's ID
12
- const res = await axios.delete(domain + '/something/' + eventId);
12
+ const res = await axios.delete(domain + '/events/' + eventId);
13
13
  return res.data;
14
14
  },
15
15
  },
@@ -53,15 +53,14 @@ export const event = {
53
53
  // onSubmit has two params
54
54
  // the first param - formValues, while the second is the row's ID
55
55
  console.log(formValues);
56
- const res = await axios.delete(domain + '/something/' + eventId);
56
+ const res = await axios.delete(domain + '/events/' + eventId);
57
57
  return res.data;
58
58
  },
59
59
  },
60
60
  ],
61
61
  getRequest: async () => {
62
- // const res = await axios.get(domain + '/events');
63
- // return res.data;
64
- return [];
62
+ const res = await axios.get(domain + '/events');
63
+ return res.data;
65
64
  },
66
65
  tableFields: [
67
66
  {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "admin_lsac",
3
3
  "private": false,
4
- "version": "2.0.20",
4
+ "version": "2.0.22",
5
5
  "description": "Generic Admin Panel created for storing events' data",
6
6
  "scripts": {
7
7
  "dev": "vite",
@@ -12,7 +12,7 @@
12
12
  "lint-staged": "lint-staged"
13
13
  },
14
14
  "lint-staged": {
15
- "*.{js,ts,tsx, jsx}": [
15
+ "*.{js,ts,tsx,jsx}": [
16
16
  "eslint --quiet --fix"
17
17
  ],
18
18
  "*.{json,md,html,js,jsx,ts,tsx}": [