/src/views/setup.js

https://bitbucket.org/LorenK/sdconnect · JavaScript · 157 lines · 139 code · 16 blank · 2 comment · 6 complexity · a176dbe4f0da39639015b6982651989a MD5 · raw file

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