Refactor: shift management API and UI components

This commit is contained in:
2025-10-20 13:49:42 +07:00
parent 4a9b6c9d01
commit d2c755c03d
5 changed files with 760 additions and 356 deletions

View File

@@ -6,9 +6,75 @@ const getAllShift = async (queryParams) => {
method: 'get',
prefix: `shift?${queryParams.toString()}`,
});
return response;
console.log('getAllShift response:', response);
console.log('Query params:', queryParams.toString());
// Check if response has error
if (response.error) {
console.error('getAllShift error response:', response);
return {
status: response.statusCode || 500,
data: {
data: [],
paging: {
page: 1,
limit: 10,
total: 0,
page_total: 0
},
total: 0
},
error: response.message
};
}
// Check if backend returns paginated data
if (response.paging) {
const totalData = response.data?.[0]?.total_data || response.rows || response.data?.length || 0;
return {
status: response.statusCode || 200,
data: {
data: response.data || [],
paging: {
page: response.paging.current_page || 1,
limit: response.paging.current_limit || 10,
total: totalData,
page_total: response.paging.total_page || Math.ceil(totalData / (response.paging.current_limit || 10))
},
total: totalData
}
};
}
// Fallback: If backend returns all data without pagination
const params = Object.fromEntries(queryParams);
const currentPage = parseInt(params.page) || 1;
const currentLimit = parseInt(params.limit) || 10;
const allData = response.data || [];
const totalData = allData.length;
// Client-side pagination
const startIndex = (currentPage - 1) * currentLimit;
const endIndex = startIndex + currentLimit;
const paginatedData = allData.slice(startIndex, endIndex);
return {
status: response.statusCode || 200,
data: {
data: paginatedData,
paging: {
page: currentPage,
limit: currentLimit,
total: totalData,
page_total: Math.ceil(totalData / currentLimit)
},
total: totalData
}
};
} catch (error) {
console.error('getAllShift error:', error);
console.error('getAllShift catch error:', error);
return {
status: 500,
data: {
@@ -38,12 +104,27 @@ const createShift = async (queryParams) => {
const response = await SendRequest({
method: 'post',
prefix: `shift`,
data: queryParams,
params: queryParams,
});
console.log('createShift full response:', response);
console.log('createShift payload sent:', queryParams);
// Check if response has error flag
if (response.error) {
return {
statusCode: response.statusCode || 500,
data: null,
message: response.message || 'Request failed',
rows: 0
};
}
// Backend returns: { statusCode, message, rows, data: [shift_object] }
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message
data: response.data?.[0] || response.data,
message: response.message,
rows: response.rows
};
};
@@ -51,12 +132,27 @@ const updateShift = async (id, queryParams) => {
const response = await SendRequest({
method: 'put',
prefix: `shift/${id}`,
data: queryParams,
params: queryParams,
});
console.log('updateShift full response:', response);
console.log('updateShift payload sent:', queryParams);
// Check if response has error flag
if (response.error) {
return {
statusCode: response.statusCode || 500,
data: null,
message: response.message || 'Request failed',
rows: 0
};
}
// Backend returns: { statusCode, message, rows, data: [shift_object] }
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message
data: response.data?.[0] || response.data,
message: response.message,
rows: response.rows
};
};
@@ -65,10 +161,24 @@ const deleteShift = async (id) => {
method: 'delete',
prefix: `shift/${id}`,
});
console.log('deleteShift full response:', response);
// Check if response has error flag
if (response.error) {
return {
statusCode: response.statusCode || 500,
data: null,
message: response.message || 'Request failed',
rows: 0
};
}
// Backend returns: { statusCode, message, rows: null, data: true }
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message
message: response.message,
rows: response.rows
};
};