trithuc-mvc-react 1.3.0 → 1.5.0

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/api/index.js CHANGED
@@ -18,6 +18,7 @@ export const getDatasFromTable = async ({ tableName, page, pageSize, data }) =>
18
18
  });
19
19
  return res.data;
20
20
  };
21
+
21
22
  export const getDataFromTable = async ({ tableName, id }) => {
22
23
  const res = await api.get(`/Admin/${tableName}/GetDetail`, {
23
24
  params: {
@@ -1,15 +1,10 @@
1
- import { Table, TableBody, TableContainer } from "@mui/material";
1
+ import { Table, TableBody, TableCell, TableContainer, TableRow } from "@mui/material";
2
2
  import TablePaginationCustom from "../table/TablePagination";
3
3
  import { useMemo, useState } from "react";
4
4
 
5
5
  import { TableHead } from "./TableHead";
6
6
  import { useMutation, useQuery, useQueryClient } from "react-query";
7
- import {
8
- changeStatusDataToTable,
9
- deleteDataFromTable,
10
- deleteMultipleDataFromTable,
11
- getDatasFromTable,
12
- } from "../../api";
7
+ import { changeStatusDataToTable, deleteDataFromTable, deleteMultipleDataFromTable, getDatasFromTable } from "../../api";
13
8
  import TableRowsLoader from "../table/TableRowsLoader";
14
9
  import { toast } from "react-toastify";
15
10
  import { useConfirm } from "material-ui-confirm";
@@ -33,14 +28,14 @@ const DataTable = () => {
33
28
  tableName: tableName,
34
29
  page: page + 1,
35
30
  pageSize: rowsPerPage,
36
- data: dataSearch,
31
+ data: dataSearch
37
32
  }),
38
33
  keepPreviousData: true,
39
34
  onSuccess: ({ PermissionModel, status }) => {
40
35
  if (!Permission && status) {
41
36
  setPermission(PermissionModel);
42
37
  }
43
- },
38
+ }
44
39
  });
45
40
  const changeStatusMutation = useMutation(changeStatusDataToTable, {
46
41
  onSuccess: () => {
@@ -49,7 +44,7 @@ const DataTable = () => {
49
44
  },
50
45
  onError: () => {
51
46
  toast.error(" Có lỗi xảy ra !");
52
- },
47
+ }
53
48
  });
54
49
  const deleteMutation = useMutation(deleteDataFromTable, {
55
50
  onSuccess: ({ status }) => {
@@ -63,7 +58,7 @@ const DataTable = () => {
63
58
  },
64
59
  onError: () => {
65
60
  toast.error(" Có lỗi xảy ra !");
66
- },
61
+ }
67
62
  });
68
63
  const deleteMultipleMutation = useMutation(deleteMultipleDataFromTable, {
69
64
  onSuccess: () => {
@@ -73,7 +68,7 @@ const DataTable = () => {
73
68
  },
74
69
  onError: () => {
75
70
  toast.error(" Có lỗi xảy ra !");
76
- },
71
+ }
77
72
  });
78
73
 
79
74
  const handleDelete = (id) => {
@@ -81,7 +76,7 @@ const DataTable = () => {
81
76
  .then(() => {
82
77
  deleteMutation.mutate({
83
78
  id,
84
- tableName,
79
+ tableName
85
80
  });
86
81
  })
87
82
  .catch(() => {});
@@ -89,7 +84,7 @@ const DataTable = () => {
89
84
  const handleChangeStatus = (Id) => {
90
85
  changeStatusMutation.mutate({
91
86
  tableName,
92
- id: Id,
87
+ id: Id
93
88
  });
94
89
  };
95
90
  const handlEdit = (item) => {
@@ -101,7 +96,7 @@ const DataTable = () => {
101
96
  let total = data?.total ?? 0;
102
97
  return {
103
98
  rows: rows,
104
- total,
99
+ total
105
100
  };
106
101
  }, [data]);
107
102
 
@@ -142,7 +137,7 @@ const DataTable = () => {
142
137
  .then(() => {
143
138
  deleteMultipleMutation.mutate({
144
139
  tableName,
145
- ids: selected,
140
+ ids: selected
146
141
  });
147
142
  })
148
143
  .catch(() => {});
@@ -157,12 +152,7 @@ const DataTable = () => {
157
152
  onDeleteMultiple={handleDeleteMultiple}
158
153
  />
159
154
  <Table className="border">
160
- <TableHead
161
- headLabel={columns}
162
- onSelectAllClick={handleSelectAllClick}
163
- numSelected={selected?.length}
164
- rowCount={rows.length}
165
- />
155
+ <TableHead headLabel={columns} onSelectAllClick={handleSelectAllClick} numSelected={selected?.length} rowCount={rows.length} />
166
156
  {isLoading ? (
167
157
  <TableRowsLoader rowsNum={5} colsNum={columns.length + 4} />
168
158
  ) : (
@@ -179,6 +169,12 @@ const DataTable = () => {
179
169
  onDelete={handleDelete}
180
170
  />
181
171
  ))}
172
+
173
+ {rows?.length == 0 && (
174
+ <TableRow>
175
+ <TableCell colSpan={columns.length + 4} align="center">Không có dữ liệu</TableCell>
176
+ </TableRow>
177
+ )}
182
178
  </TableBody>
183
179
  )}
184
180
  </Table>
@@ -11,7 +11,7 @@ import { useController, useFormContext } from "react-hook-form";
11
11
  import { debounce } from "lodash";
12
12
  import { useDataTable } from "./hooks";
13
13
 
14
- export function FilterElement({ name, type, label, keyValue, keyLabel, childrenFields, datas, loading = false, onChange = () => { } }) {
14
+ export function FilterElement({ name, type, label, keyValue, keyLabel, childrenFields, datas, loading = false, onChange = () => { }, size= "small" }) {
15
15
  const { control, setValue } = useFormContext();
16
16
 
17
17
  const {
@@ -38,11 +38,14 @@ export function FilterElement({ name, type, label, keyValue, keyLabel, childrenF
38
38
  onFieldChange(e);
39
39
  handleFilterChangeDebounce(name, e.target.value);
40
40
  }}
41
- size="small"
41
+ size={size}
42
42
  placeholder={label}
43
- startAdornment={<InputAdornment position="start">
44
- <SearchOutlined />
45
- </InputAdornment>} />
43
+ startAdornment={
44
+ <InputAdornment position="start">
45
+ <SearchOutlined />
46
+ </InputAdornment>
47
+ }
48
+ />
46
49
  );
47
50
 
48
51
  case "autocomplete":
@@ -51,7 +54,7 @@ export function FilterElement({ name, type, label, keyValue, keyLabel, childrenF
51
54
  {...name}
52
55
  disablePortal
53
56
  loading={loading}
54
- size="small"
57
+ size={size}
55
58
  noOptionsText="Không có dữ liệu"
56
59
  fullWidth
57
60
  options={datas ?? []}
@@ -81,6 +84,7 @@ export function FilterElement({ name, type, label, keyValue, keyLabel, childrenF
81
84
  id="demo-simple-select"
82
85
  name={name}
83
86
  fullWidth
87
+ size={size}
84
88
  label={label}
85
89
  value={value ?? ""}
86
90
  onChange={(e) => {
@@ -1,11 +1,11 @@
1
- import { Slider, Toolbar } from "@mui/material";
1
+ import { Slider, Toolbar } from "@mui/material";
2
2
  import { useFormContext } from "react-hook-form";
3
3
  import Grid from "@mui/material/Unstable_Grid2";
4
4
  import DateRangePicker from "../date/DateRangePicker";
5
5
  import { FilterElement } from "./FilterElement";
6
6
  import { useDataTable } from "./hooks";
7
7
 
8
- export const FilterGod = ({ filters }) => {
8
+ export const FilterGod = ({ filters, elementSize = "medium" }) => {
9
9
  const { handleSubmit } = useFormContext();
10
10
  const onSubmit = (data) => console.log(data);
11
11
  const { setDataSearch, dataSearch } = useDataTable();
@@ -18,7 +18,8 @@ export const FilterGod = ({ filters }) => {
18
18
  sx={{
19
19
  px: 1,
20
20
  my: 2,
21
- display: "block"
21
+ display: "flex",
22
+ alignItems: "center"
22
23
  }}
23
24
  >
24
25
  <Grid
@@ -26,7 +27,8 @@ export const FilterGod = ({ filters }) => {
26
27
  rowSpacing={2}
27
28
  columnSpacing={{ xs: 2 }}
28
29
  sx={{
29
- px: 1
30
+ px: 1,
31
+ flex: 1
30
32
  }}
31
33
  >
32
34
  {filters.map(({ field, ...rest }) => {
@@ -41,7 +43,7 @@ export const FilterGod = ({ filters }) => {
41
43
  [field[1]]: value[1]
42
44
  }));
43
45
  }}
44
- size="small"
46
+ size={elementSize}
45
47
  value={[dataSearch?.[field[0]], dataSearch?.[field[1]]]}
46
48
  />
47
49
  </Grid>
@@ -50,9 +52,16 @@ export const FilterGod = ({ filters }) => {
50
52
 
51
53
  if (rest.type === "slider-range") {
52
54
  return (
53
- <Grid key={field.toString()} xs={12} sm={6} md={4} xl={3} sx={{
54
- px:4
55
- }}>
55
+ <Grid
56
+ key={field.toString()}
57
+ xs={12}
58
+ sm={6}
59
+ md={4}
60
+ xl={3}
61
+ sx={{
62
+ px: 4
63
+ }}
64
+ >
56
65
  <Slider
57
66
  onChange={(e, value) => {
58
67
  setDataSearch({
@@ -61,7 +70,7 @@ export const FilterGod = ({ filters }) => {
61
70
  [field[1]]: value[1]
62
71
  });
63
72
  }}
64
- size="small"
73
+ size={elementSize}
65
74
  marks={rest.marks}
66
75
  defaultValue={rest.defaultValue}
67
76
  valueLabelDisplay="auto"
@@ -73,7 +82,7 @@ export const FilterGod = ({ filters }) => {
73
82
  }
74
83
  return (
75
84
  <Grid key={field} xs={12} sm={6} md={4} xl={3}>
76
- <FilterElement name={field} {...rest} />
85
+ <FilterElement name={field} {...rest} size={elementSize} />
77
86
  </Grid>
78
87
  );
79
88
  })}
@@ -218,18 +218,20 @@ function FormField({
218
218
  format={DEFAULT_DATE_FORMAT}
219
219
  slotProps={{
220
220
  textField: {
221
- fullWidth: true, error: Boolean(error),
221
+ fullWidth: true,
222
+ error: Boolean(error),
222
223
  helperText: error?.message
223
224
  },
224
225
  popper: {
225
226
  disablePortal: false,
226
227
  popperOptions: {
227
- strategy: 'fixed'
228
- },
228
+ strategy: "fixed"
229
+ }
230
+ },
231
+ actionBar: {
232
+ actions: ["clear"]
229
233
  }
230
234
  }}
231
-
232
-
233
235
  />
234
236
  );
235
237
  }}
@@ -3,18 +3,30 @@ import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
3
3
  import { EditOutlined } from "@mui/icons-material";
4
4
  import { useDataTable, usePermission } from "./hooks";
5
5
 
6
-
7
6
  // material
8
7
  import { styled } from "@mui/material/styles";
9
8
  import MoreMenu from "../MoreMenu";
10
-
11
-
12
-
9
+ import { useMemo } from "react";
13
10
 
14
11
  export const TableRowRender = ({ index, row, selected, onSelect, onChangeStatus, onDelete, onEdit }) => {
15
12
  const { selectedField, columns, statusKey, disableStatus, tableActions } = useDataTable();
16
- const { canEdit, canDelete } = usePermission();
17
- console.log("tableActions", tableActions);
13
+ const { canEdit, canDelete, canView, canAction } = usePermission();
14
+
15
+ const { tableActionsOnTable, tableActionsOnMoreMenu } = useMemo(() => {
16
+ const tableActionsAfterFilter = [...tableActions].filter(({ permissionType }) => {
17
+ if (permissionType == "view" && !canView) {
18
+ return false;
19
+ }
20
+ if (permissionType == "action" && !canAction) {
21
+ return false;
22
+ }
23
+ return true;
24
+ });
25
+ const tableActionsOnTable = tableActionsAfterFilter.filter(({ isOnTable = false }) => isOnTable);
26
+ const tableActionsOnMoreMenu = tableActionsAfterFilter.filter(({ isOnTable = false }) => !isOnTable);
27
+ return { tableActionsOnTable, tableActionsOnMoreMenu };
28
+ }, [canView, canAction, tableActions]);
29
+
18
30
  return (
19
31
  <TableRow hover key={row[selectedField]} selected={selected}>
20
32
  <TableCell padding="checkbox">
@@ -70,8 +82,19 @@ export const TableRowRender = ({ index, row, selected, onSelect, onChangeStatus,
70
82
  </IconButton>
71
83
  </Tooltip>
72
84
  )}
85
+ {tableActionsOnTable.map(({ title, onClick, element }) => (
86
+ <Tooltip key={title} title={title}>
87
+ <IconButton
88
+ onClick={() => {
89
+ onClick(row);
90
+ }}
91
+ >
92
+ {element}
93
+ </IconButton>
94
+ </Tooltip>
95
+ ))}
73
96
 
74
- {<MoreMenu actions={tableActions} data={row}/>}
97
+ {<MoreMenu actions={tableActionsOnMoreMenu} data={row} />}
75
98
  </TableCell>
76
99
  </TableRow>
77
100
  );
@@ -86,5 +109,3 @@ export const RootStyle = styled(Toolbar)(({ theme }) => ({
86
109
  height: 56,
87
110
  minHeight: 50
88
111
  }));
89
-
90
-
@@ -6,14 +6,16 @@ export function useDataTable() {
6
6
  }
7
7
  export function usePermission() {
8
8
  const { Permission, setPermission } = useContext(PermissionContext);
9
- const { canEdit, canDelete, canDeleteMulti, canSave, canCreate } = useMemo(() => {
9
+ const { canEdit, canDelete, canDeleteMulti, canSave, canCreate, canAction, canView } = useMemo(() => {
10
10
  const canEdit = !Permission || Permission.Edit;
11
11
  const canDelete = !Permission || Permission.Delete;
12
12
  const canDeleteMulti = !Permission || Permission.DeleteMulti;
13
13
  const canSave = !Permission || Permission.Save;
14
14
  const canCreate = !Permission || Permission.Create;
15
- return { canEdit, canDelete, canDeleteMulti, canSave, canCreate };
15
+ const canAction = !Permission || Permission.Action;
16
+ const canView = !Permission || Permission.View;
17
+ return { canEdit, canDelete, canDeleteMulti, canSave, canCreate, canAction, canView };
16
18
  }, [Permission]);
17
19
 
18
- return { Permission, setPermission, canEdit, canDelete, canDeleteMulti, canSave, canCreate };
20
+ return { Permission, setPermission, canEdit, canDelete, canDeleteMulti, canSave, canCreate, canAction, canView };
19
21
  }
@@ -1,4 +1,4 @@
1
- import { Button, IconButton, Stack, Tooltip, Typography } from "@mui/material";
1
+ import { Button, Card, IconButton, Stack, Tooltip, Typography } from "@mui/material";
2
2
 
3
3
  import { useEffect, useMemo, useState } from "react";
4
4
 
@@ -34,7 +34,10 @@ DataManagement.propTypes = {
34
34
  statusKey: PropTypes.string,
35
35
  tableActions: PropTypes.array,
36
36
  disableEditor: PropTypes.bool,
37
- onAddClick: PropTypes.func
37
+ onAddClick: PropTypes.func,
38
+ onEditClick: PropTypes.func,
39
+ tabPanel: PropTypes.node,
40
+ filterSize: PropTypes.oneOf(["small", "medium"])
38
41
  };
39
42
 
40
43
  function DataManagement({
@@ -42,20 +45,21 @@ function DataManagement({
42
45
  title,
43
46
  tableName,
44
47
  selectedField = "Id",
45
- filters : tableFilters = [],
48
+ filters: tableFilters = [],
46
49
  editorFields = [],
47
50
  validationSchema = {},
48
51
  statusKey = "Status",
49
52
  disableStatus = false,
50
53
  tableActions = [],
51
54
  disableEditor = false,
52
- onAddClick = () => {
53
-
54
- }
55
+ onAddClick = () => {},
56
+ onEditClick = () => {},
57
+ tabPanel,
58
+ filterSize = "medium"
55
59
  }) {
56
60
  const [openEditorDialog, setOpenEditorDialog] = useState(false);
57
61
  const [selectedEditItem, setSelectedEditItem] = useState(null);
58
-
62
+
59
63
  const [Permission, setPermission] = useState(null);
60
64
 
61
65
  const { defaults, filters } = useMemo(() => {
@@ -67,12 +71,17 @@ function DataManagement({
67
71
  return prev;
68
72
  }, {});
69
73
  return { defaults, filters };
70
-
71
74
  }, [tableFilters]);
72
- useEffect(() => {
73
- setDataSearch({ ...dataSearch,...defaults });
74
75
 
76
+ useEffect(() => {
77
+ if (selectedEditItem && disableEditor) {
78
+ onEditClick(selectedEditItem);
79
+ }
80
+ }, [selectedEditItem]);
81
+ useEffect(() => {
82
+ setDataSearch({ ...dataSearch, ...defaults });
75
83
  }, [filters]);
84
+
76
85
  const [dataSearch, setDataSearch] = useState({ ...defaults });
77
86
  const values = useMemo(() => {
78
87
  return {
@@ -105,7 +114,7 @@ function DataManagement({
105
114
  <DataTableContext.Provider value={values}>
106
115
  <PermissionContext.Provider value={permissionValues}>
107
116
  <FormProvider {...methods}>
108
- <Stack direction="row" justifyContent={"space-between"} sx={{ px: 2, pt: 2 }}>
117
+ <Stack direction="row" justifyContent={"space-between"} sx={{ mb: 4 }}>
109
118
  <Typography variant="h4">{title}</Typography>
110
119
  <Stack direction="row" spacing={1}>
111
120
  <Tooltip title="Làm mới">
@@ -113,7 +122,7 @@ function DataManagement({
113
122
  variant="outlined"
114
123
  color="primary"
115
124
  onClick={() => {
116
- setDataSearch({...defaults});
125
+ setDataSearch({ ...defaults });
117
126
  [...filters].forEach((filter) => {
118
127
  filter?.onChange?.();
119
128
  });
@@ -143,11 +152,19 @@ function DataManagement({
143
152
  )}
144
153
  </Stack>
145
154
  </Stack>
146
- <FilterGod filters={filters} />
147
- <DataTable />
155
+ <Card>
156
+ {tabPanel}
157
+ <FilterGod filters={filters} elementSize={filterSize} />
158
+ <DataTable />
159
+ </Card>
148
160
  </FormProvider>
149
161
  {disableEditor || (
150
- <EditorDialog open={openEditorDialog} onClose={() => setOpenEditorDialog(false)} defaultValues={selectedEditItem} fields={editorFields} />
162
+ <EditorDialog
163
+ open={openEditorDialog}
164
+ onClose={() => setOpenEditorDialog(false)}
165
+ defaultValues={selectedEditItem}
166
+ fields={editorFields}
167
+ />
151
168
  )}
152
169
  </PermissionContext.Provider>
153
170
  </DataTableContext.Provider>
@@ -7,8 +7,6 @@ import MoreVertIcon from "@mui/icons-material/MoreVert";
7
7
  // ----------------------------------------------------------------------
8
8
 
9
9
  MoreMenu.propTypes = {
10
- onDelete: PropTypes.func,
11
- userName: PropTypes.string,
12
10
  actions: PropTypes.array,
13
11
  data: PropTypes.any
14
12
  };
@@ -1 +1,2 @@
1
1
  export const DEFAULT_DATE_FORMAT = "DD/MM/YYYY";
2
+ export const DEFAULT_DATETIME_FORMAT = "dd MMM yyyy HH:mm";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trithuc-mvc-react",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"