Skip to content

Commit

Permalink
Add discard options for archetypes
Browse files Browse the repository at this point in the history
Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 committed Nov 8, 2023
1 parent 56fc0e9 commit cf4cfee
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
149 changes: 148 additions & 1 deletion client/src/app/pages/archetypes/archetypes-page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { Trans, useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import {
Button,
Expand Down Expand Up @@ -57,6 +57,8 @@ import { AxiosError } from "axios";
import { Paths } from "@app/Paths";
import { SimplePagination } from "@app/components/SimplePagination";
import { TablePersistenceKeyPrefix } from "@app/Constants";
import { useDeleteAssessmentMutation } from "@app/queries/assessments";
import { useDeleteReviewMutation } from "@app/queries/reviews";

const Archetypes: React.FC = () => {
const { t } = useTranslation();
Expand All @@ -71,6 +73,11 @@ const Archetypes: React.FC = () => {
const [archetypeToEdit, setArchetypeToEdit] = useState<Archetype | null>(
null
);
const [assessmentToDiscard, setAssessmentToDiscard] =
React.useState<Archetype | null>(null);

const [reviewToDiscard, setReviewToDiscard] =
React.useState<Archetype | null>(null);

const [archetypeToDuplicate, setArchetypeToDuplicate] =
useState<Archetype | null>(null);
Expand All @@ -97,6 +104,70 @@ const Archetypes: React.FC = () => {
}),
onError
);
const onDeleteAssessmentSuccess = (name: string) => {
pushNotification({
title: t("toastr.success.assessmentDiscarded", {
application: name,
}),
variant: "success",
});
};

const onDeleteError = (error: AxiosError) => {
pushNotification({
title: getAxiosErrorMessage(error),
variant: "danger",
});
};

const { mutate: deleteAssessment } = useDeleteAssessmentMutation(
onDeleteAssessmentSuccess,
onDeleteError
);

const discardAssessment = async (archetype: Archetype) => {
try {
if (archetype.assessments) {
await Promise.all(
archetype.assessments.map(async (assessment) => {
await deleteAssessment({
assessmentId: assessment.id,
archetypeId: archetype.id,
});
})
);
}
} catch (error) {
console.error("Error while deleting assessments:", error);
}
};

const onDeleteReviewSuccess = (name: string) => {
pushNotification({
title: t("toastr.success.reviewDiscarded", {
application: name,
}),
variant: "success",
});
};

const { mutate: deleteReview } = useDeleteReviewMutation(
onDeleteReviewSuccess,
onDeleteError
);

const discardReview = async (archetype: Archetype) => {
try {
if (archetype.review?.id) {
await deleteReview({
id: archetype.review.id,
name: archetype.name,
});
}
} catch (error) {
console.error("Error while deleting review:", error);
}
};

const tableControls = useLocalTableControls({
persistTo: "urlParams",
Expand Down Expand Up @@ -319,6 +390,24 @@ const Archetypes: React.FC = () => {
title: t("actions.edit"),
onClick: () => setArchetypeToEdit(archetype),
},
...(archetype?.assessments?.length
? [
{
title: t("actions.discardAssessment"),
onClick: () =>
setAssessmentToDiscard(archetype),
},
]
: []),
...(archetype?.review
? [
{
title: t("actions.discardReview"),
onClick: () =>
setReviewToDiscard(archetype),
},
]
: []),
{ isSeparator: true },
{
title: t("actions.delete"),
Expand Down Expand Up @@ -386,6 +475,64 @@ const Archetypes: React.FC = () => {
onClose={() => setArchetypeToDuplicate(null)}
/>
</Modal>
<ConfirmDialog
title={t("dialog.title.discard", {
what: t("terms.assessment").toLowerCase(),
})}
titleIconVariant={"warning"}
isOpen={assessmentToDiscard !== null}
message={
<span>
<Trans
i18nKey="dialog.message.discardAssessment"
values={{
applicationName: assessmentToDiscard?.name,
}}
>
The assessment(s) for <strong>{assessmentToDiscard?.name}</strong>{" "}
will be discarded. Do you wish to continue?
</Trans>
</span>
}
confirmBtnVariant={ButtonVariant.primary}
confirmBtnLabel={t("actions.continue")}
cancelBtnLabel={t("actions.cancel")}
onCancel={() => setAssessmentToDiscard(null)}
onClose={() => setAssessmentToDiscard(null)}
onConfirm={() => {
discardAssessment(assessmentToDiscard!);
setAssessmentToDiscard(null);
}}
/>
<ConfirmDialog
title={t("dialog.title.discard", {
what: t("terms.review").toLowerCase(),
})}
titleIconVariant={"warning"}
isOpen={reviewToDiscard !== null}
message={
<span>
<Trans
i18nKey="dialog.message.discardReview"
values={{
applicationName: reviewToDiscard?.name,
}}
>
The review for <strong>{reviewToDiscard?.name}</strong> will be
discarded, as well as the review result. Do you wish to continue?
</Trans>
</span>
}
confirmBtnVariant={ButtonVariant.primary}
confirmBtnLabel={t("actions.continue")}
cancelBtnLabel={t("actions.cancel")}
onCancel={() => setReviewToDiscard(null)}
onClose={() => setReviewToDiscard(null)}
onConfirm={() => {
discardReview(reviewToDiscard!);
setReviewToDiscard(null);
}}
/>

{/* Delete confirm modal */}
<ConfirmDialog
Expand Down
12 changes: 12 additions & 0 deletions client/src/app/queries/archetypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@ import {
getArchetypes,
updateArchetype,
} from "@app/api/rest";
import {
assessmentsQueryKey,
assessmentsByItemIdQueryKey,
} from "./assessments";
import { reviewsQueryKey } from "./reviews";

export const ARCHETYPES_QUERY_KEY = "archetypes";
export const ARCHETYPE_QUERY_KEY = "archetype";

export const useFetchArchetypes = () => {
const queryClient = useQueryClient();

Check warning on line 22 in client/src/app/queries/archetypes.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/archetypes.ts#L22

Added line #L22 was not covered by tests
const { isLoading, isSuccess, error, refetch, data } = useQuery({
initialData: [],
queryKey: [ARCHETYPES_QUERY_KEY],
queryFn: getArchetypes,
refetchInterval: 5000,
onSuccess: () => {
queryClient.invalidateQueries([reviewsQueryKey]);
queryClient.invalidateQueries([assessmentsQueryKey]);
queryClient.invalidateQueries([assessmentsByItemIdQueryKey]);

Check warning on line 31 in client/src/app/queries/archetypes.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/archetypes.ts#L28-L31

Added lines #L28 - L31 were not covered by tests
},

onError: (error: AxiosError) => console.log(error),
});

Expand Down

0 comments on commit cf4cfee

Please sign in to comment.