Skip to content

Commit e9f2a13

Browse files
collins-selfcollins-self
authored andcommitted
fix: update LocationDrawingsButton to use location IDs for error handling
1 parent f726153 commit e9f2a13

1 file changed

Lines changed: 31 additions & 30 deletions

File tree

Source/Applications/SystemCenter/wwwroot/Scripts/TSX/SystemCenter/CommonComponents/LocationDrawingsButton.tsx

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ const LocationDrawingsButton: React.FC<LocationDrawingsButtonProps> = (props) =>
1414
const [selectedLocation, setSelectedLocation] = React.useState<OpenXDA.Types.Location>();
1515
const [multipleLocations, setMultipleLocations] = React.useState<boolean>(false);
1616
const [showDrawingsModal, setShowDrawingsModal] = React.useState<boolean>(false);
17-
const [locationsWithErrors, setLocationsWithErrors] = React.useState<Map<OpenXDA.Types.Location, string[]>>(new Map())
17+
const [locationsWithErrors, setLocationsWithErrors] = React.useState<Map<number, string[]>>(new Map())
1818
const LocationDrawingController = new GenericController(`${homePath}api/LocationDrawing`, "Name", true);
1919

20-
const isValid = (location, drawingData) => {
20+
const isValid = (location: OpenXDA.Types.Location, drawingData) => {
2121
let e = [];
2222

23-
if (location == undefined
23+
if (!location
2424
|| (location.Alias == ""
2525
&& location.Description == ""
2626
&& location.ID == 0
@@ -36,9 +36,9 @@ const LocationDrawingsButton: React.FC<LocationDrawingsButtonProps> = (props) =>
3636

3737
React.useEffect(() => { // Generates the map of errors for each location
3838
if (props.Locations.length > 1) setMultipleLocations(true);
39-
else setMultipleLocations(false); // TODO: check for undefined location isValid is not doing it
39+
else setMultipleLocations(false);
4040
for (const location of props.Locations) {
41-
if (location?.ID) {
41+
if (location) {
4242
setPageState('loading');
4343
LocationDrawingController.PagedSearch([], 'Name', true, 1, location.ID)
4444
.done((result) => {
@@ -49,38 +49,39 @@ const LocationDrawingsButton: React.FC<LocationDrawingsButtonProps> = (props) =>
4949
.fail(() => setPageState('error'));
5050
}
5151
}
52-
}, [props.Locations]);
52+
}, [props.Locations]); // ? Calls too frequently
5353

54-
const handleAddLocationError = (locMap: Map<OpenXDA.Types.Location, string[]>) => {
54+
const handleAddLocationError = (locMap: Map<number, string[]>) => {
5555
setLocationsWithErrors(prev => {
5656
const newMap = new Map(prev);
57-
locMap.forEach((errors, loc) => {
58-
if (newMap.has(loc)) {
59-
const existingErrors = newMap.get(loc);
60-
newMap.set(loc, Array.from(new Set([...existingErrors, ...errors])));
61-
} else {
62-
newMap.set(loc, errors);
57+
locMap.forEach((errors, locID) => {
58+
if (newMap.has(locID)) { // If the location already has errors
59+
const existingErrors = newMap.get(locID);
60+
newMap.set(locID, Array.from(new Set([...existingErrors, ...errors]))); // supresses duplicate values
61+
} else { // otherwise add new
62+
newMap.set(locID, errors);
6363
}
6464
});
6565
return newMap;
6666
});
6767
}
6868

69-
const handleRemoveLocationError = (loc: OpenXDA.Types.Location) => {
69+
const handleRemoveLocationError = (locID: number) => {
7070
setLocationsWithErrors(prev => {
7171
const newMap = new Map(prev);
72-
newMap.delete(loc);
72+
newMap.delete(locID);
7373
return newMap;
7474
});
7575
}
7676

7777
const updateLocationErrors = (loc: OpenXDA.Types.Location, errors: string[]) => {
78-
if (errors.length > 0 && loc != undefined) {
79-
const locationErrorsMap = new Map<OpenXDA.Types.Location, string[]>();
80-
locationErrorsMap.set(loc, errors);
78+
if (locationsWithErrors.has(loc?.ID) // Remove if the location has
79+
&& locationsWithErrors.get(loc?.ID).length > errors.length) {// fewer errors than before
80+
handleRemoveLocationError(loc.ID);
81+
} else if (errors.length > 0) {
82+
const locationErrorsMap = new Map<number, string[]>();
83+
locationErrorsMap.set(loc?.ID, errors);
8184
handleAddLocationError(locationErrorsMap);
82-
} else {
83-
handleRemoveLocationError(loc);
8485
}
8586
}
8687

@@ -97,39 +98,39 @@ const LocationDrawingsButton: React.FC<LocationDrawingsButtonProps> = (props) =>
9798
{!multipleLocations
9899
? <>
99100
<button
100-
className={locationsWithErrors.size > 0 ? "btn btn-primary disabled" : "btn btn-primary"}
101-
onClick={() => locationsWithErrors.size > 0 ? null : setShowDrawingsModal(true)}
101+
className={locationsWithErrors.has(props.Locations[0]?.ID) ? "btn btn-primary disabled" : "btn btn-primary"}
102+
onClick={() => locationsWithErrors.has(props.Locations[0]?.ID) ? null : handleShowDrawingsModal(props.Locations[0])}
102103
data-tooltip={"DrawingsModal"}
103104
onMouseEnter={() => setHover('drawings')}
104105
onMouseLeave={() => setHover('none')}
105106
>Open {props.Locations[0]?.Name} Drawings
106107
</button>
107108
<ToolTip
108-
Show={locationsWithErrors.size > 0 && hover === 'drawings'}
109+
Show={locationsWithErrors.has(props.Locations[0]?.ID) && hover === 'drawings'}
109110
Theme={'dark'}
110111
Position={'top'}
111112
Target={"DrawingsModal"}
112113
Zindex={9999}
113-
> {locationsWithErrors.get(props.Locations[0])?.map((e, i) => <p key={i}>{CrossMark} {e}</p>)}
114+
> {locationsWithErrors.get(props.Locations[0]?.ID)?.map((e, i) => <p key={i}>{CrossMark} {e}</p>)}
114115
</ToolTip>
115116
</>
116117
: <BtnDropdown
117118
Label={'Open ' + props.Locations[0]?.Name + ' Drawings'}
118119
Callback={() => handleShowDrawingsModal(props.Locations[0])}
119120
TooltipContent={
120-
<>{locationsWithErrors.get(props.Locations[0])?.map((e, i) => <p key={i}>{CrossMark} {e}</p>)}</>
121+
<>{locationsWithErrors.get(props.Locations[0]?.ID)?.map((e, i) => <p key={i}>{CrossMark} {e}</p>)}</>
121122
}
122-
ShowToolTip={locationsWithErrors.has(props.Locations[0])}
123-
Disabled={locationsWithErrors.has(props.Locations[0])}
123+
ShowToolTip={locationsWithErrors.has(props.Locations[0]?.ID)}
124+
Disabled={locationsWithErrors.has(props.Locations[0]?.ID)}
124125
BtnClass={'btn-primary'}
125126
Options={props.Locations.slice(1).map((loc, i) => ({
126127
Label: 'Open ' + loc?.Name + ' Drawings',
127128
Callback: () => handleShowDrawingsModal(loc),
128-
Disabled: locationsWithErrors.has(loc),
129+
Disabled: locationsWithErrors.has(loc?.ID),
129130
ToolTipContent: <>{
130-
locationsWithErrors.get(loc)?.map((e, i) => <p key={i}>{CrossMark} {e}</p>)
131+
locationsWithErrors.get(loc?.ID)?.map((e, i) => <p key={i}>{CrossMark} {e}</p>)
131132
}</>,
132-
ShowToolTip: locationsWithErrors.has(loc),
133+
ShowToolTip: locationsWithErrors.has(loc?.ID),
133134
ToolTipLocation: "left",
134135
Key: i
135136
}))}

0 commit comments

Comments
 (0)