Skip to content

Commit

Permalink
Merge pull request #77 from jolynloh/BE/stopmatch
Browse files Browse the repository at this point in the history
Add confirmation popup when navigate away from matching pages
  • Loading branch information
feliciagan authored Oct 20, 2024
2 parents 08fa523 + b4317b7 commit 2db16f9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
46 changes: 22 additions & 24 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
"@mui/material": "^6.1.0",
"@uiw/react-md-editor": "^4.0.4",
"axios": "^1.7.7",
"history": "^5.3.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.53.0",
"react-material-ui-carousel": "^3.4.2",
"react-router-dom": "^6.26.2",
"react-router-dom": "^6.3.0",
"react-toastify": "^10.0.5",
"socket.io-client": "^4.8.0",
"vite-plugin-svgr": "^4.2.0"
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/contexts/MatchContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
import { useAuth } from "./AuthContext";
import { toast } from "react-toastify";
import useAppNavigate from "../components/UseAppNavigate";
import { UNSAFE_NavigationContext } from "react-router-dom";
import { Action, type History, type Transition } from "history";

type MatchUser = {
id: string;
Expand Down Expand Up @@ -109,6 +111,8 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
const [matchPending, setMatchPending] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(true);

const navigator = useContext(UNSAFE_NavigationContext).navigator as History;

useEffect(() => {
if (user) {
setMatchUser({
Expand All @@ -135,6 +139,16 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
openSocketConnection();
matchSocket.emit(MatchEvents.USER_CONNECTED, matchUser?.id);

const unblock = navigator.block((transition: Transition) => {
if (
transition.action === Action.Replace ||
confirm(ABORT_MATCH_PROCESS_CONFIRMATION_MESSAGE)
) {
unblock();
appNavigate(transition.location.pathname);
}
});

const handleBeforeUnload = (e: BeforeUnloadEvent) => {
e.preventDefault();
e.returnValue = ABORT_MATCH_PROCESS_CONFIRMATION_MESSAGE; // for legacy support, does not actually display message
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ const Home: React.FC = () => {
}
onClick={() => {
setIsQueryingQnDB(true);
getQuestionList(1, 10, "", complexities, categories, dispatch);
getQuestionList(1, 1, "", complexities, categories, dispatch);
}}
>
{isQueryingQnDB ? <CircularProgress /> : "Find my match!"}
Expand Down

0 comments on commit 2db16f9

Please sign in to comment.