Skip to content

Commit

Permalink
feat: show warning on unload
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlosfarah committed Apr 12, 2024
1 parent a34c545 commit 3435ddf
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/modules/interaction/ParticipantInteraction.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement, useState } from 'react';
import { ReactElement, useEffect, useState } from 'react';

import { Button } from '@mui/material';
import Box from '@mui/material/Box';
Expand Down Expand Up @@ -121,6 +121,22 @@ const ParticipantInteraction = (): ReactElement => {
const [interaction, setInteraction] =
useState<Interaction>(defaultInteraction);

useEffect(() => {
const handleBeforeUnload = (event: BeforeUnloadEvent): void => {
if (!interaction.completed) {
// Perform actions before the component unloads
event.preventDefault();

// eslint-disable-next-line no-alert,no-restricted-globals
confirm('Confirm refresh');
}
};
window.addEventListener('beforeunload', handleBeforeUnload);
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, [interaction]);

function startInteraction(): void {
const updatedInteraction = { ...interaction };
updatedInteraction.started = true;
Expand Down

0 comments on commit 3435ddf

Please sign in to comment.