subjectPostActions.js 5.85 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,
    SUBJECT_REPLY_REQUEST,
    SUBJECT_REPLY_SUCCESS,
    SUBJECT_REPLY_FAILURE,
    SUBJECT_REPLY_UPDATE,
} = 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,postInfo,id} = 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={
            commentId: id||'',
            cidTo: postInfo.authorUid||'',
            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,
    }
}

export function commentWithParams(params) {
    return (dispatch, getState) => {
        dispatch(startComment());
        let {subject} = getState();
        let assets = subject.assets.toJS();
        if (assets.length) {
            for (let i = 0; i < assets.length; i++) {
                let asset = assets[i]

                new PostingService().uploadImageAsset(asset)
                .then(response => {
                    dispatch(updateCommentProgress(response, params));
                }).catch(error => {
                    dispatch(commentFail(error));
                });
            }
        } else {
            dispatch(commitComment(params));
        }
    }
}

export function startComment() {
    return {
        type: SUBJECT_REPLY_REQUEST,
    }
}

export function commitComment(params) {
    return dispatch => {
        new PostingService().commitReply(params)
        .then(response => {
            dispatch(commentSuccess(response));
        }).catch(error => {
            dispatch(commentFail(error));
        });
    }
}

export function updateCommentProgress(url, params) {
    return (dispatch, getState) => {
        let {subject} = getState();
        let {assetsUrlStr, assetFinishCount} = subject;
        let newStr = assetsUrlStr + url + ',';
        let newCount = assetFinishCount + 1;
        let payloadData = {
            newStr,
            newCount,
        };
        console.log(payloadData);
        dispatch(commentImageStateChanged(payloadData));
        dispatch(() => {
            let {subject} = getState();
            if (subject.assetFinishCount == subject.assets.toJS().length) {
                let newParams = {
                    ...params,
                    images: subject.assetsUrlStr,
                }
                console.log(newParams);
                dispatch(commitComment(newParams));
            }

        });
    }
}

export function commentImageStateChanged(payloadData) {
    return {
        type: SUBJECT_REPLY_UPDATE,
        payload: payloadData,
    }
}

export function commentSuccess(json) {
    return {
        type: SUBJECT_REPLY_SUCCESS,
        payload: json,
    }
}

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