如何使用react native调用另一个动作的redux动作?

时间:2018-06-11 12:59:40

标签: react-native react-redux redux-thunk

这是我的行动档案。它管理登录会话独占。 我想将数据内容保存在不同的reducer中,比如DataContentReducer或类似的东西。检查我应该执行调用的注释行。 我也在使用redux-think(不确定这是否相关......以防万一)

import {Actions} from 'react-native-router-flux'
import {SESSION_USER_DATA,SESSION_LOGIN_FAILURE,SESSION_LOGIN_IN_PROGRESS,SESSION_FIREBASE} from "./Types"
import {SessionLogin} from "../services/SrvcSession";
import * as firebase from 'firebase'
import {BACKEND} from '../config/backend'

export const login = (username, password) => {
  return dispatch => {
    dispatch({ type: SESSION_LOGIN_IN_PROGRESS,payload: true})
    SessionLogin(username,password)
        .then(res => res.data.successfully ? loginSuccessfully(res,dispatch,true) : loginFailure(res.data.error,dispatch) )
        .catch(error => loginFailure(error.code,dispatch))
  }
}
const loginSuccessfully = (res, dispatch,reallySuccessfully) => {
  const payload = res.data.data
  console.log(payload)
  dispatch ({type: SESSION_USER_DATA, payload})
  if(reallySuccessfully) {
    const firebaseApp = firebase.initializeApp(BACKEND.firebase.config)
    firebaseApp.auth().signInWithEmailAndPassword(payload.uuid + '@something.com', payload.extraToken1)
        .then(res => {
            dispatch ({type: SESSION_FIREBASE, payload: firebaseApp.database().ref()})
            dispatch({ type: SESSION_LOGIN_IN_PROGRESS,payload: false})
            //I'd like to create a subscription on the users's 
            //root key over here and store this in another reducer.
            //How to ?
            Actions.contacts()
        })
        .catch(error => loginFailure(error,dispatch))
  }
}
const loginFailure = (error, dispatch) => {
  dispatch({ type: SESSION_LOGIN_IN_PROGRESS,payload: false})
  dispatch ({
    type: SESSION_LOGIN_FAILURE,
    payload: error.code
  }

0 个答案:

没有答案