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 (