HyeonJun Jeon

[Style] Separate storage of Subjects and Schedules

import React, { useEffect, useReducer, useState } from "react";
import { Navigate, Route, Routes } from "react-router-dom";
import localforage from "localforage";
import Month from "../components/Month";
import Header from "../components/Header";
import Side from "../components/Side";
import { initTempSubjects } from "../utils/Test";
import { subForage } from "../utils/LocalForage";
export const CalendarStateContext = React.createContext();
......@@ -14,7 +14,7 @@ const render = (subsObj, args) => {
case "CHECKED":
const sub = subsObj[args.code];
sub.selected = !sub.selected;
localforage.setItem(args.code, sub);
subForage.setItem(args.code, sub);
return { ...subsObj, [args.code]: sub };
case "INIT":
return args.subsObj;
......@@ -40,7 +40,7 @@ const Calendar = () => {
const subCodeLst = JSON.parse(localStorage.getItem("Subjects"));
let tsubsObj = {};
for (const code of subCodeLst) {
tsubsObj[code] = await localforage.getItem(code);
tsubsObj[code] = await subForage.getItem(code);
}
dispatch({ type: "INIT", subsObj: tsubsObj });
}
......
import localforage from "localforage";
const subForage = localforage.createInstance({
name: "localforage",
storeName: "subjects",
});
const scheForage = localforage.createInstance({
name: "localforage",
storeName: "schedules",
});
export { subForage, scheForage };
import localforage from "localforage";
import { subForage, scheForage } from "./LocalForage";
const initTempSubjects = async () => {
scheForage.setItem("20220503", [
{ type: "zoom", category: "과목A", label: "", start: [9, 30] },
{ type: "ecampus", category: "과목B", label: "과제", end: [23, 59] },
]);
let tcolors = [
"red",
"green",
......@@ -16,40 +21,26 @@ const initTempSubjects = async () => {
name: "과목A",
color: tcolors[0],
selected: true,
schedule: [
{ type: "zoom", category: "과목A", label: "", start: [9, 30] },
],
},
2: {
name: "과목B",
color: tcolors[1],
selected: true,
schedule: [
{
date: new Date(2022, 5, 3),
type: "ecampus",
category: "과목B",
label: "과제",
end: [23, 59],
},
],
},
};
await localforage.setItem("1", subObj["1"]);
await localforage.setItem("2", subObj["2"]);
await subForage.setItem("1", subObj["1"]);
await subForage.setItem("2", subObj["2"]);
for (let i = 2; i < 7; i++) {
let code = (i + 1).toString();
let tsub = {
date: new Date(2022, 5, 3),
name: "과목" + code,
color: tcolors[i],
selected: true,
schedule: [],
};
subCodeLst.push(code);
await localforage.setItem(code, tsub);
await subForage.setItem(code, tsub);
}
localStorage.setItem("Subjects", JSON.stringify(subCodeLst));
};
......