GridItem.js
4.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { useContext, useEffect, useRef, useState } from "react";
import localforage from "localforage";
import { toYMD, toYMDStr } from "../utils/Dates";
import { CalendarStateContext } from "../pages/Calendar";
import ScheduleItem from "./ScheduleItem";
import axios from "axios";
const GridItem = ({ targetDate }) => {
const { state: calState, subsObj } = useContext(CalendarStateContext);
const { month: calMonth } = toYMD(calState.date);
const { month, date } = toYMD(targetDate);
const [schedules, setSchedules] = useState();
const [state, setState] = useState({
label: "",
startTime: "",
endTime: "",
description: "",
url: "",
type: "assignment",
subjectID: 0,
});
useEffect(() => {
async function loadScheduleItems() {
const params = {
userID: await localforage.getItem("userID"),
date: toYMDStr(targetDate, "-"),
day: targetDate.getDay(),
};
const { data: scdate } = await axios.get(
"http://3.34.173.161:3001/db/schedules_date",
{ params }
);
const { data: sctime } = await axios.get(
"http://3.34.173.161:3001/db/schedules_time",
{ params }
);
const { data: scrpeat } = await axios.get(
"http://3.34.173.161:3001/db/schedules_repeat",
{ params }
);
const subs = await localforage.getItem("subjects");
setState({ ...state, subjectID: subs[0].subjectID });
setSchedules(scrpeat.concat(scdate, sctime));
}
loadScheduleItems();
}, [targetDate]);
const handleChangeState = (e) => {
setState({
...state,
[e.target.name]: e.target.value,
});
};
const finishSchedule = async (table, uid) => {
for (const i in schedules)
if (schedules[i].uid === uid) {
await axios.delete("http://3.34.173.161:3001/db/schedule", {
data: {
table,
uid,
userID: await localforage.getItem("userID"),
},
});
schedules.splice(i, 1);
setSchedules(schedules);
}
};
const popupRef = useRef();
const click = async (e) => {
if (e.target.classList.contains("gi")) {
popupRef.current.style.display = "flex";
} else if (e.target.className === "gipc") {
popupRef.current.style.display = "none";
} else if (e.target.className === "gipa") {
// (userID, label, subjectID, type, description, url, date)
const table =
state.startTime || state.endTime ? "schedules_time" : "schedules_date";
const { data: sche } = await axios.post(
"http://3.34.173.161:3001/db/" + table,
{
userID: await localforage.getItem("userID"),
...state,
date: toYMDStr(targetDate, "-"),
}
);
setSchedules(schedules.concat(sche));
popupRef.current.style.display = "none";
}
};
return (
<div
className="GridItem gi"
relative={month - calMonth || null}
onClick={click}
>
<span className="date gi">
{calMonth !== month ? month + "/" + date : date}
</span>
{schedules &&
schedules.map((sche, index) => (
<ScheduleItem key={index} schedule={sche} finish={finishSchedule} />
))}
<div className="gi_popup" popup="true" ref={popupRef}>
<span>일정 추가</span>
<div className="gipd">
<input
name="label"
placeholder="이름"
value={state.label}
onChange={handleChangeState}
/>
<select
name="subjectID"
value={state.subjectID}
onChange={handleChangeState}
>
{Object.values(subsObj).map((sub, index) => (
<option key={index} value={sub.subjectID}>
{sub.name}
</option>
))}
</select>
<select name="type" value={state.type} onChange={handleChangeState}>
<option value={"assignment"}>E-Campus</option>
<option value={"zoom"}>Zoom</option>
</select>
<input
name="startTime"
placeholder="시작 (HH:MM)"
value={state.startTime}
onChange={handleChangeState}
/>
<input
name="endTime"
placeholder="종료 (HH:MM)"
value={state.endTime}
onChange={handleChangeState}
/>
<textarea
name="description"
placeholder="설명"
value={state.description}
onChange={handleChangeState}
/>
<textarea
name="url"
placeholder="링크"
value={state.url}
onChange={handleChangeState}
/>
</div>
<div className="gip_btn">
<button className="gipc">취소</button>
<button className="gipa">추가</button>
</div>
</div>
</div>
);
};
export default GridItem;