Skip to content

Commit 78ed216

Browse files
committed
73
1 parent ca656f8 commit 78ed216

File tree

6 files changed

+20
-158
lines changed

6 files changed

+20
-158
lines changed

db.json

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -889,138 +889,12 @@
889889
"createdAt": 1657240162434,
890890
"id": 127
891891
},
892-
{
893-
"title": "f",
894-
"body": "f",
895-
"publish": false,
896-
"createdAt": 1657240197883,
897-
"id": 128
898-
},
899-
{
900-
"title": "f",
901-
"body": "f",
902-
"publish": false,
903-
"createdAt": 1657240200199,
904-
"id": 129
905-
},
906892
{
907893
"title": "f",
908894
"body": "f",
909895
"publish": false,
910896
"createdAt": 1657240212897,
911897
"id": 130
912-
},
913-
{
914-
"title": "f",
915-
"body": "f",
916-
"publish": false,
917-
"createdAt": 1657240407068,
918-
"id": 131
919-
},
920-
{
921-
"title": "f",
922-
"body": "f",
923-
"publish": false,
924-
"createdAt": 1657240408910,
925-
"id": 132
926-
},
927-
{
928-
"title": "f",
929-
"body": "f",
930-
"publish": false,
931-
"createdAt": 1657240415516,
932-
"id": 133
933-
},
934-
{
935-
"title": "f",
936-
"body": "f",
937-
"publish": false,
938-
"createdAt": 1657240423386,
939-
"id": 134
940-
},
941-
{
942-
"title": "f",
943-
"body": "f",
944-
"publish": false,
945-
"createdAt": 1657240424008,
946-
"id": 135
947-
},
948-
{
949-
"title": "f",
950-
"body": "f",
951-
"publish": false,
952-
"createdAt": 1657240424604,
953-
"id": 136
954-
},
955-
{
956-
"title": "f",
957-
"body": "f",
958-
"publish": false,
959-
"createdAt": 1657240424829,
960-
"id": 137
961-
},
962-
{
963-
"title": "f",
964-
"body": "f",
965-
"publish": false,
966-
"createdAt": 1657240425122,
967-
"id": 138
968-
},
969-
{
970-
"title": "f",
971-
"body": "f",
972-
"publish": false,
973-
"createdAt": 1657240426359,
974-
"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
1003-
},
1004-
{
1005-
"title": "b",
1006-
"body": "b",
1007-
"publish": false,
1008-
"createdAt": 1657774497818,
1009-
"id": 149
1010-
},
1011-
{
1012-
"title": "a",
1013-
"body": "a",
1014-
"publish": false,
1015-
"createdAt": 1657775679723,
1016-
"id": 150
1017-
},
1018-
{
1019-
"title": "b",
1020-
"body": "b",
1021-
"publish": false,
1022-
"createdAt": 1657775748192,
1023-
"id": 151
1024898
}
1025899
]
1026900
}

src/App.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import NavBar from './components/NavBar';
77
import routes from './routes';
88
import Toast from './components/Toast';
99
import useToast from './hooks/toast';
10+
import { useSelector } from 'react-redux';
1011

1112
function App() {
12-
const [toasts, addToast, deleteToast] = useToast();
13+
const toasts = useSelector(state => state.toast.toasts);
14+
const { deleteToast } = useToast();
1315
return (
1416
<Router>
1517
<NavBar />
@@ -20,15 +22,12 @@ function App() {
2022
<div className="container mt-3">
2123
<Switch>
2224
{routes.map((route) => {
23-
const Component = route.component;
2425
return <Route
25-
key={route.path}
26-
exact
27-
path={route.path}
28-
// component={route.component}
29-
>
30-
<Component addToast={addToast} />
31-
</Route>;
26+
key={route.path}
27+
exact
28+
path={route.path}
29+
component={route.component}
30+
/>
3231
})}
3332
</Switch>
3433
</div>

src/components/.BlogList.js.swp

20 KB
Binary file not shown.

src/components/BlogList.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import LoadingSpinner from '../components/LoadingSpinner';
66
import Pagination from './Pagination';
77
import { useLocation } from 'react-router-dom';
88
import propTypes from 'prop-types';
9-
import Toast from '../components/Toast';
109
import useToast from '../hooks/toast';
1110

1211
const BlogList = ({ isAdmin }) => {
@@ -21,7 +20,7 @@ const BlogList = ({ isAdmin }) => {
2120
const [numberOfPages, setNumberOfPages] = useState(0);
2221
const [searchText, setSearchText] = useState('');
2322

24-
const [toasts, addToast, deleteToast] = useToast();
23+
const { addToast } = useToast();
2524
const limit = 5;
2625

2726
useEffect(() => {
@@ -110,10 +109,6 @@ const BlogList = ({ isAdmin }) => {
110109

111110
return (
112111
<div>
113-
<Toast
114-
toasts={toasts}
115-
deleteToast={deleteToast}
116-
/>
117112
<input
118113
type="text"
119114
placeholder="Search.."

src/hooks/toast.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import { useState, useRef } from 'react';
21
import { v4 as uuidv4 } from 'uuid';
3-
import { addToast as add } from '../store/toastSlice';
2+
import { addToast as add, removeToast } from '../store/toastSlice';
43
import { useDispatch } from 'react-redux';
54

65
const useToast = () => {
7-
const [, setToastRerender] = useState(false);
8-
const toasts = useRef([]);
96
const dispatch = useDispatch();
107

118
const deleteToast = (id) => {
12-
const filteredToasts = toasts.current.filter(toast => {
13-
return toast.id !== id;
14-
});
15-
16-
toasts.current = filteredToasts;
17-
setToastRerender(prev => !prev);
9+
dispatch(removeToast(id));
1810
}
1911

2012
const addToast = (toast) => {
@@ -25,19 +17,16 @@ const useToast = () => {
2517
}
2618

2719
dispatch(add(toastWithId));
28-
// toasts.current = [...toasts.current, toastWithId];
29-
// setToastRerender(prev => !prev);
3020

3121
setTimeout(() => {
32-
deleteToast(id, toasts, setToastRerender);
22+
deleteToast(id);
3323
}, 5000);
3424
};
3525

36-
return [
37-
toasts.current,
26+
return {
3827
addToast,
3928
deleteToast
40-
];
29+
};
4130
};
4231

4332
export default useToast;

src/store/toastSlice.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ const toastSlice = createSlice({
1010
reducers: {
1111
addToast: (state, action) => {
1212
state.toasts.push(action.payload);
13+
},
14+
removeToast: (state, action) => {
15+
state.toasts = state.toasts.filter(toast => {
16+
return toast.id !== action.payload
17+
})
1318
}
1419
}
1520
})
1621

17-
export const { addToast } = toastSlice.actions;
22+
export const { addToast, removeToast } = toastSlice.actions;
1823

1924
export default toastSlice.reducer;

0 commit comments

Comments
 (0)