Skip to content

Commit 883b2c1

Browse files
committed
Add create module button
1 parent 46d55c8 commit 883b2c1

File tree

1 file changed

+92
-3
lines changed

1 file changed

+92
-3
lines changed

frontend/src/components/pages/Course.js

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,65 @@
11
import React, { useEffect, useState } from 'react';
22
import { useParams } from 'react-router-dom';
3+
import Cookies from 'js-cookie';
4+
import Notiflix from 'notiflix';
5+
import {Modal} from '../Modal.js';
36

47
export function Course() {
58
const { courseID } = useParams();
69
const [courseInfo, setCourseInfo] = useState(null);
10+
const [userInfo, setUserInfo] = useState(null);
711
const [error, setError] = useState(null);
12+
const [newModuleName, setNewModuleName] = useState({
13+
title: "",
14+
});
15+
16+
const handleChange = (e) => {
17+
const { name, value } = e.target;
18+
setNewModuleName({[name]: value });
19+
};
20+
21+
const handleCreateModule = async (e) =>{
22+
console.log(newModuleName)
23+
if (newModuleName.title === ""){
24+
return
25+
}
26+
27+
const userId = Cookies.get('userId');
28+
try {
29+
const response = await fetch(`http://localhost:4000/create-module/${userId}/${courseID}`, {
30+
method: "POST",
31+
headers: {
32+
"Content-Type": "application/json",
33+
},
34+
body: JSON.stringify({
35+
title: newModuleName.title,
36+
}),
37+
});
38+
39+
const data = await response.json();
40+
if (response.ok) {
41+
Notiflix.Notify.success("Module creation successful!");
42+
setTimeout(() => {
43+
window.location.reload();
44+
}, 500);
45+
} else {
46+
Notiflix.Notify.failure(data.message || "Module creation failed");
47+
}
48+
} catch (error) {
49+
Notiflix.Notify.failure("Error occurred during module creation");
50+
}
51+
};
852

953
useEffect(() => {
10-
fetch(`http://localhost:4000/course/${courseID}`)
54+
const userId = Cookies.get('userId');
55+
56+
if (!userId) {
57+
setError('User ID not found');
58+
return;
59+
}
60+
61+
async function fetchCourse() {
62+
fetch(`http://localhost:4000/course/${courseID}`)
1163
.then((response) => {
1264
if (!response.ok) {
1365
throw new Error('Network response was not ok');
@@ -16,10 +68,26 @@ export function Course() {
1668
})
1769
.then((data) => setCourseInfo(data.course))
1870
.catch((error) => setError(error.message));
71+
}
72+
73+
async function fetchUser() {
74+
await fetch(`http://localhost:4000/user/${userId}`)
75+
.then((response) => {
76+
if (!response.ok) {
77+
throw new Error('Network response was not ok');
78+
}
79+
return response.json();
80+
})
81+
.then((data) => setUserInfo(data.user))
82+
.catch((error) => setError(error.message));
83+
}
84+
85+
fetchCourse();
86+
fetchUser();
1987
}, [courseID]);
2088

2189
if (error) return <p>Error: {error}</p>;
22-
if (!courseInfo) return <p>Loading...</p>;
90+
if (!courseInfo || !userInfo) return <p>Loading...</p>;
2391

2492
var moduleList = [];
2593
//Push all modules into moduleList
@@ -46,9 +114,30 @@ export function Course() {
46114
);
47115
});
48116

117+
let createButton = null;
118+
if (userInfo.role === "educator"){
119+
createButton = <Modal
120+
title={"Create New Module"}
121+
trigger={
122+
<div className="bg-blue-500 p-2 rounded shadow hover:bg-blue-700 m-auto text-center text-sm text-white font-semibold hover:cursor-pointer" >
123+
<div>+ Add Module</div>
124+
</div>
125+
}
126+
inputFields={{
127+
title : "Module Name",
128+
}}
129+
changeHandler={handleChange}
130+
confirmHandler={handleCreateModule}
131+
/>
132+
}
133+
49134
return (
50135
<div className="p-6">
51-
<h1 className="text-2xl font-bold mb-4">{courseInfo.title}</h1>
136+
<div className="flex justify-between flex-wrap mb-5">
137+
<h1 className="text-2xl font-bold mb-4">{courseInfo.title}</h1>
138+
{createButton}
139+
</div>
140+
52141
<div className="grid grid-cols-1 gap-6">
53142
{moduleList}
54143
</div>

0 commit comments

Comments
 (0)