diff --git a/FE/src/App.js b/FE/src/App.js index 6c8b0b8..0089b67 100644 --- a/FE/src/App.js +++ b/FE/src/App.js @@ -1,8 +1,11 @@ -import React, { useLayoutEffect } from "react"; +/* eslint-disable no-unused-vars */ + +import React, { useEffect, useLayoutEffect } from "react"; import Reset from "styled-reset"; import { createGlobalStyle } from "styled-components"; -import { useRecoilState } from "recoil"; +import { useRecoilState, useSetRecoilState } from "recoil"; import userAtom from "./atoms/userAtom"; +import diaryAtom from "./atoms/diaryAtom"; import Header from "./components/Header/Header"; import HomePage from "./pages/HomePage"; import MainPage from "./pages/MainPage"; @@ -36,6 +39,7 @@ const GlobalStyle = createGlobalStyle` function App() { const [userState, setUserState] = useRecoilState(userAtom); + const setDiaryState = useSetRecoilState(diaryAtom); useLayoutEffect(() => { let accessToken = localStorage.getItem("accessToken"); @@ -45,6 +49,14 @@ function App() { } }, []); + useEffect(() => { + window.onpopstate = (event) => { + if (event.state) { + setDiaryState(event.state); + } + }; + }, []); + return (
diff --git a/FE/src/components/DiaryModal/DiaryCreateModal.js b/FE/src/components/DiaryModal/DiaryCreateModal.js index fe71189..abc5e5f 100644 --- a/FE/src/components/DiaryModal/DiaryCreateModal.js +++ b/FE/src/components/DiaryModal/DiaryCreateModal.js @@ -1,6 +1,6 @@ /* eslint-disable */ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { useRecoilValue, useSetRecoilState } from "recoil"; import { useMutation, useQuery } from "react-query"; import styled from "styled-components"; @@ -25,8 +25,24 @@ function DiaryCreateModal() { const userState = useRecoilValue(userAtom); const setDiaryState = useSetRecoilState(diaryAtom); + useEffect(() => { + const handleBeforeUnload = (e) => { + e.preventDefault(); + e.returnValue = ""; + }; + + window.addEventListener("beforeunload", handleBeforeUnload); + + return () => { + window.removeEventListener("beforeunload", handleBeforeUnload); + }; + }, []); + const closeModal = () => { - setDiaryState((prev) => ({ ...prev, isCreate: false })); + setDiaryState((prev) => ({ + ...prev, + isCreate: false, + })); }; const addTag = (e) => { @@ -378,7 +394,6 @@ const DiaryModalContentInputBox = styled.textarea` resize: none; - word_wrap: break-word; word-break: break-all; &::placeholder { diff --git a/FE/src/components/DiaryModal/DiaryDeleteModal.js b/FE/src/components/DiaryModal/DiaryDeleteModal.js index 7fb4920..af33750 100644 --- a/FE/src/components/DiaryModal/DiaryDeleteModal.js +++ b/FE/src/components/DiaryModal/DiaryDeleteModal.js @@ -47,11 +47,7 @@ function DiaryDeleteModal() { diaryUuid: diaryState.diaryUuid, accessToken: userState.accessToken, }); - setDiaryState((prev) => ({ - ...prev, - isRead: false, - isDelete: false, - })); + window.history.back(); }} > 확인 diff --git a/FE/src/components/DiaryModal/DiaryListModal.js b/FE/src/components/DiaryModal/DiaryListModal.js index aacbd90..cc6ab85 100644 --- a/FE/src/components/DiaryModal/DiaryListModal.js +++ b/FE/src/components/DiaryModal/DiaryListModal.js @@ -106,11 +106,22 @@ function DiaryListModal() { src={zoomIn} alt='zoom-in' onClick={() => { - setDiaryState((prev) => ({ - ...prev, - isRead: true, - isList: false, - })); + setDiaryState((prev) => { + window.history.pushState( + { + ...prev, + isRead: true, + isList: false, + }, + "", + "", + ); + return { + ...prev, + isRead: true, + isList: false, + }; + }); }} /> diff --git a/FE/src/components/DiaryModal/DiaryReadModal.js b/FE/src/components/DiaryModal/DiaryReadModal.js index 38e5196..3268a16 100644 --- a/FE/src/components/DiaryModal/DiaryReadModal.js +++ b/FE/src/components/DiaryModal/DiaryReadModal.js @@ -121,11 +121,22 @@ function DiaryReadModal() { {data.title} { - setDiaryState((prev) => ({ - ...prev, - isRead: false, - isUpdate: true, - })); + setDiaryState((prev) => { + window.history.pushState( + { + ...prev, + isRead: false, + isUpdate: true, + }, + "", + "", + ); + return { + ...prev, + isRead: false, + isUpdate: true, + }; + }); }} > { + const handleBeforeUnload = (e) => { + e.preventDefault(); + e.returnValue = ""; + }; + window.addEventListener("beforeunload", handleBeforeUnload); + + return () => { + window.removeEventListener("beforeunload", handleBeforeUnload); + }; + }, []); + const closeModal = () => { - setDiaryState((prev) => ({ ...prev, isUpdate: false })); + window.history.back(); }; const addTag = (e) => { @@ -353,7 +365,6 @@ const DiaryModalContentInputBox = styled.textarea` resize: none; - word_wrap: break-word; word-break: break-all; &::placeholder { diff --git a/FE/src/components/SideBar/SideBar.js b/FE/src/components/SideBar/SideBar.js index 755ce95..3ccb2fb 100644 --- a/FE/src/components/SideBar/SideBar.js +++ b/FE/src/components/SideBar/SideBar.js @@ -21,11 +21,18 @@ function SideBar() { ...prev, isSideBar: false, })); - setDiaryState((prev) => ({ - ...prev, - isRead: false, - isList: false, - })); + setDiaryState((prev) => { + window.history.pushState( + { ...prev, isRead: false, isList: false }, + "", + "", + ); + return { + ...prev, + isRead: false, + isList: false, + }; + }); }} > 일기 쓰기 @@ -36,11 +43,25 @@ function SideBar() { ...prev, isSideBar: false, })); - setDiaryState({ - isCreate: false, - isRead: false, - isDelete: false, - isList: true, + setDiaryState((prev) => { + window.history.pushState( + { + ...prev, + isCreate: false, + isRead: false, + isUpdate: false, + isList: true, + }, + "", + "", + ); + return { + ...prev, + isCreate: false, + isRead: false, + isUpdate: false, + isList: true, + }; }); }} > diff --git a/FE/src/index.js b/FE/src/index.js index 1552f57..e8e7d1b 100644 --- a/FE/src/index.js +++ b/FE/src/index.js @@ -6,11 +6,9 @@ import App from "./App"; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( - - - - - - - , + + + + + , ); diff --git a/FE/src/pages/MainPage.js b/FE/src/pages/MainPage.js index aa2b7e0..5deb83e 100644 --- a/FE/src/pages/MainPage.js +++ b/FE/src/pages/MainPage.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect } from "react"; import styled from "styled-components"; import { useRecoilState } from "recoil"; import diaryAtom from "../atoms/diaryAtom"; @@ -12,12 +12,32 @@ import DiaryLoadingModal from "../components/DiaryModal/DiaryLoadingModal"; function MainPage() { const [diaryState, setDiaryState] = useRecoilState(diaryAtom); + useEffect(() => { + setDiaryState((prev) => { + const newState = { + ...prev, + isCreate: false, + isRead: false, + isUpdate: false, + isList: false, + }; + window.history.pushState(newState, "", ""); + return newState; + }); + }, []); + return ( <> { e.preventDefault(); - setDiaryState((prev) => ({ ...prev, isCreate: true, isRead: false })); + setDiaryState((prev) => ({ + ...prev, + isCreate: true, + isRead: false, + isUpdate: false, + isList: false, + })); }} /> {diaryState.isCreate ? : null}