groupPurchaseDetailActions.js 7.01 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,
    SHOWCOUPONVIEW,
    SHOWSNAPSHOOTSHARE,
    COUPONINFO_REQUEST,
    COUPONINFO_SUCCESS,
    COUPONINFO_FAILURE,
} = require('../../constants/actionTypes').default;

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

export function showCouponView(show) {
    return {
        type: SHOWCOUPONVIEW,
        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 couponInfoRequest() {
    return {
        type: COUPONINFO_REQUEST,
    };
}

export function couponInfoSuccess(json) {
    return {
        type: COUPONINFO_SUCCESS,
        payload: json
    }
}

export function couponInfoFailure(error) {
    return {
        type: COUPONINFO_FAILURE,
        payload: error
    }
}


export function fetchActivityGroupDetail(FP_NAME, FP_PARAM, callback) {
  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 => {
            // 注释上报事件
            // try {
            //     let event_params = {
            //         FP_NAME,
            //         FP_PARAM,
            //         PRD_ID: '',
            //         PRD_SKN: json.membershipItems[0].productSkn,
            //         PRD_TYPE: 1,
            //     }
            //     YH_CommonHelper.logEvent('YB_GDS_DT_INFO', event_params);
            // } catch (e) {
            //     console.log(e);
            // }
        dispatch(activityGroupDetailSuccess(json));
        callback && callback(json.lackNum);
      })
      .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).fetchGroupDetailProductList(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 = '3c3a94fd6c6e19508b6921acd7f6cbad';
    return new GroupPurchaseService(app.host).fetchDetailResourceInfo(content_code)
    .then(json => {
        let resourceList = json.length > 0 && json[0].data ? json[0].data : [];
        resourceList.forEach(function (item, index) {
            item.yh_exposureData = {
                P_NAME: Platform.OS === 'ios' ? 'iFP_GroupPurchaseDetail' : 'aFP_GroupPurchaseDetail',
                P_PARAM: content_code,
                F_URL: item.url,
                I_INDEX: index,
                exposureEnd: 1
            };
            return item
        })

      dispatch(resourceInfoSuccess(json));
    })
    .catch(error => {
      dispatch(resourceInfoFailure(error));
    });
  };
}

export function fetchCouponInfo(orderCode, lackNum) {
  return (dispatch, getState) => {
    let {app: {host}} = getState();

    let fetchCouponInfo = (orderCode ,uid) => {
      dispatch(couponInfoRequest());
      return new GroupPurchaseService(host).fetchCouponInfo(orderCode, uid)
        .then(json => {
            if (typeof json === 'string' && json) {
              dispatch(showCouponView(true));
              dispatch(couponInfoSuccess(json));
            } else if (lackNum != 0) {
              dispatch(showShareView(true));
            }
        })
        .catch(error => {
          if (lackNum != 0) {
            dispatch(showShareView(true));
          }
            dispatch(couponInfoFailure(error));
        });
    };

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

      });
    });
  };
}