app.js
· 2.1 KiB · JavaScript
Raw
import React, { Component } from 1 react1;
import "./App.css"
class Home extends Component {
url = "http://localhost:8001/courses/"
state = {
show: false,
data:[],
rating:
}
componentDidMount = () => {
this.handleGetData()
}
handleGetData = () => {
fetch(this.url + "get")
.then((res) => res.json())
.then((json) => {
this.setState({ data: json })
})
}
handleApply = async (id) => {
const requestoption = {
method: ’post1,
headers: { ’Content-Type1: 1 application/json’ }
};
fetch(this.url + 'enroll/' + id, requestoption) •then(() => {
this.handleGetData()
})
};
handleRating = (e) => {
this.setState({ rating: e.target.value })
}
handleAddRating = async (id) => {
const requestoption = {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ rating: this.state.rating }) }
fetch(this.url + "rating/" + id, requestoption)
•then(() => {
this.handleGetData()
})
}
handleDrop = async (id) => {
const requestoption = {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
};
fetch(this.url + "drop/" + id, requestoption)
•then(() => {
this.handleGetData()
})
}
render() {
return (
<div className="home">
<header>
<h2>ABC Learning</h2>
</header>
<div className="cardContainer">
{this.state.data.map(courses => { return (
<div className="card">
<ul>
<div className="header">
<li>{courses.courseName}</li>
<li>{courses.courseDept}</li>
<li>{courses.description}</li>
{courses.isApplied &&
<li>
{!courses.isRated &&
<li>Rate:
<select
className="rating" name="rating" onChange={this.handleRating}>
<option>l</option>
<option>2</option> <option>3</option> <option>4</option> <option>5</option> </select>
<button className="rate" onClick={() => this.handleAddRating(courses._id)}>Add</button>
</li>
}
{courses.isApplied && <button className="drop" onClick={() => this.handleDrop(courses._id)}>Drop Course</button>
~ }
</li>
}
{1 courses.isApplied && <lixbutton className="btn" onClick={() => this.handleApply(courses._id)}>Apply</buttonx/li>
}
</div> <div className="footer">
<li>{courses.duration} hrs .
{courses.noOfRatings} Ratings . {courses.rating}/5</li>
</div>
</ul> </div>
</div>
</div>
);
}
}
export default Home
1 | import React, { Component } from 1 react1; |
2 | import "./App.css" |
3 | class Home extends Component { |
4 | url = "http://localhost:8001/courses/" |
5 | state = { |
6 | show: false, |
7 | data:[], |
8 | rating: |
9 | } |
10 | componentDidMount = () => { |
11 | this.handleGetData() |
12 | } |
13 | handleGetData = () => { |
14 | fetch(this.url + "get") |
15 | .then((res) => res.json()) |
16 | .then((json) => { |
17 | this.setState({ data: json }) |
18 | }) |
19 | } |
20 | handleApply = async (id) => { |
21 | const requestoption = { |
22 | method: ’post1, |
23 | headers: { ’Content-Type1: 1 application/json’ } |
24 | }; |
25 | fetch(this.url + 'enroll/' + id, requestoption) •then(() => { |
26 | this.handleGetData() |
27 | }) |
28 | }; |
29 | handleRating = (e) => { |
30 | this.setState({ rating: e.target.value }) |
31 | } |
32 | handleAddRating = async (id) => { |
33 | const requestoption = { |
34 | method: 'PATCH', |
35 | headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ rating: this.state.rating }) } |
36 | fetch(this.url + "rating/" + id, requestoption) |
37 | •then(() => { |
38 | this.handleGetData() |
39 | }) |
40 | } |
41 | handleDrop = async (id) => { |
42 | const requestoption = { |
43 | method: 'DELETE', |
44 | headers: { 'Content-Type': 'application/json' }, |
45 | }; |
46 | fetch(this.url + "drop/" + id, requestoption) |
47 | •then(() => { |
48 | this.handleGetData() |
49 | }) |
50 | } |
51 | render() { |
52 | return ( |
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 |
70 | className="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 | } |
92 | export default Home |
93 |