Skip to content

Commit 918eb10

Browse files
committed
Show confirmation popup message before user leaves (unloads) matching pages
1 parent e48ece1 commit 918eb10

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

frontend/src/contexts/MatchContext.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { createContext, useContext, useEffect, useState } from "react";
44
import { matchSocket } from "../utils/matchSocket";
55
import {
6+
ABORT_MATCH_PROCESS_CONFIRMATION_MESSAGE,
67
FAILED_MATCH_REQUEST_MESSAGE,
78
MATCH_CONNECTION_ERROR,
89
MATCH_ENDED_MESSAGE,
@@ -130,11 +131,22 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
130131
openSocketConnection();
131132
matchSocket.emit(MatchEvents.USER_CONNECTED, matchUser?.id);
132133

133-
window.addEventListener("beforeunload", () => closeSocketConnection());
134+
const handleBeforeUnload = (e: BeforeUnloadEvent) => {
135+
e.preventDefault();
136+
e.returnValue = ABORT_MATCH_PROCESS_CONFIRMATION_MESSAGE; // for legacy support, does not actually display message
137+
};
138+
139+
const handleUnload = () => {
140+
closeSocketConnection();
141+
};
142+
143+
window.addEventListener("beforeunload", handleBeforeUnload);
144+
window.addEventListener("unload", handleUnload);
134145

135146
return () => {
136147
closeSocketConnection();
137-
window.removeEventListener("beforeunload", () => closeSocketConnection());
148+
window.removeEventListener("beforeunload", handleBeforeUnload);
149+
window.removeEventListener("unload", handleUnload);
138150
};
139151
}, [matchUser?.id, location.pathname]);
140152

frontend/src/pages/NewQuestion/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import reducer, {
1515
import { toast } from "react-toastify";
1616

1717
import {
18+
ABORT_CREATE_OR_EDIT_QUESTION_CONFIRMATION_MESSAGE,
1819
complexityList,
1920
FAILED_QUESTION_CREATE,
2021
FILL_ALL_FIELDS,
@@ -47,11 +48,7 @@ const NewQuestion = () => {
4748
selectedComplexity ||
4849
selectedCategories.length > 0
4950
) {
50-
if (
51-
!confirm(
52-
"Are you sure you want to leave this page? All process will be lost."
53-
)
54-
) {
51+
if (!confirm(ABORT_CREATE_OR_EDIT_QUESTION_CONFIRMATION_MESSAGE)) {
5552
return;
5653
}
5754
}

frontend/src/pages/QuestionEdit/index.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ArrowBackIcon from "@mui/icons-material/ArrowBack";
1111
import { toast } from "react-toastify";
1212

1313
import {
14+
ABORT_CREATE_OR_EDIT_QUESTION_CONFIRMATION_MESSAGE,
1415
complexityList,
1516
FAILED_QUESTION_UPDATE,
1617
FILL_ALL_FIELDS,
@@ -61,11 +62,7 @@ const QuestionEdit = () => {
6162
}, [state.selectedQuestion]);
6263

6364
const handleBack = () => {
64-
if (
65-
!confirm(
66-
"Are you sure you want to leave this page? All process will be lost."
67-
)
68-
) {
65+
if (!confirm(ABORT_CREATE_OR_EDIT_QUESTION_CONFIRMATION_MESSAGE)) {
6966
return;
7067
}
7168
navigate("/questions");

frontend/src/utils/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ export const MATCH_CONNECTION_ERROR =
9494
export const QUESTION_DOES_NOT_EXIST_ERROR =
9595
"There are no questions with the specified complexity and category. Please try another combination.";
9696

97+
/* Alerts & Dialog Boxes */
98+
// Questions
99+
export const ABORT_CREATE_OR_EDIT_QUESTION_CONFIRMATION_MESSAGE =
100+
"Are you sure you want to leave this page? All process will be lost.";
101+
102+
// Match
103+
export const ABORT_MATCH_PROCESS_CONFIRMATION_MESSAGE =
104+
"Are you sure you want to leave the matching process?";
105+
97106
/* Image paths */
98107
export const FIND_MATCH_FORM_PATH = "/find_match_form.png";
99108
export const MATCH_FOUND_PATH = "/match_found.png";

0 commit comments

Comments
 (0)