contentActions.js 9.14 KB
'use strict';

import ReactNative from 'react-native';
import ContentService from '../../services/ContentService';
import Moment from "moment";

const {
    SET_CONTENT_LIST_ID,
    SET_CONTENT_CATEGORY_NAME,
    SET_CONTENT_TIP_FLAG,

    //消息类型
    FETCH_CONTENT_TYPE_REQUEST,
    FETCH_CONTENT_TYPE_SUCCESS,
    FETCH_CONTENT_TYPE_FAILURE,

    //消息内容
    FETCH_CONTENT_LIST_REQUEST,
    FETCH_CONTENT_LIST_SUCCESS,
    FETCH_CONTENT_LIST_FAILURE,

    //回复评论
    ADD_ARTICLE_COMMENT_REQUEST,
    ADD_ARTICLE_COMMENT_SUCCESS,
    ADD_ARTICLE_COMMENT_FAILURE,

    //关注用户
    UPDATE_ATTENTION_REQUEST,
    UPDATE_ATTENTION_SUCCESS,
    UPDATE_ATTENTION_FAILURE,

    //清除消息内容
    MESSAGE_CLEAR_OTHER_LIST,
    MESSAGE_CLEAR_COMMENT_LIST,

} = require('../../constants/actionTypes').default;

export function setContentListId(id) {
	return {
      type: SET_CONTENT_LIST_ID,
      payload: id,
    };
}

export function setContentCategoryName(name) {
	return {
		type: SET_CONTENT_CATEGORY_NAME,
		payload: name,
	};
}

export function setContentTipFlag(value) {
	return {
		type: SET_CONTENT_TIP_FLAG,
		payload: value,
	};
}

export function getContentTypeRequest() {
    return {
        type: FETCH_CONTENT_TYPE_REQUEST,
    };
}

export function getContentTypeSuccess(json) {
    return {
        type: FETCH_CONTENT_TYPE_SUCCESS,
        payload: json
    }
}

export function getContentTypeFailure(error) {
    return {
        type: FETCH_CONTENT_TYPE_FAILURE,
        payload: error
    }
}

export function getContentListRequest(isRefresh, isLatest) {
    return {
        type: FETCH_CONTENT_LIST_REQUEST,
        isLatest,
        isRefresh
    };
}

export function getContentListSuccess(json, isLatest) {
    return {
        type: FETCH_CONTENT_LIST_SUCCESS,
        payload: json,
        isLatest,
    }
}

export function getContentListFailure(error, isLatest) {
    return {
        type: FETCH_CONTENT_LIST_FAILURE,
        payload: error,
        isLatest,
    }
}

export function addArticleCommentRequest() {
    return {
        type: ADD_ARTICLE_COMMENT_REQUEST,
    }
}

export function addArticleCommentSuccess(json) {
    return {
        type: ADD_ARTICLE_COMMENT_SUCCESS,
        payload: json,
    }
}

export function addArticleCommentFailure(error) {
    return {
        type: ADD_ARTICLE_COMMENT_FAILURE,
        payload: error,
    }
}

export function updateAttentionRequest() {
    return {
        type: UPDATE_ATTENTION_REQUEST,
    }
}

export function updateAttentionSuccess(json) {
    return {
        type: UPDATE_ATTENTION_SUCCESS,
        payload: json,
    }
}

export function updateAttentionFailure(error) {
    return {
        type: UPDATE_ATTENTION_FAILURE,
        payload: error,
    }
}

//清除评论消息
export function clearCommentMessageListAction() {
    return {
        type: MESSAGE_CLEAR_COMMENT_LIST,
    }
}

export function clearOtherMessageListAction() {
    return {
        type: MESSAGE_CLEAR_OTHER_LIST,
    }
}

/*
 * 获取通知类型
 */
export function getContentType() {
    return (dispatch, getState) => {
        let {app, content} = getState();
        let {isFetching} = content.toJS().contentType
        if (isFetching) {
            return;
        }
        let processor = ()=> {
            dispatch(getContentTypeRequest());
            return new ContentService().fetchContentType()
                .then(json => {
                    let listJson = json ? json['grassInBoxTypeInfoList'] : [];
                    let list = listJson.map((item)=> {
                        return {
                            description: item.description,
                            type: item.type,
                            actionUrl: item.actionUrl,
                            icon: {uri: item.imgUrl},
                            unReadCount: item.unReadCount,
                        }
                    })
                    dispatch(getContentTypeSuccess(list));
                })
                .catch(error => {
                    dispatch(getContentTypeFailure(error));
                });
        }
        ReactNative.NativeModules.YH_CommonHelper.uid()
            .then(uid => {
                processor()
            })
            .catch(error => {
                ReactNative.NativeModules.YH_CommonHelper.login()
                    .then(uid => {
                        processor()
                    })
                    .catch(error => {

                    });
            });
    };
}

//查询消息
export function getContentList(isRefresh, type) {
    return (dispatch, getState) => {
        let {app, content} = getState();
        let {isFetching, endReached, currentPage, list} = type == 4 ? content.get('commentList').toJS() : content.get('contentList').toJS();
        if (isFetching || (!isRefresh && endReached)) {
            return;
        }
        let oldList = isRefresh ? [] : list
        let processor = ()=> {
            dispatch(getContentListRequest(isRefresh, type == 4));
            return new ContentService().fetchContentList(isRefresh ? 1 : currentPage + 1, content.pageSize, type)
                .then(json => {
                    let payload = parseContentList(json);
                    let list = [...oldList, ...payload.list];
                    payload.list = list;
                    payload.type = type;

                    if (payload.currentPage == 1 && payload.list.length == 0) {
          						 payload.shouldShowEmpty = true;
          					}
                    dispatch(getContentListSuccess(payload, type == 4));
                })
                .catch(error => {
                    dispatch(getContentListFailure(error, type == 4));
                });
        }
        ReactNative.NativeModules.YH_CommonHelper.uid()
            .then(uid => {
                processor()
            })
            .catch(error => {
                ReactNative.NativeModules.YH_CommonHelper.login()
                    .then(uid => {
                        processor()
                    })
                    .catch(error => {

                    });
            });
    };
}

function parseContentList(json) {
    let {data, page, totalPage} = json;
    let parsedList = data ? data.map((item) => {
        return Object.assign({},item,{

          headIco: item.headIco,
          userName: item.userName,

          isMutualAttention: item.isMutualAttention,

          commentId: item.commentId,
          parentCommentId: item.parentCommentId,
          commentContent: item.commentContent,
          parentContent: item.parentCommentContent,

          coverImg: item.coverImg,
          articleId: item.articleId,
          content: item.content,

          businessType: item.businessType,

          createTime: Moment(new Date(item.createTime * 1000)).format('YYYY.MM.DD HH:mm'),
        })
    }) : [];

    let endReached = parsedList.length == 0 || page == totalPage;
    let pageCount = totalPage;
    return {
        currentPage: page,
        list: parsedList,
        endReached,
        pageCount,
    }
}

//回复评论
export function addArticleCommentAction(content, destId, commentId, addType, columnType) {
    return (dispatch, getState) => {
        let {app} = getState();
        let processor = (uid)=> {
            dispatch(addArticleCommentRequest());
            return new ContentService().addArticleComment(content, destId, uid, commentId, addType, columnType)
                .then(json => {
                    dispatch(addArticleCommentSuccess(json));
                })
                .catch(error => {
                    dispatch(addArticleCommentFailure(error));
                });
        }
        ReactNative.NativeModules.YH_CommonHelper.uid()
            .then(uid => {
                processor(uid)
            })
            .catch(error => {
                ReactNative.NativeModules.YH_CommonHelper.login()
                    .then(uid => {
                        processor(uid)
                    })
                    .catch(error => {

                    });
            });
    };
}

//关注用户
export function updateAttentionAction(attentionStatus) {
    return (dispatch, getState) => {
        let {app} = getState();
        let processor = (uid)=> {
            dispatch(updateAttentionRequest());
            return new ContentService().updateAttention(uid, attentionStatus)
                .then(json => {
                    dispatch(updateAttentionSuccess(json));
                })
                .catch(error => {
                    dispatch(updateAttentionFailure(error));
                });
        }
        ReactNative.NativeModules.YH_CommonHelper.uid()
            .then(uid => {
                processor(uid)
            })
            .catch(error => {
                ReactNative.NativeModules.YH_CommonHelper.login()
                    .then(uid => {
                        processor(uid)
                    })
                    .catch(error => {
                    });
            });
    };
}



export function clearCommentMessageList() {
    return (dispatch) => {
        dispatch(clearCommentMessageListAction());
    };
}

export function clearOtherMessage() {
    return (dispatch) => {
        dispatch(clearOtherMessageListAction());
    };
}