1
1
import { useEffect , useState } from 'react' ;
2
2
import axios from 'axios' ;
3
3
import { useHistory , useParams } from 'react-router' ;
4
- import { bool } from 'prop-types' ;
5
4
import propTypes from 'prop-types' ;
6
5
7
6
const BlogForm = ( { editing } ) => {
@@ -14,6 +13,8 @@ const BlogForm = ({ editing }) => {
14
13
const [ originalBody , setOriginalBody ] = useState ( '' ) ;
15
14
const [ publish , setPublish ] = useState ( false ) ;
16
15
const [ originalPublish , setOriginalPublish ] = useState ( false ) ;
16
+ const [ titleError , setTitleError ] = useState ( false ) ;
17
+ const [ bodyError , setBodyError ] = useState ( false ) ;
17
18
18
19
useEffect ( ( ) => {
19
20
if ( editing ) {
@@ -42,30 +43,49 @@ const BlogForm = ({ editing }) => {
42
43
}
43
44
} ;
44
45
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
+
45
62
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
+ }
64
85
}
65
86
} ;
66
87
67
88
const onChangePublish = ( e ) => {
68
- console . log ( e . target . checked )
69
89
setPublish ( e . target . checked ) ;
70
90
} ;
71
91
@@ -75,23 +95,29 @@ const BlogForm = ({ editing }) => {
75
95
< div className = "mb-3" >
76
96
< label className = "form-label" > Title</ label >
77
97
< input
78
- className = " form-control"
98
+ className = { ` form-control ${ titleError ? 'border-danger' : '' } ` }
79
99
value = { title }
80
100
onChange = { ( event ) => {
81
101
setTitle ( event . target . value ) ;
82
102
} }
83
103
/>
104
+ { titleError && < div className = "text-danger" >
105
+ Title is required.
106
+ </ div > }
84
107
</ div >
85
108
< div className = "mb-3" >
86
109
< label className = "form-label" > Body</ label >
87
110
< textarea
88
- className = " form-control"
111
+ className = { ` form-control ${ bodyError ? 'border-danger' : '' } ` }
89
112
value = { body }
90
113
onChange = { ( event ) => {
91
114
setBody ( event . target . value ) ;
92
115
} }
93
116
rows = "10"
94
117
/>
118
+ { bodyError && < div className = "text-danger" >
119
+ Body is required.
120
+ </ div > }
95
121
</ div >
96
122
< div className = "form-check mb-3" >
97
123
< input
0 commit comments