Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Update query when assessment is deleted #1386

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ export const ApplicationsTable: React.FC = () => {
await Promise.all(
application.assessments.map(async (assessment) => {
await deleteAssessment({
id: assessment.id,
name: application.name,
assessmentId: assessment.id,
applicationName: application.name,
});
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ const DynamicAssessmentActionsRow: FunctionComponent<
if (assessment) {
try {
await deleteAssessmentAsync({
name: assessment.name,
id: assessment.id,
assessmentId: assessment.id,
applicationName: application?.name,
applicationId: application?.id,
archetypeId: archetype?.id,
}).then(() => {
createAssessment();
});
Expand Down Expand Up @@ -257,8 +259,10 @@ const DynamicAssessmentActionsRow: FunctionComponent<
variant="plain"
onClick={() => {
deleteAssessment({
id: assessment.id,
name: assessment.name,
assessmentId: assessment.id,
applicationName: application?.name,
applicationId: application?.id,
archetypeId: archetype?.id,
});
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { AxiosError } from "axios";
import {
assessmentsByItemIdQueryKey,
useDeleteAssessmentMutation,
useUpdateAssessmentMutation,
} from "@app/queries/assessments";
import { useQueryClient } from "@tanstack/react-query";
Expand Down Expand Up @@ -77,12 +78,14 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
onHandleUpdateAssessmentSuccess
);

const { mutate: deleteAssessmentMutation } = useDeleteAssessmentMutation();

const { t } = useTranslation();

const [currentStep, setCurrentStep] = useState(0);

const [isConfirmDialogOpen, setIsConfirmDialogOpen] =
React.useState<boolean>(false);
const [assessmentToCancel, setAssessmentToCancel] =
React.useState<Assessment | null>(null);

const history = useHistory();

Expand Down Expand Up @@ -509,35 +512,50 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
onNext={() => setCurrentStep((current) => current + 1)}
onBack={() => setCurrentStep((current) => current - 1)}
onClose={() => {
setIsConfirmDialogOpen(true);
assessment && setAssessmentToCancel(assessment);
}}
onGoToStep={(step) => setCurrentStep(step.id as number)}
/>
{isConfirmDialogOpen && (
{assessmentToCancel && (
<ConfirmDialog
title={t("dialog.title.leavePage")}
isOpen
message={t("dialog.message.leavePage")}
confirmBtnVariant={ButtonVariant.primary}
confirmBtnLabel={t("actions.continue")}
cancelBtnLabel={t("actions.cancel")}
onCancel={() => setIsConfirmDialogOpen(false)}
onClose={() => setIsConfirmDialogOpen(false)}
onCancel={() => setAssessmentToCancel(null)}
onClose={() => setAssessmentToCancel(null)}
onConfirm={() => {
setIsConfirmDialogOpen(false);
if (isArchetype) {
assessmentToCancel.status === "empty" &&
deleteAssessmentMutation({
assessmentId: assessmentToCancel.id,
applicationName: assessmentToCancel.application?.name,
applicationId: assessmentToCancel.application?.id,
archetypeId: assessmentToCancel.archetype?.id,
});

history.push(
formatPath(Paths.archetypeAssessmentActions, {
archetypeId: assessment?.archetype?.id,
})
);
} else {
assessmentToCancel.status === "empty" &&
deleteAssessmentMutation({
assessmentId: assessmentToCancel.id,
applicationName: assessmentToCancel.application?.name,
applicationId: assessmentToCancel.application?.id,
archetypeId: assessmentToCancel.archetype?.id,
});
history.push(
formatPath(Paths.applicationAssessmentActions, {
applicationId: assessment?.application?.id,
})
);
}
setAssessmentToCancel(null);
}}
/>
)}
Expand Down
31 changes: 23 additions & 8 deletions client/src/app/queries/assessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import { AxiosError } from "axios";
import { Assessment, InitialAssessment } from "@app/api/models";
import { QuestionnairesQueryKey } from "./questionnaires";
import { ARCHETYPE_QUERY_KEY } from "./archetypes";

export const assessmentsQueryKey = "assessments";
export const assessmentQueryKey = "assessment";
Expand Down Expand Up @@ -70,21 +71,35 @@
};

export const useDeleteAssessmentMutation = (
onSuccess: (name: string) => void,
onError: (err: AxiosError) => void
onSuccess?: (applicationName: string) => void,
onError?: (err: AxiosError) => void
) => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (args: { name: string; id: number }) =>
deleteAssessment(args.id),
onSuccess: (_, args) => {
onSuccess(args.name);
mutationFn: (args: {
assessmentId: number;
applicationName?: string;
applicationId?: number;
archetypeId?: number;
}) => {
const deletedAssessment = deleteAssessment(args.assessmentId);

Check warning on line 86 in client/src/app/queries/assessments.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/assessments.ts#L85-L86

Added lines #L85 - L86 were not covered by tests

queryClient.invalidateQueries([assessmentQueryKey, args?.assessmentId]);
queryClient.invalidateQueries([ARCHETYPE_QUERY_KEY, args?.archetypeId]);

Check warning on line 89 in client/src/app/queries/assessments.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/assessments.ts#L88-L89

Added lines #L88 - L89 were not covered by tests
queryClient.invalidateQueries([
assessmentsByItemIdQueryKey,
args.id,
QuestionnairesQueryKey,
args?.archetypeId,
]);
queryClient.invalidateQueries([

Check warning on line 94 in client/src/app/queries/assessments.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/assessments.ts#L94

Added line #L94 was not covered by tests
assessmentsByItemIdQueryKey,
args?.applicationId,
]);

return deletedAssessment;

Check warning on line 99 in client/src/app/queries/assessments.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/assessments.ts#L99

Added line #L99 was not covered by tests
},
onSuccess: (_, args) => {

Check warning on line 101 in client/src/app/queries/assessments.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/assessments.ts#L101

Added line #L101 was not covered by tests
onSuccess && onSuccess(args?.applicationName || "Unknown");
},
onError: onError,
});
Expand Down
Loading