diff --git a/db.json b/db.json index d9c2c90..18d78f9 100644 --- a/db.json +++ b/db.json @@ -1,8 +1,8 @@ { "posts": [ { - "title": "reactjs", - "body": "reactjs ", + "title": "reactjs1111", + "body": "reactjs", "createdAt": 1644556415774, "id": 4 }, diff --git a/src/components/BlogForm.js b/src/components/BlogForm.js index 69b1e29..61b6ce8 100644 --- a/src/components/BlogForm.js +++ b/src/components/BlogForm.js @@ -8,15 +8,26 @@ const BlogForm = ({ editing }) => { const { id } = useParams(); const [title, setTitle] = useState(''); + const [originalTitle, setOriginalTitle] = useState(''); const [body, setBody] = useState(''); + const [originalBody, setOriginalBody] = useState(''); useEffect(() => { - axios.get(`http://localhost:3001/posts/${id}`).then(res => { - setTitle(res.data.title); - setBody(res.data.body); - }) - }, [id]); - + if (editing) { + axios.get(`http://localhost:3001/posts/${id}`).then(res => { + setTitle(res.data.title); + setOriginalTitle(res.data.title); + setBody(res.data.body); + setOriginalBody(res.data.body); + }) + } + }, [id, editing]); + + const isEdited = () => { + console.log('a') + return title !== originalTitle || body !== originalBody; + }; + const onSubmit = () => { if (editing) { axios.patch(`http://localhost:3001/posts/${id}`, { @@ -24,6 +35,7 @@ const BlogForm = ({ editing }) => { body, }).then(res => { console.log(res); + history.push(`/blogs/${id}`) }) } else { axios.post('http://localhost:3001/posts', { @@ -63,6 +75,7 @@ const BlogForm = ({ editing }) => {