Skip to content

Commit 9062c80

Browse files
committed
68
1 parent 7ca70e5 commit 9062c80

File tree

4 files changed

+76
-60
lines changed

4 files changed

+76
-60
lines changed

db.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,34 @@
972972
"publish": false,
973973
"createdAt": 1657240426359,
974974
"id": 139
975+
},
976+
{
977+
"title": "asdf",
978+
"body": "asdf",
979+
"publish": false,
980+
"createdAt": 1657255138326,
981+
"id": 140
982+
},
983+
{
984+
"title": "asdf",
985+
"body": "asdf",
986+
"publish": false,
987+
"createdAt": 1657255140428,
988+
"id": 141
989+
},
990+
{
991+
"title": "5",
992+
"body": "5",
993+
"publish": false,
994+
"createdAt": 1657255273601,
995+
"id": 142
996+
},
997+
{
998+
"title": "5",
999+
"body": "5",
1000+
"publish": false,
1001+
"createdAt": 1657255275073,
1002+
"id": 143
9751003
}
9761004
]
9771005
}

src/components/BlogForm.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { useEffect, useState, useRef } from 'react';
1+
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';
65
import Toast from '../components/Toast';
6+
import useToast from '../hooks/toast';
77

88
const BlogForm = ({ editing }) => {
9+
const [toasts, addToast, deleteToast] = useToast();
910
const history = useHistory();
1011
const { id } = useParams();
1112

@@ -17,8 +18,6 @@ const BlogForm = ({ editing }) => {
1718
const [originalPublish, setOriginalPublish] = useState(false);
1819
const [titleError, setTitleError] = useState(false);
1920
const [bodyError, setBodyError] = useState(false);
20-
const [, setToastRerender] = useState(false);
21-
const toasts = useRef([]);
2221

2322
useEffect(() => {
2423
if (editing) {
@@ -63,30 +62,6 @@ const BlogForm = ({ editing }) => {
6362
return validated;
6463
}
6564

66-
const deleteToast = (id) => {
67-
const filteredToasts = toasts.current.filter(toast => {
68-
return toast.id !== id;
69-
});
70-
71-
toasts.current = filteredToasts;
72-
setToastRerender(prev => !prev);
73-
}
74-
75-
const addToast = (toast) => {
76-
const id = uuidv4();
77-
const toastWithId = {
78-
...toast,
79-
id
80-
}
81-
82-
toasts.current = [...toasts.current, toastWithId];
83-
setToastRerender(prev => !prev);
84-
85-
setTimeout(() => {
86-
deleteToast(id);
87-
}, 5000);
88-
};
89-
9065
const onSubmit = () => {
9166
setTitleError(false);
9267
setBodyError(false);
@@ -124,7 +99,7 @@ const BlogForm = ({ editing }) => {
12499
return (
125100
<div>
126101
<Toast
127-
toasts={toasts.current}
102+
toasts={toasts}
128103
deleteToast={deleteToast}
129104
/>
130105
<h1>{editing ? 'Edit' : 'Create'} a blog post</h1>

src/components/BlogList.js

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import axios from 'axios';
2-
import { useState, useEffect, useCallback, useRef } from 'react';
2+
import { useState, useEffect, useCallback } from 'react';
33
import Card from '../components/Card';
44
import { useHistory } from 'react-router';
55
import LoadingSpinner from '../components/LoadingSpinner';
66
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';
10+
import useToast from '../hooks/toast';
1111

1212
const BlogList = ({ isAdmin }) => {
1313
const history = useHistory();
@@ -20,8 +20,8 @@ const BlogList = ({ isAdmin }) => {
2020
const [numberOfPosts, setNumberOfPosts] = useState(0);
2121
const [numberOfPages, setNumberOfPages] = useState(0);
2222
const [searchText, setSearchText] = useState('');
23-
const [, setToastRerender] = useState(false);
24-
const toasts = useRef([]);
23+
24+
const [toasts, addToast, deleteToast] = useToast();
2525
const limit = 5;
2626

2727
useEffect(() => {
@@ -60,32 +60,6 @@ const BlogList = ({ isAdmin }) => {
6060
setCurrentPage(parseInt(pageParam) || 1);
6161
getPosts(parseInt(pageParam) || 1)
6262
}, []);
63-
64-
const deleteToast = (id) => {
65-
const filteredToasts = toasts.current.filter(toast => {
66-
return toast.id !== id;
67-
});
68-
69-
toasts.current = filteredToasts;
70-
setToastRerender(prev => !prev);
71-
}
72-
73-
const addToast = (toast) => {
74-
const id = uuidv4();
75-
const toastWithId = {
76-
...toast,
77-
id
78-
}
79-
toasts.current = [
80-
...toasts.current,
81-
toastWithId
82-
];
83-
setToastRerender(prev => !prev);
84-
85-
setTimeout(() => {
86-
deleteToast(id);
87-
}, 5000);
88-
};
8963

9064
const deleteBlog = (e, id) => {
9165
e.stopPropagation();
@@ -137,7 +111,7 @@ const BlogList = ({ isAdmin }) => {
137111
return (
138112
<div>
139113
<Toast
140-
toasts={toasts.current}
114+
toasts={toasts}
141115
deleteToast={deleteToast}
142116
/>
143117
<input

src/hooks/toast.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useState, useRef } from 'react';
2+
import { v4 as uuidv4 } from 'uuid';
3+
4+
const useToast = () => {
5+
const [, setToastRerender] = useState(false);
6+
const toasts = useRef([]);
7+
8+
const deleteToast = (id) => {
9+
const filteredToasts = toasts.current.filter(toast => {
10+
return toast.id !== id;
11+
});
12+
13+
toasts.current = filteredToasts;
14+
setToastRerender(prev => !prev);
15+
}
16+
17+
const addToast = (toast) => {
18+
const id = uuidv4();
19+
const toastWithId = {
20+
...toast,
21+
id
22+
}
23+
24+
toasts.current = [...toasts.current, toastWithId];
25+
setToastRerender(prev => !prev);
26+
27+
setTimeout(() => {
28+
deleteToast(id, toasts, setToastRerender);
29+
}, 5000);
30+
};
31+
32+
return [
33+
toasts.current,
34+
addToast,
35+
deleteToast
36+
];
37+
};
38+
39+
export default useToast;

0 commit comments

Comments
 (0)