Last active 1749834459

Revision d1db05c79a949350835e0fc86ac42ff8cd9b6885

app.js Raw
1import React, { Component } from 1 react1;
2import "./App.css"
3class Home extends Component {
4url = "http://localhost:8001/courses/"
5state = {
6show: false,
7data:[],
8rating:
9}
10componentDidMount = () => {
11this.handleGetData()
12}
13handleGetData = () => {
14fetch(this.url + "get")
15.then((res) => res.json())
16.then((json) => {
17this.setState({ data: json })
18})
19}
20handleApply = async (id) => {
21const requestoption = {
22method: post1,
23headers: { Content-Type1: 1 application/json }
24}
25fetch(this.url + 'enroll/' + id, requestoption) then(() => {
26this.handleGetData()
27})
28};
29handleRating = (e) => {
30this.setState({ rating: e.target.value })
31}
32handleAddRating = async (id) => {
33const requestoption = {
34method: 'PATCH',
35headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ rating: this.state.rating }) }
36fetch(this.url + "rating/" + id, requestoption)
37then(() => {
38this.handleGetData()
39})
40}
41handleDrop = async (id) => {
42const requestoption = {
43method: 'DELETE',
44headers: { 'Content-Type': 'application/json' },
45}
46fetch(this.url + "drop/" + id, requestoption)
47then(() => {
48this.handleGetData()
49})
50}
51render() {
52return (
53<div className="home">
54<header>
55<h2>ABC Learning</h2>
56</header>
57<div className="cardContainer">
58{this.state.data.map(courses => { return (
59<div className="card">
60<ul>
61<div className="header">
62<li>{courses.courseName}</li>
63<li>{courses.courseDept}</li>
64<li>{courses.description}</li>
65{courses.isApplied &&
66<li>
67{!courses.isRated &&
68<li>Rate:
69<select
70className="rating" name="rating" onChange={this.handleRating}>
71<option>l</option>
72<option>2</option> <option>3</option> <option>4</option> <option>5</option> </select>
73<button className="rate" onClick={() => this.handleAddRating(courses._id)}>Add</button>
74</li>
75}
76{courses.isApplied && <button className="drop" onClick={() => this.handleDrop(courses._id)}>Drop Course</button>
77~ }
78</li>
79}
80{1 courses.isApplied && <lixbutton className="btn" onClick={() => this.handleApply(courses._id)}>Apply</buttonx/li>
81}
82</div> <div className="footer">
83<li>{courses.duration} hrs .
84{courses.noOfRatings} Ratings . {courses.rating}/5</li>
85</div>
86</ul> </div>
87</div>
88</div>
89)
90}
91}
92export default Home
93