sectionActions.js 7.78 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(sid) {
    return {
        type: SECTION_HEADER_REQUEST,
		payload: sid
    };
}

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

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

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

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

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

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

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

export function header(sid) {
	return (dispatch, getState) => {
		let {section} = getState();

		let item = section.items.get(sid);

		if (item.isFetching) {
			return;
		}
		dispatch(headerRequest(sid));
        return new SectionService().header(item.id)
            .then(json => {
				let payload = parseHeader(json);
                dispatch(headerSuccess(sid, payload));
            })
            .catch(error => {
				dispatch(headerFailure(sid, error));
            });
	};
}

// 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(sid, ptr = false) {
	return (dispatch, getState) => {
		let {section, user} = getState();
		let item = section.items.get(sid);

		// 接口请求跳出的条件:
		// 前置条件:下拉刷新优先级高于上拉加载
		if (ptr) {
			//下拉刷新直接执行
		} else {
			// 1.当次请求不是下拉刷新,同时正在进行下拉刷新的请求,跳出
			// 2.当次请求不是下拉刷新,同时接口请求正在加载中, 跳出
			// 3.当次请求不是下拉刷新,数据已全部加载完成,跳出
			if (item.ptr || item.new.isFetching || item.new.endReached) {
				return;
			}
		}

		dispatch(newPostRequest(sid, ptr));
		let uid = user.profile.uid;
		let forumCode = item.id;
		let lastedTime = 0;
		if (!ptr) {
			lastedTime = item.new.lastedTime;
		}
		let limit = 10;
        return new SectionService().newPost(uid, lastedTime, forumCode, limit)
            .then(json => {
				let payload = parseNewPost(json);
				if (!ptr) {
					let oldList = item.new.list.toJS();
					let list = [...oldList, ...payload.list];
					payload.list = list;
				}
                dispatch(newPostSuccess(sid, payload));
            })
            .catch(error => {
                dispatch(newPostFailure(sid, error));
            });
	};
}

export function hotPost(sid, ptr = false) {
	return (dispatch, getState) => {
		let {section, user} = getState();
		let item = section.items.get(sid);
		// 接口请求跳出的条件:
		// 前置条件:下拉刷新优先级高于上拉加载
		if (ptr) {
			//下拉刷新直接执行
		} else {
			// 1.当次请求不是下拉刷新,同时正在进行下拉刷新的请求,跳出
			// 2.当次请求不是下拉刷新,同时接口请求正在加载中, 跳出
			// 3.当次请求不是下拉刷新,数据已全部加载完成,跳出
			if (item.ptr || item.hot.isFetching || item.hot.endReached) {
				return;
			}
		}

		dispatch(hotPostRequest(sid, ptr));
		let uid = user.profile.uid;
		let forumCode = item.id;
		let lastedTime = 0;
		if (!ptr) {
			lastedTime = item.hot.lastedTime;
		}
		let limit = 10;
        return new SectionService().hotPost(uid, lastedTime, forumCode, limit)
            .then(json => {
				let payload = parseNewPost(json);
				if (!ptr) {
					let oldList = item.hot.list.toJS();
					let list = [...oldList, ...payload.list];
					payload.list = list;
				}
                dispatch(hotPostSuccess(sid, payload));
            })
            .catch(error => {
                dispatch(hotPostFailure(sid, error));
            });
	};
}

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

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

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

	let uri = json.forumPic ? json.forumPic : '';
	let title = json.forumName ? json.forumName : '';
	let desc = json.forumDesc ? json.forumDesc : '';
	let post = number10KFormater(json.postsNum);
	let comment = number10KFormater(json.commentsNum);
	let like = number10KFormater(json.praiseNum);

	let header = {
		uri,
		title,
		desc,
		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) : '';
		title = title === '' ? desc : title;
		let id = item.id ? item.id : 0;

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