11import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
3- import Axios from 'axios' ;
3+ import axios from 'axios' ;
44import '../../css/registration/SignUp.scss' ;
55
6+ const { REACT_APP_SVR_API } = process . env ;
7+
8+ /**
9+ * @description Validates all inputs to theses constraints
10+ */
11+ const validate = ( email , password , confirmedPassword , fullName , location ) => ( {
12+ email : email . length === 0 ,
13+ password : password . length < 8 || password !== confirmedPassword ,
14+ fullName : fullName . length === 0 ,
15+ location : location . length === 0 ,
16+ } ) ;
17+
18+ const WarningBanner = warn_data =>
19+ // eslint-disable-next-line implicit-arrow-linebreak
20+ warn_data . warn ? < div className = "warning" > { warn_data . message } </ div > : null ;
21+
622class SignUp extends Component {
7- constructor ( props ) {
23+ constructor ( props ) {
824 super ( props ) ;
925
1026 this . state = {
@@ -14,46 +30,48 @@ class SignUp extends Component {
1430 confirmPassword : '' ,
1531 location : '' ,
1632 fullName : '' ,
17- // Might have to refactored this...
33+ // Might have to refactor this...
1834 touched : {
1935 email : false ,
2036 password : false ,
2137 } ,
2238 } ;
2339 }
24-
40+
2541 /**
2642 * Creates a user with the server
2743 */
2844 createUserHandler = ( ) => {
2945 const { email, password, location, fullName } = this . state ;
46+ const { handleSignup } = this . props ;
3047 const data = {
3148 email,
3249 password,
3350 location,
3451 fullName,
3552 } ;
36- Axios . post ( 'http://localhost:5000/api/user/' , data )
53+ axios
54+ . post ( `${ REACT_APP_SVR_API } /user/` , data )
3755 . then ( response => {
3856 console . log ( response ) ;
3957 // Stores token in local storeage for the time being
4058 localStorage . setItem ( 'app-token' , response . data . token ) ;
4159 // Sweet Alert for successful registration
42- this . props . handleSignup ( ) ;
60+ handleSignup ( ) ;
4361 } )
4462 . catch ( error => {
4563 try {
4664 // Handles errors that are not HTTP specific
47- console . log ( error ) ;
65+ console . error ( error ) ;
4866 this . setState ( { showRegistrationFailure : true } ) ;
4967 if ( ! error . status ) {
50- console . log ( 'A network error has occured.' ) ;
68+ console . error ( 'A network error has occured.' ) ;
5169 } else if ( error . response . status === 400 ) {
52- console . log ( 'Bad Request' ) ;
70+ console . error ( 'Bad Request' ) ;
5371 } else if ( error . response . status === 500 ) {
54- console . log ( 'Something bad happended on the server.' ) ;
72+ console . error ( 'Something bad happended on the server.' ) ;
5573 } else {
56- console . log ( 'An unknown error has occurred' ) ;
74+ console . error ( 'An unknown error has occurred' ) ;
5775 }
5876 } catch ( ex ) {
5977 Promise . reject ( ex ) ;
@@ -84,11 +102,11 @@ class SignUp extends Component {
84102 } ;
85103
86104 handleBlur = field => event => {
87- this . setState ( {
88- touched : { ...this . state . touched , [ field ] : true } ,
89- } ) ;
105+ this . setState ( prevState => ( {
106+ touched : { ...prevState . touched , [ field ] : true } ,
107+ } ) ) ;
90108 } ;
91-
109+
92110 /**
93111 * @description Controls the submit button
94112 */
@@ -102,34 +120,27 @@ class SignUp extends Component {
102120 } ;
103121
104122 canBeSubmitted ( ) {
105- const { email, password, confirmPassword, location, fullName } = this . state ;
123+ const { email, password, confirmPassword, location, fullName } = this . state ;
106124 const errors = validate (
107125 email ,
108126 password ,
109- confirmPassword , // Moda
127+ confirmPassword , // Moda
110128 fullName ,
111- location
129+ location ,
112130 ) ;
113131 const isDisabled = Object . keys ( errors ) . some ( x => errors [ x ] ) ;
114132 return ! isDisabled ;
115133 }
116-
117134
118135 render ( ) {
119- const { email, password, confirmPassword, location, fullName } = this . state ;
136+ const { email, password, confirmPassword, location, fullName, touched } = this . state ;
120137 const { backToLogin } = this . props ;
121- const errors = validate (
122- email ,
123- password ,
124- confirmPassword ,
125- fullName ,
126- location
127- ) ;
138+ const errors = validate ( email , password , confirmPassword , fullName , location ) ;
128139 const isDisabled = Object . keys ( errors ) . some ( x => errors [ x ] ) ;
129140
130141 const shouldMarkError = field => {
131142 const hasError = errors [ field ] ;
132- const shouldShow = this . state . touched [ field ] ;
143+ const shouldShow = touched [ field ] ;
133144
134145 return hasError ? shouldShow : false ;
135146 } ;
@@ -141,64 +152,63 @@ class SignUp extends Component {
141152 < input
142153 className = { shouldMarkError ( 'email' ) ? 'error' : '' }
143154 type = "text"
144- value = { this . state . email }
155+ value = { email }
145156 onChange = { this . handleEmailChange }
146157 onBlur = { this . handleBlur ( 'email' ) }
147- 158+ 148159 />
149160 < h3 className = "input-labels" > Password</ h3 >
150161 < input
151162 className = { shouldMarkError ( 'password' ) ? 'error' : '' }
152163 type = "password"
153- value = { this . state . password }
164+ value = { password }
154165 onChange = { this . handlePasswordChange }
155166 onBlur = { this . handleBlur ( 'password' ) }
156- placeholder = ' Minimum length 8 characters'
167+ placeholder = " Minimum length 8 characters"
157168 />
158169 < h3 className = "input-labels" > Confirm Password</ h3 >
159170 < input
160171 className = { shouldMarkError ( 'confirmedPassword' ) ? 'error' : '' }
161172 type = "password"
162- value = { this . state . confirmPassword }
173+ value = { confirmPassword }
163174 onChange = { this . handleConfirmPassword }
164175 onBlur = { this . handleBlur ( 'confirmPassword' ) }
165176 />
166177 < h3 className = "input-labels" > Full Name</ h3 >
167178 < input
168179 className = { shouldMarkError ( 'fullName' ) ? 'error' : '' }
169180 type = "text"
170- value = { this . state . fullName }
181+ value = { fullName }
171182 onChange = { this . handleFullName }
172183 onBlur = { this . handleBlur ( 'fullName' ) }
173- placeholder = ' Jane Doe'
184+ placeholder = " Jane Doe"
174185 />
175186 < h3 className = "input-labels" > Location</ h3 >
176187 < input
177188 className = { shouldMarkError ( 'location' ) ? 'error' : '' }
178189 type = "text"
179- value = { this . state . location }
190+ value = { location }
180191 onChange = { this . handleLocation }
181192 onBlur = { this . handleBlur ( 'location' ) }
182- placeholder = ' Example: Neverwinter'
193+ placeholder = " Example: Neverwinter"
183194 />
184- < p >
185- By clicking "Sign Up" you are agreeing
186- to our
187- { ' ' }
195+ { /* <p>
196+ {`By clicking "Sign Up" you are agreeing to our `}
188197 <a href="https://www.termsandcondiitionssample.com/live.php?token=bYAxBa2kby8ugr9x4eWMbKKgXnxOQyNg" rel="noopener noreferrer" target="_blank">Terms and Agreement</a>
189- </ p >
198+ </p> */ }
190199 < button
191200 id = "register-button"
192201 type = "button"
193202 onClick = { this . createUserHandler }
194203 disabled = { isDisabled }
195204 >
196- Sign Up
205+ { ` Sign Up` }
197206 </ button >
198207 < p >
199- Go back to
200- { ' ' }
201- < button id = 'login-button' type = "button" onClick = { backToLogin ( ) } > Log in!</ button >
208+ { `Go back to ` }
209+ < button id = "login-button" type = "button" onClick = { backToLogin ( ) } >
210+ { `Log in!` }
211+ </ button >
202212 </ p >
203213 </ form >
204214 < WarningBanner
@@ -210,26 +220,6 @@ Go back to
210220 }
211221}
212222
213- /**
214- * @description Validates all inputs to theses constraints
215- */
216- const validate = ( email , password , confirmedPassword , fullName , location ) => ( {
217- email : email . length === 0 ,
218- password : password . length < 8 || password !== confirmedPassword ,
219- fullName : fullName . length === 0 ,
220- location : location . length === 0 ,
221- } ) ;
222-
223- function WarningBanner ( props ) {
224- if ( ! props . warn ) {
225- return null ;
226- }
227- return < div className = "warning" >
228- { ' ' }
229- { props . message }
230- </ div > ;
231- }
232-
233223SignUp . propTypes = {
234224 handleSignup : PropTypes . func . isRequired ,
235225} ;
0 commit comments