Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/hooks/usePrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect } from 'react';
import { useBlocker } from 'react-router-dom';

// should be isDirty
export const usePrompt = (when: boolean) => {
export const usePrompt = (when: boolean, onLeave?: () => void) => {
const blocker = useBlocker(({ currentLocation, nextLocation }) => {
return when && currentLocation.pathname !== nextLocation.pathname;
});
Expand All @@ -14,15 +14,15 @@ export const usePrompt = (when: boolean) => {

useEffect(() => {
if (blocker.state !== 'blocked') return;
console.log(when);
if (!when) return;

if (window.confirm('정말로 이동하시겠습니까?')) {
onLeave?.();
blocker.proceed();
} else {
blocker.reset();
}
}, [blocker.state]);
}, [blocker.state, when, onLeave]);

useEffect(() => {
if (when) {
Expand Down
15 changes: 10 additions & 5 deletions src/pages/Posting/Posting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ArticleFeedbackPanel from '@/components/features/Feedback/ArticleFeedback
import PostEditor from '@/components/features/Post/PostEditor';
import { CommonLayout } from '@/components/layout/CommonLayout';

import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { RiErrorWarningFill } from 'react-icons/ri';
import {
Navigate,
Expand Down Expand Up @@ -107,7 +107,14 @@ const Posting = () => {
const buttonFontSize = isMobile ? '12px' : '16px';
const [progress, setProgress] = useState(0);

usePrompt(!disablePrompt);
const clearPostingCache = useCallback(() => {
sessionStorage.removeItem(`posting-articleId-${id}`);
sessionStorage.removeItem(`posting-content-${id}`);
sessionStorage.removeItem(`posting-title-${id}`);
setPostImages([]);
}, [id, setPostImages]);

usePrompt(!disablePrompt, clearPostingCache);

useEffect(() => {
document.documentElement.scrollTo({ top: 0, left: 0, behavior: 'instant' });
Expand Down Expand Up @@ -250,9 +257,7 @@ const Posting = () => {
const savedId = await handleSaveArticle();

if (savedId) {
sessionStorage.removeItem(`posting-articleId-${id}`);
sessionStorage.removeItem(`posting-content-${id}`);
sessionStorage.removeItem(`posting-title-${id}`);
clearPostingCache();
setDashboardView('article');
setSelectedPostId(savedId);
setDisablePrompt(true);
Expand Down
Loading