feat: auto-complete warehouse transfer row.

This commit is contained in:
a.bouhuolia
2022-02-14 23:30:52 +02:00
parent 913245d202
commit bb56790ce9
7 changed files with 226 additions and 34 deletions

View File

@@ -0,0 +1,86 @@
// @ts-nocheck
import React from 'react';
import { useItem } from 'hooks/query';
interface IItemMeta {
rowIndex: number;
columnId: string;
itemId: number;
sourceWarehouseId: number;
distentionWarehouseId: number;
}
type CellLoading = any;
interface IWarehouseMeta {
warehouseId: number;
warehouseQuantity: number;
warehouseQuantityFormatted: string;
}
interface IRow {
rowIndex: number;
columnId: number;
itemId: number;
warehouses: IWarehouseMeta[];
}
/**
* Fetches the item warehouse quantity.
* @returns
*/
export const useFetchItemWarehouseQuantity = () => {
// Holds the table row meta of the given row index.
const [tableRow, setTableRow] = React.useState<IItemMeta | null>(null);
// Table cells loading coords.
const [cellsLoading, setCellsLoading] = React.useState<CellLoading | null>(
null,
);
// Fetches the item warehouse locations.
const {
data: item,
isLoading: isItemLoading,
isSuccess: isItemSuccess,
} = useItem(tableRow?.itemId, {
enabled: !!(tableRow && tableRow.itemId),
});
// Effects with row cells loading state.
React.useEffect(() => {
setCellsLoading(null);
if (isItemLoading && tableRow) {
setCellsLoading([
[tableRow.rowIndex, 'quantity'],
[tableRow.rowIndex, 'source_warehouse'],
[tableRow.rowIndex, 'destination_warehouse'],
]);
}
}, [isItemLoading, tableRow]);
// New table row meta.
const newRowMeta = React.useMemo(() => {
return isItemSuccess
? {
...tableRow,
warehouses: [],
}
: null;
}, [isItemSuccess, tableRow]);
// Reset the table row.
const resetTableRow = React.useCallback(() => {
setTableRow(null);
setCellsLoading(null);
}, []);
return {
setTableRow,
resetTableRow,
cellsLoading,
newRowMeta,
};
};