trithuc-mvc-react 2.5.2 → 2.6.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
@@ -1,10 +1,12 @@
1
1
  import axios from "axios";
2
2
 
3
3
  let baseUrl = "";
4
+ let apiUrl = "";
4
5
 
5
- export function configure({ customBaseUrl }) {
6
+ export function configure({ customBaseUrl,customApiUrl }) {
6
7
  if (customBaseUrl) {
7
8
  baseUrl = customBaseUrl;
9
+ apiUrl = customApiUrl;
8
10
  } else {
9
11
  console.warn("baseUrl chưa được định cấu hình. Sử dụng giá trị mặc định.");
10
12
  }
@@ -15,50 +17,123 @@ export function getBaseUrl() {
15
17
  }
16
18
 
17
19
  const api = axios.create({
18
- baseURL: getBaseUrl()??'/',
20
+ baseURL: getBaseUrl() ?? "/",
19
21
  transitional: {
20
22
  silentJSONParsing: false
21
23
  },
22
24
  responseType: "json"
23
25
  });
26
+
27
+ // Hàm để cài đặt token mới
28
+ export function setAuthToken(token) {
29
+ api.defaults.headers.common["Authorization"] = `Bearer ${token}`;
30
+ }
31
+
32
+ // Cài đặt token từ localStorage khi khởi tạo
33
+ const token = localStorage.getItem("token");
34
+ if (token) {
35
+ setAuthToken(token);
36
+ }
24
37
  export default api;
25
38
 
26
39
  export const getDatasFromTable = async ({ tableName, page, pageSize, data }) => {
27
- const res = await api.get(`${getBaseUrl()}/Admin/${tableName}/LoadData`, {
28
- params: {
29
- json: JSON.stringify(data),
30
- page,
31
- pageSize
40
+ try {
41
+ let res;
42
+ // Trường hợp dùng tableName với MVC4
43
+ if (!apiUrl) {
44
+ res = await api.get(`${getBaseUrl()}/Admin/${tableName}/LoadData`, {
45
+ params: {
46
+ json: JSON.stringify(data),
47
+ page,
48
+ pageSize
49
+ }
50
+ });
51
+ } else {
52
+ // Trường hợp dùng api .NET Core
53
+ res = await api.get(`${getBaseUrl()}/${apiUrl}/${tableName}`, {
54
+ params: {
55
+ PageNumber: page,
56
+ PageSize: pageSize,
57
+ ...data
58
+ }
59
+ });
32
60
  }
33
- });
34
- return res.data;
61
+
62
+ return res.data;
63
+ } catch (error) {
64
+ console.error("Error fetching data:", error);
65
+ throw error;
66
+ }
35
67
  };
36
68
 
37
69
  export const getDataFromTable = async ({ tableName, id }) => {
38
- const res = await api.get(`${getBaseUrl()}/Admin/${tableName}/GetDetail`, {
39
- params: {
40
- id
41
- }
42
- });
43
- return res.data;
70
+ try {
71
+ const url = apiUrl ? `${getBaseUrl()}/${apiUrl}/${tableName}` : `${getBaseUrl()}/Admin/${tableName}/GetDetail`;
72
+
73
+ const res = await api.get(url, {
74
+ params: {
75
+ id
76
+ }
77
+ });
78
+ return res.data;
79
+ } catch (error) {
80
+ console.error("Error fetching data:", error);
81
+ throw error;
82
+ }
44
83
  };
84
+
45
85
  export const deleteDataFromTable = async ({ tableName, id }) => {
46
- const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/Delete`, {
47
- id
48
- });
49
- return res.data;
86
+ if (!apiUrl) {
87
+ // Khi không có apiUrl, thực hiện DELETE đến URL cụ thể với tableName
88
+ const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/Delete`, {
89
+ id
90
+ });
91
+ return res.data;
92
+ } else {
93
+ // Khi có apiUrl, chuyển id thành mảng string và thực hiện DELETE
94
+ const ids = Array.isArray(id) ? id : [id]; // Chuyển id thành mảng nếu chưa phải là mảng
95
+ const res = await api.delete(`${getBaseUrl()}/${apiUrl}/${tableName}`, {
96
+ data: ids, // Truyền ids như là dữ liệu của body
97
+ headers: {
98
+ 'Content-Type': 'application/json-patch+json' // Đảm bảo Content-Type là đúng
99
+ }
100
+ });
101
+ return res.data;
102
+ }
50
103
  };
51
104
  export const deleteMultipleDataFromTable = async ({ tableName, ids }) => {
52
- const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/DeleteMulti`, {
53
- ids
54
- });
55
- return res.data;
105
+ if (!apiUrl) {
106
+ const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/DeleteMulti`, {
107
+ ids
108
+ });
109
+ return res.data;
110
+ } else {
111
+ const res = await api.delete(`${getBaseUrl()}/${apiUrl}/${tableName}`, {
112
+ data: ids, // Truyền ids như là dữ liệu của body
113
+ headers: {
114
+ 'Content-Type': 'application/json' // Đảm bảo Content-Type là đúng
115
+ }
116
+ });
117
+ return res.data;
118
+ }
56
119
  };
57
120
  export const saveDataToTable = async ({ tableName, data }) => {
58
- const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/SaveData`, {
59
- json: JSON.stringify({ ...data })
60
- });
61
- return res.data;
121
+ if (!apiUrl) {
122
+ const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/SaveData`, {
123
+ json: JSON.stringify({ ...data })
124
+ });
125
+ return res.data;
126
+ } else {
127
+ if (!data.id) {
128
+ // Khi không có ID, thực hiện POST đến apiUrl
129
+ const res = await api.post(`${getBaseUrl()}/${apiUrl}/${tableName}`, data);
130
+ return res.data;
131
+ } else {
132
+ // Khi có ID, thực hiện PUT đến apiUrl với ID trong URL
133
+ const res = await api.put(`${getBaseUrl()}/${apiUrl}/${tableName}/${data.id}`, data);
134
+ return res.data;
135
+ }
136
+ }
62
137
  };
63
138
  export const changeStatusDataToTable = async ({ tableName, id }) => {
64
139
  const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/ChangeStatus`, {
@@ -67,10 +142,10 @@ export const changeStatusDataToTable = async ({ tableName, id }) => {
67
142
  return res.data;
68
143
  };
69
144
 
70
- export const exportExcel = async ({ tableName,data }) => {
71
- const res = await api.get(`${getBaseUrl()}/Admin/${tableName}/ExportData`,{
145
+ export const exportExcel = async ({ tableName, data }) => {
146
+ const res = await api.get(`${getBaseUrl()}/Admin/${tableName}/ExportData`, {
72
147
  params: {
73
- json: JSON.stringify(data),
148
+ json: JSON.stringify(data)
74
149
  }
75
150
  });
76
151
  return res.data;
@@ -83,4 +158,4 @@ export const uploadFile = async (formData) => {
83
158
  }
84
159
  });
85
160
  return res.data;
86
- };
161
+ };
@@ -98,7 +98,7 @@ export const TableRowRender = ({
98
98
 
99
99
  {!disableCellThaoTac && (
100
100
  <TableCell align="center">
101
- {!disableEdit && (
101
+ {!disableEdit && canEdit && (
102
102
  <Tooltip title="Chỉnh sửa">
103
103
  <IconButton size={downXl ? "small" : "medium"} onClick={() => onEdit(row)}>
104
104
  <EditOutlined fontSize="inherit" color="primary" />
@@ -112,7 +112,7 @@ export const TableRowRender = ({
112
112
  </IconButton>
113
113
  </Tooltip>
114
114
  )}
115
- {!disableDelete && (
115
+ {!disableDelete && canDelete && (
116
116
  <Tooltip title="Xóa">
117
117
  <IconButton
118
118
  size={downXl ? "small" : "medium"}
@@ -19,6 +19,7 @@ DataManagement.propTypes = {
19
19
  columns: PropTypes.array,
20
20
  title: PropTypes.string,
21
21
  titleButtons: PropTypes.array,
22
+ apiUrl: PropTypes.string,
22
23
  tableName: PropTypes.string,
23
24
  selectedField: PropTypes.string,
24
25
  filters: PropTypes.arrayOf(
@@ -55,6 +56,7 @@ function DataManagement({
55
56
  columns = [],
56
57
  title,
57
58
  titleButtons = [],
59
+ apiUrl,
58
60
  tableName,
59
61
  selectedField = "Id",
60
62
  filters: tableFilters = [],
@@ -67,7 +69,8 @@ function DataManagement({
67
69
 
68
70
  tableActions = [],
69
71
  multipleActions = [],
70
-
72
+ disableDelete = false,
73
+ disableEdit = false,
71
74
  disableEditor = false,
72
75
  onAddClick = () => {},
73
76
  onEditClick = () => {},
@@ -84,7 +87,7 @@ function DataManagement({
84
87
  const [selectedEditItem, setSelectedEditItem] = useState(null);
85
88
  const [openViewDialog, setOpenViewDialog] = useState(false);
86
89
 
87
- const { canCreate } = usePermission(tableName);
90
+ const { canCreate } = usePermission(apiUrl?apiUrl:tableName);
88
91
  const { defaults, filters } = useMemo(() => {
89
92
  const filters = tableFilters.filter(({ type }) => type !== "default");
90
93
  const defaultFilters = tableFilters.filter(({ type }) => type === "default");
@@ -114,6 +117,7 @@ function DataManagement({
114
117
  const hasTabpanel = !!tabPanel;
115
118
  return {
116
119
  sttLuyKe,
120
+ apiUrl,
117
121
  tableName,
118
122
  selectedField,
119
123
  titleButtons,
@@ -137,6 +141,7 @@ function DataManagement({
137
141
  };
138
142
  }, [
139
143
  sttLuyKe,
144
+ apiUrl,
140
145
  tableName,
141
146
  selectedField,
142
147
  titleButtons,
@@ -210,7 +215,7 @@ function DataManagement({
210
215
  {tabPanel}
211
216
  <FilterGod filters={filters} elementSize={elementSize} setPage={setPage} />
212
217
  {backParentNavigator}
213
- <DataTable multipleActions={multipleActions} page={page} setPage={setPage} disableEdit={disableEditor} disableDelete = {disableEditor}/>
218
+ <DataTable multipleActions={multipleActions} page={page} setPage={setPage} disableEdit={disableEdit} disableDelete = {disableDelete}/>
214
219
  </Card>
215
220
  </FormProvider>
216
221
 
@@ -1,17 +1,17 @@
1
1
  import { useContext, useMemo } from "react";
2
2
  import { PermissionContext } from "../contexts";
3
3
 
4
- const usePermission = (tableName) => {
5
- if (!tableName) {
6
- throw new Error("tableName is required");
7
- }
4
+ const usePermission = (apiUrl,tableName) => {
5
+ // if (!tableName && !apiUrl) {
6
+ // throw new Error("api is required");
7
+ // }
8
8
  const context = useContext(PermissionContext);
9
9
  if (!context) {
10
10
  throw new Error("usePermission must be used within a PermissionProvider");
11
11
  }
12
12
  const { permissionMap, setPermission } = context;
13
13
 
14
- const permission = permissionMap[tableName];
14
+ const permission = permissionMap[apiUrl?apiUrl:tableName];
15
15
 
16
16
  const { canEdit, canDelete, canDeleteMulti, canSave, canCreate, canAction, canView, canImport } = useMemo(() => {
17
17
  const canEdit = !permission || permission.Edit;
@@ -27,7 +27,7 @@ const usePermission = (tableName) => {
27
27
  }, [permission]);
28
28
 
29
29
  const set = (permission) => {
30
- setPermission({ ...permissionMap, [tableName]: permission });
30
+ setPermission({ ...permissionMap, [apiUrl?apiUrl:tableName]: permission });
31
31
  };
32
32
 
33
33
  return { permission, set, canEdit, canDelete, canDeleteMulti, canSave, canCreate, canAction, canView, canImport };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trithuc-mvc-react",
3
- "version": "2.5.2",
3
+ "version": "2.6.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"