-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathManageBook.js
More file actions
174 lines (164 loc) · 4.64 KB
/
ManageBook.js
File metadata and controls
174 lines (164 loc) · 4.64 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
168
169
170
171
172
173
174
import React, { useEffect, useState } from "react";
import "../assets/styles/ManageBook.css";
import { useAuth } from "../utils/auth";
import TutorModal from "./TutorModal";
/**Yian Chen
* component that renders booking schedule of student
* @returns JSX of manage booking UI
*/
function ManageBook() {
const auth = useAuth();
const [schedule, setSchedule] = useState([]);
const [remove, setRemove] = useState(false);
const [modalOpen, setModalOpen] = useState(false);
const [tutorInfo, setTutorInfo] = useState(null);
// const navigate = useNavigate();
//function that closes modal when triggered
const handleModal = () => {
setModalOpen(false);
};
/**Yian
* this function gets the schedule of the user and makes a copy to schedule
*/
useEffect(() => {
try {
const fetchSchedule = async () => {
const res = await fetch("/api/getSchedule");
const resSchedule = await res.json();
const sched = resSchedule.data.schedule;
setSchedule([...sched]);
};
//only fetch schedule if user is logged in
if (auth.user) {
fetchSchedule();
}
} catch (err) {
console.error(err);
}
}, [remove]);
/**
* Yian Chen
* function that removes class from database
* @param {String} date
* @param {String} time
* @param {String} tutor
*/
const removeClass = async (date, time, tutor) => {
try {
let scheduleObj = {};
scheduleObj.user = auth.user;
scheduleObj.date = date;
scheduleObj.time = time;
scheduleObj.tutor = tutor;
const res = await fetch("/deleteClass", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(scheduleObj),
});
const resMsg = await res.json();
setRemove(true);
alert(resMsg.msg);
} catch (err) {
console.error(err);
alert(`There was an error, please contact customer support`);
}
};
/**Yian Chen
* function that renders delete button
* @param {String} date
* @param {String} time
* @param {String} tutor
* @returns
*/
const renderDeleteBtn = (date, time, tutor) => {
return (
<button
className="deleteBtnBook"
onClick={() => {
// Good to know that I can use this window.confirm thing to handle the confirmation for delete actions
const confirmBox = window.confirm("Are you sure you want to cancel?");
if (confirmBox === true) {
removeClass(date, time, tutor);
}
}}
>
cancel class
</button>
);
};
/**
* Yian Chen
* function that gets TutorInfo when clicked
* @param {int} tutor_id
*/
const getTutorInfo = async (tutor_id) => {
try {
const res = await fetch(`/book/tutors/${tutor_id}`);
const tutor = await res.json();
setTutorInfo(tutor.data);
setModalOpen(true);
} catch (err) {
console.error(`there was an error ${err}`);
}
};
/**
* function that renders class button
* @param {int} tutor_id
* @returns button UI
*/
const renderClassInfoBtn = (tutor_id) => {
return (
<button
className="checkClassBtn"
onClick={(evt) => {
evt.preventDefault();
getTutorInfo(tutor_id);
}}
>
details
</button>
);
};
return (
<div className="mainDivBook" role="main">
<div className="innerDivBook">
<h1 className="titleBook">My Schedule</h1>
{schedule.map((i, idx) => {
return (
<div className="line1" key={`${i.date}_${idx}`}>
<div className="scheduleDivBook">
<p className="datep">
<strong>Date :</strong> {i.date}
</p>
<p className="timep">
<strong>Time :</strong> {i.time}
</p>
<p className="tutorp">
<strong>Tutor :</strong> {i.tutor}
</p>
<p className="subjectp">
<strong>Subject :</strong> {i.subject}
</p>
<span className="detailBtnSpanBook">
{renderClassInfoBtn(i.tutor_ID)}
</span>
<span className="deleteBtnSpanBook">
{renderDeleteBtn(i.date, i.time, i.tutor)}
</span>
</div>
</div>
);
})}
<div className="tutorModal">
{modalOpen ? (
<TutorModal handleModal={handleModal} tutorInfo={tutorInfo} />
) : null}
</div>
</div>
</div>
);
}
ManageBook.propTypes = {};
export default ManageBook;