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

fix: block navigation while in edit mode #1636

Merged
merged 4 commits into from
Sep 26, 2024
Merged
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
@@ -1,5 +1,5 @@
import React, { useContext, useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { useNavigate, useParams, useBlocker } from 'react-router-dom';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { AxiosError } from 'axios';
import {
Expand Down Expand Up @@ -338,10 +338,10 @@ const SeedlotReviewContent = () => {
onSuccess: async (_data, variables) => {
await queryClient.invalidateQueries({ queryKey: ['seedlots', seedlotNumber] });
await queryClient.invalidateQueries({ queryKey: ['seedlot-full-form', seedlotNumber] });
setIsReadMode(true);
if (variables.statusOnSave !== 'SUB') {
navigate(`/seedlots/details/${seedlotNumber}/?statusOnSave=${variables.statusOnSave}`);
}
setIsReadMode(true);
}
});

Expand All @@ -361,10 +361,10 @@ const SeedlotReviewContent = () => {
onSuccess: async (_data, variables) => {
await queryClient.invalidateQueries({ queryKey: ['seedlots', seedlotNumber] });
await queryClient.invalidateQueries({ queryKey: ['seedlot-full-form', seedlotNumber] });
setIsReadMode(true);
if (variables.statusOnSave !== 'SUB') {
navigate(`/seedlots/details/${seedlotNumber}/?statusOnSave=${variables.statusOnSave}`);
}
setIsReadMode(true);
}
});

Expand Down Expand Up @@ -513,6 +513,23 @@ const SeedlotReviewContent = () => {
closeCancelModal();
};

/**
* Custom blocker function to prevent navigation with unsaved changes.
*/
const blockerFunction = () => {
if (
!isReadMode
&& !tscSeedlotMutation.isLoading
&& !statusOnlyMutation.isLoading
) {
setIsCancelModalOpen(true); // Show modal if there are unsaved changes
return true; // Block navigation
}
return false; // Allow navigation
};

useBlocker(blockerFunction);

return (
<FlexGrid className="seedlot-review-grid">
<Loading
Expand Down
Loading