Skip to content

Commit 0fa9e29

Browse files
committedJun 15, 2024
listing and delete api implemented
1 parent 788b893 commit 0fa9e29

File tree

1 file changed

+52
-57
lines changed

1 file changed

+52
-57
lines changed
 

‎src/components/home.tsx

+52-57
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { Component } from "react";
22
import axios from "axios";
33
import Swal from "sweetalert2";
4-
import {
5-
Form,
6-
Row,
7-
Col,
8-
InputGroup,
9-
FormControl,
10-
Table,
11-
} from "react-bootstrap";
4+
import { Button, Table } from "react-bootstrap";
125

136
const api = axios.create({
147
baseURL: process.env.REACT_APP_BASE_URL,
@@ -17,55 +10,49 @@ console.log(process.env);
1710
export default class Home extends Component {
1811
state = {
1912
users_data: [],
20-
city: "Delhi",
21-
country: "IN",
22-
rdate: "",
23-
rday: "",
24-
icon: "",
25-
currentTemp: "",
26-
tempDesc: "",
27-
name: "",
28-
email: "",
29-
countryCode: "",
30-
mobile: "",
3113
};
3214

3315
constructor(props: any) {
3416
console.log("I am constructor");
3517
//Super(): It is used to call the constructor of its parent class.
3618
//This is required when we need to access some variables of its parent class.
3719
super(props);
38-
this.getWeatherData();
20+
this.getUsersListing();
3921
}
4022

41-
//http://api.openweathermap.org/data/2.5/weather?q=delhi&appid=8584915e301635faa851c9666a5108a6
42-
getWeatherData = () => {
23+
getUsersListing = () => {
4324
api
4425
.get(`/user/list`)
4526
.then((res) => {
4627
console.log(res?.data?.data);
4728
this.setState({
4829
users_data: res?.data?.data,
4930
});
50-
// this.setState({
51-
// currentTemp: res.data.main.temp,
52-
// country: res.data.sys.country,
53-
// icon: `http://openweathermap.org/img/w/${res.data.weather[0].icon}.png`,
54-
// tempDesc: res.data.weather[0].description,
55-
// rdate: `${Date().toLocaleString().split(" ")[1]} ${
56-
// Date().toLocaleString().split(" ")[2]
57-
// }, ${Date().toLocaleString().split(" ")[3]} `,
58-
// rday: Date().toLocaleString().split(" ")[0],
59-
// });
6031
})
6132
.catch((error) => {
62-
Swal.fire(`Data not found on city name ${this.state.city}.`);
33+
Swal.fire(`Users data not found.`);
34+
});
35+
};
36+
37+
deleteUser = (id: string) => {
38+
api
39+
.delete(`/user/remove`, {
40+
data: {
41+
id: id,
42+
},
43+
})
44+
.then((res) => {
45+
Swal.fire(`Users deleted.`);
46+
this.getUsersListing();
47+
})
48+
.catch((error) => {
49+
Swal.fire(`Users data not found.`);
6350
});
6451
};
6552

6653
submitCity = (e: any) => {
6754
e.preventDefault();
68-
this.getWeatherData();
55+
this.getUsersListing();
6956
};
7057

7158
onChange = (e: any) => {
@@ -79,39 +66,47 @@ export default class Home extends Component {
7966
<div className="page-content page-container" id="page-content">
8067
<div className="padding">
8168
<div className="row container d-flex justify-content-center">
82-
<div className="col-lg-8 col-md-10 col-sm-12 grid-margin stretch-card">
69+
<div className="col-lg-10 col-md-10 col-sm-12 grid-margin stretch-card">
8370
<div className="card card-weather">
8471
<div className="card-body">
8572
<div className="row">
86-
{this.state.users_data ? (
73+
{this.state.users_data &&
74+
this.state.users_data.length > 0 ? (
8775
<Table striped bordered hover size="sm" variant="dark">
8876
<thead>
8977
<tr>
9078
<th>#</th>
91-
<th>First Name</th>
92-
<th>Last Name</th>
93-
<th>Username</th>
79+
<th>Name</th>
80+
<th>Email</th>
81+
<th>Phone</th>
82+
<th>Role</th>
83+
<th>Action</th>
9484
</tr>
9585
</thead>
9686
<tbody>
97-
<tr>
98-
<td>1</td>
99-
<td>Mark</td>
100-
<td>Otto</td>
101-
<td>@mdo</td>
102-
</tr>
103-
<tr>
104-
<td>2</td>
105-
<td>Jacob</td>
106-
<td>Thornton</td>
107-
<td>@fat</td>
108-
</tr>
109-
<tr>
110-
<td>3</td>
111-
<td>Mark</td>
112-
<td>Otto</td>
113-
<td>@mdo</td>
114-
</tr>
87+
{users_data.map((v: any, i) => (
88+
<tr>
89+
<td>{i + 1}</td>
90+
<td>{v?.name}</td>
91+
<td>{v?.email}</td>
92+
<td>
93+
+{v?.countryCode}-{v?.mobile}
94+
</td>
95+
<td>{v?.role}</td>
96+
<td>
97+
<Button className="btn-sm btn-warning">
98+
Edit
99+
</Button>
100+
&nbsp;
101+
<Button
102+
className="btn-sm btn-danger"
103+
onClick={() => this.deleteUser(v?._id)}
104+
>
105+
Delete
106+
</Button>
107+
</td>
108+
</tr>
109+
))}
115110
</tbody>
116111
</Table>
117112
) : (

0 commit comments

Comments
 (0)
Please sign in to comment.