ignite-jhipster
Version:
A React Native boilerplate for JHipster apps.
102 lines (91 loc) • 2.81 kB
JavaScript
import React from 'react'
import { Alert, ScrollView, Text, TouchableHighlight } from 'react-native'
import { connect } from 'react-redux'
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
import { Navigation } from 'react-native-navigation'
import t from 'tcomb-form-native'
import ForgotPasswordActions from './forgot-password.reducer'
import styles from './forgot-password-screen.styles'
let Form = t.form.Form
class ForgotPasswordScreen extends React.Component {
constructor (props) {
super(props)
Navigation.events().bindComponent(this)
this.state = {
formModel: t.struct({
email: t.String
}),
formValue: this.props.forgotPassword,
formOptions: {
email: {
returnKeyType: 'done',
onSubmitEditing: () => this.submitForm()
}
},
success: false
}
this.submitForm = this.submitForm.bind(this)
this.formChange = this.formChange.bind(this)
}
submitForm () {
this.setState({
success: false
})
// call getValue() to get the values of the form
const value = this.refs.form.getValue()
if (value) { // if validation fails, value will be null
this.props.resetPassword(value.email)
}
}
componentWillReceiveProps (newProps) {
// Did the update attempt complete?
if (!newProps.fetching) {
if (newProps.error) {
if (newProps.error === 'WRONG') {
Alert.alert('Error', 'Something when wrong resetting your password', [{ text: 'OK' }])
}
} else {
this.setState({
success: true
})
Alert.alert('Success', 'Password reset email sent', [{ text: 'OK' }])
Navigation.popToRoot(this.props.componentId)
}
}
}
formChange (newValue) {
this.setState({
formValue: newValue
})
}
render () {
return (
<KeyboardAwareScrollView>
<ScrollView style={styles.container}>
<Form
ref='form'
type={this.state.formModel}
options={this.state.formOptions}
value={this.state.formValue}
onChange={this.formChange}
/>
<TouchableHighlight style={styles.button} onPress={this.submitForm} underlayColor='#99d9f4'>
<Text style={styles.buttonText}>Reset</Text>
</TouchableHighlight>
</ScrollView>
</KeyboardAwareScrollView>
)
}
}
const mapStateToProps = (state) => {
return {
fetching: state.forgotPassword.fetching,
error: state.forgotPassword.error
}
}
const mapDispatchToProps = (dispatch) => {
return {
resetPassword: (email) => dispatch(ForgotPasswordActions.forgotPasswordRequest(email))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(ForgotPasswordScreen)