Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 19x 19x 19x 19x 19x 19x 19x 19x 2x 19x 5x 5x 5x 1x 4x 4x 5x 19x 7x 6x 19x 3x 3x 3x 3x 3x 3x 3x 3x 19x 1x 1x 19x 9x 9x 9x 9x 19x 18x 19x 18x 19x 18x 2x 2x | /*---------------------------------------------------------------------------------------------
* 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<EC3ReportConfigurationLabel[] | undefined>(props.template.labels);
const [reportTables, setReportTables] = useState<string[] | undefined>(undefined);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [oDataTable, setoDataTable] = useState<ODataTable[]>([]);
const [editableAssemblyIndex, setEditableAssemblyIndex] = useState<number>();
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 Iif (action === "delete") {
newArray.splice(index, 1);
} else {
newArray.splice(index, 1, updatedAssembly);
}
setAllAssemblies(newArray);
} else E{
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 {
Iif (!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<HTMLButtonElement, MouseEvent>) => {
setEditableAssemblyIndex(index);
e.stopPropagation();
},
[setEditableAssemblyIndex],
);
const onAssemblyDelete = useCallback(
(assembly: EC3ReportConfigurationLabel, index: number) => (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
e.stopPropagation();
onAssemblyDataChange(assembly, index, "delete");
},
[onAssemblyDataChange],
);
return (
<div className="ec3w-create-template-step-two">
<div className="ec3w-assembly-list">
{allAssemblies &&
allAssemblies.length > 0 &&
allAssemblies?.map((assembly, i) => {
return (
<ExpandableBlock
className="ec3w-assembly-expandable-block"
title={`${assembly.name}`}
endIcon={
<ButtonGroup>
<IconButton key={`edit-assembly${i}`} styleType="borderless" onClick={onAssemblyEdit(i)}>
<SvgEdit />
</IconButton>
<IconButton
key={`delete-assembly${i}`}
disabled={allAssemblies.length === 1}
styleType="borderless"
onClick={onAssemblyDelete(assembly, i)}
>
<SvgDelete />
</IconButton>
</ButtonGroup>
}
key={i}
isExpanded={editableAssemblyIndex ? i === editableAssemblyIndex : i === 0}
>
<AssemblyItem
assembly={assembly}
allAssemblies={allAssemblies}
currentAssemblyIndex={i}
editableAssemblyIndex={editableAssemblyIndex}
oDataTable={oDataTable}
reportTables={reportTables}
isLoading={isLoading}
onAssemblyDataChange={onAssemblyDataChange}
setTemplate={props.setTemplate}
template={props.template}
/>
</ExpandableBlock>
);
})}
<div className="ec3w-button-row-above-stepper">
<Button
className="add-new-button"
styleType="borderless"
title="Add new"
disabled={allAssemblies?.length === reportTables?.length}
startIcon={<SvgAdd />}
onClick={() => {
// add new empty item to the assemblies array
addNewEmptyAssembly();
}}
>
{EC3Widget.translate("addNewAssembly")}
</Button>
</div>
</div>
<div className="ec3w-stepper-footer">
<Button
onClick={() => {
props.updateCurrentStep(0);
props.setTemplate({ ...props.template, labels: allAssemblies ?? [] });
}}
className="ec3w-footer-button"
>
{EC3Widget.translate("backButton")}
</Button>
<Button
styleType="high-visibility"
className="ec3w-footer-button"
data-testid="ec3-step-two-next-button"
disabled={isNextDisabled}
onClick={() => {
// send back updated assemblies to the parent
props.setTemplate({ ...props.template, labels: allAssemblies ?? [] });
props.updateCurrentStep(2);
}}
>
{EC3Widget.translate("nextButton")}
</Button>
<Button onClick={props.onCancelClick}> {EC3Widget.translate("cancelButton")}</Button>
</div>
</div>
);
};
|