Skip to content

Commit

Permalink
Hotfix mesh map empty objects (#450)
Browse files Browse the repository at this point in the history
* chore(mesh-wide): fix set ref state for new links

* chore(mesh-wide): fix reference state is not set

* chore(mesh-wide): wrap links and coordinates on a try catch

* chore(mesh-wide): fix links inside links

* chore(mesh-wide): prevent deleted node on reference

* chore(mesh-wide): fix new link is down

* chore(mesh-wide): prevent FIXME coords
  • Loading branch information
selankon authored Sep 17, 2024
1 parent 75c87fc commit 3aaab3e
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ const SelectedLink = ({
);
};

const LinkFeatureDetail = ({ actual, reference }: LinkMapFeature) => {
const linkToShow = reference ?? actual;
const LinkFeatureDetail = ({ linkToShow, actual }: LinkMapFeature) => {
// const linkToShow = reference ?? actual;
const [selectedLink, setSelectedLink] = useState(0);
const { errors } = usePointToPointErrors({
id: linkToShow.id,
Expand Down Expand Up @@ -224,12 +224,15 @@ const LinkFeatureDetail = ({ actual, reference }: LinkMapFeature) => {
);
};

export const LinkReferenceStatus = ({ reference }: LinkMapFeature) => {
export const LinkReferenceStatus = ({
linkToShow,
reference,
}: LinkMapFeature) => {
const isNewLink = !reference;

const { errors } = usePointToPointErrors({
id: reference.id,
type: reference.type,
id: linkToShow.id,
type: linkToShow.type,
});

const {
Expand All @@ -240,7 +243,7 @@ export const LinkReferenceStatus = ({ reference }: LinkMapFeature) => {

// Check if there are errors of global reference state to shown
const { reference: fetchDataReference } = getQueryByLinkType(
reference.type
linkToShow.type
);
const { data: referenceData, isError: isReferenceError } =
fetchDataReference({});
Expand All @@ -263,83 +266,72 @@ export const LinkReferenceStatus = ({ reference }: LinkMapFeature) => {
};
if (!allNodes) return {};
// Then reduce the nodes to update
return reference.nodes.reduce((acc, node) => {
return linkToShow.nodes.reduce((acc, node) => {
// If the node with node name exist get the ipv4 and hostname
if (allNodes[node]) {
acc[allNodes[node].ipv4] = allNodes[node].hostname;
}
return acc;
}, {});
}, [meshWideNodesReference, meshWideNodesActual, reference.nodes]);
}, [meshWideNodesReference, meshWideNodesActual, linkToShow.nodes]);

// Mutation to update the reference state
const { callMutations } = useSetLinkReferenceState({
linkType: reference.type,
linkToUpdate: reference,
linkType: linkToShow.type,
linkToUpdate: linkToShow,
isDown,
isNewLink,
nodesToUpdate,
});

// Show confirmation modal before run mutations
const setReferenceState = useCallback(async () => {
confirmModal(
reference.type,
Object.values(nodesToUpdate),
isDown,
async () => {
try {
const res = await callMutations();
if (res.errors.length) {
console.log("Errors");
throw new Error("Error setting new reference state!");
}
showToast({
text: <Trans>New reference state set!</Trans>,
});
} catch (error) {
showToast({
text: <Trans>Error setting new reference state!</Trans>,
});
} finally {
closeModal();
}
try {
const res = await callMutations();
if (res.errors.length) {
console.log("Errors");
throw new Error("Error setting new reference state!");
}
);
}, [
callMutations,
closeModal,
confirmModal,
isDown,
nodesToUpdate,
reference.type,
showToast,
]);
showToast({
text: <Trans>New reference state set!</Trans>,
});
} catch (error) {
showToast({
text: <Trans>Error setting new reference state!</Trans>,
});
} finally {
closeModal();
}
}, [callMutations, closeModal, showToast]);

let btnText = (
<Trans>
Set reference state for this
<br /> {dataTypeNameMapping(reference.type)}
<br /> {dataTypeNameMapping(linkToShow.type)}
</Trans>
);
if (isDown) {
// If is down and not a new link.
// Could happen that one of the links is not on the state yet and the other is not
if (isDown && !isNewLink) {
btnText = (
<Trans>
Delete this {dataTypeNameMapping(reference.type)}
Delete this {dataTypeNameMapping(linkToShow.type)}
<br />
from reference state
</Trans>
);
}

let errorMessage = <Trans>Same status as in the reference state</Trans>;
if (referenceError) {
errorMessage = <Trans>Reference is not set or has errors</Trans>;
} else if (errors?.hasErrors) {
errorMessage = <Trans>This link has errors</Trans>;
} else if (isNewLink) {

if (isNewLink) {
errorMessage = (
<Trans>This Link is not registered on the reference state</Trans>
);
} else if (referenceError) {
errorMessage = <Trans>Reference is not set or has errors</Trans>;
} else if (errors?.hasErrors) {
errorMessage = <Trans>This link has errors</Trans>;
}

const hasError = errors?.hasErrors || referenceError;
Expand All @@ -350,7 +342,14 @@ export const LinkReferenceStatus = ({ reference }: LinkMapFeature) => {
<StatusAndButton
isError={hasError}
btn={showSetReferenceButton && btnText}
onClick={setReferenceState}
onClick={() =>
confirmModal({
dataType: linkToShow.type,
nodes: Object.values(nodesToUpdate),
isDown,
cb: setReferenceState,
})
}
>
{errorMessage}
</StatusAndButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export const LinkLine = ({ referenceLink, actualLink }: ILinkLineProps) => {
const _setSelectedFeature = () => {
setSelectedMapFeature({
id: linkToShow.id,
feature: { reference: linkToShow, actual: actualLink },
feature: {
reference: referenceLink,
actual: actualLink,
linkToShow,
},
type: "link",
});
};
Expand Down
17 changes: 11 additions & 6 deletions plugins/lime-plugin-mesh-wide/src/components/configPage/modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,17 @@ export const useSetNodeInfoReferenceStateModal = () => {
export const useSetLinkReferenceStateModal = () => {
const { toggleModal, setModalState, isModalOpen, closeModal } = useModal();

const confirmModal = (
dataType: MeshWideMapDataTypeKeys,
nodes: string[],
isDown: boolean,
cb: () => Promise<void>
) => {
const confirmModal = ({
dataType,
nodes,
isDown,
cb,
}: {
dataType: MeshWideMapDataTypeKeys;
nodes: string[];
isDown: boolean;
cb: () => Promise<void>;
}) => {
let title = (
<Trans>
Set reference state for this {dataTypeNameMapping(dataType)}?
Expand Down
Loading

0 comments on commit 3aaab3e

Please sign in to comment.