lavoce #2
@@ -1,31 +1,66 @@
|
|||||||
import { SendRequest } from '../components/Global/ApiRequest';
|
import { SendRequest } from '../components/Global/ApiRequest';
|
||||||
|
|
||||||
const getAllUser = async (queryParams) => {
|
const getAllUser = async (queryParams) => {
|
||||||
|
try {
|
||||||
|
console.log('getAllUser queryParams:', queryParams.toString());
|
||||||
|
|
||||||
const response = await SendRequest({
|
const response = await SendRequest({
|
||||||
method: 'get',
|
method: 'get',
|
||||||
prefix: `user?${queryParams.toString()}`,
|
prefix: `user?${queryParams.toString()}`,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Parse query params to get page and limit
|
console.log('getAllUser response:', response);
|
||||||
|
|
||||||
|
// Backend now handles pagination, just return the response
|
||||||
|
// Expected backend response structure:
|
||||||
|
// {
|
||||||
|
// statusCode: 200,
|
||||||
|
// data: [...users],
|
||||||
|
// paging: { page, limit, total, page_total }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Check if backend returns paginated data
|
||||||
|
if (response.paging) {
|
||||||
|
// Filter out super admin users (is_sa = true)
|
||||||
|
const allData = response.data || [];
|
||||||
|
const filteredData = allData.filter(user => user.is_sa !== true && user.is_sa !== 1);
|
||||||
|
|
||||||
|
// Recalculate pagination info after filtering
|
||||||
|
const totalAfterFilter = filteredData.length;
|
||||||
|
const currentPage = response.paging.page || 1;
|
||||||
|
const currentLimit = response.paging.limit || 10;
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: response.statusCode || 200,
|
||||||
|
data: {
|
||||||
|
data: filteredData,
|
||||||
|
paging: {
|
||||||
|
page: currentPage,
|
||||||
|
limit: currentLimit,
|
||||||
|
total: totalAfterFilter,
|
||||||
|
page_total: Math.ceil(totalAfterFilter / currentLimit)
|
||||||
|
},
|
||||||
|
total: totalAfterFilter
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: If backend returns all data without pagination (old behavior)
|
||||||
const params = Object.fromEntries(queryParams);
|
const params = Object.fromEntries(queryParams);
|
||||||
const currentPage = parseInt(params.page) || 1;
|
const currentPage = parseInt(params.page) || 1;
|
||||||
const currentLimit = parseInt(params.limit) || 10;
|
const currentLimit = parseInt(params.limit) || 10;
|
||||||
|
|
||||||
// Backend returns all data, so we need to do client-side pagination
|
|
||||||
const allData = response.data || [];
|
const allData = response.data || [];
|
||||||
|
|
||||||
// Filter out users with is_sa = true
|
// Filter out users with is_sa = true or 1 (client-side filtering)
|
||||||
const filteredData = allData.filter(user => user.is_sa !== true);
|
const filteredData = allData.filter(user => user.is_sa !== true && user.is_sa !== 1);
|
||||||
const totalData = filteredData.length;
|
const totalData = filteredData.length;
|
||||||
|
|
||||||
// Calculate start and end index for current page
|
// Client-side pagination
|
||||||
const startIndex = (currentPage - 1) * currentLimit;
|
const startIndex = (currentPage - 1) * currentLimit;
|
||||||
const endIndex = startIndex + currentLimit;
|
const endIndex = startIndex + currentLimit;
|
||||||
|
|
||||||
// Slice data for current page
|
|
||||||
const paginatedData = filteredData.slice(startIndex, endIndex);
|
const paginatedData = filteredData.slice(startIndex, endIndex);
|
||||||
|
|
||||||
// Transform response to match TableList expected structure
|
|
||||||
return {
|
return {
|
||||||
status: response.statusCode || 200,
|
status: response.statusCode || 200,
|
||||||
data: {
|
data: {
|
||||||
@@ -39,6 +74,24 @@ const getAllUser = async (queryParams) => {
|
|||||||
total: totalData
|
total: totalData
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('getAllUser error:', error);
|
||||||
|
// Return empty data on error to prevent app crash
|
||||||
|
return {
|
||||||
|
status: 500,
|
||||||
|
data: {
|
||||||
|
data: [],
|
||||||
|
paging: {
|
||||||
|
page: 1,
|
||||||
|
limit: 10,
|
||||||
|
total: 0,
|
||||||
|
page_total: 0
|
||||||
|
},
|
||||||
|
total: 0
|
||||||
|
},
|
||||||
|
error: error.message
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getUserById = async (id) => {
|
const getUserById = async (id) => {
|
||||||
@@ -103,4 +156,23 @@ const approveUser = async (user_id) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export { getAllUser, getUserById, createUser, updateUser, deleteUser, approveUser };
|
const changePassword = async (user_id, new_password) => {
|
||||||
|
const response = await SendRequest({
|
||||||
|
method: 'put',
|
||||||
|
prefix: `user/change-password/${user_id}`,
|
||||||
|
params: {
|
||||||
|
new_password: new_password
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Change Password Response:', response);
|
||||||
|
|
||||||
|
// Return full response with statusCode
|
||||||
|
return {
|
||||||
|
statusCode: response.statusCode || 200,
|
||||||
|
data: response.data,
|
||||||
|
message: response.message || 'Password berhasil diubah'
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export { getAllUser, getUserById, createUser, updateUser, deleteUser, approveUser, changePassword };
|
||||||
Reference in New Issue
Block a user