HyeonJun Jeon

[Modify] Save subjects in localStorage

...@@ -4,14 +4,19 @@ import "../styles/Side.css"; ...@@ -4,14 +4,19 @@ import "../styles/Side.css";
4 import SideSubject from "./SideSubject"; 4 import SideSubject from "./SideSubject";
5 5
6 const Side = () => { 6 const Side = () => {
7 - const { subs, setSubs } = useContext(CalendarStateContext); 7 + const { subsObj, dispatch } = useContext(CalendarStateContext);
8 8
9 const renderSubs = () => { 9 const renderSubs = () => {
10 const sideSubjects = []; 10 const sideSubjects = [];
11 11
12 - for (let i = 0; i < subs.length; i++) { 12 + for (const code in subsObj) {
13 sideSubjects.push( 13 sideSubjects.push(
14 - <SideSubject key={i} subject={subs[i]} setSubs={setSubs} /> 14 + <SideSubject
15 + key={code}
16 + code={code}
17 + subject={subsObj[code]}
18 + dispatch={dispatch}
19 + />
15 ); 20 );
16 } 21 }
17 22
......
1 -const SideSubject = ({ subject }) => { 1 +const SideSubject = ({ code, subject, dispatch }) => {
2 - let isChecked = false;
3 const defaultColor = "#EFEFEF"; 2 const defaultColor = "#EFEFEF";
4 - const bgc = "background-color";
5 3
6 const check = (e) => { 4 const check = (e) => {
7 - if (isChecked) e.target.style["background-color"] = defaultColor; 5 + dispatch({ type: "CHECKED", code });
6 + if (subject.selected) e.target.style["background-color"] = defaultColor;
8 else e.target.style["background-color"] = subject.color; 7 else e.target.style["background-color"] = subject.color;
9 - isChecked = !isChecked;
10 }; 8 };
11 9
12 return ( 10 return (
...@@ -15,7 +13,7 @@ const SideSubject = ({ subject }) => { ...@@ -15,7 +13,7 @@ const SideSubject = ({ subject }) => {
15 className="ssc" 13 className="ssc"
16 onClick={check} 14 onClick={check}
17 style={{ 15 style={{
18 - [bgc]: isChecked ? subject.color : defaultColor, 16 + backgroundColor: subject.selected ? subject.color : defaultColor,
19 }} 17 }}
20 ></div> 18 ></div>
21 19
......
...@@ -5,7 +5,7 @@ import App from "./App"; ...@@ -5,7 +5,7 @@ import App from "./App";
5 5
6 const root = ReactDOM.createRoot(document.getElementById("root")); 6 const root = ReactDOM.createRoot(document.getElementById("root"));
7 root.render( 7 root.render(
8 - <React.StrictMode> 8 + // <React.StrictMode>
9 <App /> 9 <App />
10 - </React.StrictMode> 10 + // </React.StrictMode>
11 ); 11 );
......
1 -import React, { useState } from "react"; 1 +import React, { useReducer, useState } from "react";
2 import { Navigate, Route, Routes } from "react-router-dom"; 2 import { Navigate, Route, Routes } from "react-router-dom";
3 3
4 import Month from "../components/Month"; 4 import Month from "../components/Month";
5 import Header from "../components/Header"; 5 import Header from "../components/Header";
6 import Side from "../components/Side"; 6 import Side from "../components/Side";
7 -import Subject from "../utils/Subject";
8 7
9 export const CalendarStateContext = React.createContext(); 8 export const CalendarStateContext = React.createContext();
10 9
10 +const render = (subsObj, args) => {
11 + switch (args.type) {
12 + case "CHECKED":
13 + const sub = subsObj[args.code];
14 + sub.selected = !sub.selected;
15 + localStorage.setItem("S" + args.code, JSON.stringify(sub));
16 + return { ...subsObj, [args.code]: sub };
17 + default:
18 + return subsObj;
19 + }
20 +};
21 +
11 const Calendar = () => { 22 const Calendar = () => {
12 console.log("visit Calendar"); 23 console.log("visit Calendar");
13 24
...@@ -18,8 +29,9 @@ const Calendar = () => { ...@@ -18,8 +29,9 @@ const Calendar = () => {
18 date: new Date(), 29 date: new Date(),
19 }); 30 });
20 31
32 + let subLst;
21 if (!localStorage.getItem("Subjects")) { 33 if (!localStorage.getItem("Subjects")) {
22 - let tsubs = []; 34 + subLst = [];
23 let tcolors = [ 35 let tcolors = [
24 "red", 36 "red",
25 "green", 37 "green",
...@@ -30,16 +42,26 @@ const Calendar = () => { ...@@ -30,16 +42,26 @@ const Calendar = () => {
30 "chartreuse", 42 "chartreuse",
31 ]; 43 ];
32 for (let i = 0; i < 7; i++) { 44 for (let i = 0; i < 7; i++) {
33 - tsubs.push(new Subject("과목" + (i + 1), tcolors[i], true)); 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));
34 } 49 }
35 - localStorage.setItem("Subjects", JSON.stringify(tsubs)); 50 + localStorage.setItem("Subjects", JSON.stringify(subLst));
36 } 51 }
37 - const [subs, setSubs] = useState( 52 +
38 - JSON.parse(localStorage.getItem("Subjects")) 53 + subLst = JSON.parse(localStorage.getItem("Subjects"));
39 - ); 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);
40 60
41 return ( 61 return (
42 - <CalendarStateContext.Provider value={{ state, setState, subs, setSubs }}> 62 + <CalendarStateContext.Provider
63 + value={{ state, setState, subsObj, dispatch }}
64 + >
43 {session ? ( 65 {session ? (
44 <div className="Calendar"> 66 <div className="Calendar">
45 <Header /> 67 <Header />
......