Guest User

Untitled

a guest
Sep 11th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import {
  3. LOGIN,
  4. LOGIN_FAILED,
  5. LOGOUT
  6. } from '../types'
  7.  
  8. const defaultState = {
  9. isLoggedIn: false,
  10. UserName: '',
  11. UserEmail: '',
  12. UserPassword: ''
  13. };
  14.  
  15. export default function reducer(state = defaultState, action) {
  16. switch (action.type) {
  17. case LOGIN:
  18. return Object.assign({}, state, {
  19. isLoggedIn: true,
  20. UserName: action.UserName,
  21. UserEmail: action.UserEmail,
  22. UserPassword: action.UserPassword
  23. });
  24. case LOGOUT:
  25. return Object.assign({}, state, {
  26. isLoggedIn: false,
  27. UserName: '',
  28. UserEmail: '',
  29. UserPassword: ''
  30. });
  31. case LOGIN_FAILED:
  32. return {
  33. UserName: '',
  34. UserEmail: '',
  35. UserPassword: '',
  36. isLoggedIn: false
  37. }
  38. default:
  39. return state;
  40. }
  41.  
  42. import {
  43. LOGIN,
  44. LOGIN_FAILED,
  45. LOGOUT
  46. } from '../types'
  47.  
  48. export const login = (UserName, UserEmail, UserPassword) => async (dispatch) => {
  49. function onSuccess(success) {
  50. dispatch({ type: LOGIN, payload: success })
  51. return success
  52. }
  53. function onError(error) {
  54. dispatch({ type: LOGIN_FAILED, error })
  55. return error
  56. }
  57. try {
  58. const { UserEmail } = this.state ;
  59. const { UserName } = this.state ;
  60. const { UserPassword } = this.state ;
  61. const res = await fetch('https://lifestormweb.000webhostapp.com/User_Login.php', {
  62. method: 'POST',
  63. headers: {
  64. 'Accept': 'application/json',
  65. 'Content-Type': 'application/json',
  66. },
  67. body: JSON.stringify({
  68.  
  69. email: UserEmail,
  70.  
  71. password: UserPassword,
  72.  
  73. name: UserName
  74.  
  75. })
  76.  
  77. }).then((response) => response.json())
  78. .then((responseJson) => {
  79.  
  80. // If server response message same as Data Matched
  81. if(responseJson === 'Data Matched')
  82. {
  83. //Then open Profile activity and send user email to profile activity.
  84. this.props.navigation.navigate("Profil");
  85.  
  86. }
  87. else{
  88.  
  89. Alert.alert(responseJson);
  90. }
  91.  
  92. })
  93. const success = await res.json()
  94. return onSuccess(success)
  95. } catch (error) {
  96. return onError(error)
  97. }
  98. };
  99.  
  100. export const logout = () => {
  101. return {
  102. type: 'LOGOUT'
  103. };
  104. };
  105.  
  106. export class Login extends Component {
  107. state = {
  108. UserName: '',
  109. UserEmail: '',
  110. UserPassword: ''
  111. }
  112.  
  113. userLogin (e) {
  114. this.props.onLogin(this.state.UserName, this.state.UserEmail, this.state.UserPassword);
  115. e.preventDefault();
  116. }
  117.  
  118. render() {
  119. return (
  120. <View style={styles.container}>
  121. <View style={styles.loginTextCont}>
  122. <Text style={{fontSize: 36, fontFamily: "Futura" }}>
  123. Willkommen zu</Text> <Text style={{fontSize: 36, fontFamily: "Futura", color:'#ff0000' }}>LifeStorm!</Text>
  124. <View style={{width: 10, height: 5 }} />
  125. </View>
  126. <TextInput style={styles.inputBox}
  127. autoCapitalize='none'
  128. autoCorrect={false}
  129. autoFocus={true}
  130. keyboardType='email-address'
  131. placeholder="Ihre Name"
  132. placeholderTextColor = "#ffffff"
  133. selectionColor="#ffffff"
  134. value={this.state.UserName}
  135. onChangeText={(text) => this.setState({ UserName: text })} />
  136. <TextInput style={styles.inputBox}
  137. autoCapitalize='none'
  138. autoCorrect={false}
  139. autoFocus={true}
  140. keyboardType='email-address'
  141. placeholder="Ihre E-Mail"
  142. placeholderTextColor = "#ffffff"
  143. selectionColor="#ffffff"
  144. value={this.state.UserEmail}
  145. onChangeText={(text) => this.setState({ UserEmail: text })} />
  146. <TextInput style={styles.inputBox}
  147. placeholder='Password'
  148. autoCapitalize='none'
  149. autoCorrect={false}
  150. placeholder="Ihre Passwort"
  151. placeholderTextColor = "#ffffff"
  152. selectionColor="#ffffff"
  153. secureTextEntry={true}
  154. value={this.state.UserPassword}
  155. onChangeText={(text) => this.setState({ UserPassword: text })} />
  156. <TouchableOpacity
  157. style={styles.button}
  158. onPress={(e) => this.userLogin(e)}
  159. >
  160. <Text style={styles.buttonText}>Sich einloggen</Text>
  161. </TouchableOpacity>
  162. <View style={styles.signupTextCont}>
  163. <Text style={styles.signupText}>
  164. Haben Sie kein Konto?
  165. </Text>
  166. <TouchableOpacity onPress={()=>this.props.navigation.navigate("Register")}> <Text style={styles.signupButton}> Sich anmelden</Text></TouchableOpacity>
  167. </View>
  168. </View>
  169. );
  170. }
  171. }
  172.  
  173. const mapStateToProps = (state, ownProps) => {
  174. return {
  175. isLoggedIn: state.auth.isLoggedIn,
  176. };
  177. }
  178.  
  179. const mapDispatchToProps = (dispatch) => {
  180. return {
  181. onLogin: (UserName, UserEmail, UserPassword) => { dispatch(login(UserName, UserEmail, UserPassword)); }
  182. }
  183. }
  184.  
  185. export default connect(mapStateToProps, mapDispatchToProps)(Login);
  186.  
  187. Actions must be plain objects. Use custom middleware for async actions
Add Comment
Please, Sign In to add comment