Last active 1749834459

app.js Raw
1import axios from "axios";
2import React, { useEffect, useState } from "react";
3import "./App.css";
4
5function 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
132export default Home;
133