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

[FE] feat: 새로고침 및 창을 닫는 동작에 대해 경고 alert 띄우기 #851

Merged
merged 5 commits into from
Oct 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface UseNavigateBlockerProps {
isOpenModalDisablingBlocker: boolean;
openNavigateConfirmModal: () => void;
}

/**
* @param isOpenModalDisablingBlocker : 이동 확인 모달과 리뷰 제출 확인 모달 모달들이 열려있는 지 여부 (모달의 이동/제출 버튼 클릭 시 페이지 이동해야하기 때문에 필요)
* 작성한 답변이 있는 상태에서 작성한 페이지에서 다른 페이지로 이동하려할때 이동을 막거나, 이동을 진행하는 blocker를 반환하는 훅
Expand All @@ -17,10 +18,17 @@ const useNavigateBlocker = ({ isOpenModalDisablingBlocker, openNavigateConfirmMo

const isAnswerInProgress = () => {
if (!answerMap) return false;

return [...answerMap.values()].some((answer) => !!answer.selectedOptionIds?.length || !!answer.text?.length);
};

// 페이지 새로고침 및 닫기에 대한 처리 by BeforeUnloadEvent
const handleNavigationBlock = (event: BeforeUnloadEvent) => {
if (isAnswerInProgress() && !isOpenModalDisablingBlocker) {
event.preventDefault();
}
};

// 페이지 히스토리에 영향을 주는 페이지 이동은 useBlocker가 처리
const blocker = useBlocker(({ currentLocation, nextLocation }) => {
if (isOpenModalDisablingBlocker) return false;
const isLeavingPage = currentLocation.pathname !== nextLocation.pathname;
Expand All @@ -33,6 +41,14 @@ const useNavigateBlocker = ({ isOpenModalDisablingBlocker, openNavigateConfirmMo
}
}, [blocker]);

useEffect(() => {
window.addEventListener('beforeunload', handleNavigationBlock);

return () => {
window.removeEventListener('beforeunload', handleNavigationBlock);
};
}, [answerMap, isOpenModalDisablingBlocker]);

return {
blocker,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const useCardFormModal = () => {
closeModal,
isOpen,
isOpenModalDisablingBlocker:
isOpen(CARD_FORM_MODAL_KEY.navigateConfirm) || isOpen(CARD_FORM_MODAL_KEY.submitConfirm),
Copy link
Contributor

@BadaHertz52 BadaHertz52 Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breadCrumb를 사용한 react router 이동 시, confirm modal을 띄워주고 해당 모달에서 확인등으로 경로 이동하려면 navigateConfirm도 조건에 포함되야합니다

Copy link
Contributor Author

@ImxYJL ImxYJL Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[아까 바다와 나눴던 이야기 정리!]
isOpenModalDisablingBlocker에 대한 주석이 작성 확인/내용 확인 모달이 열려 있는가? 로 되어 있어서 두 조건을 체크하는 조건식으로 변경했었어요.
그러다 이 변수가 react-router-dom의 useBlocker 훅을 사용해 라우팅을 막는 상태인가?를 체크하는 의미로 살짝 변경(?)돼서 그에 맞춰 변수명을 isUnblocked로 바꿨습니다.

isOpen(CARD_FORM_MODAL_KEY.recheck) || isOpen(CARD_FORM_MODAL_KEY.submitConfirm),
};
};

Expand Down