Skip to content

Commit 3c67338

Browse files
committed
Updated Delete API
1 parent 584c646 commit 3c67338

File tree

10 files changed

+78
-31
lines changed

10 files changed

+78
-31
lines changed

nodejs/routes/applicant.routes.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ router.get('/update-applicant/:id', (req, res) =>{
4848
});
4949
// url: http://localhost:4000/applicants/update-applicant/document_id
5050

51-
// update a specified applicant data
51+
// update a specified applicant data using PUT API method
5252
router.put('/update-applicant/:id', (req, res) => {
5353
applicantSchema.findByIdAndUpdate(req.params.id, req.body)
5454
.then(data => res.json({ msg: 'Data updated successfully' }))
5555
.catch(err => res.status(400).json({ error: 'Unable to update this data' }));
5656
});
57+
58+
/* Delete a specific applicant data by id requested by api */
59+
router.delete('/delete-applicant/:id', (req, res) => {
60+
applicantSchema.findByIdAndRemove(req.params.id, req.body)
61+
.then(data => res.json({msg:'Data is successfully deleted.'}))
62+
.catch(err => res.status(400).json({error: 'Data not deleted'}));
63+
});
64+
5765
// export the router
5866
module.exports = router

react/reactapp/package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

react/reactapp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"react": "^18.2.0",
1111
"react-dom": "^18.2.0",
1212
"react-icons": "^4.8.0",
13+
"react-minimal-side-navigation": "^1.9.2",
1314
"react-router-dom": "^6.9.0",
1415
"react-scripts": "5.0.1",
1516
"web-vitals": "^2.1.4",

react/reactapp/src/App.css

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@
3737
}
3838
}
3939
.container {
40-
display: block;
41-
max-width: 90%;
42-
width: 100%;
43-
margin: 50px auto;
44-
padding: 10px;
45-
border-radius: 0 #8c6a55;
46-
height: 100%;
40+
margin-top: 100px;
41+
padding: 50px;
4742

4843
}
4944

react/reactapp/src/App2.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import './App.css';
1010
import Login from './components/login/Login'; // Login component from components
1111
import Dashboard from './components/Dashboard/Dashboard' // import Dashboard component
1212
import UseToken from './components/useToken';
13-
import Navbar from './components/Header/Navbar'
13+
import Navbar from './components/navbar/Navbar'
1414
import ApplicationForm from './components/Dashboard/ApplicationForm'
1515
import ShowApplicants from './components/Dashboard/ShowApplicants'
1616
import EditApplicant from './components/Dashboard/EditApplicant'
@@ -28,9 +28,7 @@ function App() {
2828
<>
2929
<Navbar />
3030
<div className="container">
31-
32-
<BrowserRouter>
33-
<Routes>
31+
<Routes>
3432
<Route path="/" element={<ApplicationForm />}>
3533
</Route>
3634
</Routes>
@@ -47,10 +45,9 @@ function App() {
4745
<Route path="/edit-applicant/:id" element={<EditApplicant />}>
4846
</Route>
4947
</Routes>
50-
</BrowserRouter>
5148
</div>
5249
</>
5350
);
5451
}
5552

56-
export default App;
53+
export default App;

react/reactapp/src/components/Dashboard/EditApplicant.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ function EditApplicant(){
88
// define states
99
const [applicant, setApplicant] = useState([])
1010
const {id} = useParams();
11-
/* useParams is one of the several react hooks in ract router
11+
/* useParams is one of the several react hooks in react router
1212
it used to retreive route paramaters from the component
1313
rendered by the matching route
1414
*/
1515
// add title
1616
useEffect(() =>{
17-
document.title ='Aplicants List';
17+
document.title ='Edit applicant';
1818
})
1919

2020
// API: get a single applicant data by ID

react/reactapp/src/components/Dashboard/ShowApplicants.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import React from 'react';
44
import './table.css';
55
import axios from 'axios';
66
import {useState, useEffect} from 'react';
7-
import {Link} from 'react-router-dom'
7+
import {Link, useNavigate} from 'react-router-dom'
88

99

1010
function ShowApplicants(){
1111

1212
// define state variable to store data from api
1313
const [applicants, setApplicants] = useState([]);
14+
const navigate = useNavigate()
1415

1516
// add title
1617
useEffect(() =>{
@@ -22,14 +23,32 @@ function ShowApplicants(){
2223
axios.get('http://localhost:4000/applicants/list-applicants')
2324
.then((res) =>{
2425
setApplicants(res.data); // set api data to useState as an array
26+
2527
})
2628
.catch(err => { // catch error message
2729
console.log('Data not found.' +err.message)
2830
});
2931
}, []);
3032

3133
//console.log(applicants)
34+
/*
35+
DELETE API:
36+
onclickdelete a single data from database
37+
the api call with a selected id to delete
38+
39+
*/
40+
const onClickDelete =(id) =>{
41+
axios.delete(`http://localhost:4000/applicants/delete-applicant/${id}`)
42+
.then((res) => {
43+
navigate('/') // navigate to list applicates
44+
})
45+
.catch((err) =>{
46+
console.log('Error to delete data.'+err.message)
47+
48+
});
49+
};
3250

51+
// return the layout
3352
return (
3453

3554
<div className="table-wraper">
@@ -68,7 +87,8 @@ function ShowApplicants(){
6887
<Link className="edit-link" to={`/edit-applicant/${data._id}`}>
6988
<i className="fa-solid fa-pen-to-square"></i></Link>
7089
&nbsp;&nbsp;&nbsp;&nbsp;
71-
<Link className="edit-link" to={"/delete-applicant/" + data._id}>
90+
<Link onClick = {() => { window.confirm('Are you sure you wish to delete this data?', )
91+
&& onClickDelete(data._id)}}>
7292
<i className="fa-sharp fa-solid fa-trash" style={{color:'#f41032'}}></i>
7393
</Link>
7494
</td>

react/reactapp/src/components/Dashboard/table.css

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ h2{
1515
}
1616

1717
.fl-table {
18-
border-radius: 5px;
19-
font-size: 12px;
20-
font-weight: normal;
21-
border: none;
22-
border-collapse: collapse;
23-
width: 100%;
24-
max-width: 100%;
25-
white-space: nowrap;
26-
background-color: white;
18+
border-radius: 5px;
19+
font-size: 12px;
20+
font-weight: normal;
21+
border: none;
22+
border-collapse: collapse;
23+
width: 100%;
24+
max-width: 100%;
25+
white-space: nowrap;
26+
background-color: white;
2727
}
2828

2929
.fl-table td, .fl-table th {

react/reactapp/src/components/Header/Navbar.js renamed to react/reactapp/src/components/navbar/Navbar.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// just checkout the css file in nav.css and modify the layout
44

55
import React, {useState, useEffect} from 'react'
6-
import { BrowserRouter, Link } from "react-router-dom";
6+
import { Routes, Link } from "react-router-dom";
77
import './nav.css'
88

99
export default function Navbar() {
@@ -36,19 +36,19 @@ export default function Navbar() {
3636
<nav>
3737
{(toggleMenu || screenWidth > 500) && (
3838
<ul className="list">
39-
<BrowserRouter>
39+
<Routes>
4040
<Link to={'/application'}>
4141
<li className="items">Application</li>
4242
</Link>
4343
<Link to={'/list-applicants'}>
4444
<li className="items">Show Applicants</li>
4545
</Link>
46-
</BrowserRouter>
46+
</Routes>
4747

4848
</ul>
4949
)}
5050

5151
<button onClick={toggleNav} className="btn">=</button>
5252
</nav>
5353
)
54-
}
54+
}

0 commit comments

Comments
 (0)