Skip to content

Commit f598855

Browse files
committed
66
1 parent b0e81b5 commit f598855

File tree

3 files changed

+201
-10
lines changed

3 files changed

+201
-10
lines changed

db.json

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,165 @@
11
{
22
"posts": [
3+
{
4+
"title": "1",
5+
"body": "1",
6+
"publish": false,
7+
"createdAt": 1655689602717,
8+
"id": 1
9+
},
10+
{
11+
"title": "1",
12+
"body": "1",
13+
"publish": false,
14+
"createdAt": 1655689978573,
15+
"id": 2
16+
},
317
{
418
"title": "2",
519
"body": "2",
620
"publish": false,
7-
"createdAt": 1655353112963,
21+
"createdAt": 1655690060587,
22+
"id": 3
23+
},
24+
{
25+
"title": "new blog",
26+
"body": "hello",
27+
"publish": false,
28+
"createdAt": 1655690156736,
29+
"id": 4
30+
},
31+
{
32+
"title": "5",
33+
"body": "5",
34+
"publish": false,
35+
"createdAt": 1655690257065,
36+
"id": 5
37+
},
38+
{
39+
"title": "5",
40+
"body": "5",
41+
"publish": false,
42+
"createdAt": 1655690639921,
43+
"id": 6
44+
},
45+
{
46+
"title": "5",
47+
"body": "5",
48+
"publish": false,
49+
"createdAt": 1655690642822,
50+
"id": 7
51+
},
52+
{
53+
"title": "5",
54+
"body": "5",
55+
"publish": false,
56+
"createdAt": 1655690659788,
57+
"id": 8
58+
},
59+
{
60+
"title": "5",
61+
"body": "5",
62+
"publish": false,
63+
"createdAt": 1655690662307,
64+
"id": 9
65+
},
66+
{
67+
"title": "5",
68+
"body": "5",
69+
"publish": false,
70+
"createdAt": 1655690723870,
871
"id": 10
72+
},
73+
{
74+
"title": "5",
75+
"body": "5",
76+
"publish": false,
77+
"createdAt": 1655690726379,
78+
"id": 11
79+
},
80+
{
81+
"title": "5",
82+
"body": "5",
83+
"publish": false,
84+
"createdAt": 1655690810566,
85+
"id": 12
86+
},
87+
{
88+
"title": "5",
89+
"body": "5",
90+
"publish": false,
91+
"createdAt": 1655690813446,
92+
"id": 13
93+
},
94+
{
95+
"title": "5",
96+
"body": "5",
97+
"publish": false,
98+
"createdAt": 1655690866548,
99+
"id": 14
100+
},
101+
{
102+
"title": "5",
103+
"body": "5",
104+
"publish": false,
105+
"createdAt": 1655690869248,
106+
"id": 15
107+
},
108+
{
109+
"title": "1",
110+
"body": "1",
111+
"publish": false,
112+
"createdAt": 1655690901403,
113+
"id": 16
114+
},
115+
{
116+
"title": "1",
117+
"body": "1",
118+
"publish": false,
119+
"createdAt": 1655690904291,
120+
"id": 17
121+
},
122+
{
123+
"title": "2",
124+
"body": "3",
125+
"publish": false,
126+
"createdAt": 1655690949947,
127+
"id": 18
128+
},
129+
{
130+
"title": "2",
131+
"body": "3",
132+
"publish": false,
133+
"createdAt": 1655690952243,
134+
"id": 19
135+
},
136+
{
137+
"title": "1",
138+
"body": "1",
139+
"publish": false,
140+
"createdAt": 1655690997156,
141+
"id": 20
142+
},
143+
{
144+
"title": "1",
145+
"body": "1",
146+
"publish": false,
147+
"createdAt": 1655690999887,
148+
"id": 21
149+
},
150+
{
151+
"title": "1",
152+
"body": "1",
153+
"publish": false,
154+
"createdAt": 1655691070469,
155+
"id": 22
156+
},
157+
{
158+
"title": "1",
159+
"body": "1",
160+
"publish": false,
161+
"createdAt": 1655691073175,
162+
"id": 23
9163
}
10164
]
11165
}

src/components/BlogForm.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { useEffect, useState } from 'react';
22
import axios from 'axios';
33
import { useHistory, useParams } from 'react-router';
44
import propTypes from 'prop-types';
5+
import { v4 as uuidv4 } from 'uuid';
6+
import Toast from '../components/Toast';
57

68
const BlogForm = ({ editing }) => {
79
const history = useHistory();
@@ -15,6 +17,7 @@ const BlogForm = ({ editing }) => {
1517
const [originalPublish, setOriginalPublish] = useState(false);
1618
const [titleError, setTitleError] = useState(false);
1719
const [bodyError, setBodyError] = useState(false);
20+
const [toasts, setToasts] = useState([]);
1821

1922
useEffect(() => {
2023
if (editing) {
@@ -59,6 +62,27 @@ const BlogForm = ({ editing }) => {
5962
return validated;
6063
}
6164

65+
const deleteToast = (id) => {
66+
const filteredToasts = toasts.filter(toast => {
67+
return toast.id !== id;
68+
});
69+
70+
setToasts(filteredToasts);
71+
}
72+
73+
const addToast = (toast) => {
74+
const id = uuidv4();
75+
const toastWithId = {
76+
...toast,
77+
id
78+
}
79+
setToasts(prev => [...prev, toastWithId]);
80+
81+
setTimeout(() => {
82+
deleteToast(id);
83+
}, 5000);
84+
};
85+
6286
const onSubmit = () => {
6387
setTitleError(false);
6488
setBodyError(false);
@@ -79,7 +103,11 @@ const BlogForm = ({ editing }) => {
79103
publish,
80104
createdAt: Date.now()
81105
}).then(() => {
82-
history.push('/admin');
106+
addToast({
107+
type: 'success',
108+
text: 'Successfully created!'
109+
});
110+
// history.push('/admin');
83111
})
84112
}
85113
}
@@ -91,6 +119,10 @@ const BlogForm = ({ editing }) => {
91119

92120
return (
93121
<div>
122+
<Toast
123+
toasts={toasts}
124+
deleteToast={deleteToast}
125+
/>
94126
<h1>{editing ? 'Edit' : 'Create'} a blog post</h1>
95127
<div className="mb-3">
96128
<label className="form-label">Title</label>

src/components/BlogList.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ const BlogList = ({ isAdmin }) => {
5959
setCurrentPage(parseInt(pageParam) || 1);
6060
getPosts(parseInt(pageParam) || 1)
6161
}, []);
62-
63-
const addToast = (toast) => {
64-
const toastWithId = {
65-
...toast,
66-
id: uuidv4()
67-
}
68-
setToasts(prev => [...prev, toastWithId]);
69-
};
7062

7163
const deleteToast = (id) => {
7264
const filteredToasts = toasts.filter(toast => {
@@ -76,6 +68,19 @@ const BlogList = ({ isAdmin }) => {
7668
setToasts(filteredToasts);
7769
}
7870

71+
const addToast = (toast) => {
72+
const id = uuidv4();
73+
const toastWithId = {
74+
...toast,
75+
id
76+
}
77+
setToasts(prev => [...prev, toastWithId]);
78+
79+
setTimeout(() => {
80+
deleteToast(id);
81+
}, 5000);
82+
};
83+
7984
const deleteBlog = (e, id) => {
8085
e.stopPropagation();
8186

0 commit comments

Comments
 (0)