Skip to content

Commit

Permalink
Add error messages for LogIn form
Browse files Browse the repository at this point in the history
* Add error messages for each field LogIn form
* Add network error message
* Closes #27
  • Loading branch information
axelclark committed Dec 31, 2018
1 parent c509b6e commit 5c4a242
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions screens/LogInScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class LogInScreen extends React.Component {
inputEmail: '',
inputPassword: '',
login: true,
errorMessage: ''
errorMessage: '',
emailError: '',
passwordError: '',
}
}

Expand All @@ -65,7 +67,16 @@ class LogInScreen extends React.Component {
this.setState({ inputPassword })
}

resetErrors() {
this.setState({
errorMessage: '',
emailError: '',
passwordError: ''
})
}

toggleLogIn(login) {
this.resetErrors()
this.setState({ login })
}

Expand All @@ -78,14 +89,34 @@ class LogInScreen extends React.Component {
}
}

handleError(error) {
error.graphQLErrors.map(({ message }) => (
this.setState({ errorMessage: message })
))
handleError({ graphQLErrors, networkError }) {
if (graphQLErrors) {
graphQLErrors.map(({ message, details }) => {
this.setState({ errorMessage: message })
if (details) {
if (details.email) {
this.setState({ emailError: details.email.join() })
}
if (details.password) {
this.setState({ passwordError: details.password.join() })
}
}
})
}
if (networkError) {
this.setState({ errorMessage: networkError.message })
}
}

render() {
const { inputEmail, inputPassword, login, errorMessage } = this.state
const {
inputEmail,
inputPassword,
login,
errorMessage,
emailError,
passwordError,
} = this.state

const logInParams = {
email: inputEmail,
Expand All @@ -104,14 +135,16 @@ class LogInScreen extends React.Component {
<Header title={'Fore Score'}/>
<View style={styles.formContainer}>

<FormValidationMessage>{errorMessage}</FormValidationMessage>

<FormLabel>Email</FormLabel>
<FormInput
onChangeText={(text) => this.updateEmail(text)}
inputValue={inputEmail}
textContentType={'emailAddress'}
keyboardType={'email-address'}
/>
<FormValidationMessage>{errorMessage}</FormValidationMessage>
<FormValidationMessage>{emailError}</FormValidationMessage>

<FormLabel>Password</FormLabel>
<FormInput
Expand All @@ -120,6 +153,7 @@ class LogInScreen extends React.Component {
textContentType={'password'}
secureTextEntry={true}
/>
<FormValidationMessage>{passwordError}</FormValidationMessage>

<Mutation
mutation={login ? LOGIN_MUTATION : CREATE_USER_MUTATION}
Expand Down

0 comments on commit 5c4a242

Please sign in to comment.