Skip to content

Commit 8dab248

Browse files
committed
79
1 parent ed514a3 commit 8dab248

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/components/BlogForm.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ 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 useToast from '../hooks/toast';
6+
import LoadingSpinner from './LoadingSpinner';
57

6-
const BlogForm = ({ editing, addToast }) => {
8+
const BlogForm = ({ editing }) => {
79
const history = useHistory();
810
const { id } = useParams();
911

@@ -15,6 +17,9 @@ const BlogForm = ({ editing, addToast }) => {
1517
const [originalPublish, setOriginalPublish] = useState(false);
1618
const [titleError, setTitleError] = useState(false);
1719
const [bodyError, setBodyError] = useState(false);
20+
const [loading, setLoading] = useState(true);
21+
const [error, setError] = useState('');
22+
const { addToast } = useToast();
1823

1924
useEffect(() => {
2025
if (editing) {
@@ -25,7 +30,17 @@ const BlogForm = ({ editing, addToast }) => {
2530
setOriginalBody(res.data.body);
2631
setPublish(res.data.publish);
2732
setOriginalPublish(res.data.publish);
33+
setLoading(false);
34+
}).catch(e => {
35+
setError('something went wrong in db');
36+
addToast({
37+
type: 'danger',
38+
text: 'something went wrong in db'
39+
})
40+
setLoading(false);
2841
})
42+
} else {
43+
setLoading(false);
2944
}
3045
}, [id, editing]);
3146

@@ -71,6 +86,11 @@ const BlogForm = ({ editing, addToast }) => {
7186
}).then(res => {
7287
console.log(res);
7388
history.push(`/blogs/${id}`)
89+
}).catch(e => {
90+
addToast({
91+
type: 'danger',
92+
text: 'We could not update blog'
93+
})
7494
})
7595
} else {
7696
axios.post('http://localhost:3001/posts', {
@@ -84,6 +104,11 @@ const BlogForm = ({ editing, addToast }) => {
84104
text: 'Successfully created!'
85105
});
86106
history.push('/admin');
107+
}).catch(e => {
108+
addToast({
109+
type: 'danger',
110+
text: 'We could not create blog'
111+
})
87112
})
88113
}
89114
}
@@ -93,6 +118,14 @@ const BlogForm = ({ editing, addToast }) => {
93118
setPublish(e.target.checked);
94119
};
95120

121+
if (loading) {
122+
return <LoadingSpinner />
123+
}
124+
125+
if (error) {
126+
return (<div>{error}</div>)
127+
}
128+
96129
return (
97130
<div>
98131
<h1>{editing ? 'Edit' : 'Create'} a blog post</h1>

src/components/BlogList.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ const BlogList = ({ isAdmin }) => {
7777
text: 'Successfully deleted',
7878
type: 'success'
7979
});
80+
}).catch(e => {
81+
addToast({
82+
text: 'The blog could not be deleted.',
83+
type: 'danger'
84+
})
8085
});
8186
};
8287

src/pages/ShowPage.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@ import { useEffect, useState } from "react";
44
import LoadingSpinner from "../components/LoadingSpinner";
55
import { Link } from "react-router-dom";
66
import { useSelector } from "react-redux";
7+
import useToast from "../hooks/toast";
78

89
const ShowPage = () => {
910
const { id } = useParams();
1011
const [post, setPost] = useState(null);
1112
const [loading, setLoading] = useState(true);
1213
const isLoggedIn = useSelector(state => state.auth.isLoggedIn);
14+
const { addToast } = useToast();
15+
const [error, setError] = useState('');
1316

1417
const getPost = (id) => {
1518
axios.get(`http://localhost:3001/posts/${id}`).then((res) => {
1619
setPost(res.data)
1720
setLoading(false);
21+
}).catch(e => {
22+
setError('something went wrong in db');
23+
addToast({
24+
type: 'danger',
25+
text: 'something went wrong in db'
26+
})
27+
setLoading(false);
1828
})
1929
};
2030

@@ -30,6 +40,10 @@ const ShowPage = () => {
3040
return <LoadingSpinner />
3141
}
3242

43+
if (error) {
44+
return (<div>{error}</div>)
45+
}
46+
3347
return (
3448
<div>
3549
<div className="d-flex">

0 commit comments

Comments
 (0)