Skip to content

Commit

Permalink
fix: block navigation while in edit mode (#1636)
Browse files Browse the repository at this point in the history
Co-authored-by: Ricardo Campos <[email protected]>
  • Loading branch information
craigyu and RMCampos authored Sep 26, 2024
1 parent 5718aad commit e6bba8e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions frontend/src/views/Seedlot/SeedlotReview/SeedlotReviewContent.tsx
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

0 comments on commit e6bba8e

Please sign in to comment.