app.js
· 3.9 KiB · JavaScript
Raw
import axios from "axios";
import React, { useEffect, useState } from "react";
import "./App.css";
function Home() {
const [data, setData] = useState([]);
const [rating, setRating] = useState(1);
const [refresh, setRefresh] = useState(false); // To trigger re-render on data changes
// Fetch course data
useEffect(() => {
const fetchData = async () => {
const response = await axios.get("http://localhost:8001/courses/get");
setData(response.data);
};
fetchData();
}, [refresh]);
// Apply for a course
const handleApply = async (id) => {
try {
const response = await axios.post(`http://localhost:8001/courses/enroll/${id}`);
alert("You have successfully enrolled for this course");
setRefresh(!refresh); // Refresh data after applying
} catch (error) {
alert("Error applying for the course");
}
};
// Update rating state on change
const handleRating = (event) => {
setRating(event.target.value);
};
// Add a rating to a course
const handleAddRating = async (id) => {
try {
await axios.patch(`http://localhost:8001/courses/rating/${id}`, { rating: Number(rating)});
setRefresh(!refresh); // Refresh data after rating
} catch (error) {
alert("Error adding rating");
}
};
// Drop a course
const handleDrop = async (id) => {
try {
await axios.delete(`http://localhost:8001/courses/drop/${id}`);
setRefresh(!refresh); // Refresh data after dropping
} catch (error) {
alert("Error dropping the course");
}
};
return (
<div className="home">
<header>
<h2>ABC Learning</h2>
</header>
<div className="cardContainer">
{data.map((course) => (
<div className="card" key={course._id}>
<ul>
<div className="header">
{/* Render course details */}
<li data-testid="course-name">{course.courseName}</li>
<li data-testid="course-dept">{course.courseDept}</li>
<li data-testid="course-description">{course.description}</li>
{/* Rating functionality */}
<li>
<>
Rate:
<select
className="rating"
name="rating"
data-testid="select-box"
value={rating}
onChange={handleRating}
>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<button
className="rate"
data-testid="add-rate"
onClick={() => handleAddRating(course._id)}
>
Add
</button>
</>
</li>
{/* Drop course button */}
<li>
<button
className="drop"
data-testid="drop"
onClick={() => handleDrop(course._id)}
>
Drop Course
</button>
</li>
{/* Apply button */}
<li>
<button
className="btn"
data-testid="apply"
onClick={() => handleApply(course._id)}
>
Apply
</button>
</li>
</div>
<div className="footer">
{/* Footer contents can go here */}
<li data-testid="footer">Course Footer</li>
</div>
</ul>
</div>
))}
</div>
</div>
);
}
export default Home;
1 | import axios from "axios"; |
2 | import React, { useEffect, useState } from "react"; |
3 | import "./App.css"; |
4 | |
5 | function Home() { |
6 | const [data, setData] = useState([]); |
7 | const [rating, setRating] = useState(1); |
8 | const [refresh, setRefresh] = useState(false); // To trigger re-render on data changes |
9 | |
10 | // Fetch course data |
11 | useEffect(() => { |
12 | const fetchData = async () => { |
13 | const response = await axios.get("http://localhost:8001/courses/get"); |
14 | setData(response.data); |
15 | }; |
16 | fetchData(); |
17 | }, [refresh]); |
18 | |
19 | // Apply for a course |
20 | const handleApply = async (id) => { |
21 | try { |
22 | const response = await axios.post(`http://localhost:8001/courses/enroll/${id}`); |
23 | alert("You have successfully enrolled for this course"); |
24 | setRefresh(!refresh); // Refresh data after applying |
25 | } catch (error) { |
26 | alert("Error applying for the course"); |
27 | } |
28 | }; |
29 | |
30 | // Update rating state on change |
31 | const handleRating = (event) => { |
32 | setRating(event.target.value); |
33 | }; |
34 | |
35 | // Add a rating to a course |
36 | const handleAddRating = async (id) => { |
37 | try { |
38 | await axios.patch(`http://localhost:8001/courses/rating/${id}`, { rating: Number(rating)}); |
39 | |
40 | setRefresh(!refresh); // Refresh data after rating |
41 | } catch (error) { |
42 | alert("Error adding rating"); |
43 | } |
44 | }; |
45 | |
46 | // Drop a course |
47 | const handleDrop = async (id) => { |
48 | try { |
49 | await axios.delete(`http://localhost:8001/courses/drop/${id}`); |
50 | setRefresh(!refresh); // Refresh data after dropping |
51 | } catch (error) { |
52 | alert("Error dropping the course"); |
53 | } |
54 | }; |
55 | |
56 | return ( |
57 | <div className="home"> |
58 | <header> |
59 | <h2>ABC Learning</h2> |
60 | </header> |
61 | <div className="cardContainer"> |
62 | {data.map((course) => ( |
63 | <div className="card" key={course._id}> |
64 | <ul> |
65 | <div className="header"> |
66 | {/* Render course details */} |
67 | <li data-testid="course-name">{course.courseName}</li> |
68 | <li data-testid="course-dept">{course.courseDept}</li> |
69 | <li data-testid="course-description">{course.description}</li> |
70 | |
71 | {/* Rating functionality */} |
72 | <li> |
73 | <> |
74 | Rate: |
75 | <select |
76 | className="rating" |
77 | name="rating" |
78 | data-testid="select-box" |
79 | value={rating} |
80 | onChange={handleRating} |
81 | > |
82 | <option value="1">1</option> |
83 | <option value="2">2</option> |
84 | <option value="3">3</option> |
85 | <option value="4">4</option> |
86 | <option value="5">5</option> |
87 | </select> |
88 | <button |
89 | className="rate" |
90 | data-testid="add-rate" |
91 | onClick={() => handleAddRating(course._id)} |
92 | > |
93 | Add |
94 | </button> |
95 | </> |
96 | </li> |
97 | |
98 | {/* Drop course button */} |
99 | <li> |
100 | <button |
101 | className="drop" |
102 | data-testid="drop" |
103 | onClick={() => handleDrop(course._id)} |
104 | > |
105 | Drop Course |
106 | </button> |
107 | </li> |
108 | |
109 | {/* Apply button */} |
110 | <li> |
111 | <button |
112 | className="btn" |
113 | data-testid="apply" |
114 | onClick={() => handleApply(course._id)} |
115 | > |
116 | Apply |
117 | </button> |
118 | </li> |
119 | </div> |
120 | <div className="footer"> |
121 | {/* Footer contents can go here */} |
122 | <li data-testid="footer">Course Footer</li> |
123 | </div> |
124 | </ul> |
125 | </div> |
126 | ))} |
127 | </div> |
128 | </div> |
129 | ); |
130 | } |
131 | |
132 | export default Home; |
133 |