initSetupEpics.js 3.76 KB
Newer Older
李彥志's avatar
李彥志 committed
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
import { from, of } from 'rxjs';
import {
  map, mergeMap, catchError
} from 'rxjs/operators';
import { ofType } from 'redux-observable';
import { BASIC_MEASUREMENT, USER_STANDARD } from '../../constants';
import {
  updatePersonalData,
  getPersonalDefinedData,
  updatePersonalDefinedData
} from '../../services/initSetupService';
import {
  setParamsFromStorage
} from '../../services/storageService';
import ActionTypes from '../../actions';
import appGlobal from '../../lib/common/AppGlobal';

const updatePersonalDetailHandler = (response, action) => {
  if (response !== null) {
    const saveObj = {};
    if (action.payload.body
      && (action.payload.body.updateType === 'StandardValue')
      || (action.payload.body.updateType === 'bodyMeasurement')) {
      saveObj.weight = action.payload.body.weight;
      saveObj.height = action.payload.body.height;
      Object.assign(saveObj, { token: appGlobal.token() || '' });
      // 存AppStorage
      setParamsFromStorage(BASIC_MEASUREMENT, saveObj);
    }
    return {
      type: ActionTypes.UPDATE_PERSONAL_DETAIL_SUCCESS,
      payload: response
    };
  }
  throw response;
};

const updatePersonalDefineHandler = (response, action) => {
  if (response !== null) {
    const saveObj = {};
    saveObj.diastolicLow = action.payload.body.diastolicLow;
    saveObj.diastolicHigh = action.payload.body.diastolicHigh;
    saveObj.systolicLow = action.payload.body.systolicLow;
    saveObj.systolicHigh = action.payload.body.systolicHigh;
    saveObj.heartRateLow = action.payload.body.heartRateLow;
    saveObj.heartRateHigh = action.payload.body.heartRateHigh;
    saveObj.dailyStep = action.payload.body.dailyStep;
    saveObj.sleepTime = action.payload.body.sleepTime;
    // 將個人指標設定參數存放在global變數中
    appGlobal.setUserStandard(saveObj);
    Object.assign(saveObj, { token: appGlobal.token() || '' });
    // 存AppStorage
    setParamsFromStorage(USER_STANDARD, saveObj);
    if (action.payload.body && action.payload.body.updateType === 'Finish') {
      return {
        type: ActionTypes.HOME,
        payload: { accountUID: appGlobal.uid() }
      };
    }
    return {
      type: ActionTypes.UPDATE_PERSONAL_DEFINE_SUCCESS,
      payload: response
    };
  }
  throw response;
};

const getPersonalDefinedDataHandler = (response) => {
  if (response !== null) {
    return {
      type: ActionTypes.GET_PERSONAL_DEFINED_DATA_SUCCESS,
      payload: response
    };
  }
  throw response;
};

const updatePersonalDetailFailure = error => ({
  type: ActionTypes.UPDATE_PERSONAL_DETAIL_FAILURE,
  payload: error
});

const updatePersonalDefineFailure = error => ({
  type: ActionTypes.UPDATE_PERSONAL_DEFINE_FAILURE,
  payload: error
});

const getPersonalDefinedDataFailure = error => ({
  type: ActionTypes.GET_PERSONAL_DEFINED_DATA_FAILURE,
  payload: error
});

export const updatePersonalDetailEpic = action$ => action$.pipe(
  ofType(ActionTypes.UPDATE_PERSONAL_DETAIL_REQUEST),
  mergeMap(action => from(updatePersonalData(action.payload.body)).pipe(
    map(response => updatePersonalDetailHandler(response, action)),
    catchError(error => of(updatePersonalDetailFailure(error)))
  ))
);

export const getPersonalDefinedDataEpic = action$ => action$.pipe(
  ofType(ActionTypes.GET_PERSONAL_DEFINED_DATA_REQUEST),
  mergeMap(() => from(getPersonalDefinedData()).pipe(
    map(response => getPersonalDefinedDataHandler(response)),
    catchError(error => of(getPersonalDefinedDataFailure(error)))
  ))
);

export const updatePersonalDefineEpic = action$ => action$.pipe(
  ofType(ActionTypes.UPDATE_PERSONAL_DEFINE_REQUEST),
  mergeMap(action => from(updatePersonalDefinedData(action.payload.body)).pipe(
    map(response => updatePersonalDefineHandler(response, action)),
    catchError(error => of(updatePersonalDefineFailure(error)))
  ))
);