Procházet zdrojové kódy

splash+save login details

David před 9 roky
rodič
revize
d57ec26f2a

+ 2 - 1
package.json

@@ -21,7 +21,8 @@
     "react-native-video": "^0.9.0",
     "react-redux": "^4.4.6",
     "redux": "^3.6.0",
-    "redux-thunk": "^2.1.0"
+    "redux-thunk": "^2.1.0",
+    "sha256": "^0.2.0"
   },
   "jest": {
     "preset": "jest-react-native"

+ 6 - 1
src/Router.js

@@ -12,14 +12,19 @@ import Menu from './scenes/Menu';
 import Teoria from './scenes/Clase/Teoria';
 import LSListaClases from './scenes/LSListaClases';
 import MyAbout from './scenes/MyAbout';
+import Splash from './scenes/Splash';
 
 const scenes = Actions.create(
 	<Scene key='container'>
 
 		<Scene key='auth' hideNavBar>
+			<Scene key='splash' component={Splash}
+				sceneStyle={{ paddingTop: 0 }}
+				hideNavBar title='splash' initial
+			/>
 			<Scene
 				key='login' component={LoginForm}
-				sceneStyle={{ paddingTop: 0 }} hideNavBar title='Ingresar' initial
+				sceneStyle={{ paddingTop: 0 }} hideNavBar title='Ingresar'
 			/>
 			<Scene key='register' component={RegisterForm} title='Registrarse' />
 		</Scene>

+ 9 - 5
src/actions/AuthActions.js

@@ -8,6 +8,8 @@ import {
   LOGIN_USER
 } from './types';
 
+import { saveLogin } from './LocalStorageActions';
+
 export const emailChanged = (text) => {
   return {
     type: EMAIL_CHANGED,
@@ -23,11 +25,12 @@ export const passwordChanged = (text) => {
 };
 
 export const loginUser = ({ email, password }) => {
-  return (dispatch) => {
+	return (dispatch) => {
+	const SHA256 = require('sha256');
     dispatch({ type: LOGIN_USER });
-    post('login.php', { user: email, pass: password })
+    post('login.php', { user: email, pass: SHA256(password) })
       .then(
-        (response) => loginUserSuccess(dispatch, response)
+        (response) => loginUserSuccess(dispatch, response, password)
       ).catch(
         (error) => loginUserFail(dispatch, error)
       );
@@ -39,10 +42,11 @@ const loginUserFail = (dispatch, error) => {
   dispatch({ type: LOGIN_USER_FAIL, payload: error });
 };
 
-const loginUserSuccess = (dispatch, user) => {
+const loginUserSuccess = (dispatch, user, password='') => {
   dispatch({
     type: LOGIN_USER_SUCCESS,
     payload: user
   });
-  Actions.Menu();
+  saveLogin({ user: user.email, password: password })
+  Actions.Menu({type: 'reset'});
 };

+ 28 - 1
src/actions/LocalStorageActions.js

@@ -1,9 +1,14 @@
 import {
   LS_CLASE_SAVE,
   LS_CLASE_DELETE,
-  LS_CLASE_INIT
+  LS_CLASE_INIT,
+  LS_USER_SAVE,
+  LS_USER_GET
 } from './types';
 
+import { loginUser } from './AuthActions';
+import { Actions } from 'react-native-router-flux';
+
 const ls = require('react-native-local-storage');
 
 export const saveClase = ({ clase }) => {
@@ -56,3 +61,25 @@ export const initLSClase = () => {
 		});
 	};
 };
+
+export const saveLogin = ({ user, password }) => {
+	ls.save('user', { user, password });
+}
+
+export const getLogin = () => {
+	return (dispatch) => {
+		ls.get('user').then((data) => {
+			let payload = data;
+			if (data === null || data === undefined) {
+				payload = { user: 'asd', passowrd: ''};
+				ls.save('user', payload);
+			}
+
+			dispatch({
+				type: LS_USER_GET,
+				payload
+			});
+			Actions.login({ user: payload.user, password: payload.password, type: 'replace' });
+		});
+	};
+};

+ 2 - 2
src/actions/RegisterActions.js

@@ -1,5 +1,5 @@
 import {
-  PROP_CHANGED,
+  REGISTER_PROP_CHANGED,
   REGISTER_USER,
   REGISTER_USER_SUCCESS,
   REGISTER_USER_FAILURE
@@ -7,7 +7,7 @@ import {
 
 export const propChanged = ({ key, val }) => {
   return {
-    type: PROP_CHANGED,
+    type: REGISTER_PROP_CHANGED,
     payload: { key, val }
   };
 };

+ 4 - 1
src/actions/types.js

@@ -18,7 +18,7 @@ export const CLASE_FETCH = 'clase_fetch';
 export const PROFESOR_FETCH = 'profesor_fetch';
 export const PROFESOR_FETCH_SUCCESS = 'profesor_fetch_success';
 
-export const PROP_CHANGED = 'prop_changed';
+export const REGISTER_PROP_CHANGED = 'prop_changed';
 export const REGISTER_USER = 'register_user';
 export const REGISTER_USER_SUCCESS = 'register_user_success';
 export const REGISTER_USER_FAILURE = 'register_user_failure';
@@ -26,3 +26,6 @@ export const REGISTER_USER_FAILURE = 'register_user_failure';
 export const LS_CLASE_SAVE = 'clase_save';
 export const LS_CLASE_DELETE = 'clase_delete';
 export const LS_CLASE_INIT = 'clase_init';
+
+export const LS_USER_GET = 'ls_user_get';
+export const LS_USER_SAVE = 'ls_user_save';

+ 13 - 10
src/reducers/AuthReducer.js

@@ -3,30 +3,33 @@ import {
   PASSWORD_CHANGED,
   LOGIN_USER_SUCCESS,
   LOGIN_USER_FAIL,
-  LOGIN_USER
+  LOGIN_USER,
+  LS_USER_GET
 } from '../actions/types';
 
 const INITIAL_STATE = {
-  email: 'davidventura27@gmail.com',
-  password: 'cdb9af22908c2c6d1d29961f3a8a23022ff8e21d9ea50f40ebc72759c6cb76dc',
+  email: '',
+  password: '',
   user: null,
   error: '',
   loading: false
 };
 
 export default (state = INITIAL_STATE, action) => {
-  switch (action.type) {
+	switch (action.type) {
+	case LS_USER_GET:
+		return { ...state, email: action.payload.user, password: action.payload.password };
     case EMAIL_CHANGED:
-      return { ...state, email: action.payload };
+		return { ...state, email: action.payload };
     case PASSWORD_CHANGED:
-      return { ...state, password: action.payload };
+		return { ...state, password: action.payload };
     case LOGIN_USER_SUCCESS:
-      return { ...state, user: action.payload, error: '', loading: false };
+		return { ...state, user: action.payload, error: '', loading: false };
     case LOGIN_USER_FAIL:
-      return { ...state, user: null, error: action.payload, password: '', loading: false };
+		return { ...state, user: null, error: action.payload, password: '', loading: false };
     case LOGIN_USER:
-      return { ...state, loading: true, error: '' };
+		return { ...state, loading: true, error: '' };
     default:
-      return state;
+		return state;
   }
 };

+ 10 - 3
src/reducers/LocalStorageReducer.js

@@ -1,11 +1,14 @@
 import {
   LS_CLASE_SAVE,
   LS_CLASE_DELETE,
-  LS_CLASE_INIT
+  LS_CLASE_INIT,
+  LS_USER_GET,
+  LOGIN_USER_SUCCESS
 } from '../actions/types';
 
 const INITIAL_STATE = {
-  clases: []
+  clases: [],
+  user: {}
 };
 
 export default (state = INITIAL_STATE, action) => {
@@ -18,7 +21,11 @@ export default (state = INITIAL_STATE, action) => {
 			return { ...state, clases: state.clases.filter(c => c._id !== action.payload) };
 		case LS_CLASE_INIT:
 			console.log('clases init value: ', action.payload);
-			return { clases: action.payload };
+			return { ...state, clases: action.payload };
+		case LS_USER_GET:
+			return { ...state, user: action.payload };
+		case LOGIN_USER_SUCCESS:
+			return state;
 		default:
 			return state;
   }

+ 1 - 8
src/scenes/Menu.js

@@ -3,16 +3,9 @@ import { View, Text, TouchableOpacity } from 'react-native';
 import { connect } from 'react-redux';
 import { Actions } from 'react-native-router-flux';
 
-import { initLSClase } from '../actions/';
 import { Card, CardSection } from '../components/common';
 
 class Menu extends Component {
-	componentWillMount() {
-		this.props.initLSClase();
-	}
-	componentWillReceiveProps() {
-		this.props.initLSClase();
-	}
 
 	render() {
 		return (
@@ -62,4 +55,4 @@ class Menu extends Component {
 	}
 }
 
-export default connect(null, { initLSClase })(Menu);
+export default connect(null)(Menu);

+ 1 - 1
src/scenes/MyAbout.js

@@ -59,7 +59,7 @@ class MyAbout extends Component {
 			<View style={viewStyle}>
 			<Icon size={30 } style={[textColor, iconStyle]} name='md-time' />
 			<Text style={textColor}>
-			Abierto Lunes a Viernes 9 a 18hs.
+				Abierto Lunes a Viernes 9 a 18 horas.
 			</Text>
 			</View>
 			</View>

+ 50 - 0
src/scenes/Splash.js

@@ -0,0 +1,50 @@
+import React, { Component } from 'react';
+import { Text, TextInput, View, Image, TouchableOpacity } from 'react-native';
+import { connect } from 'react-redux';
+import { Actions } from 'react-native-router-flux';
+import { loginUser, initLSClase, getLogin } from '../actions/';
+
+class Splash extends Component {
+	componentWillMount() {
+		this.props.initLSClase();
+		this.props.getLogin();
+	}
+
+	/*componentWillReceiveProps() {
+		this.props.initLSClase();
+		this.props.getLogin();
+	}*/
+	
+	render() {
+		console.log();
+		return (
+			<View
+			style={{ flex: 1,
+				backgroundColor: '#3B618B',
+				alignItems: 'center',
+				paddingTop: 55 }}
+			>
+
+				<Image
+				source={require('../../img/logo.png')}
+				resizeMode='contain'
+				style={{ height: 80, marginTop: 20, marginBottom: 20 }}
+				/>
+				<Text style={{ color: 'white', fontSize: 18, marginBottom: 30 }}>
+					PLATAFORMA DE ESTUDIO
+				</Text>
+
+				</View>
+			);
+		}
+}
+
+const mapStateToProps = ( { LocalStorageReducer } ) => {
+	const { user } = LocalStorageReducer;
+	console.log(user);
+	return { user };
+};
+
+export default connect(mapStateToProps, {
+	loginUser, initLSClase, getLogin
+})(Splash);