HyeonJun Jeon

[Modify] Save subjects in localForage

1 -import React, { useReducer, useState } from "react"; 1 +import React, { useEffect, useReducer, useState } from "react";
2 import { Navigate, Route, Routes } from "react-router-dom"; 2 import { Navigate, Route, Routes } from "react-router-dom";
3 +import localforage from "localforage";
3 4
4 import Month from "../components/Month"; 5 import Month from "../components/Month";
5 import Header from "../components/Header"; 6 import Header from "../components/Header";
6 import Side from "../components/Side"; 7 import Side from "../components/Side";
8 +import { initTempSubjects } from "../utils/Test";
7 9
8 export const CalendarStateContext = React.createContext(); 10 export const CalendarStateContext = React.createContext();
9 11
...@@ -12,8 +14,10 @@ const render = (subsObj, args) => { ...@@ -12,8 +14,10 @@ const render = (subsObj, args) => {
12 case "CHECKED": 14 case "CHECKED":
13 const sub = subsObj[args.code]; 15 const sub = subsObj[args.code];
14 sub.selected = !sub.selected; 16 sub.selected = !sub.selected;
15 - localStorage.setItem("S" + args.code, JSON.stringify(sub)); 17 + localforage.setItem(args.code, sub);
16 return { ...subsObj, [args.code]: sub }; 18 return { ...subsObj, [args.code]: sub };
19 + case "INIT":
20 + return args.subsObj;
17 default: 21 default:
18 return subsObj; 22 return subsObj;
19 } 23 }
...@@ -28,35 +32,20 @@ const Calendar = () => { ...@@ -28,35 +32,20 @@ const Calendar = () => {
28 scope: "month", 32 scope: "month",
29 date: new Date(), 33 date: new Date(),
30 }); 34 });
35 + const [subsObj, dispatch] = useReducer(render, {});
31 36
32 - let subLst; 37 + useEffect(() => {
33 - if (!localStorage.getItem("Subjects")) { 38 + async function initSubsObj() {
34 - subLst = []; 39 + if (!localStorage.getItem("Subjects")) await initTempSubjects();
35 - let tcolors = [ 40 + const subCodeLst = JSON.parse(localStorage.getItem("Subjects"));
36 - "red", 41 + let tsubsObj = {};
37 - "green", 42 + for (const code of subCodeLst) {
38 - "blue", 43 + tsubsObj[code] = await localforage.getItem(code);
39 - "orange", 44 + }
40 - "gold", 45 + dispatch({ type: "INIT", subsObj: tsubsObj });
41 - "aqua",
42 - "chartreuse",
43 - ];
44 - for (let i = 0; i < 7; i++) {
45 - let code = i + 1;
46 - let tsub = { name: "과목" + code, color: tcolors[i], selected: true };
47 - subLst.push(code);
48 - localStorage.setItem("S" + code, JSON.stringify(tsub));
49 } 46 }
50 - localStorage.setItem("Subjects", JSON.stringify(subLst)); 47 + initSubsObj();
51 - } 48 + }, []);
52 -
53 - subLst = JSON.parse(localStorage.getItem("Subjects"));
54 - let tsubsObj = {};
55 - for (const code of subLst) {
56 - tsubsObj[code] = JSON.parse(localStorage.getItem("S" + code));
57 - }
58 -
59 - const [subsObj, dispatch] = useReducer(render, tsubsObj);
60 49
61 return ( 50 return (
62 <CalendarStateContext.Provider 51 <CalendarStateContext.Provider
......
1 import { useState } from "react"; 1 import { useState } from "react";
2 import { useNavigate } from "react-router-dom"; 2 import { useNavigate } from "react-router-dom";
3 -import localforage from "localforage"; 3 +import { initTempSubjects } from "../utils/Test";
4 4
5 const Debug = () => { 5 const Debug = () => {
6 const [state, setState] = useState({ input: "", output: "" }); 6 const [state, setState] = useState({ input: "", output: "" });
...@@ -13,8 +13,10 @@ const Debug = () => { ...@@ -13,8 +13,10 @@ const Debug = () => {
13 }; 13 };
14 14
15 const handleSubmit = async (e) => { 15 const handleSubmit = async (e) => {
16 - await localforage.setItem("test", state.input); 16 + if (state.input === "init") await initTempSubjects();
17 - const result = await localforage.getItem("test"); 17 +
18 + const result = state.input;
19 +
18 setState({ ...state, output: result }); 20 setState({ ...state, output: result });
19 }; 21 };
20 22
......
1 +import localforage from "localforage";
2 +
3 +const initTempSubjects = async () => {
4 + let tcolors = [
5 + "red",
6 + "green",
7 + "blue",
8 + "orange",
9 + "gold",
10 + "aqua",
11 + "chartreuse",
12 + ];
13 + const subCodeLst = ["1", "2"];
14 + const subObj = {
15 + 1: {
16 + name: "과목A",
17 + color: tcolors[0],
18 + selected: true,
19 + schedule: [
20 + { type: "zoom", category: "과목A", label: "", start: [9, 30] },
21 + ],
22 + },
23 + 2: {
24 + name: "과목B",
25 + color: tcolors[1],
26 + selected: true,
27 + schedule: [
28 + {
29 + date: new Date(2022, 5, 3),
30 + type: "ecampus",
31 + category: "과목B",
32 + label: "과제",
33 + end: [23, 59],
34 + },
35 + ],
36 + },
37 + };
38 +
39 + await localforage.setItem("1", subObj["1"]);
40 + await localforage.setItem("2", subObj["2"]);
41 +
42 + for (let i = 2; i < 7; i++) {
43 + let code = (i + 1).toString();
44 + let tsub = {
45 + date: new Date(2022, 5, 3),
46 + name: "과목" + code,
47 + color: tcolors[i],
48 + selected: true,
49 + schedule: [],
50 + };
51 + subCodeLst.push(code);
52 + await localforage.setItem(code, tsub);
53 + }
54 + localStorage.setItem("Subjects", JSON.stringify(subCodeLst));
55 +};
56 +
57 +export { initTempSubjects };