trithuc-mvc-react 2.5.3 → 2.6.1

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,122 @@ 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
+ return res.data;
62
+ } catch (error) {
63
+ console.error("Error fetching data:", error);
64
+ throw error;
65
+ }
35
66
  };
36
67
 
37
68
  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;
69
+ try {
70
+ const url = apiUrl ? `${getBaseUrl()}/${apiUrl}/${tableName}` : `${getBaseUrl()}/Admin/${tableName}/GetDetail`;
71
+
72
+ const res = await api.get(url, {
73
+ params: {
74
+ id
75
+ }
76
+ });
77
+ return res.data;
78
+ } catch (error) {
79
+ console.error("Error fetching data:", error);
80
+ throw error;
81
+ }
44
82
  };
83
+
45
84
  export const deleteDataFromTable = async ({ tableName, id }) => {
46
- const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/Delete`, {
47
- id
48
- });
49
- return res.data;
85
+ if (!apiUrl) {
86
+ // Khi không có apiUrl, thực hiện DELETE đến URL cụ thể với tableName
87
+ const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/Delete`, {
88
+ id
89
+ });
90
+ return res.data;
91
+ } else {
92
+ // Khi có apiUrl, chuyển id thành mảng string và thực hiện DELETE
93
+ const ids = Array.isArray(id) ? id : [id]; // Chuyển id thành mảng nếu chưa phải là mảng
94
+ const res = await api.delete(`${getBaseUrl()}/${apiUrl}/${tableName}`, {
95
+ data: ids, // Truyền ids như là dữ liệu của body
96
+ headers: {
97
+ 'Content-Type': 'application/json-patch+json' // Đảm bảo Content-Type là đúng
98
+ }
99
+ });
100
+ return res.data;
101
+ }
50
102
  };
51
103
  export const deleteMultipleDataFromTable = async ({ tableName, ids }) => {
52
- const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/DeleteMulti`, {
53
- ids
54
- });
55
- return res.data;
104
+ if (!apiUrl) {
105
+ const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/DeleteMulti`, {
106
+ ids
107
+ });
108
+ return res.data;
109
+ } else {
110
+ const res = await api.delete(`${getBaseUrl()}/${apiUrl}/${tableName}`, {
111
+ data: ids, // Truyền ids như là dữ liệu của body
112
+ headers: {
113
+ 'Content-Type': 'application/json' // Đảm bảo Content-Type là đúng
114
+ }
115
+ });
116
+ return res.data;
117
+ }
56
118
  };
57
119
  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;
120
+ if (!apiUrl) {
121
+ const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/SaveData`, {
122
+ json: JSON.stringify({ ...data })
123
+ });
124
+ return res.data;
125
+ } else {
126
+ if (!data.Id) {
127
+ // Khi không có ID, thực hiện POST đến apiUrl
128
+ const res = await api.post(`${getBaseUrl()}/${apiUrl}/${tableName}`, data);
129
+ return res.data;
130
+ } else {
131
+ // Khi có ID, thực hiện PUT đến apiUrl với ID trong URL
132
+ const res = await api.put(`${getBaseUrl()}/${apiUrl}/${tableName}/${data.Id}`, data);
133
+ return res.data;
134
+ }
135
+ }
62
136
  };
63
137
  export const changeStatusDataToTable = async ({ tableName, id }) => {
64
138
  const res = await api.post(`${getBaseUrl()}/Admin/${tableName}/ChangeStatus`, {
@@ -67,10 +141,10 @@ export const changeStatusDataToTable = async ({ tableName, id }) => {
67
141
  return res.data;
68
142
  };
69
143
 
70
- export const exportExcel = async ({ tableName,data }) => {
71
- const res = await api.get(`${getBaseUrl()}/Admin/${tableName}/ExportData`,{
144
+ export const exportExcel = async ({ tableName, data }) => {
145
+ const res = await api.get(`${getBaseUrl()}/Admin/${tableName}/ExportData`, {
72
146
  params: {
73
- json: JSON.stringify(data),
147
+ json: JSON.stringify(data)
74
148
  }
75
149
  });
76
150
  return res.data;
@@ -83,4 +157,4 @@ export const uploadFile = async (formData) => {
83
157
  }
84
158
  });
85
159
  return res.data;
86
- };
160
+ };
@@ -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 = [],
@@ -85,7 +87,7 @@ function DataManagement({
85
87
  const [selectedEditItem, setSelectedEditItem] = useState(null);
86
88
  const [openViewDialog, setOpenViewDialog] = useState(false);
87
89
 
88
- const { canCreate } = usePermission(tableName);
90
+ const { canCreate } = usePermission(apiUrl?apiUrl:tableName);
89
91
  const { defaults, filters } = useMemo(() => {
90
92
  const filters = tableFilters.filter(({ type }) => type !== "default");
91
93
  const defaultFilters = tableFilters.filter(({ type }) => type === "default");
@@ -115,6 +117,7 @@ function DataManagement({
115
117
  const hasTabpanel = !!tabPanel;
116
118
  return {
117
119
  sttLuyKe,
120
+ apiUrl,
118
121
  tableName,
119
122
  selectedField,
120
123
  titleButtons,
@@ -138,6 +141,7 @@ function DataManagement({
138
141
  };
139
142
  }, [
140
143
  sttLuyKe,
144
+ apiUrl,
141
145
  tableName,
142
146
  selectedField,
143
147
  titleButtons,
@@ -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.3",
3
+ "version": "2.6.1",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"