11/* eslint-disable no-alert */
22import { React , useState } from 'react' ;
33import { Row , Col , Form , Button } from 'react-bootstrap' ;
4+ import { showSuccessMessage , showErrorMessage } from "../helpers/alerts"
5+ import { JournalValidation } from '../helpers/validate' ;
46
57function AddJournal ( ) {
68 const [ title , setTitle ] = useState ( '' ) ;
@@ -13,10 +15,28 @@ function AddJournal() {
1315 const [ lastYear , setLastYear ] = useState ( '' ) ;
1416 const [ policyType , setPolicyType ] = useState ( '' ) ;
1517 const [ domain , setDomain ] = useState ( '' ) ;
18+ const [ success , setSuccess ] = useState ( "" ) ;
19+ const [ error , setError ] = useState ( "" ) ;
1620
21+ const emptyFields = ( ) => {
22+ setTitle ( "" ) ;
23+ setUrl ( "" ) ;
24+ setIssn ( "" ) ;
25+ setRating ( "" ) ;
26+ setDate ( "" ) ;
27+ setPolicyTitle ( "" ) ;
28+ setFirstYear ( "" ) ;
29+ setLastYear ( "" ) ;
30+ setPolicyType ( "" ) ;
31+ setDomain ( "" ) ;
32+ }
1733 const handleSubmit = ( e ) => {
1834 e . preventDefault ( ) ;
19-
35+ setSuccess ( "" ) ;
36+ setError ( "" ) ;
37+ const check = JournalValidation ( title , url , issn , rating , policyTitle , firstYear ,
38+ lastYear , policyType , domain , date )
39+ if ( check ) {
2040 const policies = {
2141 title : policyTitle ,
2242 first_year : firstYear ,
@@ -25,37 +45,55 @@ function AddJournal() {
2545 } ;
2646 const journal = { title, url, issn, rating, date, policies, domain } ;
2747
48+ try {
49+ window . scrollTo ( 0 , 0 ) ;
2850 fetch ( 'https://journal-policy-tracker.herokuapp.com/api/journals' , {
2951 method : 'POST' ,
3052 headers : { 'Content-Type' : 'application/json' } ,
3153 body : JSON . stringify ( journal ) ,
32- } ) . then ( ( ) => {
33- alert ( 'Journal added successfully!' ) ;
34- } ) ;
54+ } )
55+ emptyFields ( ) ;
56+ setSuccess ( "Journal Added Successfuly" )
57+ }
58+ catch ( err )
59+ {
60+ setError ( "Cannot Add Journal" )
61+ }
62+ }
63+ else {
64+ window . scrollTo ( 0 , 0 ) ;
65+ setError ( "Invalid Input" )
66+ }
67+
3568 } ;
3669
3770 return (
3871 < Row >
3972 < Col className = 'm-auto' >
4073 < Form className = 'login-form responsive' onSubmit = { handleSubmit } >
4174 < Form . Group className = 'mb-3' controlId = 'formBasicTitle' >
75+ { success && showSuccessMessage ( success ) }
76+ { error && showErrorMessage ( error ) }
4277 < Form . Label > Title</ Form . Label >
4378 < Form . Control
4479 type = 'text'
4580 placeholder = 'Journal title'
4681 onChange = { ( e ) => setTitle ( e . target . value ) }
82+ value = { title }
4783 />
4884 </ Form . Group >
4985 < Form . Group className = 'mb-3' controlId = 'formBasicUrl' >
5086 < Form . Label > URL</ Form . Label >
51- < Form . Control type = 'text' placeholder = 'URL' onChange = { ( e ) => setUrl ( e . target . value ) } />
87+ < Form . Control type = 'text' placeholder = 'URL'
88+ onChange = { ( e ) => setUrl ( e . target . value ) } value = { url } />
5289 </ Form . Group >
5390 < Form . Group className = 'mb-3' controlId = 'formBasicIssn' >
5491 < Form . Label > ISSN</ Form . Label >
5592 < Form . Control
5693 type = 'text'
5794 placeholder = 'ISSN'
5895 onChange = { ( e ) => setIssn ( e . target . value ) }
96+ value = { issn }
5997 />
6098 </ Form . Group >
6199 < Form . Group className = 'mb-3' controlId = 'formBasicRating' >
@@ -64,6 +102,7 @@ function AddJournal() {
64102 type = 'text'
65103 placeholder = 'Rating'
66104 onChange = { ( e ) => setRating ( e . target . value ) }
105+ value = { rating }
67106 />
68107 </ Form . Group >
69108 < Form . Group className = 'mb-3' controlId = 'formBasicDate' >
@@ -72,6 +111,7 @@ function AddJournal() {
72111 type = 'date'
73112 placeholder = 'Date'
74113 onChange = { ( e ) => setDate ( e . target . value ) }
114+ value = { date }
75115 />
76116 </ Form . Group >
77117 < Form . Group className = 'mb-3' controlId = 'formBasicPolicies' >
@@ -80,21 +120,25 @@ function AddJournal() {
80120 type = 'text'
81121 placeholder = 'Policy Title'
82122 onChange = { ( e ) => setPolicyTitle ( e . target . value ) }
123+ value = { policyTitle }
83124 />
84125 < Form . Control
85126 type = 'text'
86127 placeholder = 'First Year'
87128 onChange = { ( e ) => setFirstYear ( e . target . value ) }
129+ value = { firstYear }
88130 />
89131 < Form . Control
90132 type = 'text'
91133 placeholder = 'Last Year'
92134 onChange = { ( e ) => setLastYear ( e . target . value ) }
135+ value = { lastYear }
93136 />
94137 < Form . Control
95138 type = 'text'
96139 placeholder = 'Type'
97140 onChange = { ( e ) => setPolicyType ( e . target . value ) }
141+ value = { policyType }
98142 />
99143 </ Form . Group >
100144 < Form . Group className = 'mb-3' controlId = 'formBasicDomain' >
@@ -103,6 +147,7 @@ function AddJournal() {
103147 type = 'text'
104148 placeholder = 'Domain'
105149 onChange = { ( e ) => setDomain ( e . target . value ) }
150+ value = { domain }
106151 />
107152 </ Form . Group >
108153 < Button variant = 'primary' type = 'submit' >
0 commit comments