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

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

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 (ptr) {
			//下拉刷新直接执行
		} else {
			// 1.当次请求不是下拉刷新,同时正在进行下拉刷新的请求,跳出
			// 2.当次请求不是下拉刷新,同时接口请求正在加载中, 跳出
			// 3.当次请求不是下拉刷新,数据已全部加载完成,跳出
			if (section.ptr || section.new.isFetching || section.new.endReached) {
				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 (ptr) {
			//下拉刷新直接执行
		} else {
			// 1.当次请求不是下拉刷新,同时正在进行下拉刷新的请求,跳出
			// 2.当次请求不是下拉刷新,同时接口请求正在加载中, 跳出
			// 3.当次请求不是下拉刷新,数据已全部加载完成,跳出
			if (section.ptr || section.hot.isFetching || section.hot.endReached) {
				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 uri = json.forumPic ? json.forumPic : '';
	let title = json.forumName ? json.forumName : '';
	let post = number10KFormater(json.postsNum);
	let comment = number10KFormater(json.commentsNum);
	let like = number10KFormater(json.praiseNum);

	let header = {
		uri,
		title,
		post,
		comment,
		like,
	};

	let noticeList = [];
	let noticeDuration = '3';
	let noticeOpen = 'N';
	if (textNoticeList && textNoticeList.list) {
		noticeList = textNoticeList.list;
		if (textNoticeList.time) {
			noticeDuration = textNoticeList.time;
		};
		if (textNoticeList.open) {
			noticeOpen = textNoticeList.open;
		}
	}

	return {
		notice: {
			list: noticeList,
			duration: parseFloat(noticeDuration),
			open: noticeOpen,
		},
		header,
	};
	return json;
}

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

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

			if (item.templateKey === 'image') {
				let thumb = {
					src: 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(createTime);
		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,
	}
}