feat: add brand device management with error code handling and navigation

This commit is contained in:
2025-10-21 17:07:36 +07:00
parent fb3e500139
commit cf063822eb
8 changed files with 677 additions and 365 deletions

135
src/api/master-brand.jsx Normal file
View File

@@ -0,0 +1,135 @@
import { SendRequest } from '../components/Global/ApiRequest';
const getAllBrands = async (queryParams) => {
try {
const response = await SendRequest({
method: 'get',
prefix: `brand?${queryParams.toString()}`,
});
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
}
};
}
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;
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('getAllBrands error:', error);
return {
status: 500,
data: {
data: [],
paging: {
page: 1,
limit: 10,
total: 0,
page_total: 0
},
total: 0
},
error: error.message
};
}
};
const getBrandById = async (id) => {
const response = await SendRequest({
method: 'get',
prefix: `brand/${id}`,
});
return response.data;
};
const createBrand = async (queryParams) => {
const response = await SendRequest({
method: 'post',
prefix: `brand`,
params: queryParams,
});
if (Array.isArray(response) && response.length === 0) {
return {
statusCode: 500,
data: null,
message: 'Request failed',
rows: 0
};
}
return {
statusCode: response.statusCode || 200,
data: response.data?.[0] || response.data,
message: response.message,
rows: response.rows
};
};
const updateBrand = async (brand_id, queryParams) => {
const response = await SendRequest({
method: 'put',
prefix: `brand/${brand_id}`,
params: queryParams,
});
if (Array.isArray(response) && response.length === 0) {
return {
statusCode: 500,
data: null,
message: 'Request failed',
rows: 0
};
}
return {
statusCode: response.statusCode || 200,
data: response.data?.[0] || response.data,
message: response.message,
rows: response.rows
};
};
const deleteBrand = async (queryParams) => {
const response = await SendRequest({
method: 'delete',
prefix: `brand/${queryParams}`,
});
return {
statusCode: response.statusCode || 200,
data: response.data,
message: response.message,
rows: response.rows
};
};
export { getAllBrands, getBrandById, createBrand, updateBrand, deleteBrand };