HyeonJun Jeon

[Modify] All storages to localForage

...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
6 6
7 - Front-End : React 7 - Front-End : React
8 - Back-End : Node.js 8 - Back-End : Node.js
9 -- Storage : LocalStorage, LocalForage 9 +- Storage : LocalForage
10 - Crawl : Puppeteer 10 - Crawl : Puppeteer
11 11
12 <br> 12 <br>
......
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,32 +27,35 @@ const render = (subsObj, args) => { ...@@ -26,32 +27,35 @@ 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 ? (
55 <div className="Calendar"> 59 <div className="Calendar">
56 <Header /> 60 <Header />
57 <div className="content"> 61 <div className="content">
...@@ -63,9 +67,6 @@ const Calendar = () => { ...@@ -63,9 +67,6 @@ const Calendar = () => {
63 </Routes> 67 </Routes>
64 </div> 68 </div>
65 </div> 69 </div>
66 - ) : (
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
9 + const navigate = useNavigate();
10 + useEffect(() => {
11 + async function where() {
8 let destination; 12 let destination;
9 - if (session) { 13 + if (await dataForage.getItem("session")) {
10 destination = "/calendar/month"; 14 destination = "/calendar/month";
11 } else { 15 } else {
12 destination = "/login"; 16 destination = "/login";
13 } 17 }
18 + navigate(destination);
19 + }
14 20
15 - const navigate = useNavigate(); 21 + where();
16 - useEffect(() => navigate(destination), [destination, navigate]); 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 };
......