{"size": 1810, "ext": "js", "max_stars_repo_path": "src/actions/comment.js", "max_stars_repo_name": "HUJINLIANG/HJL-Blog-react", "max_stars_repo_stars_event_min_datetime": "2016-09-21T07:47:13.000Z", "max_stars_repo_stars_event_max_datetime": "2016-09-21T07:47:13.000Z", "max_issues_repo_path": "src/actions/comment.js", "max_issues_repo_name": "HUJINLIANG/HJL-Blog-react", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/actions/comment.js", "max_forks_repo_name": "HUJINLIANG/HJL-Blog-react", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.28125, "max_line_length": 80, "alphanum_fraction": 0.5447513812}
/**
 * Created by jialao on 2016/7/25.
 */
import * as types from './types'
import api from '../api'
import {showMsg} from './other'
import {commentsNumberAdd} from './article'

export const getCommentList = (id) => {
    return {
        type:types.COMMENT_LIST,
        promise:api.getCommentList(id)
    }
};

export function addComment(comment){
    return (dispatch,getState) => {
        return api.addNewComment(comment)
            .then(response => ({json:response.data,status:response.statusText}))
            .then(({json,status}) => {
                if(status !== 'OK'){
                    return dispatch(showMsg(json.data.error_msg) || '添加评论失败')
                }
               
                dispatch(showMsg('添加评论成功','success'));
                dispatch(commentsNumberAdd());
                return dispatch(receiveAddComment(json.data))
            }).catch(e => {
                return dispatch(showMsg(e.data.error_msg||'添加评论失败'))
            })

    }
}

function receiveAddComment(comment){
    return {
        type:types.ADD_COMMENT_SUCCESS,
        comment:comment
    }
}

export function addReply(cid,reply){
    return (dispatch,getState) => {
        return api.addNewReply(cid,reply)
            .then(response => ({json:response.data,status:response.statusText}))
            .then(({json,status}) => {
                if(status !== 'OK'){
                    return dispatch(showMsg('回复失败'))
                }
                dispatch(showMsg('添加回复成功','success'));
                return dispatch(receiveAddReply(cid,json.data))
            }).catch(err => {
                dispatch(err.data.error_msg||'回复失败')
            })
    }
}

function receiveAddReply(cid,replys){
    return {
        type:types.ADD_REPLY_SUCCESS,
        cid:cid,
        replys:replys
    }
}