feat: implement user management API functions and create ListUser component for user listing

This commit is contained in:
2025-10-08 14:45:41 +07:00
parent 9806593319
commit bc46328832
3 changed files with 497 additions and 5 deletions

103
src/api/user.jsx Normal file
View File

@@ -0,0 +1,103 @@
import { SendRequest } from '../components/Global/ApiRequest';
const getAllUser = async (queryParams) => {
const response = await SendRequest({
method: 'get',
prefix: `user?${queryParams.toString()}`,
});
// Parse query params to get page and limit
const params = Object.fromEntries(queryParams);
const currentPage = parseInt(params.page) || 1;
const currentLimit = parseInt(params.limit) || 10;
// Backend returns all data, so we need to do client-side pagination
const allData = response.data || [];
const totalData = allData.length;
// Calculate start and end index for current page
const startIndex = (currentPage - 1) * currentLimit;
const endIndex = startIndex + currentLimit;
// Slice data for current page
const paginatedData = allData.slice(startIndex, endIndex);
// Transform response to match TableList expected structure
return {
status: response.statusCode || 200,
data: {
data: paginatedData,
paging: {
page: currentPage,
limit: currentLimit,
total: totalData,
page_total: Math.ceil(totalData / currentLimit)
},
total: totalData
}
};
};
const getUserById = async (id) => {
const response = await SendRequest({
method: 'get',
prefix: `user/${id}`,
});
return response.data;
};
const createUser = async (queryParams) => {
const response = await SendRequest({
method: 'post',
prefix: `user`,
params: queryParams,
});
// Return full response with statusCode
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message
};
};
const updateUser = async (user_id, queryParams) => {
const response = await SendRequest({
method: 'put',
prefix: `user/${user_id}`,
params: queryParams,
});
// Return full response with statusCode
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message
};
};
const deleteUser = async (queryParams) => {
const response = await SendRequest({
method: 'delete',
prefix: `user/${queryParams}`,
});
// Return full response with statusCode
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message
};
};
const approveUser = async (user_id) => {
const response = await SendRequest({
method: 'put',
prefix: `user/${user_id}/approve`,
});
// Return full response with statusCode
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message
};
};
export { getAllUser, getUserById, createUser, updateUser, deleteUser, approveUser };