Skip to content

Commit 7d06175

Browse files
committed
63
1 parent 250618d commit 7d06175

File tree

2 files changed

+62
-22
lines changed

2 files changed

+62
-22
lines changed

db.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@
8383
"publish": false,
8484
"createdAt": 1647211568779,
8585
"id": 15
86+
},
87+
{
88+
"title": "",
89+
"body": "",
90+
"publish": false,
91+
"createdAt": 1653628899948,
92+
"id": 16
93+
},
94+
{
95+
"title": "tetet",
96+
"body": "body",
97+
"publish": false,
98+
"createdAt": 1653630771975,
99+
"id": 17
86100
}
87101
]
88102
}

src/components/BlogForm.js

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useEffect, useState } from 'react';
22
import axios from 'axios';
33
import { useHistory, useParams } from 'react-router';
4-
import { bool } from 'prop-types';
54
import propTypes from 'prop-types';
65

76
const BlogForm = ({ editing }) => {
@@ -14,6 +13,8 @@ const BlogForm = ({ editing }) => {
1413
const [originalBody, setOriginalBody] = useState('');
1514
const [publish, setPublish] = useState(false);
1615
const [originalPublish, setOriginalPublish] = useState(false);
16+
const [titleError, setTitleError] = useState(false);
17+
const [bodyError, setBodyError] = useState(false);
1718

1819
useEffect(() => {
1920
if (editing) {
@@ -42,30 +43,49 @@ const BlogForm = ({ editing }) => {
4243
}
4344
};
4445

46+
const validateForm = () => {
47+
let validated = true;
48+
49+
if (title === '') {
50+
setTitleError(true);
51+
validated = false;
52+
}
53+
54+
if (body === '') {
55+
setBodyError(true);
56+
validated = false;
57+
}
58+
59+
return validated;
60+
}
61+
4562
const onSubmit = () => {
46-
if (editing) {
47-
axios.patch(`http://localhost:3001/posts/${id}`, {
48-
title,
49-
body,
50-
publish
51-
}).then(res => {
52-
console.log(res);
53-
history.push(`/blogs/${id}`)
54-
})
55-
} else {
56-
axios.post('http://localhost:3001/posts', {
57-
title,
58-
body,
59-
publish,
60-
createdAt: Date.now()
61-
}).then(() => {
62-
history.push('/admin');
63-
})
63+
setTitleError(false);
64+
setBodyError(false);
65+
if (validateForm()) {
66+
if (editing) {
67+
axios.patch(`http://localhost:3001/posts/${id}`, {
68+
title,
69+
body,
70+
publish
71+
}).then(res => {
72+
console.log(res);
73+
history.push(`/blogs/${id}`)
74+
})
75+
} else {
76+
axios.post('http://localhost:3001/posts', {
77+
title,
78+
body,
79+
publish,
80+
createdAt: Date.now()
81+
}).then(() => {
82+
history.push('/admin');
83+
})
84+
}
6485
}
6586
};
6687

6788
const onChangePublish = (e) => {
68-
console.log(e.target.checked)
6989
setPublish(e.target.checked);
7090
};
7191

@@ -75,23 +95,29 @@ const BlogForm = ({ editing }) => {
7595
<div className="mb-3">
7696
<label className="form-label">Title</label>
7797
<input
78-
className="form-control"
98+
className={`form-control ${titleError ? 'border-danger': ''}`}
7999
value={title}
80100
onChange={(event) => {
81101
setTitle(event.target.value);
82102
}}
83103
/>
104+
{titleError && <div className="text-danger">
105+
Title is required.
106+
</div>}
84107
</div>
85108
<div className="mb-3">
86109
<label className="form-label">Body</label>
87110
<textarea
88-
className="form-control"
111+
className={`form-control ${bodyError ? 'border-danger': ''}`}
89112
value={body}
90113
onChange={(event) => {
91114
setBody(event.target.value);
92115
}}
93116
rows="10"
94117
/>
118+
{bodyError && <div className="text-danger">
119+
Body is required.
120+
</div>}
95121
</div>
96122
<div className="form-check mb-3">
97123
<input

0 commit comments

Comments
 (0)