refactor plant section
This commit is contained in:
@@ -2,12 +2,15 @@ import { SendRequest } from '../components/Global/ApiRequest';
|
||||
|
||||
const getAllPlantSection = async (queryParams) => {
|
||||
try {
|
||||
// Ensure queryParams is URLSearchParams object
|
||||
const params = queryParams instanceof URLSearchParams ? queryParams : new URLSearchParams(queryParams);
|
||||
|
||||
const response = await SendRequest({
|
||||
method: 'get',
|
||||
prefix: `plant-sub-section?${queryParams.toString()}`,
|
||||
prefix: `plant-sub-section?${params.toString()}`,
|
||||
});
|
||||
console.log('getAllPlantSection response:', response);
|
||||
console.log('Query params:', queryParams.toString());
|
||||
console.log('Query params:', params.toString());
|
||||
|
||||
// Backend response structure:
|
||||
// {
|
||||
@@ -23,17 +26,23 @@ const getAllPlantSection = async (queryParams) => {
|
||||
|
||||
// Check if backend returns paginated data
|
||||
if (response.paging) {
|
||||
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
|
||||
// Extract total_data from first record, or fallback to total_limit or rows
|
||||
const totalData = response.data?.[0]?.total_data || response.paging.total_limit || response.rows || response.data?.length || 0;
|
||||
|
||||
// Use total_limit as total count, handle 0 values for page/limit
|
||||
const currentPage = response.paging.current_page || 1;
|
||||
const currentLimit = response.paging.current_limit || 10;
|
||||
const totalPages = response.paging.total_page || Math.ceil(totalData / currentLimit);
|
||||
|
||||
return {
|
||||
status: response.statusCode || 200,
|
||||
data: {
|
||||
data: response.data || [],
|
||||
paging: {
|
||||
page: response.paging.current_page || 1,
|
||||
limit: response.paging.current_limit || 10,
|
||||
page: currentPage,
|
||||
limit: currentLimit,
|
||||
total: totalData,
|
||||
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
|
||||
page_total: totalPages
|
||||
},
|
||||
total: totalData
|
||||
}
|
||||
@@ -41,9 +50,9 @@ const getAllPlantSection = async (queryParams) => {
|
||||
}
|
||||
|
||||
// Fallback: If backend returns all data without pagination (old behavior)
|
||||
const params = Object.fromEntries(queryParams);
|
||||
const currentPage = parseInt(params.page) || 1;
|
||||
const currentLimit = parseInt(params.limit) || 10;
|
||||
const parsedParams = Object.fromEntries(params);
|
||||
const currentPage = parseInt(parsedParams.page) || 1;
|
||||
const currentLimit = parseInt(parsedParams.limit) || 10;
|
||||
|
||||
const allData = response.data || [];
|
||||
const totalData = allData.length;
|
||||
|
||||
Reference in New Issue
Block a user