|
|
@@ -13,6 +13,8 @@ import { TypeEnum } from "@/app/utils/typeEnum"; |
|
|
import axios from "axios"; |
|
|
import axios from "axios"; |
|
|
import { BASE_API_URL, NEXT_PUBLIC_API_URL } from "@/config/api"; |
|
|
import { BASE_API_URL, NEXT_PUBLIC_API_URL } from "@/config/api"; |
|
|
import axiosInstance from "@/app/(main)/axios/axiosInstance"; |
|
|
import axiosInstance from "@/app/(main)/axios/axiosInstance"; |
|
|
|
|
|
import { deleteItem } from "@/app/api/settings/item/actions"; |
|
|
|
|
|
import { deleteDialog, successDialog } from "../Swal/CustomAlerts"; |
|
|
|
|
|
|
|
|
type Props = { |
|
|
type Props = { |
|
|
items: ItemsResult[]; |
|
|
items: ItemsResult[]; |
|
|
@@ -50,8 +52,6 @@ const ItemsSearch: React.FC<Props> = ({ items }) => { |
|
|
[router], |
|
|
[router], |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
const onDeleteClick = useCallback((item: ItemsResult) => {}, [router]); |
|
|
|
|
|
|
|
|
|
|
|
const checkItemStatus = useCallback((item: ItemsResult): "complete" | "missing" => { |
|
|
const checkItemStatus = useCallback((item: ItemsResult): "complete" | "missing" => { |
|
|
// Check if type exists and is not empty |
|
|
// Check if type exists and is not empty |
|
|
const hasType = item.type != null && String(item.type).trim() !== ""; |
|
|
const hasType = item.type != null && String(item.type).trim() !== ""; |
|
|
@@ -76,48 +76,6 @@ const ItemsSearch: React.FC<Props> = ({ items }) => { |
|
|
return "missing"; |
|
|
return "missing"; |
|
|
}, []); |
|
|
}, []); |
|
|
|
|
|
|
|
|
const columns = useMemo<Column<ItemsResultWithStatus>[]>( |
|
|
|
|
|
() => [ |
|
|
|
|
|
{ |
|
|
|
|
|
name: "id", |
|
|
|
|
|
label: t("Details"), |
|
|
|
|
|
onClick: onDetailClick, |
|
|
|
|
|
buttonIcon: <EditNote />, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "code", |
|
|
|
|
|
label: t("Code"), |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "name", |
|
|
|
|
|
label: t("Name"), |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "type", |
|
|
|
|
|
label: t("Type"), |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "status", |
|
|
|
|
|
label: t("Status"), |
|
|
|
|
|
renderCell: (item) => { |
|
|
|
|
|
const status = item.status || checkItemStatus(item); |
|
|
|
|
|
if (status === "complete") { |
|
|
|
|
|
return <Chip label={t("Complete")} color="success" size="small" />; |
|
|
|
|
|
} else { |
|
|
|
|
|
return <Chip label={t("Missing Data")} color="warning" size="small" />; |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "action", |
|
|
|
|
|
label: t(""), |
|
|
|
|
|
buttonIcon: <GridDeleteIcon />, |
|
|
|
|
|
onClick: onDeleteClick, |
|
|
|
|
|
}, |
|
|
|
|
|
], |
|
|
|
|
|
[onDeleteClick, onDetailClick, t, checkItemStatus], |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const refetchData = useCallback( |
|
|
const refetchData = useCallback( |
|
|
async (filterObj: SearchQuery) => { |
|
|
async (filterObj: SearchQuery) => { |
|
|
const authHeader = axiosInstance.defaults.headers["Authorization"]; |
|
|
const authHeader = axiosInstance.defaults.headers["Authorization"]; |
|
|
@@ -134,8 +92,6 @@ const ItemsSearch: React.FC<Props> = ({ items }) => { |
|
|
`${NEXT_PUBLIC_API_URL}/items/getRecordByPage`, |
|
|
`${NEXT_PUBLIC_API_URL}/items/getRecordByPage`, |
|
|
{ params }, |
|
|
{ params }, |
|
|
); |
|
|
); |
|
|
console.log("API Response:", response); |
|
|
|
|
|
console.log("First record keys:", response.data?.records?.[0] ? Object.keys(response.data.records[0]) : "No records"); |
|
|
|
|
|
if (response.status == 200) { |
|
|
if (response.status == 200) { |
|
|
// Normalize field names and add status to each item |
|
|
// Normalize field names and add status to each item |
|
|
const itemsWithStatus: ItemsResultWithStatus[] = response.data.records.map((item: any) => { |
|
|
const itemsWithStatus: ItemsResultWithStatus[] = response.data.records.map((item: any) => { |
|
|
@@ -150,18 +106,12 @@ const ItemsSearch: React.FC<Props> = ({ items }) => { |
|
|
qcCategory: item.qcCategory || (qcCategoryId ? { id: qcCategoryId } : undefined), |
|
|
qcCategory: item.qcCategory || (qcCategoryId ? { id: qcCategoryId } : undefined), |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
console.log("Normalized item:", { |
|
|
|
|
|
id: normalizedItem.id, |
|
|
|
|
|
LocationCode: normalizedItem.LocationCode, |
|
|
|
|
|
qcCategoryId: qcCategoryId, |
|
|
|
|
|
qcCategory: normalizedItem.qcCategory |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
...normalizedItem, |
|
|
...normalizedItem, |
|
|
status: checkItemStatus(normalizedItem), |
|
|
status: checkItemStatus(normalizedItem), |
|
|
}; |
|
|
}; |
|
|
}); |
|
|
}); |
|
|
|
|
|
console.log("Fetched items data:", itemsWithStatus); |
|
|
setFilteredItems(itemsWithStatus as ItemsResult[]); |
|
|
setFilteredItems(itemsWithStatus as ItemsResult[]); |
|
|
setTotalCount(response.data.total); |
|
|
setTotalCount(response.data.total); |
|
|
return response; // Return the data from the response |
|
|
return response; // Return the data from the response |
|
|
@@ -185,6 +135,64 @@ const ItemsSearch: React.FC<Props> = ({ items }) => { |
|
|
refetchData, |
|
|
refetchData, |
|
|
]); |
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
const onDeleteClick = useCallback( |
|
|
|
|
|
(item: ItemsResult) => { |
|
|
|
|
|
deleteDialog(async () => { |
|
|
|
|
|
if (item.id) { |
|
|
|
|
|
const itemId = typeof item.id === "string" ? parseInt(item.id, 10) : item.id; |
|
|
|
|
|
if (!isNaN(itemId)) { |
|
|
|
|
|
await deleteItem(itemId); |
|
|
|
|
|
await refetchData(filterObj); |
|
|
|
|
|
await successDialog(t("Delete Success"), t); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}, t); |
|
|
|
|
|
}, |
|
|
|
|
|
[refetchData, filterObj, t], |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const columns = useMemo<Column<ItemsResultWithStatus>[]>( |
|
|
|
|
|
() => [ |
|
|
|
|
|
{ |
|
|
|
|
|
name: "id", |
|
|
|
|
|
label: t("Details"), |
|
|
|
|
|
onClick: onDetailClick, |
|
|
|
|
|
buttonIcon: <EditNote />, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "code", |
|
|
|
|
|
label: t("Code"), |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "name", |
|
|
|
|
|
label: t("Name"), |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "type", |
|
|
|
|
|
label: t("Type"), |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "status", |
|
|
|
|
|
label: t("Status"), |
|
|
|
|
|
renderCell: (item) => { |
|
|
|
|
|
const status = item.status || checkItemStatus(item); |
|
|
|
|
|
if (status === "complete") { |
|
|
|
|
|
return <Chip label={t("Complete")} color="success" size="small" />; |
|
|
|
|
|
} else { |
|
|
|
|
|
return <Chip label={t("Missing Data")} color="warning" size="small" />; |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
name: "action", |
|
|
|
|
|
label: t(""), |
|
|
|
|
|
buttonIcon: <GridDeleteIcon />, |
|
|
|
|
|
onClick: onDeleteClick, |
|
|
|
|
|
}, |
|
|
|
|
|
], |
|
|
|
|
|
[onDeleteClick, onDetailClick, t, checkItemStatus], |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
const onReset = useCallback(() => { |
|
|
const onReset = useCallback(() => { |
|
|
setFilteredItems(items); |
|
|
setFilteredItems(items); |
|
|
}, [items]); |
|
|
}, [items]); |
|
|
|