admin_lsac 2.0.7 → 2.0.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/README.md
CHANGED
|
@@ -63,14 +63,6 @@ You will need to create an array of schemas for your own admin panel.
|
|
|
63
63
|
## What is a schema?
|
|
64
64
|
|
|
65
65
|
```
|
|
66
|
-
export interface PageSchema {
|
|
67
|
-
name: string;
|
|
68
|
-
actions: Action[];
|
|
69
|
-
rowActions: RowAction[];
|
|
70
|
-
getRequest: () => Promise<any[]>;
|
|
71
|
-
tableFields: TableField[];
|
|
72
|
-
}
|
|
73
|
-
|
|
74
66
|
export interface TableField {
|
|
75
67
|
name: string;
|
|
76
68
|
access: (obj: any) => string | number | React.ReactElement;
|
|
@@ -79,7 +71,7 @@ export interface TableField {
|
|
|
79
71
|
export interface SimpleField {
|
|
80
72
|
name: string;
|
|
81
73
|
label: string;
|
|
82
|
-
type:
|
|
74
|
+
type: 'string' | 'number' | 'file' | 'date';
|
|
83
75
|
validationSchema: any;
|
|
84
76
|
}
|
|
85
77
|
|
|
@@ -91,22 +83,23 @@ export interface Option {
|
|
|
91
83
|
export interface DropdownField {
|
|
92
84
|
name: string;
|
|
93
85
|
label: string;
|
|
94
|
-
type:
|
|
95
|
-
|
|
96
|
-
|
|
86
|
+
type: 'dropdown';
|
|
87
|
+
validationSchema?: any;
|
|
88
|
+
options?: Option[];
|
|
89
|
+
getOptions?: () => Promise<Option[]>;
|
|
97
90
|
}
|
|
98
91
|
|
|
99
92
|
export interface ObjectField {
|
|
100
93
|
name: string;
|
|
101
94
|
label: string;
|
|
102
|
-
type:
|
|
95
|
+
type: 'object';
|
|
103
96
|
fields: FormField[];
|
|
104
97
|
}
|
|
105
98
|
|
|
106
99
|
export interface ArrayField {
|
|
107
100
|
name: string;
|
|
108
101
|
label: string;
|
|
109
|
-
type:
|
|
102
|
+
type: 'array';
|
|
110
103
|
element: Exclude<FormField, ArrayField>;
|
|
111
104
|
}
|
|
112
105
|
|
|
@@ -115,20 +108,28 @@ export type FormField = SimpleField | DropdownField | ObjectField | ArrayField;
|
|
|
115
108
|
export interface Action {
|
|
116
109
|
name: string;
|
|
117
110
|
fields?: FormField[];
|
|
118
|
-
onSubmit: (obj1: any, obj2?: any) => Promise<any>;
|
|
111
|
+
onSubmit: (obj1: any, obj2?: any, obj3?: any) => Promise<any>;
|
|
119
112
|
}
|
|
120
113
|
|
|
121
114
|
export interface RowAction {
|
|
122
115
|
name: string;
|
|
123
116
|
fields?: FormField[];
|
|
124
|
-
initialise?: (obj: any) => any
|
|
125
|
-
onSubmit: (obj1: any, obj2?: any) => Promise<any>;
|
|
117
|
+
initialise?: (obj: any) => Promise<any>;
|
|
118
|
+
onSubmit: (obj1: any, obj2?: any, obj3?: any) => Promise<any>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface PageSchema {
|
|
122
|
+
name: string;
|
|
123
|
+
actions: Action[];
|
|
124
|
+
rowActions: RowAction[];
|
|
125
|
+
getRequest: () => Promise<any[]>;
|
|
126
|
+
tableFields: TableField[];
|
|
126
127
|
}
|
|
127
128
|
```
|
|
128
129
|
|
|
129
130
|
A page schema is based on these interfaces, and it describes how one table of your admin panel would need to be structured to accomplish what the user needs to do regarding an entity. (i.e. users, teams, companies, quizzes)
|
|
130
131
|
|
|
131
|
-
The most important interface is `PageSchema`, as it
|
|
132
|
+
The most important interface is `PageSchema`, as it stands at the top of the relations between all these interfaces.
|
|
132
133
|
|
|
133
134
|
What do the fields of `PageSchema` refer to?
|
|
134
135
|
|
|
@@ -140,7 +141,7 @@ What do the fields of `PageSchema` refer to?
|
|
|
140
141
|
|
|
141
142
|
The operations for the rowActions and actions will generally have a form to complete before executing it. There are 4 types of inputs for the form part:
|
|
142
143
|
|
|
143
|
-
- simpleField = simple data like numbers, strings
|
|
144
|
+
- simpleField = simple data like numbers, strings, files and dates
|
|
144
145
|
- dropdownField = enable the user to choose an option from a predefined list
|
|
145
146
|
- arrayField = arrays of data
|
|
146
147
|
- objectField = an object containing more fields/inputs, or even other objects (some cool recursion)
|
|
@@ -148,20 +149,15 @@ The operations for the rowActions and actions will generally have a form to comp
|
|
|
148
149
|
#### Schema example
|
|
149
150
|
|
|
150
151
|
```
|
|
152
|
+
import axios from "axios";
|
|
153
|
+
import React from "react";
|
|
151
154
|
const domain = "http://localhost:3001"; // dev mode
|
|
152
155
|
|
|
153
|
-
// helper function
|
|
154
|
-
const getAuth = () => {
|
|
155
|
-
// return a token or whatever :)
|
|
156
|
-
};
|
|
157
|
-
|
|
158
156
|
const companies: PageSchema = {
|
|
159
157
|
name: "Companies",
|
|
160
158
|
|
|
161
159
|
getRequest: async () => { // retrieve all entries from the database
|
|
162
|
-
const res = await axios.get(domain + "/something"
|
|
163
|
-
headers: getAuth(),
|
|
164
|
-
});
|
|
160
|
+
const res = await axios.get(domain + "/something");
|
|
165
161
|
return res.data;
|
|
166
162
|
},
|
|
167
163
|
|
|
@@ -217,6 +213,7 @@ const companies: PageSchema = {
|
|
|
217
213
|
name: "type",
|
|
218
214
|
label: "Question Type",
|
|
219
215
|
type: "dropdown",
|
|
216
|
+
// validationSchema is missing here (optional), so it will not be a required field
|
|
220
217
|
getOptions: async () => [
|
|
221
218
|
{
|
|
222
219
|
value: "OPEN",
|
|
@@ -238,13 +235,8 @@ const companies: PageSchema = {
|
|
|
238
235
|
],
|
|
239
236
|
onSubmit: async (formValues: any) => { // onSubmit function can also be complex
|
|
240
237
|
// for actions, onSubmit will always have one param (formValues - you can change the name though)
|
|
241
|
-
// process formVaues as you wish
|
|
242
|
-
|
|
243
|
-
// Nevertheless, submitting the actual data is the most important thing
|
|
244
|
-
const result = await axios.post(domain + "/something", formValues, {
|
|
245
|
-
headers: getAuth(),
|
|
246
|
-
});
|
|
247
|
-
|
|
238
|
+
// you can also process formVaues as you wish
|
|
239
|
+
const result = await axios.post(domain + "/something", formValues);
|
|
248
240
|
return result.data;
|
|
249
241
|
}
|
|
250
242
|
},
|
|
@@ -252,16 +244,14 @@ const companies: PageSchema = {
|
|
|
252
244
|
|
|
253
245
|
rowActions: [
|
|
254
246
|
{
|
|
255
|
-
name: "
|
|
247
|
+
name: "Modify Event - Variant 1", // without form
|
|
256
248
|
onSubmit: async (eventId: number) => { // onSubmit has only param, the row's ID
|
|
257
|
-
const res = await axios.delete(
|
|
258
|
-
domain + "/something/" + eventId
|
|
259
|
-
);
|
|
249
|
+
const res = await axios.delete(domain + "/something/" + eventId);
|
|
260
250
|
return res.data;
|
|
261
251
|
},
|
|
262
252
|
},
|
|
263
253
|
{
|
|
264
|
-
name: "
|
|
254
|
+
name: "Modify Event - Variant 2", // with form
|
|
265
255
|
fields: [
|
|
266
256
|
{
|
|
267
257
|
name: "input",
|
|
@@ -271,18 +261,16 @@ const companies: PageSchema = {
|
|
|
271
261
|
},
|
|
272
262
|
],
|
|
273
263
|
onSubmit: async (formValues: any, eventId: number) => { // onSubmit has two params
|
|
274
|
-
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
);
|
|
278
|
-
return res.data;
|
|
264
|
+
// the first param - formValues, while the second is the row's ID
|
|
265
|
+
const result = await axios.patch(domain + "/something" + eventId, formValues);
|
|
266
|
+
return result.data;
|
|
279
267
|
},
|
|
280
268
|
},
|
|
281
269
|
],
|
|
282
270
|
}
|
|
283
271
|
```
|
|
284
272
|
|
|
285
|
-
Please note that the schema above is not related to any entity, and the fields of PageSchema do not necessarily have any relation between them. These are just random examples to teach you how to create your own
|
|
273
|
+
Please note that the schema above is not related to any entity, and the fields of PageSchema do not necessarily have any relation between them. These are just random examples to teach you how to create your own schema.
|
|
286
274
|
|
|
287
275
|
#### An array of schemas must be defined as below:
|
|
288
276
|
|
package/admin_lsac.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ declare module 'admin_lsac' {
|
|
|
7
7
|
export interface SimpleField {
|
|
8
8
|
name: string;
|
|
9
9
|
label: string;
|
|
10
|
-
type: 'string' | 'number' | 'file';
|
|
10
|
+
type: 'string' | 'number' | 'file' | 'date';
|
|
11
11
|
validationSchema: any;
|
|
12
12
|
}
|
|
13
13
|
export interface Option {
|
|
@@ -19,7 +19,7 @@ declare module 'admin_lsac' {
|
|
|
19
19
|
label: string;
|
|
20
20
|
type: 'dropdown';
|
|
21
21
|
validationSchema?: any;
|
|
22
|
-
options?: Option[]
|
|
22
|
+
options?: Option[];
|
|
23
23
|
getOptions?: () => Promise<Option[]>;
|
|
24
24
|
}
|
|
25
25
|
export interface ObjectField {
|
|
@@ -43,7 +43,7 @@ declare module 'admin_lsac' {
|
|
|
43
43
|
export interface RowAction {
|
|
44
44
|
name: string;
|
|
45
45
|
fields?: FormField[];
|
|
46
|
-
initialise?: (obj: any) => any
|
|
46
|
+
initialise?: (obj: any) => Promise<any>;
|
|
47
47
|
onSubmit: (obj1: any, obj2?: any, obj3?: any) => Promise<any>;
|
|
48
48
|
}
|
|
49
49
|
export interface PageSchema {
|
|
@@ -7,7 +7,7 @@ declare module 'admin_lsac' {
|
|
|
7
7
|
export interface SimpleField {
|
|
8
8
|
name: string;
|
|
9
9
|
label: string;
|
|
10
|
-
type: 'string' | 'number' | 'file';
|
|
10
|
+
type: 'string' | 'number' | 'file' | 'date';
|
|
11
11
|
validationSchema: any;
|
|
12
12
|
}
|
|
13
13
|
export interface Option {
|
|
@@ -19,7 +19,7 @@ declare module 'admin_lsac' {
|
|
|
19
19
|
label: string;
|
|
20
20
|
type: 'dropdown';
|
|
21
21
|
validationSchema?: any;
|
|
22
|
-
options?: Option[]
|
|
22
|
+
options?: Option[];
|
|
23
23
|
getOptions?: () => Promise<Option[]>;
|
|
24
24
|
}
|
|
25
25
|
export interface ObjectField {
|
|
@@ -43,7 +43,7 @@ declare module 'admin_lsac' {
|
|
|
43
43
|
export interface RowAction {
|
|
44
44
|
name: string;
|
|
45
45
|
fields?: FormField[];
|
|
46
|
-
initialise?: (obj: any) => any
|
|
46
|
+
initialise?: (obj: any) => Promise<any>;
|
|
47
47
|
onSubmit: (obj1: any, obj2?: any, obj3?: any) => Promise<any>;
|
|
48
48
|
}
|
|
49
49
|
export interface PageSchema {
|
|
@@ -22,43 +22,13 @@ function Header({ pages, data, basePath }) {
|
|
|
22
22
|
}, children: "Export Data" }) })] }) }));
|
|
23
23
|
}
|
|
24
24
|
function exportToCSV(data, filename) {
|
|
25
|
-
const formatData = (obj) => {
|
|
26
|
-
const formattedObj = {};
|
|
27
|
-
for (const key in obj) {
|
|
28
|
-
if (typeof obj[key] !== 'object') {
|
|
29
|
-
formattedObj[key] = obj[key];
|
|
30
|
-
}
|
|
31
|
-
else if (typeof obj[key] === 'object' && obj[key] != null) {
|
|
32
|
-
Object.keys(obj[key]).forEach((k) => {
|
|
33
|
-
formattedObj[`${key}.${k}`] = obj[key][k];
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return formattedObj;
|
|
38
|
-
};
|
|
39
|
-
const removeTimestamps = (obj) => {
|
|
40
|
-
const { createdAt, updatedAt, ...rest } = obj;
|
|
41
|
-
return rest;
|
|
42
|
-
};
|
|
43
|
-
const allKeys = new Set();
|
|
44
|
-
data.forEach((item) => {
|
|
45
|
-
Object.keys(formatData(removeTimestamps(item))).forEach((key) => allKeys.add(key));
|
|
46
|
-
});
|
|
47
|
-
const formattedData = data.map((item) => {
|
|
48
|
-
const newItem = formatData(removeTimestamps(item));
|
|
49
|
-
allKeys.forEach((key) => {
|
|
50
|
-
if (!(key in newItem)) {
|
|
51
|
-
newItem[key] = '';
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
return newItem;
|
|
55
|
-
});
|
|
56
25
|
const csvConfig = mkConfig({
|
|
57
26
|
useKeysAsHeaders: true,
|
|
27
|
+
showColumnHeaders: false,
|
|
58
28
|
filename: filename,
|
|
59
29
|
});
|
|
60
|
-
if (
|
|
61
|
-
const csv = generateCsv(csvConfig)(
|
|
30
|
+
if (data.length > 1) {
|
|
31
|
+
const csv = generateCsv(csvConfig)(data);
|
|
62
32
|
download(csvConfig)(csv);
|
|
63
33
|
}
|
|
64
34
|
else {
|
|
@@ -97,7 +67,7 @@ export function AdminPage({ pages, selectedPage, basePath, apiURL = '', }) {
|
|
|
97
67
|
useEffect(() => {
|
|
98
68
|
setDisplayData(data.map((table_data) => selectedPage.tableFields.map((field) => field.access(table_data))));
|
|
99
69
|
}, [data]);
|
|
100
|
-
return (_jsxs(_Fragment, { children: [_jsx(Header, { pages: pages, data:
|
|
70
|
+
return (_jsxs(_Fragment, { 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) => ({
|
|
101
71
|
accessorFn: (row) => {
|
|
102
72
|
if (typeof row[index] === 'boolean') {
|
|
103
73
|
row[index] = String(row[index]);
|
|
@@ -120,7 +90,7 @@ export function AdminPage({ pages, selectedPage, basePath, apiURL = '', }) {
|
|
|
120
90
|
if (rowActionsMenu) {
|
|
121
91
|
rowActionsMenu.remove();
|
|
122
92
|
}
|
|
123
|
-
}, children: rowAction.name
|
|
93
|
+
}, children: rowAction.name }, index)));
|
|
124
94
|
} }) }), _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 && selectedAction.fields?.length > 0 ? (_jsx(GenericForm, { action: selectedAction, onClose: onClose, rowId: rowId, rowObject: rowObject })) : (_jsx(Button, { colorScheme: "blue", mr: 3, onClick: () => {
|
|
125
95
|
selectedAction.onSubmit(rowId);
|
|
126
96
|
onClose();
|
package/dist/esm/src/schema.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface DropdownField {
|
|
|
18
18
|
label: string;
|
|
19
19
|
type: 'dropdown';
|
|
20
20
|
validationSchema?: any;
|
|
21
|
-
options?: Option[]
|
|
21
|
+
options?: Option[];
|
|
22
22
|
getOptions?: () => Promise<Option[]>;
|
|
23
23
|
}
|
|
24
24
|
export interface ObjectField {
|
|
@@ -42,7 +42,7 @@ export interface Action {
|
|
|
42
42
|
export interface RowAction {
|
|
43
43
|
name: string;
|
|
44
44
|
fields?: FormField[];
|
|
45
|
-
initialise?: (obj: any) => any
|
|
45
|
+
initialise?: (obj: any) => Promise<any>;
|
|
46
46
|
onSubmit: (obj1: any, obj2?: any, obj3?: any) => Promise<any>;
|
|
47
47
|
}
|
|
48
48
|
export interface PageSchema {
|
|
@@ -1,36 +1,65 @@
|
|
|
1
|
-
|
|
1
|
+
import axios from 'axios';
|
|
2
2
|
import React from 'react';
|
|
3
|
-
|
|
3
|
+
const domain = 'http://localhost:3001';
|
|
4
4
|
export const event = {
|
|
5
5
|
name: 'Events',
|
|
6
6
|
actions: [],
|
|
7
7
|
rowActions: [
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
8
|
+
{
|
|
9
|
+
name: 'Delete Event - Variant 1', // without form
|
|
10
|
+
onSubmit: async (eventId) => {
|
|
11
|
+
// onSubmit has only param, the row's ID
|
|
12
|
+
const res = await axios.delete(domain + '/something/' + eventId);
|
|
13
|
+
return res.data;
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: 'Delete Event - Variant 2', // with form
|
|
18
|
+
fields: [
|
|
19
|
+
{
|
|
20
|
+
name: 'input',
|
|
21
|
+
label: 'Mock Field',
|
|
22
|
+
type: 'string',
|
|
23
|
+
validationSchema: '',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'type',
|
|
27
|
+
label: 'Question Type',
|
|
28
|
+
type: 'dropdown',
|
|
29
|
+
// validationSchema is missing here (optional), so it will not be a required field
|
|
30
|
+
getOptions: async () => [
|
|
31
|
+
{
|
|
32
|
+
value: 'OPEN',
|
|
33
|
+
display: 'Open',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
value: 'SINGLE',
|
|
37
|
+
display: 'Single',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
value: 'MULTIPLE',
|
|
41
|
+
display: 'Multiple',
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
initialise: async (eventId) => {
|
|
47
|
+
const res = await axios.get(domain + '/events/' + eventId);
|
|
48
|
+
const obj = {};
|
|
49
|
+
obj.input = res.data.name;
|
|
50
|
+
return obj;
|
|
51
|
+
},
|
|
52
|
+
onSubmit: async (formValues, eventId) => {
|
|
53
|
+
// onSubmit has two params
|
|
54
|
+
// the first param - formValues, while the second is the row's ID
|
|
55
|
+
console.log(formValues);
|
|
56
|
+
const res = await axios.delete(domain + '/something/' + eventId);
|
|
57
|
+
return res.data;
|
|
58
|
+
},
|
|
59
|
+
},
|
|
31
60
|
],
|
|
32
61
|
getRequest: async () => {
|
|
33
|
-
// const res = await axios.get(domain +
|
|
62
|
+
// const res = await axios.get(domain + '/events');
|
|
34
63
|
// return res.data;
|
|
35
64
|
return [];
|
|
36
65
|
},
|