/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ import { Button, ButtonGroup, ExpandableBlock, IconButton } from "@itwin/itwinui-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import type { Configuration } from "./EC3/Template"; import React from "react"; import type { EC3ReportConfigurationLabel, ODataTable, Report } from "@itwin/insights-client"; import "./TemplateModificationStepTwo.scss"; import { SvgAdd, SvgDelete, SvgEdit } from "@itwin/itwinui-icons-react"; import { useApiContext } from "./context/APIContext"; import { AssemblyItem } from "./AssemblyItem"; import { EC3Widget } from "../EC3Widget"; export interface TemplateModificationStepTwoProps { template: Configuration; updateCurrentStep: (currentStep: number) => void; onCancelClick?: () => void; setTemplate: (template: Configuration) => void; fetchedReports?: Report[]; } export enum AssemblyCreationDropdownType { material, elementName, elementQuantity, } export const TemplateModificationStepTwo = (props: TemplateModificationStepTwoProps) => { const [allAssemblies, setAllAssemblies] = useState(props.template.labels); const [reportTables, setReportTables] = useState(undefined); const [isLoading, setIsLoading] = useState(false); const [oDataTable, setoDataTable] = useState([]); const [editableAssemblyIndex, setEditableAssemblyIndex] = useState(); const oDataClient = useApiContext().oDataClient; const { config: { getAccessToken }, } = useApiContext(); useEffect(() => { setAllAssemblies(props.template.labels); }, [props.template.labels]); const onAssemblyDataChange = useCallback( (updatedAssembly: EC3ReportConfigurationLabel, index: number, action?: "add" | "delete") => { if (allAssemblies) { const newArray = [...allAssemblies]; if (action === "add") { newArray.splice(0, 0, updatedAssembly); } else if (action === "delete") { newArray.splice(index, 1); } else { newArray.splice(index, 1, updatedAssembly); } setAllAssemblies(newArray); } else { setAllAssemblies([updatedAssembly]); } }, [allAssemblies], ); const isNextDisabled = useMemo( () => !allAssemblies || allAssemblies.length === 0 || allAssemblies.some( (assembly) => !assembly.name || !assembly.elementNameColumn || !assembly.elementQuantityColumn || assembly.materials.length === 0 || !assembly.reportTable, ), [allAssemblies], ); const initReportTableSelection = useCallback(async () => { try { if (!props.template.reportId) throw new Error("Invalid report."); const token = await getAccessToken(); setIsLoading(true); const reportMetadataResponse = await oDataClient.getODataReportMetadata(token, props.template.reportId); setoDataTable(reportMetadataResponse); setReportTables(reportMetadataResponse.map((d) => d.name ?? "")); setIsLoading(false); } catch (e) { setIsLoading(false); throw new Error("Could not get Odata Tables"); } }, [getAccessToken, oDataClient, props.template.reportId]); const addNewEmptyAssembly = useCallback(() => { setEditableAssemblyIndex(0); onAssemblyDataChange( { name: "", elementNameColumn: "UserLabel", elementQuantityColumn: "", materials: [], reportTable: "", }, 0, "add", ); }, [onAssemblyDataChange]); useEffect(() => { const init = async () => { if (allAssemblies === undefined || allAssemblies.length === 0) addNewEmptyAssembly(); if (oDataTable === undefined || oDataTable.length === 0) await initReportTableSelection(); }; init(); // eslint-disable-line @typescript-eslint/no-floating-promises }, [addNewEmptyAssembly, allAssemblies, initReportTableSelection, oDataTable]); const onAssemblyEdit = useCallback( (index: number) => (e: React.MouseEvent) => { setEditableAssemblyIndex(index); e.stopPropagation(); }, [setEditableAssemblyIndex], ); const onAssemblyDelete = useCallback( (assembly: EC3ReportConfigurationLabel, index: number) => (e: React.MouseEvent) => { e.stopPropagation(); onAssemblyDataChange(assembly, index, "delete"); }, [onAssemblyDataChange], ); return (
{allAssemblies && allAssemblies.length > 0 && allAssemblies?.map((assembly, i) => { return ( } key={i} isExpanded={editableAssemblyIndex ? i === editableAssemblyIndex : i === 0} > ); })}
); };