Advertisement
Guest User

Untitled

a guest
Sep 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. import React from 'react'
  2. import {View, TouchableOpacity, Text} from 'react-native'
  3. import {withRouter} from 'react-router-dom'
  4. import {TextInput, Button} from '../components'
  5. import {layout} from '../styles'
  6. import { gql, graphql, compose } from 'react-apollo'
  7.  
  8. const createUserMutation = gql`
  9. mutation CreateUserMutation($name: String!, $email: String!, $password: String!) {
  10. createUser(
  11. name: $name,
  12. authProvider: {
  13. email: {
  14. email: $email,
  15. password: $password
  16. }
  17. }
  18. ) {
  19. id
  20. }
  21.  
  22. signinUser(email: {
  23. email: $email,
  24. password: $password
  25. }) {
  26. token
  27. user {
  28. id
  29. }
  30. }
  31. }
  32. `
  33.  
  34. const signinUserMutation = gql`
  35. mutation SigninUserMutation($email: String!, $password: String!) {
  36. signinUser(email: {
  37. email: $email,
  38. password: $password
  39. }) {
  40. token
  41. user {
  42. id
  43. }
  44. }
  45. }
  46. `
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. @withRouter
  54. export default compose(
  55. graphql(createUserMutation, {name:'createUserMutate'}),
  56. graphql(signinUserMutation,{name:'signinUserMutate'})
  57. )(class extends React.Component{
  58.  
  59. state={mode:'login', email:'', password:'', name:''}
  60.  
  61. switchMode = () => {
  62. const {mode} = this.state
  63. this.setState({mode: mode==='login' ? 'signup':'login'})
  64. }
  65.  
  66. confirm = async () => {
  67. const {name, email, password, mode} = this.state
  68.  
  69. const { createUserMutate,
  70. signinUserMutate,
  71. history:{push} } = this.props
  72.  
  73. let result
  74. if (mode==='login'){
  75. result = await
  76. signinUserMutate({
  77. variables:{email, password}
  78. })
  79.  
  80. } else{
  81. result = await
  82. createUserMutate({
  83. variables:{name, email, password}
  84. })
  85. }
  86.  
  87. const {data:{signinUser:{user:{id},token}}} = result
  88. this.saveUserData(id, token)
  89. push('/new/1')
  90. }
  91.  
  92. saveUserData = (userId, token) => {
  93. localStorage.setItem('userId', userId)
  94. localStorage.setItem('token', token)
  95. }
  96.  
  97. buttonConfig = () =>{
  98. const {mode} = this.state
  99. return [
  100. {
  101. label: mode==='login' ? 'Login': 'Create An Account',
  102. onPress:this.confirm
  103. },{
  104. label: mode==='login'
  105. ? 'need to create an account?'
  106. : 'already have an account?',
  107. onPress: this.switchMode
  108. }
  109. ]
  110. }
  111.  
  112. inputConfig = () =>{
  113.  
  114. const {mode} = this.state
  115.  
  116. return [
  117. {
  118. placeholder: 'name',
  119. onChangeText:t=>this.setState({name:t}),
  120. dontRender: mode==='login'
  121. },{
  122. placeholder:'email',
  123. onChangeText:t=>this.setState({email:t})
  124. },{
  125. placeholder:'password',
  126. secureTextEntry: true,
  127. onChangeText:t=>this.setState({password:t})
  128. },
  129. ]
  130. }
  131.  
  132.  
  133. render (){
  134.  
  135. const {authed, history:{push}} = this.props
  136. const {mode, email, password, name} = this.state
  137.  
  138. return <View>
  139. {this.inputConfig().map( ({dontRender,...p},i)=> !dontRender &&
  140. <TextInput key={i} {...p} /> )}
  141. <View style={layout.row} >
  142. {this.buttonConfig().map( (p,i) => <Button key={i} {...p} />)}
  143. </View>
  144. </View>
  145.  
  146. }
  147. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement