PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/views/setup.js

https://bitbucket.org/LorenK/sdconnect
JavaScript | 157 lines | 139 code | 16 blank | 2 comment | 6 complexity | a176dbe4f0da39639015b6982651989a MD5 | raw file
  1import React, { Component } from 'react'
  2import {
  3    View,
  4    Text,
  5} from 'react-native'
  6import { STextInput } from '../components/textInput'
  7import icons from '../constants/icons'
  8import colors from '../constants/colors'
  9import errors from '../constants/errors'
 10import { loginStyles } from '../styles/page'
 11import styles from '../styles/textInput'
 12import textStyles from '../styles/textStyles'
 13import Form from '../components/form'
 14import { Button } from '../components/button'
 15
 16import { Page, FormLayout } from "../components/page"
 17import { Firebase } from '../utils/firebase'
 18
 19class Setup extends Page {
 20    constructor(props) {
 21        super(props, new FormLayout("SETUP", false))
 22        this.renderContent = this.renderContent.bind(this)
 23        this.componentDidMount = this.componentDidMount.bind(this)
 24    }
 25
 26    componentDidMount() {
 27        this.setState({
 28            email: '',
 29            emailError: undefined,
 30            password: '',
 31            passwordError: undefined,
 32            confirmPassword: '',
 33            confirmPasswordError: undefined,
 34            genericError: undefined
 35        })
 36    }
 37    
 38    renderContent() {        
 39        const {
 40            email, emailError,
 41            password, passwordError,
 42            confirmPassword, confirmPasswordError,
 43            genericError
 44        } = this.state
 45
 46        return(
 47            <View>
 48                <Form style={{padding: 24}}>
 49                    <STextInput
 50                        label={"Email"}
 51                        onChangeText={(email) => this.setState({email})}
 52                        value={email}
 53                        errorValue={emailError}
 54                    />
 55                    <STextInput
 56                        label={"New Password"}
 57                        onChangeText={(password) => this.setState({password})}
 58                        value={password}
 59                        secureTextEntry={true}
 60                        errorValue={passwordError}
 61                    />
 62                    <STextInput
 63                        label={"Confirm Password"}
 64                        onChangeText={(confirmPassword) => this.setState({confirmPassword})}
 65                        value={confirmPassword}
 66                        secureTextEntry={true}
 67                        errorValue={confirmPasswordError}
 68                    />
 69                    <Text style={textStyles.error}>{genericError}</Text>
 70                    
 71                    <View style={loginStyles.buttonView}>
 72                        <Button style={{flex: 1}} icon={icons.forward} onPress={this.validateForm} />
 73                    </View>
 74                </Form>
 75                <View style={{ backgroundColor: "#000" }}>
 76                    <Text>LOG OUT</Text>
 77                </View>
 78            </View>
 79        )
 80    }
 81
 82    signOut = () => Firebase.signOut().then(this.showLogin())
 83
 84    clearErrors() {
 85        this.setState({
 86            emailError: undefined,
 87            passwordError: undefined,
 88            confirmPasswordError: undefined,
 89            genericError: undefined
 90        })
 91    }
 92
 93    validateForm = () => {
 94        this.showLoader()
 95        this.clearErrors()
 96
 97        let errorCount = 0
 98        const { email, password, confirmPassword } = this.state
 99
100        // TODO: Cleanup
101        if (email === '') {
102            this.setState({
103                emailError: errors.noEmailSupplied
104            })
105            errorCount++
106        }
107        if (password === '') {
108            this.setState({
109                passwordError: errors.noPasswordSupplied
110            })
111            errorCount++
112        }
113        if (confirmPassword === '') {
114            this.setState({
115                confirmPasswordError: errors.noPasswordConfirmSupplied
116            })
117            errorCount++
118        }
119        if (password !== confirmPassword) {
120            this.setState({
121                genericError: errors.passwordConfirmationMismatch
122            })
123            errorCount++
124        }
125
126        if (errorCount != 0)
127            this.hideLoader()
128        else
129            this.updateAccount()
130    }
131
132    updateAccount() {
133        const { email, password } = this.state
134        this.getFirebase().completeAccountSetup(email, password).then(() => {
135            this.showHome()
136        }).catch(e => {
137            // We had an error, sign us out and go to login page
138
139            this.hideLoader()
140            this.setState({
141                genericError: e
142            })
143            Firebase.signOut().then(() => {
144                this.showLogin()
145            })
146        })
147    }
148
149    showLogin() {
150        this.hardNavigate("Login")
151    }
152
153    showHome() {
154        this.hardNavigate("Home")
155    }
156}
157module.exports = Setup