UNPKG

ignite-jhipster

Version:

A React Native boilerplate for JHipster apps.

92 lines (81 loc) 2.58 kB
import React from 'react' import { Alert, 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' const 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() } }, } this.submitForm = this.submitForm.bind(this) this.formChange = this.formChange.bind(this) } submitForm () { // call getValue() to get the values of the form const value = this.form.getValue() if (value) { // if validation fails, value will be null this.props.resetPassword(value.email) } } componentDidUpdate(prevProps) { if (prevProps.fetching && !this.props.fetching) { if (this.props.error) { Alert.alert('Error', this.props.error, [{ text: 'OK' }]) } else { Alert.alert('Success', 'Password reset email sent', [{ text: 'OK' }]) Navigation.popToRoot(this.props.componentId) } } } formChange (newValue) { this.setState({ formValue: newValue }) } render () { return ( <KeyboardAwareScrollView style={styles.container} keyboardShouldPersistTaps={'handled'}> <Form ref={c => { this.form = c }} 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> </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)