|
|
@@ -3,7 +3,7 @@ import { InventoryLotLineResult, InventoryResult } from "@/app/api/inventory"; |
|
|
|
import { useTranslation } from "react-i18next"; |
|
|
|
import SearchBox, { Criterion } from "../SearchBox"; |
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react"; |
|
|
|
import { orderBy, uniq, uniqBy } from "lodash"; |
|
|
|
import { isEqual, orderBy, uniq, uniqBy } from "lodash"; |
|
|
|
import SearchResults, { Column } from "../SearchResults"; |
|
|
|
import { CheckCircleOutline, DoDisturb } from "@mui/icons-material"; |
|
|
|
import InventoryTable from "./InventoryTable"; |
|
|
@@ -39,14 +39,14 @@ const InventorySearch: React.FC<Props> = ({ inventories }) => { |
|
|
|
const [filteredInventories, setFilteredInventories] = useState<InventoryResult[]>([]); |
|
|
|
const [inventoriesPagingController, setInventoriesPagingController] = useState(defaultPagingController) |
|
|
|
const [inventoriesTotalCount, setInventoriesTotalCount] = useState(0) |
|
|
|
const [item, setItem] = useState<InventoryResult | null>(null) |
|
|
|
const [selectedInventory, setSelectedInventory] = useState<InventoryResult | null>(null) |
|
|
|
|
|
|
|
// Inventory Lot Line |
|
|
|
const [filteredInventoryLotLines, setFilteredInventoryLotLines] = useState<InventoryLotLineResult[]>([]); |
|
|
|
const [inventoryLotLinesPagingController, setInventoryLotLinesPagingController] = useState(defaultPagingController) |
|
|
|
const [inventoryLotLinesTotalCount, setInventoryLotLinesTotalCount] = useState(0) |
|
|
|
|
|
|
|
const [inputs, setInputs] = useState<Record<SearchParamNames, string>>({ |
|
|
|
const defaultInputs = useMemo(() => ({ |
|
|
|
itemId: "", |
|
|
|
itemCode: "", |
|
|
|
itemName: "", |
|
|
@@ -58,7 +58,8 @@ const InventorySearch: React.FC<Props> = ({ inventories }) => { |
|
|
|
currencyName: "", |
|
|
|
status: "", |
|
|
|
baseUom: "", |
|
|
|
}); |
|
|
|
}), []) |
|
|
|
const [inputs, setInputs] = useState<Record<SearchParamNames, string>>(defaultInputs); |
|
|
|
|
|
|
|
const searchCriteria: Criterion<SearchParamNames>[] = useMemo( |
|
|
|
() => [ |
|
|
@@ -80,24 +81,33 @@ const InventorySearch: React.FC<Props> = ({ inventories }) => { |
|
|
|
[t], |
|
|
|
); |
|
|
|
|
|
|
|
// Inventory |
|
|
|
const refetchInventoryData = useCallback(async ( |
|
|
|
query: Record<SearchParamNames, string>, |
|
|
|
actionType: "reset" | "search" | "paging" |
|
|
|
actionType: "reset" | "search" | "paging" | "init", |
|
|
|
pagingController: typeof defaultPagingController, |
|
|
|
) => { |
|
|
|
console.log("%c Action Type 1.", "color:red", actionType) |
|
|
|
// Avoid loading data again |
|
|
|
if (actionType === "paging" && pagingController === defaultPagingController) { |
|
|
|
return |
|
|
|
} |
|
|
|
console.log("%c Action Type 2.", "color:blue", actionType) |
|
|
|
|
|
|
|
const params: SearchInventory = { |
|
|
|
code: query?.itemCode ?? '', |
|
|
|
name: query?.itemName ?? '', |
|
|
|
type: query?.itemType.toLowerCase() === "all" ? '' : query?.itemType ?? '', |
|
|
|
pageNum: inventoriesPagingController.pageNum - 1, |
|
|
|
pageSize: inventoriesPagingController.pageSize |
|
|
|
pageNum: pagingController.pageNum - 1, |
|
|
|
pageSize: pagingController.pageSize |
|
|
|
} |
|
|
|
|
|
|
|
const response = await fetchInventories(params) |
|
|
|
|
|
|
|
if (response) { |
|
|
|
console.log(response) |
|
|
|
setInventoriesTotalCount(response.total); |
|
|
|
switch (actionType) { |
|
|
|
case "init": |
|
|
|
case "reset": |
|
|
|
case "search": |
|
|
|
setFilteredInventories(() => response.records); |
|
|
@@ -110,31 +120,43 @@ const InventorySearch: React.FC<Props> = ({ inventories }) => { |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
}, [inventoriesPagingController, setInventoriesPagingController]) |
|
|
|
}, []) |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
refetchInventoryData(inputs, "paging") |
|
|
|
refetchInventoryData(defaultInputs, "init", defaultPagingController) |
|
|
|
}, []) |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
// if (!isEqual(inventoriesPagingController, defaultPagingController)) { |
|
|
|
refetchInventoryData(inputs, "paging", inventoriesPagingController) |
|
|
|
// } |
|
|
|
}, [inventoriesPagingController]) |
|
|
|
|
|
|
|
// Inventory Lot Line |
|
|
|
const refetchInventoryLotLineData = useCallback(async ( |
|
|
|
itemId: number | null, |
|
|
|
actionType: "reset" | "search" | "paging" |
|
|
|
actionType: "reset" | "search" | "paging", |
|
|
|
pagingController: typeof defaultPagingController, |
|
|
|
) => { |
|
|
|
if (!itemId) { |
|
|
|
setItem(null) |
|
|
|
setSelectedInventory(null) |
|
|
|
setInventoryLotLinesTotalCount(0); |
|
|
|
setFilteredInventoryLotLines([]) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Avoid loading data again |
|
|
|
if (actionType === "paging" && pagingController === defaultPagingController) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
const params: SearchInventoryLotLine = { |
|
|
|
itemId: itemId, |
|
|
|
pageNum: inventoriesPagingController.pageNum - 1, |
|
|
|
pageSize: inventoriesPagingController.pageSize |
|
|
|
pageNum: pagingController.pageNum - 1, |
|
|
|
pageSize: pagingController.pageSize |
|
|
|
} |
|
|
|
|
|
|
|
const response = await fetchInventoryLotLines(params) |
|
|
|
|
|
|
|
if (response) { |
|
|
|
setInventoryLotLinesTotalCount(response.total); |
|
|
|
switch (actionType) { |
|
|
@@ -144,38 +166,61 @@ const InventorySearch: React.FC<Props> = ({ inventories }) => { |
|
|
|
break; |
|
|
|
case "paging": |
|
|
|
setFilteredInventoryLotLines((fi) => |
|
|
|
orderBy( |
|
|
|
// orderBy( |
|
|
|
uniqBy([...fi, ...response.records], "id"), |
|
|
|
["id"], ["desc"] |
|
|
|
)); |
|
|
|
// ["id"], ["desc"]) |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
}, [inventoriesPagingController, setInventoriesPagingController]) |
|
|
|
}, []) |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
refetchInventoryData(inputs, "paging") |
|
|
|
}, [inventoriesPagingController]) |
|
|
|
// if (!isEqual(inventoryLotLinesPagingController, defaultPagingController)) { |
|
|
|
refetchInventoryLotLineData(selectedInventory?.itemId ?? null, "paging", inventoryLotLinesPagingController) |
|
|
|
// } |
|
|
|
}, [inventoryLotLinesPagingController]) |
|
|
|
|
|
|
|
// Reset |
|
|
|
const onReset = useCallback(() => { |
|
|
|
refetchInventoryData(inputs, "reset"); |
|
|
|
refetchInventoryLotLineData(null, "reset"); |
|
|
|
refetchInventoryData(defaultInputs, "reset", defaultPagingController); |
|
|
|
refetchInventoryLotLineData(null, "reset", defaultPagingController); |
|
|
|
// setFilteredInventories(inventories); |
|
|
|
|
|
|
|
setInputs(() => defaultInputs) |
|
|
|
setInventoriesPagingController(() => defaultPagingController) |
|
|
|
setInventoryLotLinesPagingController(() => defaultPagingController) |
|
|
|
}, []); |
|
|
|
|
|
|
|
// Click Row |
|
|
|
const onInventoryRowClick = useCallback((item: InventoryResult) => { |
|
|
|
setItem(item) |
|
|
|
refetchInventoryLotLineData(item.itemId, "search") |
|
|
|
refetchInventoryLotLineData(item.itemId, "search", defaultPagingController) |
|
|
|
|
|
|
|
setSelectedInventory(item) |
|
|
|
setInventoryLotLinesPagingController(() => defaultPagingController) |
|
|
|
}, []) |
|
|
|
|
|
|
|
// On Search |
|
|
|
const onSearch = useCallback((query: Record<SearchParamNames, string>) => { |
|
|
|
refetchInventoryData(query, "search", defaultPagingController) |
|
|
|
refetchInventoryLotLineData(null, "search", defaultPagingController); |
|
|
|
|
|
|
|
setInputs(() => query) |
|
|
|
setInventoriesPagingController(() => defaultPagingController) |
|
|
|
setInventoryLotLinesPagingController(() => defaultPagingController) |
|
|
|
}, [refetchInventoryData]) |
|
|
|
|
|
|
|
console.log("", "color: #666", inventoriesPagingController) |
|
|
|
|
|
|
|
return ( |
|
|
|
<> |
|
|
|
<SearchBox |
|
|
|
criteria={searchCriteria} |
|
|
|
onSearch={(query) => { |
|
|
|
onSearch(query) |
|
|
|
// console.log(query) |
|
|
|
// console.log(inventories) |
|
|
|
setInputs(() => query) |
|
|
|
refetchInventoryData(query, "search") |
|
|
|
// setInputs(() => query) |
|
|
|
// refetchInventoryData(query, "search", defaultPagingController) |
|
|
|
// setFilteredInventories( |
|
|
|
// inventories.filter( |
|
|
|
// (i) => |
|
|
@@ -202,7 +247,7 @@ const InventorySearch: React.FC<Props> = ({ inventories }) => { |
|
|
|
pagingController={inventoryLotLinesPagingController} |
|
|
|
setPagingController={setInventoryLotLinesPagingController} |
|
|
|
totalCount={inventoryLotLinesTotalCount} |
|
|
|
item={item} |
|
|
|
inventory={selectedInventory} |
|
|
|
/> |
|
|
|
</> |
|
|
|
); |
|
|
|