CommonAction.js 1.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
import OneSignal from 'react-native-onesignal';
import AuthApi from '../../network/api/authApi';
import CommonApi from '../../network/api/commonApi';
import api from '../../network/axios';
import AsyncStorageService from '../../utils/AsyncStorageService';
import ActionType from '../actions/types';
export const getMyInfo = () => {
  return async (dispatch, getState) => {
    const response = await CommonApi.requestGetMyInfo();
    //console.log("response", response);
    if (response.success) {
      dispatch({
        type: ActionType.GET_USER_INFO_SUCCESS,
        data: response.user,
      });
      OneSignal.removeExternalUserId();
      return response;
      // RootNavigation.navigate(APP_NAVIGATE_SCREEN.MAIN)
    }
  };
};
export const getUserLeaves = () => {
  return async (dispatch, getState) => {
    const response = await CommonApi.requestGetLeavesDay();
    // console.log("response getUserLeaves", response);

    if (response.success) {
      dispatch({
        type: ActionType.GET_LEAVES_SUCCESS,
        data: response.data.collection,
      });
    }
  };
};

export const logoutApp = () => {
  return async (dispatch, getState) => {
    const token = await AsyncStorageService.getToken();
    const response = await AuthApi.requestLogoutApp({token: token});
    //console.log("response", response);
    if (response.success) {
      await AsyncStorageService.clear();
      api.setBaseURL('');
      //RootNavigation.replace(APP_NAVIGATE_SCREEN.LOGIN, {index: 5});
    }
  };
};
export const getUserInfoById = userId => {
  return async (dispatch, getState) => {
    try {
      const response = await CommonApi.requestGetUserInfoById(userId);
      if (response.success) {
        return response.data;
      }
    } catch {
      console.log('Have an error when getUserInfoById');
    }
  };
};