// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { InventoryPlan } from "neptune/model/inventory-plan"; import { Item } from "neptune/model/item"; import React, { useEffect, useState } from "react"; import { useEdgesState, useNodesState } from "reactflow"; import Alert from "@cloudscape-design/components/alert"; import Box from "@mui/material/Box"; import Tab from "@mui/material/Tab"; import Tabs from "@mui/material/Tabs"; import { Location } from "neptune/model/location"; import { useAppDispatch, useAppSelector } from "../app/hooks/hooks"; import { selectAllLocations, selectLocationId, setLocationId } from "../app/reducers/locationSlice"; import CRUDTable from "../components/crudTable"; import { CustomColumnDef } from "../components/customize-field-modal"; import DaysTable from "../components/daysTable"; import Dropdown from "../components/dropdown"; import InteractionFlow from "../components/interactionFlow"; import { downstream, Downstream, inventory, inventoryPlans, Locations, OptionProps, transferPlans, TransferPlans, upstream, Upstream } from "../components/makeData"; import { getCustomColumns, getCustomFields, populateInventoryPlanTable, populateItemTable, populateProjectionsTable, populateTransferPlanTable, populateUpDownStreamTable } from "../components/shared"; import { LocationType, VertexLabel } from "../constants"; /** * @returns {Box} - The tabs component */ export default function ViewTabs() { const dispatch = useAppDispatch(); const locationId = useAppSelector(selectLocationId); const apiLocations = useAppSelector(selectAllLocations); const [itemCustomCols, setItemCustomCols] = useState([]); const [locationCustomCols, setLocationCustomCols] = useState([]); const [tabValue, setTabValue] = React.useState(0); const [locationData, setLocationData] = useState([]); const [locationOptions, setLocationOptions] = useState([]); const [upstreamData, setUpstreamData] = useState(() => upstream); const [downstreamData, setDownstreamData] = useState(() => downstream); const [inventoryData, setInventoryData] = useState(() => inventory); const [plansData, setPlansData] = useState(() => inventoryPlans); const [transferPlansData, setTransferPlansData] = useState(() => transferPlans); const [items, setItems] = useState([{ label: "", value: "" }]); const [modalError, setModalError] = useState(false); const [locationModalError, setLocationModalError] = useState(false); const [modalErrorMsg, setModalErrorMsg] = useState(""); const [errorType, setErrorType] = useState(""); const [loading, setLoading] = useState(true); const [tabsGenerate, setTabsGenerate] = useState(false); const [itemId, setItemId] = useState(""); const [skuList, setSkuList] = useState([]); const [nodes, setNodes, onNodesChange] = useNodesState([]); const [edges, setEdges, onEdgesChange] = useEdgesState([]); const [selectedLocation, setSelectedLocation] = useState({ label: "", value: "" }); const [projections, setProjections] = useState[]>([]); const [currentLocation, setCurrentLocation] = useState(undefined); const [loadingOnLocationSelect, setLoadingOnLocationSelect] = useState(false); // NOSONAR: typescript:S1854 const tabChange = async function (event: React.SyntheticEvent, newValue: number) { setTabValue(newValue); switch (newValue) { case 0: await populateItemTable(itemCustomCols, setInventoryData, setSkuList, locationId, setItems); break; case 1: await populateInventoryPlanTable(setPlansData, locationId); break; case 2: await populateTransferPlanTable(setTransferPlansData, locationId); break; case 3: await populateUpDownStreamTable("downstream", setDownstreamData, locationId); break; case 4: await populateUpDownStreamTable("upstream", setUpstreamData, locationId); break; } }; const onLocationSelect = async (selectedId: string) => { setLoadingOnLocationSelect(true); dispatch(setLocationId(selectedId)); const selected = locationOptions.find((location: OptionProps) => location.value === selectedId); const currentLoc = apiLocations.find(loc => loc.id! === selectedId); const curNode = nodes.find(node => node.id === selectedLocation.value); if (curNode) { curNode.selected = false; } if (selected) { setSelectedLocation(selected); } if (currentLoc) { setCurrentLocation(currentLoc); } const newNode = nodes.find(node => node.id === selectedId); if (newNode) { newNode.selected = true; setNodes([...nodes, newNode]); } setTabsGenerate(true); setItemId(""); await populateItemTable(itemCustomCols, setInventoryData, setSkuList, selectedId, setItems); await populateInventoryPlanTable(setPlansData, selectedId); await populateTransferPlanTable(setTransferPlansData, selectedId); await populateUpDownStreamTable("downstream", setDownstreamData, selectedId); await populateUpDownStreamTable("upstream", setUpstreamData, selectedId); setLoadingOnLocationSelect(false); }; const onItemSelect = async (newValue: any) => { setItemId(newValue); populateProjectionsTable(setProjections, [], newValue); }; useEffect(() => { setLocationData( apiLocations.map((loc: Location) => { const preMappedLocation = { id: loc.id, type: loc.type, location: loc.description }; const result = getCustomFields(locationCustomCols, preMappedLocation, loc); return result; }) ); }, [apiLocations, locationCustomCols]); useEffect(() => { setLocationOptions( apiLocations.map((loc: Location) => { return { value: loc.id!, label: loc.description }; }) ); setLoading(false); }, [apiLocations]); useEffect(() => { const fetchData = async () => { await getCustomColumns(VertexLabel.ITEM, setItemCustomCols); await getCustomColumns(VertexLabel.LOCATION, setLocationCustomCols); }; fetchData(); }, []); if (loading) { return
Loading...
; } return ( {locationModalError && ( setLocationModalError(false)} dismissible statusIconAriaLabel="error" type="error" header=""> {modalErrorMsg} )} {locationData.length > 0 && ( <> )} {modalError && ( setModalError(false)} dismissible statusIconAriaLabel="error" type="error" header=""> {modalErrorMsg} )} {tabsGenerate && ( <> { } {tabValue === 0 && ( )} {tabValue === 1 && ( null} tableData={plansData} setTableData={setPlansData} dataType={"inventory-plan"} currentLocationId={locationId} skuList={skuList} setModalError={setModalError} modalErrorMsg={modalErrorMsg} setModalErrorMsg={setModalErrorMsg} selectedLocation={currentLocation} /> )} {tabValue === 2 && ( null} tableData={transferPlansData} setTableData={setTransferPlansData} dataType={"transfer-plan"} locations={locationOptions} currentLocationId={locationId} skuList={skuList} setModalError={setModalError} modalErrorMsg={modalErrorMsg} setModalErrorMsg={setModalErrorMsg} setErrorType={setErrorType} errorType={errorType} enableEditing={false} /> )} {tabValue === 3 && ( null} tableData={downstreamData} setTableData={setDownstreamData} dataType={"downstream"} currentLocationId={locationId} locations={locationOptions} setModalError={setModalError} modalErrorMsg={modalErrorMsg} setModalErrorMsg={setModalErrorMsg} setNodes={setNodes} setEdges={setEdges} /> )} {tabValue === 4 && ( null} tableData={upstreamData} setTableData={setUpstreamData} dataType={"upstream"} currentLocationId={locationId} locations={locationOptions} setModalError={setModalError} modalErrorMsg={modalErrorMsg} setModalErrorMsg={setModalErrorMsg} setNodes={setNodes} setEdges={setEdges} /> )} )} {locationId.length > 0 && itemId.length > 0 && ( )} ); }