subjectPostActions.js 3.39 KB
/**
 * # guideActions.js
 *
 * App user guide
 *
 */
'use strict';
import {Actions} from 'react-native-router-flux';
import PostingService from '../../services/PostingService';
import Immutable, {List, Record} from 'immutable';
import {
    NativeModules,
} from 'react-native';
import timeago from 'timeago.js';

const {
    SUBJECT_CONTENT_REQUEST,
    SUBJECT_CONTENT_SUCCESS,
    SUBJECT_CONTENT_FAILURE,

    SUBJECT_COMMENTS_REQUEST,
    SUBJECT_COMMENTS_SUCCESS,
    SUBJECT_COMMENTS_FAILURE,
    SUBJECT_ASSETS_SELECTED,
} = require('../../constants/actionTypes').default;

export function requestPostContent(postsId) {
    return dispatch => {
        dispatch(doRequestContent());
        let params = {postsId};
        new PostingService().getPostContent(params)
        .then(json => {
            NativeModules.YH_CommunityHelper.uid()
                .then(uid => {
                    json.LZ = (uid == json.authorInfo.uid);
                    dispatch(contentRequestSuccess(json));
                })
                .catch(error => {
                    json.LZ = false;
                    dispatch(contentRequestSuccess(json));
                });
        })
        .catch(error => {
            dispatch(contentRequestFailure(error));
        });
    }
}

export function doRequestContent() {
    return {
        type: SUBJECT_CONTENT_REQUEST,
    }
}

export function contentRequestSuccess(json) {
    return {
        type: SUBJECT_CONTENT_SUCCESS,
        payload: json,
    }
}

export function contentRequestFailure(error) {
    return {
        type: SUBJECT_CONTENT_FAILURE,
        payload: error,
    }
}

export function requestPostComments(postsId, lastedTime) {
    return dispatch => {
        dispatch(doRequestComments());
        let params = {
            postsId,
            lastedTime,
            limit: 10,
        };
        new PostingService().getPostComments(params)
        .then(json => {
            console.log(json);
            dispatch(commentsRequestSuccess(json));

        })
        .catch(error => {
            console.log(error);
            dispatch(commentsRequestFailure(error));
        });
    }
}

export function doRequestComments() {
    return {
        type: SUBJECT_COMMENTS_REQUEST,
    }
}

export function commentsRequestSuccess(json) {
    let {list} = json;
    let newList = [];
    list && list.map((obj,i)=> {
        let {createTime, reply, replyTo, blocks} = obj;
        let timeagoStr = timeago().format(createTime, 'zh_CN');
        let newBlocks = [];
        blocks && blocks.map((blockItem, i) => {
            let {commentId,content,templateKey,orderBy} = blockItem;
            let newItem = {
                "contentData": content,
                "order": orderBy,
                "templateKey": templateKey,
            };
            newBlocks.push(newItem);
        })
        let newObj={
            timeago: timeagoStr,
            headIcon: reply.headIcon,
            nickName: reply.nickName,
            LZ:false,
            blocks: newBlocks,
        }
        newList.push(newObj);
    })

    json.list = newList;
    return {
        type: SUBJECT_COMMENTS_SUCCESS,
        payload: json,
    }
}

export function commentsRequestFailure(error) {
    return {
        type: SUBJECT_COMMENTS_FAILURE,
        payload: error,
    }
}

export function assetsSelected(assets) {
    return {
        type: SUBJECT_ASSETS_SELECTED,
        payload: assets,
    }
}