|
| 1 | +import React from 'react'; |
| 2 | +import { Typography } from '@mui/material'; |
| 3 | +import { DataGrid, GridColDef } from '@mui/x-data-grid'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import { ProjectPreview } from 'shared'; |
| 6 | +import LoadingIndicator from '../../../../../components/LoadingIndicator'; |
| 7 | +import { useGetAssembliesForWbsElement, useGetMaterialsForWbsElement } from '../../../../../hooks/bom.hooks'; |
| 8 | +import ErrorPage from '../../../../ErrorPage'; |
| 9 | + |
| 10 | +interface CopyBOMProjectSectionProps { |
| 11 | + selectedProject: ProjectPreview; |
| 12 | + onSelectionChange: (materialIds: string[]) => void; |
| 13 | +} |
| 14 | + |
| 15 | +const columns: GridColDef[] = [ |
| 16 | + { field: 'materialName', headerName: 'Material Name', flex: 1 }, |
| 17 | + { field: 'manufacturer', headerName: 'Manufacturer', flex: 1 }, |
| 18 | + { field: 'materialType', headerName: 'Material Type', flex: 1 }, |
| 19 | + { field: 'assembly', headerName: 'Assembly Name', flex: 1 } |
| 20 | +]; |
| 21 | + |
| 22 | +const CopyBOMProjectSection: React.FC<CopyBOMProjectSectionProps> = ({ selectedProject, onSelectionChange }) => { |
| 23 | + const [selectedMaterialIds, setSelectedMaterialIds] = useState<string[]>([]); |
| 24 | + const { |
| 25 | + data: materials, |
| 26 | + isLoading: isLoadingMaterials, |
| 27 | + isError: isErrorMaterials, |
| 28 | + error: materialsError |
| 29 | + } = useGetMaterialsForWbsElement(selectedProject.wbsNum); |
| 30 | + |
| 31 | + const { |
| 32 | + data: assemblies, |
| 33 | + isLoading: isLoadingAssemblies, |
| 34 | + isError: isErrorAssemblies, |
| 35 | + error: assembliesError |
| 36 | + } = useGetAssembliesForWbsElement(selectedProject.wbsNum); |
| 37 | + |
| 38 | + React.useEffect(() => { |
| 39 | + if (materials) { |
| 40 | + const allIds = materials.map((m) => m.materialId); |
| 41 | + setSelectedMaterialIds(allIds); |
| 42 | + onSelectionChange(allIds); |
| 43 | + } |
| 44 | + }, [materials, onSelectionChange]); |
| 45 | + |
| 46 | + if (isLoadingMaterials || isLoadingAssemblies || !materials || !assemblies) return <LoadingIndicator />; |
| 47 | + if (isErrorMaterials) return <ErrorPage message={materialsError?.message} />; |
| 48 | + if (isErrorAssemblies) return <ErrorPage message={assembliesError?.message} />; |
| 49 | + |
| 50 | + const rows = materials.map((m) => ({ |
| 51 | + id: m.materialId, |
| 52 | + materialName: m.name, |
| 53 | + manufacturer: m.manufacturer?.name ?? '-', |
| 54 | + materialType: m.materialType.name, |
| 55 | + assembly: assemblies.find((a) => a.assemblyId === m.assemblyId)?.name ?? '-' |
| 56 | + })); |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + <Typography sx={{ mb: 1 }} variant="body2"> |
| 61 | + {selectedMaterialIds.length} material{selectedMaterialIds.length !== 1 ? 's' : ''} selected |
| 62 | + </Typography> |
| 63 | + <DataGrid |
| 64 | + rows={rows} |
| 65 | + columns={columns} |
| 66 | + checkboxSelection |
| 67 | + autoHeight |
| 68 | + selectionModel={selectedMaterialIds} |
| 69 | + onSelectionModelChange={(newModel) => { |
| 70 | + const ids = newModel as string[]; |
| 71 | + setSelectedMaterialIds(ids); |
| 72 | + onSelectionChange(ids); |
| 73 | + }} |
| 74 | + rowsPerPageOptions={[100]} |
| 75 | + hideFooterPagination |
| 76 | + sx={{ |
| 77 | + '& .MuiDataGrid-columnHeaders': { backgroundColor: '#ef4345', color: 'white' }, |
| 78 | + '& .MuiDataGrid-columnHeaders .MuiCheckbox-root': { color: 'white' } |
| 79 | + }} |
| 80 | + /> |
| 81 | + </> |
| 82 | + ); |
| 83 | +}; |
| 84 | + |
| 85 | +export default CopyBOMProjectSection; |
0 commit comments