sectionActions.js 6.29 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();
		if (section.isFetching) {
			return;
		}
		dispatch(headerRequest());
        return new SectionService().header(section.id)
            .then(json => {
				let payload = parseHeader(json);
                dispatch(headerSuccess(payload));
            })
            .catch(error => {
				dispatch(headerFailure());
            });
	};
}

export function headerOnlyDispatchSuccess() {
	return (dispatch, getState) => {
		let {section} = getState();
		if (section.isFetching) {
			return;
		}
		// dispatch(headerRequest());
        return new SectionService().header(section.id)
            .then(json => {
				let payload = parseHeader(json);
                dispatch(headerSuccess(payload));
            })
            .catch(error => {
				// dispatch(headerFailure());
            });
	};
}

export function newPost(ptr = false) {
	return (dispatch, getState) => {
		let {section, user} = getState();
		if (section.new.isFetching || (!ptr && section.new.endReached) || section.new.error !== null) {
			return;
		}
		dispatch(newPostRequest(ptr));
		let uid = user.profile.uid;
		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, user} = getState();
		if (section.hot.isFetching || (!ptr && section.hot.endReached) || section.hot.error !== null) {
			return;
		}
		dispatch(hotPostRequest(ptr));
		let uid = user.profile.uid;
		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 noticeList = [];
	let noticeDuration = '3';
	let noticeOpen = 'N';
	if (textNoticeList && textNoticeList.list) {
		noticeList = textNoticeList.list;
		if (textNoticeList.time) {
			noticeDuration = textNoticeList.time;
		};
		if (noticeList.open) {
			noticeOpen = noticeList.open;
		}
	}

	return {
		notice: {
			list: noticeList,
			duration: parseFloat(noticeDuration),
			open: noticeOpen,
		},
		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 = decodeURI(item.contentData);
			}

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

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

		let post = {
			author: {
				avatar,
				uid,
				name,
			},
			createTime,
			timeago: timeagoStr,
			isTop: item.isForumTop === 0 ? false : true,
			isLike,
			title,
			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,
	}
}