Calendar.js
2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React, { useEffect, useReducer, useState } from "react";
import { useNavigate, Route, Routes } from "react-router-dom";
import { subForage } from "../utils/LocalForage";
import Month from "../components/Month";
import Header from "../components/Header";
import Side from "../components/Side";
import localforage from "localforage";
import axios from "axios";
export const CalendarStateContext = React.createContext();
const render = (subsObj, args) => {
switch (args.type) {
case "CHECKED":
const sub = subsObj[args.code];
sub.selected = !sub.selected;
subForage.setItem(args.code, sub);
return { ...subsObj, [args.code]: sub };
case "INIT":
return args.subsObj;
default:
return subsObj;
}
};
const Calendar = () => {
console.log("visit Calendar");
const [state, setState] = useState({
scope: "month",
date: new Date(),
});
const [subsObj, dispatch] = useReducer(render, {});
const navigate = useNavigate();
useEffect(() => {
async function onMount() {
if (!(await localforage.getItem("session"))) return navigate("/login");
// get user's subjects
const userID = await localforage.getItem("userID");
const subjects = await axios.get(
"http://localhost:3001/db/user-subject",
{ params: { userID } }
).data;
console.log(subjects);
let tsubsObj = {};
for (const sub of subjects) {
tsubsObj[sub.subjectID] = sub;
}
dispatch({ type: "INIT", subsObj: tsubsObj });
}
onMount();
}, [navigate]);
return (
<CalendarStateContext.Provider
value={{ state, setState, subsObj, dispatch }}
>
<div className="Calendar">
<Header />
<div className="content">
<Side />
<Routes>
<Route path="/month/*" element={<Month />} />
<Route path="/week/*" element={<></>} />
<Route path="/day/*" element={<></>} />
</Routes>
</div>
</div>
</CalendarStateContext.Provider>
);
};
export default Calendar;