Guest User

Untitled

a guest
Apr 9th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import { ScrollView, Text, TextInput, View, Button,StyleSheet,TouchableOpacity } from 'react-native';
  4. import {loginRequest} from './../redux/actions/auth';
  5. import {bindActionCreators} from 'redux';
  6.  
  7. class Login extends Component {
  8. constructor (props) {
  9. super(props);
  10. this.state = {
  11. username: '',
  12. password: ''
  13. };
  14. }
  15.  
  16. userLogin (e) {
  17. this.props.actions.loginRequest(this.state.username, this.state.password);
  18. }
  19.  
  20. render () {
  21. return (
  22. <ScrollView style={{padding: 20,backgroundColor:'#ccc'}}>
  23. <View style = {styles.container}>
  24.  
  25. <View style={{marginLeft:15}}>
  26. <Text>Email</Text>
  27. </View>
  28.  
  29. <TextInput
  30. style = {styles.input}
  31. underlineColorAndroid = "transparent"
  32. placeholder = "Enter username"
  33. placeholderTextColor = "#9a73ef"
  34. autoCapitalize = "none"
  35. onChangeText={(text) => this.setState({ username: text })}/>
  36.  
  37. <View style={{marginLeft:15}}>
  38. <Text>Password</Text>
  39. </View>
  40.  
  41. <TextInput
  42. style = {styles.input}
  43. underlineColorAndroid = "transparent"
  44. placeholder = "Enter Password > 6 letters"
  45. placeholderTextColor = "#9a73ef"
  46. autoCapitalize = "none"
  47. onChangeText={(text) => this.setState({ password: text })}/>
  48.  
  49. <TouchableOpacity
  50. style = {styles.submitButton}
  51. onPress={(e) => this.userLogin(e)}>
  52. <Text style = {styles.submitButtonText}> Submit </Text>
  53. </TouchableOpacity>
  54.  
  55. </View>
  56. </ScrollView>
  57. );
  58. }
  59. }
  60.  
  61. const mapStateToProps = (state, ownProps) => {
  62. return {
  63. isLoggedIn: state.auth.isLoggedIn
  64. };
  65. }
  66.  
  67. function mapDispatchToProps(dispatch){
  68. return {
  69. actions : bindActionCreators({
  70. loginRequest
  71. },dispatch)
  72. };
  73. }
  74.  
  75. export default connect(mapStateToProps, mapDispatchToProps)(Login);
  76.  
  77. export function loginRequest(username,password) {
  78. alert("TEst"); // this alert comes
  79. return function (dispatch) {
  80. alert(`........Tracker......`); // execution doesn't reach here ,this alert doesn't com
  81. if(username == 'admin' && password == 'admin'){
  82. alert(`Login Success......`);
  83. dispatch({
  84. type : 'LOGIN_SUCCESS',
  85. msg : 'Logged in successfully.'
  86. });
  87. resolve(true);
  88. } else {
  89. alert(`Login Failed......`);
  90. dispatch({
  91. type : 'LOGIN_FAIL',
  92. msg : 'Please make sure you have entered valid credentials.'
  93. })
  94. reject(false);
  95. }
  96. };
  97. }
  98.  
  99. export default function reducer(state = {},action){
  100. if(action.type == 'LOGIN_SUCCESS'){
  101. alert('login success');
  102. return Object.assign({},state,{
  103. isLoggedIn:true
  104. })
  105.  
  106. } else if(action.type == 'LOGIN_FAIL'){
  107. alert('login failed');
  108. return Object.assign({},state,{
  109. isLoggedIn:false
  110. })
  111.  
  112. } else {
  113. return state;
  114. }
  115. }
  116.  
  117. import React, { Component } from 'react';
  118. import {
  119. Platform,
  120. StyleSheet,
  121. Text,
  122. View
  123. } from 'react-native';
  124. import {Provider} from 'react-redux';
  125. import store from './redux';
  126. import Application from './pages/Application';
  127.  
  128. export default class App extends Component{
  129. render() {
  130. return (
  131. <Provider store={store}>
  132. <Application />
  133. </Provider>
  134. );
  135. }
  136. }
  137.  
  138. import { createStore, applyMiddleware } from 'redux';
  139. import thunk from 'redux-thunk';
  140. import rootReducer from './reducers';
  141.  
  142. export default(initialState) => {
  143. return createStore(rootReducer, initialState, applyMiddleware(thunk));
  144. }
Add Comment
Please, Sign In to add comment