HyeonJun Jeon

[Implement] Calendar sync with DB

1 -import { useContext } from "react"; 1 +import { useContext, useEffect, useState } from "react";
2 +
3 +import { toYMD, toYMDStr } from "../utils/Dates";
4 +import { scheForage } from "../utils/LocalForage";
5 +
2 import { CalendarStateContext } from "../pages/Calendar"; 6 import { CalendarStateContext } from "../pages/Calendar";
3 -import { toYMD } from "../utils/Dates";
4 import ScheduleItem from "./ScheduleItem"; 7 import ScheduleItem from "./ScheduleItem";
5 8
6 const GridItem = ({ targetDate }) => { 9 const GridItem = ({ targetDate }) => {
7 const { state } = useContext(CalendarStateContext); 10 const { state } = useContext(CalendarStateContext);
8 - const { month } = toYMD(state.date); 11 + const { month: calMonth } = toYMD(state.date);
9 - const { month: tmonth, date: tdate } = toYMD(targetDate); 12 + const { month, date } = toYMD(targetDate);
13 + const [schedules, setSchedules] = useState();
10 14
11 - const tempSchedules = [ 15 + useEffect(() => {
12 - { type: "zoom", category: "오픈소스", label: "", start: [9, 30] }, 16 + async function loadScheduleItems() {
13 - { type: "ecampus", category: "논리회로", label: "요약", end: [23, 59] }, 17 + setSchedules(await scheForage.getItem(toYMDStr(targetDate)));
14 - ]; 18 + }
15 - const renderScheduleItems = () => { 19 + loadScheduleItems();
16 - if (targetDate.getDay() === 5) 20 + }, [targetDate]);
17 - return (
18 - <>
19 - <ScheduleItem schedule={tempSchedules[0]}></ScheduleItem>
20 - <ScheduleItem schedule={tempSchedules[1]}></ScheduleItem>
21 - </>
22 - );
23 - };
24 21
25 return ( 22 return (
26 - <div className="GridItem" relative={tmonth - month || null}> 23 + <div className="GridItem" relative={month - calMonth || null}>
27 <span className="date"> 24 <span className="date">
28 - {month !== tmonth ? tmonth + "/" + tdate : tdate} 25 + {calMonth !== month ? month + "/" + date : date}
29 </span> 26 </span>
30 - {renderScheduleItems()} 27 + {schedules &&
28 + schedules.map((sche, index) => (
29 + <ScheduleItem key={index} schedule={sche} />
30 + ))}
31 </div> 31 </div>
32 ); 32 );
33 }; 33 };
......
1 +import { useContext } from "react";
2 +
3 +import { CalendarStateContext } from "../pages/Calendar";
1 import zoomSymbol from "../assets/zoom.png"; 4 import zoomSymbol from "../assets/zoom.png";
2 import ecampusSymbol from "../assets/e-Campus.png"; 5 import ecampusSymbol from "../assets/e-Campus.png";
3 6
4 const ScheduleItem = ({ schedule }) => { 7 const ScheduleItem = ({ schedule }) => {
5 - const { type, category, label, start, end } = schedule; 8 + const { subCode, type, category, label, start, end } = schedule;
9 + const { subsObj } = useContext(CalendarStateContext);
10 + const subject = subsObj[subCode];
11 + if (!subject) {
12 + console.log("can't find " + subCode);
13 + return;
14 + }
15 + if (!subject.selected) return;
16 +
6 const selectSymbol = () => { 17 const selectSymbol = () => {
7 let symbol; 18 let symbol;
8 switch (type) { 19 switch (type) {
...@@ -18,7 +29,7 @@ const ScheduleItem = ({ schedule }) => { ...@@ -18,7 +29,7 @@ const ScheduleItem = ({ schedule }) => {
18 }; 29 };
19 30
20 return ( 31 return (
21 - <div className="ScheduleItem"> 32 + <div className="ScheduleItem" style={{ borderColor: subject.color }}>
22 <img className="s_symbol" src={selectSymbol()} alt="404" /> 33 <img className="s_symbol" src={selectSymbol()} alt="404" />
23 {start && <span className="s_start">{start[0] + ":" + start[1]}</span>} 34 {start && <span className="s_start">{start[0] + ":" + start[1]}</span>}
24 {end && <span className="s_end">{end[0] + ":" + end[1]}</span>} 35 {end && <span className="s_end">{end[0] + ":" + end[1]}</span>}
......
...@@ -35,11 +35,9 @@ const Calendar = () => { ...@@ -35,11 +35,9 @@ const Calendar = () => {
35 35
36 const navigate = useNavigate(); 36 const navigate = useNavigate();
37 useEffect(() => { 37 useEffect(() => {
38 - async function checkSession() { 38 + async function onMount() {
39 - if (!(await dataForage.getItem("session"))) navigate("/login"); 39 + if (!(await dataForage.getItem("session"))) return navigate("/login");
40 - }
41 40
42 - async function initSubsObj() {
43 if (!(await dataForage.getItem("Subjects"))) await initTempSubjects(); 41 if (!(await dataForage.getItem("Subjects"))) await initTempSubjects();
44 let tsubsObj = {}; 42 let tsubsObj = {};
45 for (const code of await dataForage.getItem("Subjects")) { 43 for (const code of await dataForage.getItem("Subjects")) {
...@@ -47,9 +45,7 @@ const Calendar = () => { ...@@ -47,9 +45,7 @@ const Calendar = () => {
47 } 45 }
48 dispatch({ type: "INIT", subsObj: tsubsObj }); 46 dispatch({ type: "INIT", subsObj: tsubsObj });
49 } 47 }
50 - 48 + onMount();
51 - checkSession();
52 - initSubsObj();
53 }, [navigate]); 49 }, [navigate]);
54 50
55 return ( 51 return (
......
...@@ -6,6 +6,14 @@ function toYMD(dateObj) { ...@@ -6,6 +6,14 @@ function toYMD(dateObj) {
6 return { year, month, date }; 6 return { year, month, date };
7 } 7 }
8 8
9 +function toYMDStr(dateObj) {
10 + return [
11 + dateObj.getFullYear(),
12 + dateObj.getMonth() + 1,
13 + dateObj.getDate(),
14 + ].join("/");
15 +}
16 +
9 function toSunday(dateObj) { 17 function toSunday(dateObj) {
10 const day = dateObj.getDay(); 18 const day = dateObj.getDay();
11 moveDate(dateObj, "day", -day); 19 moveDate(dateObj, "day", -day);
...@@ -26,4 +34,4 @@ function moveDate(dateObj, scope, distance) { ...@@ -26,4 +34,4 @@ function moveDate(dateObj, scope, distance) {
26 } 34 }
27 } 35 }
28 36
29 -export { toYMD, toSunday, moveDate }; 37 +export { toYMD, toYMDStr, toSunday, moveDate };
......
1 import { subForage, scheForage, dataForage } from "./LocalForage"; 1 import { subForage, scheForage, dataForage } from "./LocalForage";
2 +import { toYMDStr } from "./Dates";
2 3
3 -const initTempSubjects = async () => { 4 +async function initTempSubjects() {
4 - scheForage.setItem("20220503", [ 5 + const tempsch = [
5 - { type: "zoom", category: "과목A", label: "", start: [9, 30] }, 6 + {
6 - { type: "ecampus", category: "과목B", label: "과제", end: [23, 59] }, 7 + subCode: "1",
7 - ]); 8 + type: "zoom",
9 + category: "과목A",
10 + label: "",
11 + start: [9, 30],
12 + },
13 + {
14 + subCode: "2",
15 + type: "ecampus",
16 + category: "과목B",
17 + label: "과제",
18 + end: [23, 59],
19 + },
20 + ];
21 +
22 + await scheForage.setItem(toYMDStr(new Date("2022-5-20")), tempsch);
23 + await scheForage.setItem(toYMDStr(new Date("2022-5-27")), tempsch);
24 + await scheForage.setItem(toYMDStr(new Date("2022-6-3")), tempsch);
8 25
9 let tcolors = [ 26 let tcolors = [
10 "red", 27 "red",
...@@ -43,6 +60,6 @@ const initTempSubjects = async () => { ...@@ -43,6 +60,6 @@ const initTempSubjects = async () => {
43 await subForage.setItem(code, tsub); 60 await subForage.setItem(code, tsub);
44 } 61 }
45 dataForage.setItem("Subjects", subCodeLst); 62 dataForage.setItem("Subjects", subCodeLst);
46 -}; 63 +}
47 64
48 export { initTempSubjects }; 65 export { initTempSubjects };
......