1
1
import { Component } from "react" ;
2
2
import axios from "axios" ;
3
3
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" ;
12
5
13
6
const api = axios . create ( {
14
7
baseURL : process . env . REACT_APP_BASE_URL ,
@@ -17,55 +10,49 @@ console.log(process.env);
17
10
export default class Home extends Component {
18
11
state = {
19
12
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 : "" ,
31
13
} ;
32
14
33
15
constructor ( props : any ) {
34
16
console . log ( "I am constructor" ) ;
35
17
//Super(): It is used to call the constructor of its parent class.
36
18
//This is required when we need to access some variables of its parent class.
37
19
super ( props ) ;
38
- this . getWeatherData ( ) ;
20
+ this . getUsersListing ( ) ;
39
21
}
40
22
41
- //http://api.openweathermap.org/data/2.5/weather?q=delhi&appid=8584915e301635faa851c9666a5108a6
42
- getWeatherData = ( ) => {
23
+ getUsersListing = ( ) => {
43
24
api
44
25
. get ( `/user/list` )
45
26
. then ( ( res ) => {
46
27
console . log ( res ?. data ?. data ) ;
47
28
this . setState ( {
48
29
users_data : res ?. data ?. data ,
49
30
} ) ;
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
- // });
60
31
} )
61
32
. 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.` ) ;
63
50
} ) ;
64
51
} ;
65
52
66
53
submitCity = ( e : any ) => {
67
54
e . preventDefault ( ) ;
68
- this . getWeatherData ( ) ;
55
+ this . getUsersListing ( ) ;
69
56
} ;
70
57
71
58
onChange = ( e : any ) => {
@@ -79,39 +66,47 @@ export default class Home extends Component {
79
66
< div className = "page-content page-container" id = "page-content" >
80
67
< div className = "padding" >
81
68
< 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" >
83
70
< div className = "card card-weather" >
84
71
< div className = "card-body" >
85
72
< div className = "row" >
86
- { this . state . users_data ? (
73
+ { this . state . users_data &&
74
+ this . state . users_data . length > 0 ? (
87
75
< Table striped bordered hover size = "sm" variant = "dark" >
88
76
< thead >
89
77
< tr >
90
78
< 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 >
94
84
</ tr >
95
85
</ thead >
96
86
< 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
+
101
+ < Button
102
+ className = "btn-sm btn-danger"
103
+ onClick = { ( ) => this . deleteUser ( v ?. _id ) }
104
+ >
105
+ Delete
106
+ </ Button >
107
+ </ td >
108
+ </ tr >
109
+ ) ) }
115
110
</ tbody >
116
111
</ Table >
117
112
) : (
0 commit comments