ソースを参照

move post() to api + add error checking on login+cursadasfetch

David 9 年 前
コミット
8f94c56559

+ 12 - 21
src/actions/AuthActions.js

@@ -1,4 +1,5 @@
 import { Actions } from 'react-native-router-flux';
+import { post } from '../api/'
 import {
   EMAIL_CHANGED,
   PASSWORD_CHANGED,
@@ -23,33 +24,23 @@ export const passwordChanged = (text) => {
 
 export const loginUser = ({ email, password }) => {
   return (dispatch) => {
-    dispatch({ type: LOGIN_USER })
-    fetch('https://plataforma.especificosba.com.ar/back/login.php', {
-      method: 'POST',
-      headers: {
-          'Accept': 'application/json',
-          'Content-Type': 'application/json',
-      },
-      body: JSON.stringify({
-        user: email,
-        pass: password,
-      })
-    })
-    .then( (response) => response.json() )
-      .then( (responseJson) => {
-        loginUserSuccess(dispatch, responseJson);
-      });
-  //FIXME: Catch
+    dispatch({ type: LOGIN_USER });
+    post( "login.php", { user: email, pass: password } )
+      .then(
+        (response) => loginUserSuccess(dispatch, response)
+      ).catch(
+        (error) => loginUserFail(dispatch, error)
+      );
   };
-
-
 };
 
-const loginUserFail = (dispatch) => {
-  dispatch({ type: LOGIN_USER_FAIL });
+const loginUserFail = (dispatch, error) => {
+  console.log("login failed", error);
+  dispatch({ type: LOGIN_USER_FAIL, payload: error });
 };
 
 const loginUserSuccess = (dispatch, user) => {
+  console.log("success", user);
   dispatch({
     type: LOGIN_USER_SUCCESS,
     payload: user

+ 14 - 14
src/actions/CursadaActions.js

@@ -1,30 +1,30 @@
 import { Actions } from 'react-native-router-flux';
+import { post } from '../api/'
+
 import {
 	CURSADA_FETCH_SUCCESS,
 	CURSADAS_FETCH_SUCCESS,
+	CURSADAS_FETCH_FAIL,
 	CLASE_FETCH_SUCCESS,
 	CLASE_FETCH
 } from './types';
 
 export const cursadasFetch = () => {
 	return (dispatch) => {
-    fetch('https://plataforma.especificosba.com.ar/back/listaCursos.php', {
-      method: 'POST',
-      headers: {
-          'Accept': 'application/json',
-          'Content-Type': 'application/json',
-      },
-      body: JSON.stringify({
-        cursadas: 1
-      })
-    })
-    .then( (response) => response.json() )
-      .then( (responseJson) => {
+		//const path = 'listaCursos.php';
+		const path = 'qwrpoiewquewquoiewquoiewq.php';
+    post(path, { cursadas: 1 })
+      .then( (response) => {
         dispatch({
           type: CURSADAS_FETCH_SUCCESS,
-          payload: responseJson
+          payload: response
         });
-      });
+      })
+			.catch( (error) => {
+				dispatch({
+					type: CURSADAS_FETCH_FAIL
+				})
+			});
 	};
 };
 

+ 1 - 0
src/actions/types.js

@@ -4,6 +4,7 @@ export const LOGIN_USER_SUCCESS = "login_user_success";
 export const LOGIN_USER_FAIL = "login_user_fail";
 export const LOGIN_USER = "login_user";
 export const CURSADA_FETCH_SUCCESS = "cursada_fetch_success";
+export const CURSADAS_FETCH_FAIL = "cursadas_fetch_fail";
 export const CURSADAS_FETCH_SUCCESS = "cursadas_fetch_success";
 export const CLASE_FETCH_SUCCESS = "clase_fetch_success";
 export const CLASE_FETCH = "clase_fetch";

+ 25 - 0
src/api/api.js

@@ -0,0 +1,25 @@
+
+export async function post(path, data) {
+  const url = `https://plataforma.especificosba.com.ar/back/${path}`;
+  try {
+    const postParams = {
+                          method: 'POST',
+                          headers: {
+                              'Accept': 'application/json',
+                              'Content-Type': 'application/json',
+                          },
+                          body: JSON.stringify(data)
+                        };
+    let response = await fetch(url, postParams);
+    let json = await response.json();
+    //console.log(json);
+    if (response.ok)
+      return json;
+    if (json.error)
+      return Promise.reject(json.error);
+
+  } catch(error) {
+    //console.log("rejecting", path);
+    return Promise.reject("Error indefinido");
+  }
+}

+ 1 - 0
src/api/index.js

@@ -0,0 +1 @@
+export * from './api';

+ 1 - 0
src/components/LoginForm.js

@@ -58,6 +58,7 @@ class LoginForm extends Component {
         <Text style={styles.errorTextStyle}>
           {this.props.error}
         </Text>
+        
         <CardSection>
           {this.renderButton()}
         </CardSection>

+ 1 - 1
src/reducers/AuthReducer.js

@@ -23,7 +23,7 @@ export default (state = INITIAL_STATE, action) => {
     case LOGIN_USER_SUCCESS:
       return { ...state, user: action.payload, error: '', loading: false };
     case LOGIN_USER_FAIL:
-      return { ...state, error: 'Auth failed', password: '', loading: false };
+      return { ...state, user: null, error: action.payload, password: '', loading: false };
     case LOGIN_USER:
       return { ...state, loading: true, error: '' };
     default:

+ 7 - 2
src/reducers/CursadaReducer.js

@@ -1,5 +1,6 @@
 import {
   CURSADAS_FETCH_SUCCESS,
+  CURSADAS_FETCH_FAIL,
   CURSADA_FETCH_SUCCESS,
   CLASE_FETCH_SUCCESS,
   CLASE_FETCH
@@ -10,14 +11,18 @@ const INITIAL_STATE = {
   cursadas: [],
   curCursada: { clases: [] },
   curClase: { profesor: {} },
-  loading: true
+  loading: true,
+  error: false
 };
 
 export default (state = INITIAL_STATE, action) => {
   switch(action.type) {
     case CURSADAS_FETCH_SUCCESS:
       const { cursadas, cursos } = action.payload;
-      return { ...state, cursadas, cursos };
+      return { ...state, cursadas, cursos, error: false };
+    case CURSADAS_FETCH_FAIL:
+      console.log("cursadas fetch fail");
+      return { ...state, error: true };
     case CURSADA_FETCH_SUCCESS:
       return { ...state, curCursada: action.payload };
 	case CLASE_FETCH:

+ 11 - 4
src/scenes/ListaCursadas.js

@@ -31,6 +31,14 @@ class ListaCursadas extends Component  {
 	}
 
 	render() {
+		if (this.props.error) {
+			return (
+				<View>
+					<Text>Error obteniendo las cursadas</Text>
+				</View>
+			);
+		}
+
 		return (
 		<View>
 		      <ListView
@@ -44,10 +52,9 @@ class ListaCursadas extends Component  {
 };
 
 const mapStateToProps = state => {
-	/*const cursadas = _.map(state.cursadas, (val, uid) => {
-		return { ...val, uid };
-	});*/
-	return { cursadas: state.cursadasR.cursadas, cursos: state.cursadasR.cursos };
+	const { cursadas, cursos, error }  = state.cursadasR;
+	//console.log(state.cursadasR);
+	return { cursadas, cursos, error };
 };
 
 export default connect(mapStateToProps, { cursadasFetch })(ListaCursadas);