Skip to content

Commit

Permalink
refactor: Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mheggelund committed Dec 13, 2023
1 parent 105f2c3 commit 2f23ca6
Showing 1 changed file with 52 additions and 33 deletions.
85 changes: 52 additions & 33 deletions src/components/AreaCoordinates/AreaCoordinates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ export const AreaCoordinates = ({ modelId }: { modelId: string }) => {
setSubmitting(false);
setErrors({});

// If area dropdown is set to empty:
// Active area is set to default
// Coordinates are set to default
if (changes.selectedItems.length <= 0) {
setActiveArea(defaultArea);
setAreaCoordinate({
Expand All @@ -157,15 +160,23 @@ export const AreaCoordinates = ({ modelId }: { modelId: string }) => {
return;
}

const areasWithCoordinates =
const selectableAreas =
model.data?.data?.modelAreas && model.data?.data?.modelAreas;

const selectedArea = areasWithCoordinates?.filter(
const selectedArea = selectableAreas?.filter(
(area) => area.modelAreaType === changes.selectedItems[0].name,
);
console.log(selectedArea?.length);

// Area has no previous coordinates set
// Initialize
if (selectedArea?.length === 0) {
console.log(areaCoordinate);

if (activeArea) {
// Clear possible old states, set default coordinates
// Set Active area to the selected area
// Set new area to send a POST request
setAreaCoordinate({
modelAreaId: '',
coordinates: [
Expand All @@ -184,7 +195,11 @@ export const AreaCoordinates = ({ modelId }: { modelId: string }) => {
}
setActiveArea(changes.selectedItems[0]);
setNewArea(changes.selectedItems[0]);
} else if (selectedArea && selectedArea?.length > 0) {
}
// Model has previous coordinates
// New area is undefint, PUT request will be sendt
// Populate state with existing data
else if (selectedArea && selectedArea?.length > 0) {
setNewArea(undefined);
const newState: AreaCoordinateType = {
modelAreaId: selectedArea[0].modelAreaId,
Expand Down Expand Up @@ -283,6 +298,27 @@ export const AreaCoordinates = ({ modelId }: { modelId: string }) => {
return errors;
};

const clearAndUpdate = () => {
setActiveArea(defaultArea);
setAreaCoordinate({
modelAreaId: '',
coordinates: [
{
x: 0,
y: 0,
m: 0,
},
{
x: 0,
y: 0,
m: 1,
},
],
});
setUpdated(updated + 1);
setSaveAlert(true);
};

const postModelArea = async () => {
if (activeArea && areaCoordinate) {
const postRequestBody: AddAnalogueModelAreaCommandForm = {
Expand All @@ -296,28 +332,22 @@ export const AreaCoordinates = ({ modelId }: { modelId: string }) => {
});

if (coordinateRes.success) {
setActiveArea(defaultArea);
setAreaCoordinate({
modelAreaId: '',
coordinates: [
{
x: 0,
y: 0,
m: 0,
},
{
x: 0,
y: 0,
m: 1,
},
],
});
setUpdated(updated + 1);
setSaveAlert(true);
clearAndUpdate();
}
}
};

const putModelArea = async () => {
const coordinateRes = await putAreaCoordinates.mutateAsync({
id: modelId,
modelAreaId: areaCoordinate.modelAreaId,
requestBody: { coordinates: areaCoordinate.coordinates },
});
if (coordinateRes.success) {
clearAndUpdate();
}
};

const handleSubmit = () => {
setErrors(validateCoordinates(areaCoordinate));
setSubmitting(true);
Expand All @@ -330,18 +360,7 @@ export const AreaCoordinates = ({ modelId }: { modelId: string }) => {
if (newArea) {
postModelArea();
} else {
const coordinateRes = await putAreaCoordinates.mutateAsync({
id: modelId,
modelAreaId: areaCoordinate.modelAreaId,
requestBody: { coordinates: areaCoordinate.coordinates },
});
if (coordinateRes.success) {
setUpdated(updated + 1);
setActiveArea(defaultArea);
setAreaCoordinate(defaultState);
setSaveAlert(true);
setNewArea(undefined);
}
putModelArea();
}
};

Expand Down

0 comments on commit 2f23ca6

Please sign in to comment.