Guest User

Untitled

a guest
Jan 5th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { View, ActivityIndicator, TouchableHighlight } from 'react-native';
  3. import { getLogger, issueToText } from '../core/utils';
  4. import styles from '../core/styles';
  5. import { Card, Button, FormLabel, FormInput } from "react-native-elements";
  6. import { connect } from 'react-redux'
  7. import { loginAction } from '../actions/LoginActions'
  8.  
  9. class LoginComponent extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.login = this.login.bind(this)
  13. }
  14.  
  15. render() {
  16. const { error, isLoading } = this.props;
  17.  
  18. const inputFormProp = {
  19. username: '',
  20. password: ''
  21. };
  22.  
  23. return (
  24. <View style={{ paddingVertical: 20 }}>
  25. <Card>
  26. <FormLabel>Email</FormLabel>
  27. <FormInput value={inputFormProp.username} onChangeText={(text) => inputFormProp.username = text} />
  28. <FormLabel>Password</FormLabel>
  29. <FormInput value={inputFormProp.password} onChangeText={(text) => inputFormProp.password = text} />
  30.  
  31. <Button
  32. buttonStyle={{ marginTop: 20 }}
  33. backgroundColor="#03A9F4"
  34. title="SIGN IN"
  35. onPress={this.login(inputFormProp)}
  36. />
  37. </Card>
  38. <ActivityIndicator animating={this.props.isLoading} style={styles.activityIndicator} size="large" />
  39. </View>
  40. );
  41. }
  42.  
  43.  
  44. login(inputFormProp) {
  45.  
  46. console.log(this.props)
  47. const { store } = this.props.screenProps.store;
  48. console.log(loginAction)
  49. const { dispatch } = this.props
  50.  
  51. console.log(dispatch)
  52. dispatch(loginAction(inputFormProp))
  53. .then(() => {
  54. if (this.props.error === null && this.props.isLoading === false) {
  55. if (store.getState().auth.token) {
  56. this.props.navigation.navigate('ProductList', { token: store.getState().auth.token });
  57. }
  58. }
  59. })
  60. .catch(error => {
  61. });
  62. }
  63.  
  64.  
  65. }
  66.  
  67. function mapStateToProps(state) {
  68. const { error, isLoading } = state.auth
  69.  
  70. return {
  71. error,
  72. isLoading,
  73. }
  74. }
  75.  
  76. export default connect(mapStateToProps)(LoginComponent)
  77.  
  78. import React, { Component } from 'react';
  79. import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
  80. import { createLogger } from 'redux-logger';
  81. import thunk from 'redux-thunk';
  82. import { authReducer } from "./src/reducers/LoginReducer";
  83. import { productReducer } from "./src/product/service";
  84. import { ProductList } from "./src/product/ProductList";
  85. import { LoginComponent } from "./src/components/Login";
  86. import { Provider, connect } from "react-redux";
  87. import { StackNavigator, addNavigationHelpers } from "react-navigation";
  88. import Routes from "./src/core/routes";
  89.  
  90.  
  91. const AppNavigator = StackNavigator(Routes, {
  92. navigationOptions: {
  93. title: ({ state }) => {
  94. if (state.params) {
  95. return `${state.params.title}`;
  96. }
  97. }
  98. }
  99. });
  100.  
  101. const navReducer = (state, action) => {
  102. const newState = AppNavigator.router.getStateForAction(action, state);
  103. return newState || state;
  104. };
  105.  
  106. @connect(state => ({
  107. nav: state.nav
  108. }))
  109. class AppWithNavigationState extends Component {
  110. render() {
  111. return (
  112. <AppNavigator
  113. navigation={addNavigationHelpers({
  114. dispatch: this.props.dispatch,
  115. state: this.props.nav
  116. })}
  117. />
  118. );
  119. }
  120. }
  121.  
  122. const initialState = {
  123. auth: { isLoading: false, error: null },
  124. };
  125.  
  126. const rootReducer = combineReducers({ product: productReducer, auth: authReducer, nav: navReducer });
  127.  
  128. let store = createStore(rootReducer, initialState,
  129. compose(applyMiddleware(thunk, createLogger())));
  130.  
  131. export default function App() {
  132. return (
  133. <Provider store={store}>
  134. <AppWithNavigationState />
  135. </Provider>
  136. );
  137. }
  138.  
  139. import { ProductList } from "../product/ProductList";
  140. import { LoginComponent } from "../components/Login";
  141.  
  142. const Routes = {
  143. Login: { screen: LoginComponent },
  144. ProductList: { screen: ProductList }
  145. };
  146.  
  147. export default Routes;
Add Comment
Please, Sign In to add comment