homeActions.js 5.74 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';

import {Actions} from 'react-native-router-flux';
import HomeService from '../../services/HomeService';
import timeago from 'timeago.js';

const {

	HOME_BNS_REQUEST,
    HOME_BNS_SUCCESS,
    HOME_BNS_FAILURE,

	HOME_RECOMMENDATION_REQUEST,
    HOME_RECOMMENDATION_SUCCESS,
    HOME_RECOMMENDATION_FAILURE,

	GO_TO_SECTION,
	SYNC_USER_REQUEST,
    SYNC_USER_SUCCESS,
    SYNC_USER_FAILURE,

} = require('../../constants/actionTypes').default;

export function syncUserRequest() {
    return {
        type: SYNC_USER_REQUEST,
    };
}

export function syncUserSuccess(json) {
    return {
        type: SYNC_USER_SUCCESS,
        payload: json
    };
}

export function syncUserFailure(error) {
    return {
        type: SYNC_USER_FAILURE,
        payload: error
    };
}

export function syncUser(uid) {
	return (dispatch, getState) => {
		dispatch(syncUserRequest());
		let {app} = getState();
        return new HomeService().syncUser(uid, app.container)
            .then(json => {
                dispatch(syncUserSuccess(json.ssouid));
            })
            .catch(error => {
                dispatch(syncUserFailure(error));
            });
	};
}

export function bnsRequest() {
    return {
        type: HOME_BNS_REQUEST,
    };
}

export function bnsSuccess(json) {
    return {
        type: HOME_BNS_SUCCESS,
        payload: json
    };
}

export function bnsFailure(error) {
    return {
        type: HOME_BNS_FAILURE,
        payload: error
    };
}

export function recommendationRequest(ptr) {
    return {
        type: HOME_RECOMMENDATION_REQUEST,
		payload: ptr,
    };
}

export function recommendationSuccess(json) {
    return {
        type: HOME_RECOMMENDATION_SUCCESS,
        payload: json
    };
}

export function recommendationFailure(error) {
    return {
        type: HOME_RECOMMENDATION_FAILURE,
        payload: error
    };
}

export function bannerNoticeSection() {
	return (dispatch, getState) => {
		// dispatch(bnsRequest());
		let {app} = getState();
        return new HomeService().bannerNoticeSection(app.container)
            .then(json => {
				let payload = parseBNS(json);
                dispatch(bnsSuccess(payload));
            })
            .catch(error => {
                // dispatch(bnsFailure(error));
            });
	};
}

export function recommendation(ptr = false) {
	return (dispatch, getState) => {
		let {home} = getState();
		if (home.isFetching || home.recommendation.endReached) {
			return;
		}
		dispatch(recommendationRequest(ptr));

		let uid = 2222222;
		let lastedTime = 0;
		if (!ptr) {
			lastedTime = home.recommendation.lastedTime;
		}
		let limit = 10;
        return new HomeService().recommendation(uid, lastedTime, limit)
            .then(json => {
				let payload = parseRecommendation(json);
				if (!ptr) {
					let oldList = home.recommendation.list.toJS();
					let list = [...oldList, ...payload.list];
					payload.list = list;
				}
                dispatch(recommendationSuccess(payload));
            })
            .catch(error => {
                dispatch(recommendationFailure(error));
            });
	};
}

export function goToSection(section, previousScene) {
	Actions.Section();
	return {
		type: GO_TO_SECTION,
        payload: {...section, previousScene},
	};
}

function parseBNS(json) {
	let {resourceList, forumInfo} = json;
	let {advertList, textNoticeList} = resourceList;

	let section = [];
	forumInfo.map((item, i) => {
		let {hotPost, newPost} = item;
		let sectionItem = {
			header: {
				id: item.forumCode,
				logo: item.forumPic,
				title: item.forumName,
				postNum: item.postsNum,
				commentNum: item.commentsNum,
				laudCount: item.praiseNum,
			},
			hot: {
				avatar: hotPost ? hotPost.avatar : '',
				content: hotPost ? hotPost.contentData : '',
				hasImg: hotPost ? hotPost.hasImgFlg : '',
				postId: hotPost ? hotPost.postId : '',
				postTitle: hotPost ? hotPost.postsTitle : '',
			},
			new: {
				avatar: newPost ? newPost.avatar : '',
				content: newPost ? newPost.contentData : '',
				hasImg: newPost ? newPost.hasImgFlg : '',
				postId: newPost ? newPost.postId : '',
				postTitle: newPost ? newPost.postsTitle : '',
			},
			num: {
				onedayAddNum: item.oneDayAddNum,
			},
		};
		section.push(sectionItem);
	});

	let bannerDuration = advertList.speed ? advertList.speed : '3';
	let noticeDuration = textNoticeList.time ? textNoticeList.time : '3';
	return {
		banner: {
			list: advertList.list,
			duration: parseFloat(bannerDuration),
		},
		notice: {
			list: textNoticeList.list,
			duration: parseFloat(noticeDuration),
			open: textNoticeList.open,
		},
		section,
	};
}

function parseRecommendation(json) {
	let {lastedTime, list} = json;
	let posts = [];
	list.map((item, i) => {
		let {authorInfo, blocks} = item;

		let desc = '';
		let thumbs = [];
		blocks.map((item, i) => {
			if (desc === '' && item.templateKey === 'text') {
				desc = item.contentData;
			}

			if (item.templateKey === 'image') {
				let thumb = {
					src: item.contentData,
				};
				thumbs.push(thumb);
			}
		});

		let avatar = authorInfo && authorInfo.headIcon;
		let uid = authorInfo && authorInfo.uid;
		let name = authorInfo && authorInfo.name;
		let createTime = item.createTime;
		let timeagoStr = timeago().format(createTime, 'zh_CN');
		let isLike = item.hasPraise === 'Y' ? true : false;

		let post = {
			author: {
				avatar,
				uid,
				name,
			},
			createTime,
			timeago: timeagoStr,
			isTop: item.isIndexTop === 0 ? false : true,
			isLike,
			title: item.postsTitle,
			desc,
			thumbs,
			section: {
				id: item.forumCode,
				name: item.forumName,
			},
			commentCount: item.comment,
			likeCount: item.praise,
		}

		posts.push(post);
	});

	let endReached = posts.length == 0;
	return {
		lastedTime,
		list: posts,
		endReached,
	}
}