Skip to content

Commit b0e81b5

Browse files
committed
65
1 parent 1c459b0 commit b0e81b5

File tree

5 files changed

+38
-95
lines changed

5 files changed

+38
-95
lines changed

db.json

Lines changed: 4 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,11 @@
11
{
22
"posts": [
33
{
4-
"title": "reactjs123123",
5-
"body": "reactjs",
6-
"createdAt": 1644556415774,
7-
"id": 1,
8-
"publish": true
9-
},
10-
{
11-
"title": "reactjs345345",
12-
"body": "reactjs",
13-
"createdAt": 1644556415774,
14-
"id": 2,
15-
"publish": true
16-
},
17-
{
18-
"title": "reactjs456456",
19-
"body": "reactjs",
20-
"createdAt": 1644556415774,
21-
"id": 3,
22-
"publish": true
23-
},
24-
{
25-
"title": "reactjs1111",
26-
"body": "reactjs",
27-
"createdAt": 1644556415774,
28-
"id": 4,
29-
"publish": true
30-
},
31-
{
32-
"title": "redux",
33-
"body": "redux",
34-
"publish": false,
35-
"createdAt": 1646350520510,
36-
"id": 6
37-
},
38-
{
39-
"title": "redux 2",
40-
"body": "redux 2",
41-
"publish": true,
42-
"createdAt": 1646350534135,
43-
"id": 7
44-
},
45-
{
46-
"title": "react",
47-
"body": "react",
48-
"publish": false,
49-
"createdAt": 1647211176299,
50-
"id": 8
51-
},
52-
{
53-
"title": "recoiljs",
54-
"body": "recoiljs",
55-
"publish": false,
56-
"createdAt": 1647211385146,
57-
"id": 9
58-
},
59-
{
60-
"title": "test2",
61-
"body": "test2",
62-
"publish": false,
63-
"createdAt": 1647211545153,
64-
"id": 12
65-
},
66-
{
67-
"title": "test3",
68-
"body": "test3",
69-
"publish": false,
70-
"createdAt": 1647211550036,
71-
"id": 13
72-
},
73-
{
74-
"title": "test4",
75-
"body": "test4",
76-
"publish": true,
77-
"createdAt": 1647211557225,
78-
"id": 14
79-
},
80-
{
81-
"title": "test5",
82-
"body": "test5",
83-
"publish": false,
84-
"createdAt": 1647211568779,
85-
"id": 15
86-
},
87-
{
88-
"title": "tetet",
89-
"body": "body",
4+
"title": "2",
5+
"body": "2",
906
"publish": false,
91-
"createdAt": 1653630771975,
92-
"id": 17
7+
"createdAt": 1655353112963,
8+
"id": 10
939
}
9410
]
9511
}

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"react-dom": "^17.0.2",
1414
"react-router-dom": "^5.3.0",
1515
"react-scripts": "4.0.3",
16+
"uuid": "^3.4.0",
1617
"web-vitals": "^1.0.1"
1718
},
1819
"scripts": {

src/components/BlogList.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Pagination from './Pagination';
77
import { useLocation } from 'react-router-dom';
88
import propTypes from 'prop-types';
99
import Toast from '../components/Toast';
10+
import { v4 as uuidv4 } from 'uuid';
1011

1112
const BlogList = ({ isAdmin }) => {
1213
const history = useHistory();
@@ -19,6 +20,7 @@ const BlogList = ({ isAdmin }) => {
1920
const [numberOfPosts, setNumberOfPosts] = useState(0);
2021
const [numberOfPages, setNumberOfPages] = useState(0);
2122
const [searchText, setSearchText] = useState('');
23+
const [toasts, setToasts] = useState([]);
2224
const limit = 5;
2325

2426
useEffect(() => {
@@ -58,11 +60,31 @@ const BlogList = ({ isAdmin }) => {
5860
getPosts(parseInt(pageParam) || 1)
5961
}, []);
6062

63+
const addToast = (toast) => {
64+
const toastWithId = {
65+
...toast,
66+
id: uuidv4()
67+
}
68+
setToasts(prev => [...prev, toastWithId]);
69+
};
70+
71+
const deleteToast = (id) => {
72+
const filteredToasts = toasts.filter(toast => {
73+
return toast.id !== id;
74+
});
75+
76+
setToasts(filteredToasts);
77+
}
78+
6179
const deleteBlog = (e, id) => {
6280
e.stopPropagation();
6381

6482
axios.delete(`http://localhost:3001/posts/${id}`).then(() => {
6583
setPosts(prevPosts => prevPosts.filter(post => post.id !== id));
84+
addToast({
85+
text: 'Successfully deleted',
86+
type: 'success'
87+
});
6688
});
6789
};
6890

@@ -104,10 +126,8 @@ const BlogList = ({ isAdmin }) => {
104126
return (
105127
<div>
106128
<Toast
107-
toasts={[
108-
{ text: 'error'},
109-
{ text: 'success', type: 'success'},
110-
]}
129+
toasts={toasts}
130+
deleteToast={deleteToast}
111131
/>
112132
<input
113133
type="text"

src/components/Toast.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import propTypes from "prop-types";
22

3-
const Toast = ({ toasts }) => {
3+
const Toast = ({ toasts, deleteToast }) => {
44
return (
55
<div className="position-fixed bottom-0 end-0 p-2">
66
{toasts.map(toast => {
77
return (
8-
<div className={`alert alert-${toast.type || 'success'} m-0 py-2 mt-2`}>
8+
<div
9+
key={toast.id}
10+
onClick={() => {deleteToast(toast.id)}}
11+
className={`cursor-pointer alert alert-${toast.type || 'success'} m-0 py-2 mt-2`}
12+
>
913
{toast.text}
1014
</div>
1115
);
@@ -18,7 +22,8 @@ Toast.propTypes = {
1822
toasts: propTypes.arrayOf(propTypes.shape({
1923
text: propTypes.string,
2024
type: propTypes.string
21-
})).isRequired
25+
})).isRequired,
26+
deleteToast: propTypes.func.isRequired,
2227
}
2328

2429
Toast.defaultProps = {

0 commit comments

Comments
 (0)