Commit e6c5603

mo khan <mo@mokhan.ca>
2016-12-01 05:10:33
use redux for login.
1 parent c42ac1f
app/actions/actionTypes.js
@@ -1,3 +1,4 @@
 export const INCREMENT = 'INCREMENT';
 export const DECREMENT = 'DECREMENT';
 export const RECEIVED_MOVIES = 'RECEIVED_MOVIES';
+export const LOGGED_IN = 'LOGGED_IN';
app/actions/strongActions.js
@@ -0,0 +1,22 @@
+import * as types from './actionTypes';
+import Api from '../infrastructure/api';
+
+export function login(username, password) {
+  return dispatch => {
+    console.log(`dispatching login: ${username} ${password}`)
+    let body = { username: username, password: password };
+    new Api('/sessions').post(body, (json) => {
+      dispatch(logged_in(username, json))
+    });
+  };
+}
+
+export function logged_in(username, json) {
+  console.log('LOGGED_IN');
+  console.dir(json);
+  return {
+    type: types.LOGGED_IN,
+    username: username,
+    authentication_token: json.authentication_token,
+  }
+}
app/components/counter.js
@@ -3,8 +3,6 @@ import { View, Text, TouchableOpacity } from 'react-native';
 
 export default class Counter extends Component {
   render() {
-    console.log("PROPS");
-    console.dir(this.props);
     const { counter, increment, decrement, fetchMovies } = this.props;
     let moviesCount = this.props.movies.movies.length;
     return (
app/containers/awesome-app.js
@@ -1,17 +1,21 @@
 import React, { Component } from 'react';
+import { View } from 'react-native';
 import { bindActionCreators } from 'redux';
 import Counter from '../components/counter';
 import * as counterActions from '../actions/counterActions';
 import * as movieActions from '../actions/movieActions';
+import * as strongActions from '../actions/strongActions';
 import { connect } from 'react-redux';
+import LoginScreen from '../screens/login-screen'
 
 class AwesomeApp extends Component {
   render(){
     const { state, actions } = this.props;
-    console.log("PROPYS");
-    console.dir(this.props);
     return (
-      <Counter counter={state.counter.count} movies={state.movies} {...actions} {...this.props.movieActions} />
+      <View>
+        <Counter counter={state.counter.count} movies={state.movies} {...actions} {...this.props.movieActions} />
+        <LoginScreen {...this.props.strongActions}/>
+      </View>
     );
   }
 }
@@ -22,5 +26,6 @@ export default connect(state => ({
   (dispatch) => ({
     actions: bindActionCreators(counterActions, dispatch),
     movieActions: bindActionCreators(movieActions, dispatch),
+    strongActions: bindActionCreators(strongActions, dispatch),
   })
 )(AwesomeApp);
app/reducers/index.js
@@ -1,7 +1,9 @@
 import counter from './counter'
 import movies from './movies'
+import stronglifters from './stronglifters'
 
 export {
   counter,
-  movies
+  movies,
+  stronglifters
 };
app/reducers/stronglifters.js
@@ -0,0 +1,21 @@
+import * as types from '../actions/actionTypes';
+
+const initialState = {
+  username: '',
+  authentication_token: '',
+};
+
+export default function stronglifters(state = initialState, action = {}) {
+  switch(action.type) {
+    case types.LOGGED_IN:
+      console.log("LOGGED_IN REDUCER");
+      console.dir(state);
+      return {
+        ...state,
+        username: action.username,
+        authentication_token: action.authentication_token,
+      };
+    default:
+      return state;
+  }
+}
app/screens/login-screen.js
@@ -18,12 +18,12 @@ export default class LoginScreen extends Component {
     };
   }
 
-  componentDidMount() {
-    let token = this.storage.fetch('authentication_token');
-    if (token != null) {
-      this.openDashboard(this.storage.fetch('username'))
-    }
-  }
+  //componentDidMount() {
+    //let token = this.storage.fetch('authentication_token');
+    //if (token != null) {
+      //this.openDashboard(this.storage.fetch('username'))
+    //}
+  //}
 
   render() {
     return (
@@ -53,14 +53,15 @@ export default class LoginScreen extends Component {
     console.log(`attempting to login ${value.username}`);
 
     if (value) {
-      body = { username: value.username, password: value.password };
-      let that = this;
-      new Api('/sessions').post(body, (json) => {
-        console.log(json);
-        that.storage.save("authentication_token", json.token);
-        that.storage.save("username", value.username);
-        that.openDashboard(value.username);
-      });
+      this.props.login(value.username, value.password);
+      //body = { username: value.username, password: value.password };
+      //let that = this;
+      //new Api('/sessions').post(body, (json) => {
+        //console.log(json);
+        //that.storage.save("authentication_token", json.token);
+        //that.storage.save("username", value.username);
+        //that.openDashboard(value.username);
+      //});
     }
   }