sectionActions.js 5.4 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';

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

const {
	SECTION_HEADER_REQUEST,
    SECTION_HEADER_SUCCESS,
    SECTION_HEADER_FAILURE,

    SECTION_NEW_POST_REQUEST,
    SECTION_NEW_POST_SUCCESS,
    SECTION_NEW_POST_FAILURE,

    SECTION_HOT_POST_REQUEST,
    SECTION_HOT_POST_SUCCESS,
    SECTION_HOT_POST_FAILURE,

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

export function headerRequest() {
    return {
        type: SECTION_HEADER_REQUEST,
    };
}

export function headerSuccess(json) {
    return {
        type: SECTION_HEADER_SUCCESS,
        payload: json
    };
}

export function headerFailure(error) {
    return {
        type: SECTION_HEADER_FAILURE,
        payload: error
    };
}

export function newPostRequest(ptr) {
    return {
        type: SECTION_NEW_POST_REQUEST,
		payload: ptr,
    };
}

export function newPostSuccess(json) {
    return {
        type: SECTION_NEW_POST_SUCCESS,
        payload: json
    };
}

export function newPostFailure(error) {
    return {
        type: SECTION_NEW_POST_FAILURE,
        payload: error
    };
}
export function hotPostRequest(ptr) {
    return {
        type: SECTION_HOT_POST_REQUEST,
		payload: ptr,
    };
}

export function hotPostSuccess(json) {
    return {
        type: SECTION_HOT_POST_SUCCESS,
        payload: json
    };
}

export function hotPostFailure(error) {
    return {
        type: SECTION_HOT_POST_FAILURE,
        payload: error
    };
}

export function header() {
	return (dispatch, getState) => {
		let {section} = getState();
        return new SectionService().header(section.id)
            .then(json => {
				let payload = parseHeader(json);
                dispatch(headerSuccess(payload));
            })
            .catch(error => {
				__DEV__ && console.error(error);
            });
	};
}

export function newPost(ptr = false) {
	return (dispatch, getState) => {
		let {section} = getState();
		if (section.new.isFetching || section.new.endReached) {
			return;
		}
		dispatch(newPostRequest(ptr));
		let uid = 2222222;
		let forumCode = section.id;
		let lastedTime = 0;
		if (!ptr) {
			lastedTime = section.new.lastedTime;
		}
		let limit = 10;
        return new SectionService().newPost(uid, lastedTime, forumCode, limit)
            .then(json => {
				let payload = parseNewPost(json);
				if (!ptr) {
					let oldList = section.new.list.toJS();
					let list = [...oldList, ...payload.list];
					payload.list = list;
				}
                dispatch(newPostSuccess(payload));
            })
            .catch(error => {
                dispatch(newPostFailure(error));
            });
	};
}

export function hotPost(ptr = false) {
	return (dispatch, getState) => {
		let {section} = getState();
		if (section.hot.isFetching || section.hot.endReached) {
			return;
		}
		dispatch(hotPostRequest(ptr));
		let uid = 2222222;
		let forumCode = section.id;
		let lastedTime = 0;
		if (!ptr) {
			lastedTime = section.hot.lastedTime;
		}
		let limit = 10;
        return new SectionService().hotPost(uid, lastedTime, forumCode, limit)
            .then(json => {
				let payload = parseNewPost(json);
				if (!ptr) {
					let oldList = section.hot.list.toJS();
					let list = [...oldList, ...payload.list];
					payload.list = list;
				}
                dispatch(hotPostSuccess(payload));
            })
            .catch(error => {
                dispatch(hotPostFailure(error));
            });
	};
}

export function setActiveTab(tab) {
	return {
		type: SECTION_SET_ACTIVE_TAB,
		payload: tab,
	};
}

export function sectionClean() {
	return {
		type: SECTION_CLEAN,
	};
}

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

	let header = {
		uri: json.forumPic,
		title: json.forumName,
		post: json.postsNum,
		comment: json.commentsNum,
		like: json.praiseNum,
	};

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

function parseNewPost(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.isForumTop === 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,
	}
}