groupPurchaseDetailActions.js 4.44 KB
'use strict';

import ReactNative from 'react-native';
import GroupPurchaseService from '../../services/GroupPurchaseService';

const Platform = require('Platform');

const {
    SET_GROUP_NO,
    SET_ACTIVITY_ID,
    PRODUCT_LIST_REQUEST,
    PRODUCT_LIST_SUCCESS,
    PRODUCT_LIST_FAILURE,
    RESOURCEINFO_REQUEST,
    RESOURCEINFO_SUCCESS,
    RESOURCEINFO_FAILURE,
    ACTIVITY_GROUP_DETAIL_REQUEST,
    ACTIVITY_GROUP_DETAIL_SUCCESS,
    ACTIVITY_GROUP_DETAIL_FAILURE,
    UPDATE_LEFTTIME,
    SHOWSHARVIEW,
    SHOWSNAPSHOOTSHARE,
} = require('../../constants/actionTypes').default;

export function showShareView(show) {
    return {
        type: SHOWSHARVIEW,
        payload: show,
    }
}

export function showSnapshootShare(show) {
    return {
        type: SHOWSNAPSHOOTSHARE,
        payload: show,
    }
}

export function setGroupNo(groupNo) {
    return {
        type: SET_GROUP_NO,
        payload: groupNo,
    }
}
export function setDetailActivityId(activityId) {
    return {
        type: SET_ACTIVITY_ID,
        payload: activityId,
    }
}
export function productListRequest() {
    return {
        type: PRODUCT_LIST_REQUEST,
    };
}

export function productListSuccess(json) {
    return {
        type: PRODUCT_LIST_SUCCESS,
        payload: json
    }
}

export function productListFailure(error) {
    return {
        type: PRODUCT_LIST_FAILURE,
        payload: error
    }
}

export function resourceInfoRequest() {
    return {
        type: RESOURCEINFO_REQUEST,
    };
}

export function resourceInfoSuccess(json) {
    return {
        type: RESOURCEINFO_SUCCESS,
        payload: json
    }
}

export function resourceInfoFailure(error) {
    return {
        type: RESOURCEINFO_FAILURE,
        payload: error
    }
}

export function activityGroupDetailRequest() {
    return {
        type: ACTIVITY_GROUP_DETAIL_REQUEST,
    };
}

export function activityGroupDetailSuccess(json) {
    return {
        type: ACTIVITY_GROUP_DETAIL_SUCCESS,
        payload: json
    }
}

export function activityGroupDetailFailure(error) {
    return {
        type: ACTIVITY_GROUP_DETAIL_FAILURE,
        payload: error
    }
}

export function fetchActivityGroupDetail() {
  return (dispatch, getState) => {
    let {app,groupPurchaseDetail} = getState();
    let {groupNo} = groupPurchaseDetail;

    let getActivityGroupDetail = (groupNo ,uid) => {
      dispatch(activityGroupDetailRequest());
      return new GroupPurchaseService(app.host).fetchActivityGroupDetail(groupNo ,uid)
      .then(json => {
        dispatch(activityGroupDetailSuccess(json));
      })
      .catch(error => {
        dispatch(activityGroupDetailFailure(error));
      });
    }


    ReactNative.NativeModules.YH_CommonHelper.uid()
    .then(uid => {
      getActivityGroupDetail( groupNo ,uid);
    })
    .catch(error => {
      ReactNative.NativeModules.YH_CommonHelper.login()
      .then(uid => {
        getActivityGroupDetail( groupNo ,uid);
      })
      .catch(error => {

      });
    });
  };
}

export function getProductList(reload=false) {
	return (dispatch, getState) => {
		let {app, groupPurchaseDetail} = getState();
		let {productList,activityId} = groupPurchaseDetail;
    if (reload) {

		} else {
			if (productList.isFetching || productList.endReached || productList.error) {
				return;
			}
		}

		let page = productList.page + 1;
		let limit = productList.pageSize;
    let fromPage = Platform.OS === 'android' ? 'aFP_GroupPurchaseDetail' : 'iFP_GroupPurchaseDetail';

		dispatch(productListRequest());
    return new GroupPurchaseService(app.host).fetchProductList(activityId, page, limit,fromPage)
    .then(json => {
			json.endReached = json.page == json.page_total;
			if (json.page > 1) {
				let oldList = productList.list.toJS();
				let list = [...oldList, ...json.collageProductVoList];
				json.collageProductVoList = list;
			}
			dispatch(productListSuccess(json));
		})
		.catch(error => {
			dispatch(productListFailure(error));
		});
	};
}

export function updateTime(leftTime) {
    return {
        type: UPDATE_LEFTTIME,
        payload: leftTime
    }
}

export function fetchResourceInfo() {
  return (dispatch, getState) => {
    let {app} = getState();
    dispatch(resourceInfoRequest());
    let content_code = 'cea0efae77f4e04c935beb1e87181247';
    return new GroupPurchaseService(app.host).fetchDetailResourceInfo(content_code)
    .then(json => {
      dispatch(resourceInfoSuccess(json));
    })
    .catch(error => {
      dispatch(resourceInfoFailure(error));
    });
  };
}