-
Notifications
You must be signed in to change notification settings - Fork 40
/
Delete Row Data in React API
162 lines (144 loc) · 4.81 KB
/
Delete Row Data in React API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import React, { useState, useEffect } from "react";
import axios from "axios";
import {Link} from 'react-router-dom';
import { Container } from "react-bootstrap";
function User() {
const [username, setUsername] = useState("");
const [useremail, setUseremail] = useState("");
const [useraddress, setUseraddress] = useState("");
const [message, setMessage] = useState("");
const [userdata, setUserdata]= useState([]);
const handleusername = (event) => {
const user_name = event.target.value;
console.log(user_name);
setUsername(user_name);
};
const handleemail = (event) => {
const user_email = event.target.value;
console.log(user_email);
setUseremail(user_email);
};
const handleaddress = (event) => {
const user_address = event.target.value;
console.log(user_address);
setUseraddress(user_address);
};
const submitUser = async (e) => {
e.preventDefault();
const userdata = {
user_name: username,
user_email: useremail,
user_address: useraddress,
};
await axios
.post(
"http://localhost/devopsdeveloper/user/adduser",
JSON.stringify(userdata)
)
.then((result) => {
setMessage(result.data.msg);
console.log(result.data);
console.log(result.data.msg);
});
};
useEffect( ()=>{
const getuserdata= async()=>{
const requser= await fetch('http://localhost/devopsdeveloper/user/getuser/');
const resuser= await requser.json();
console.log(resuser);
setUserdata( await resuser);
}
getuserdata();
},[]);
const handleChange = async (userid)=>{
console.log(userid);
const reqdelete= await fetch(`http://localhost/devopsdeveloper/user/userdelete/${userid}`);
const resdelete= await reqdelete.json();
console.log(resdelete);
setMessage(resdelete.msg);
}
return (
<React.Fragment>
<Container className="content">
<div className="row">
<div className="col-sm-12">
<h2 className="mt-4 mb-4 fw-bold">
Insert , Update and Delete records in React Js
</h2>
{message ? (
<div className="text-success text-white">
{" "}
<h5>{message} </h5>
</div>
) : (
<></>
)}
<form onSubmit={submitUser} className="row g-3">
<div className="col-md-3">
<label className="form-label">User Name </label>
<input
type="text"
name="user_name"
className="form-control p-2"
onChange={(e) => handleusername(e)}
/>
</div>
<div className="col-md-3">
<label className="form-label">Email</label>
<input
type="text"
name="user_email"
className="form-control p-2"
onChange={(e) => handleemail(e)}
/>
</div>
<div className="col-md-3">
<label className="form-label">Address</label>
<input
type="text"
name="user_address"
className="form-control p-2"
onChange={(e) => handleaddress(e)}
/>
</div>
<div className="col-md-3">
<button type="submit" className="btn btn-primary mt-4">
Submit
</button>
</div>
</form>
</div>
<div className="col-sm-12 mt-5">
<table className="table text-white table-bordered mt-2">
<thead>
<tr>
<th scope="col">Sr. No.</th>
<th scope="col">User Name</th>
<th scope="col">User Email</th>
<th scope="col">User Address</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
{ userdata.map( (getusertbl, index)=>(
<tr key={index}>
<th scope="row">{ index+1}</th>
<td>{ getusertbl.user_name }</td>
<td>{ getusertbl.user_email }</td>
<td>{ getusertbl.user_address }</td>
<td>
<Link to={`/useredit/${ getusertbl.user_id}`} className="btn btn-success mx-2">Edit</Link>
<button className="btn btn-danger" onChange={ ()=>handleChange(getusertbl.user_id)}> Delete</button>
</td>
</tr>
) )
}
</tbody>
</table>
</div>
</div>
</Container>
</React.Fragment>
);
}
export default User;