Showing
8 changed files
with
68 additions
and
46 deletions
1 | import { useContext } from "react"; | 1 | import { useContext } from "react"; |
2 | import { useNavigate } from "react-router-dom"; | 2 | import { useNavigate } from "react-router-dom"; |
3 | + | ||
3 | import { CalendarStateContext } from "../pages/Calendar"; | 4 | import { CalendarStateContext } from "../pages/Calendar"; |
4 | import "../styles/Header.css"; | 5 | import "../styles/Header.css"; |
5 | import { moveDate, toYMD } from "../utils/Dates"; | 6 | import { moveDate, toYMD } from "../utils/Dates"; |
7 | +import { dataForage } from "../utils/LocalForage"; | ||
6 | 8 | ||
7 | const Header = () => { | 9 | const Header = () => { |
8 | const { state, setState } = useContext(CalendarStateContext); | 10 | const { state, setState } = useContext(CalendarStateContext); |
... | @@ -90,8 +92,8 @@ const Header = () => { | ... | @@ -90,8 +92,8 @@ const Header = () => { |
90 | </div> | 92 | </div> |
91 | <button | 93 | <button |
92 | className="hrb_r" | 94 | className="hrb_r" |
93 | - onClick={() => { | 95 | + onClick={async () => { |
94 | - localStorage.setItem("session", ""); | 96 | + await dataForage.setItem("session", ""); |
95 | navigate("/"); | 97 | navigate("/"); |
96 | }} | 98 | }} |
97 | > | 99 | > | ... | ... |
1 | import React, { useEffect, useReducer, useState } from "react"; | 1 | import React, { useEffect, useReducer, useState } from "react"; |
2 | -import { Navigate, Route, Routes } from "react-router-dom"; | 2 | +import { useNavigate, Route, Routes } from "react-router-dom"; |
3 | + | ||
4 | +import { initTempSubjects } from "../utils/Test"; | ||
5 | +import { dataForage, subForage } from "../utils/LocalForage"; | ||
3 | 6 | ||
4 | import Month from "../components/Month"; | 7 | import Month from "../components/Month"; |
5 | import Header from "../components/Header"; | 8 | import Header from "../components/Header"; |
6 | import Side from "../components/Side"; | 9 | import Side from "../components/Side"; |
7 | -import { initTempSubjects } from "../utils/Test"; | ||
8 | -import { subForage } from "../utils/LocalForage"; | ||
9 | 10 | ||
10 | export const CalendarStateContext = React.createContext(); | 11 | export const CalendarStateContext = React.createContext(); |
11 | 12 | ||
... | @@ -26,46 +27,46 @@ const render = (subsObj, args) => { | ... | @@ -26,46 +27,46 @@ const render = (subsObj, args) => { |
26 | const Calendar = () => { | 27 | const Calendar = () => { |
27 | console.log("visit Calendar"); | 28 | console.log("visit Calendar"); |
28 | 29 | ||
29 | - const session = localStorage.getItem("session"); | ||
30 | - | ||
31 | const [state, setState] = useState({ | 30 | const [state, setState] = useState({ |
32 | scope: "month", | 31 | scope: "month", |
33 | date: new Date(), | 32 | date: new Date(), |
34 | }); | 33 | }); |
35 | const [subsObj, dispatch] = useReducer(render, {}); | 34 | const [subsObj, dispatch] = useReducer(render, {}); |
36 | 35 | ||
36 | + const navigate = useNavigate(); | ||
37 | useEffect(() => { | 37 | useEffect(() => { |
38 | + async function checkSession() { | ||
39 | + if (!(await dataForage.getItem("session"))) navigate("/login"); | ||
40 | + } | ||
41 | + | ||
38 | async function initSubsObj() { | 42 | async function initSubsObj() { |
39 | - if (!localStorage.getItem("Subjects")) await initTempSubjects(); | 43 | + if (!(await dataForage.getItem("Subjects"))) await initTempSubjects(); |
40 | - const subCodeLst = JSON.parse(localStorage.getItem("Subjects")); | ||
41 | let tsubsObj = {}; | 44 | let tsubsObj = {}; |
42 | - for (const code of subCodeLst) { | 45 | + for (const code of await dataForage.getItem("Subjects")) { |
43 | tsubsObj[code] = await subForage.getItem(code); | 46 | tsubsObj[code] = await subForage.getItem(code); |
44 | } | 47 | } |
45 | dispatch({ type: "INIT", subsObj: tsubsObj }); | 48 | dispatch({ type: "INIT", subsObj: tsubsObj }); |
46 | } | 49 | } |
50 | + | ||
51 | + checkSession(); | ||
47 | initSubsObj(); | 52 | initSubsObj(); |
48 | - }, []); | 53 | + }, [navigate]); |
49 | 54 | ||
50 | return ( | 55 | return ( |
51 | <CalendarStateContext.Provider | 56 | <CalendarStateContext.Provider |
52 | value={{ state, setState, subsObj, dispatch }} | 57 | value={{ state, setState, subsObj, dispatch }} |
53 | > | 58 | > |
54 | - {session ? ( | 59 | + <div className="Calendar"> |
55 | - <div className="Calendar"> | 60 | + <Header /> |
56 | - <Header /> | 61 | + <div className="content"> |
57 | - <div className="content"> | 62 | + <Side /> |
58 | - <Side /> | 63 | + <Routes> |
59 | - <Routes> | 64 | + <Route path="/month/*" element={<Month />} /> |
60 | - <Route path="/month/*" element={<Month />} /> | 65 | + <Route path="/week/*" element={<></>} /> |
61 | - <Route path="/week/*" element={<></>} /> | 66 | + <Route path="/day/*" element={<></>} /> |
62 | - <Route path="/day/*" element={<></>} /> | 67 | + </Routes> |
63 | - </Routes> | ||
64 | - </div> | ||
65 | </div> | 68 | </div> |
66 | - ) : ( | 69 | + </div> |
67 | - <Navigate to="/login" /> | ||
68 | - )} | ||
69 | </CalendarStateContext.Provider> | 70 | </CalendarStateContext.Provider> |
70 | ); | 71 | ); |
71 | }; | 72 | }; | ... | ... |
1 | import { useEffect } from "react"; | 1 | import { useEffect } from "react"; |
2 | import { useNavigate } from "react-router-dom"; | 2 | import { useNavigate } from "react-router-dom"; |
3 | 3 | ||
4 | +import { dataForage } from "../utils/LocalForage"; | ||
5 | + | ||
4 | const Home = () => { | 6 | const Home = () => { |
5 | - const session = localStorage.getItem("session"); | ||
6 | console.log("visit Home"); | 7 | console.log("visit Home"); |
7 | 8 | ||
8 | - let destination; | ||
9 | - if (session) { | ||
10 | - destination = "/calendar/month"; | ||
11 | - } else { | ||
12 | - destination = "/login"; | ||
13 | - } | ||
14 | - | ||
15 | const navigate = useNavigate(); | 9 | const navigate = useNavigate(); |
16 | - useEffect(() => navigate(destination), [destination, navigate]); | 10 | + useEffect(() => { |
11 | + async function where() { | ||
12 | + let destination; | ||
13 | + if (await dataForage.getItem("session")) { | ||
14 | + destination = "/calendar/month"; | ||
15 | + } else { | ||
16 | + destination = "/login"; | ||
17 | + } | ||
18 | + navigate(destination); | ||
19 | + } | ||
20 | + | ||
21 | + where(); | ||
22 | + }, [navigate]); | ||
17 | }; | 23 | }; |
18 | 24 | ||
19 | export default Home; | 25 | export default Home; | ... | ... |
1 | -import { Navigate, useNavigate } from "react-router-dom"; | 1 | +import { useEffect } from "react"; |
2 | +import { useNavigate } from "react-router-dom"; | ||
3 | + | ||
4 | +import { dataForage } from "../utils/LocalForage"; | ||
2 | 5 | ||
3 | const Login = () => { | 6 | const Login = () => { |
4 | console.log("visit Login"); | 7 | console.log("visit Login"); |
5 | - const session = localStorage.getItem("session"); | ||
6 | 8 | ||
7 | const navigate = useNavigate(); | 9 | const navigate = useNavigate(); |
8 | - const login = () => { | 10 | + const login = async () => { |
9 | if (true) { | 11 | if (true) { |
10 | //login success | 12 | //login success |
11 | - localStorage.setItem("session", true); | 13 | + await dataForage.setItem("session", true); |
12 | 14 | ||
13 | navigate("/"); | 15 | navigate("/"); |
14 | } | 16 | } |
15 | }; | 17 | }; |
16 | 18 | ||
17 | - return session ? ( | 19 | + useEffect(() => { |
18 | - <Navigate to="/" /> | 20 | + async function render() { |
19 | - ) : ( | 21 | + if (await dataForage.getItem("session")) navigate("/"); |
22 | + } | ||
23 | + render(); | ||
24 | + }, [navigate]); | ||
25 | + | ||
26 | + return ( | ||
20 | <div className="Login"> | 27 | <div className="Login"> |
21 | <button onClick={login}>로그인</button> | 28 | <button onClick={login}>로그인</button> |
22 | </div> | 29 | </div> | ... | ... |
1 | import { Navigate, useNavigate } from "react-router-dom"; | 1 | import { Navigate, useNavigate } from "react-router-dom"; |
2 | 2 | ||
3 | import "../styles/Settings.css"; | 3 | import "../styles/Settings.css"; |
4 | +import { dataForage } from "../utils/LocalForage"; | ||
4 | 5 | ||
5 | const Settings = () => { | 6 | const Settings = () => { |
6 | console.log("visit Settings"); | 7 | console.log("visit Settings"); |
7 | 8 | ||
8 | - const session = localStorage.getItem("session"); | 9 | + const session = dataForage.getItem("session"); |
9 | 10 | ||
10 | const navigate = useNavigate(); | 11 | const navigate = useNavigate(); |
11 | 12 | ... | ... |
... | @@ -10,4 +10,9 @@ const scheForage = localforage.createInstance({ | ... | @@ -10,4 +10,9 @@ const scheForage = localforage.createInstance({ |
10 | storeName: "schedules", | 10 | storeName: "schedules", |
11 | }); | 11 | }); |
12 | 12 | ||
13 | -export { subForage, scheForage }; | 13 | +const dataForage = localforage.createInstance({ |
14 | + name: "localforage", | ||
15 | + storeName: "data", | ||
16 | +}); | ||
17 | + | ||
18 | +export { subForage, scheForage, dataForage }; | ... | ... |
1 | -import { subForage, scheForage } from "./LocalForage"; | 1 | +import { subForage, scheForage, dataForage } from "./LocalForage"; |
2 | 2 | ||
3 | const initTempSubjects = async () => { | 3 | const initTempSubjects = async () => { |
4 | scheForage.setItem("20220503", [ | 4 | scheForage.setItem("20220503", [ |
... | @@ -42,7 +42,7 @@ const initTempSubjects = async () => { | ... | @@ -42,7 +42,7 @@ const initTempSubjects = async () => { |
42 | subCodeLst.push(code); | 42 | subCodeLst.push(code); |
43 | await subForage.setItem(code, tsub); | 43 | await subForage.setItem(code, tsub); |
44 | } | 44 | } |
45 | - localStorage.setItem("Subjects", JSON.stringify(subCodeLst)); | 45 | + dataForage.setItem("Subjects", subCodeLst); |
46 | }; | 46 | }; |
47 | 47 | ||
48 | export { initTempSubjects }; | 48 | export { initTempSubjects }; | ... | ... |
-
Please register or login to post a comment