import CommentActionCreator from "../models/client/CommentActionCreator" ; import PostType from "../models/PostType" ; import GetCommentsResponse from "../models/response/GetCommentsResponse.d" ; import { Dispatch } from "redux" ; import fetch from "../shared/fetch" ; import actions from "./common" ; import { getToast as toast } from "../shared/toast" ; import Comment from "../models/Comment.d" ; export const LOAD_COMMENTS_START = "LOAD_COMMENTS_START" ; export const LOAD_COMMENTS_SUCCESS = "LOAD_COMMENTS_SUCCESS" ; export const LOAD_COMMENTS_FAILED = "LOAD_COMMENTS_FAILED" ; export const ADD_COMMENT_START = "ADD_COMMENT_START" ; export const ADD_COMMENT_SUCCESS = "ADD_COMMENT_SUCCESS" ; export const ADD_COMMENT_FAILED = "ADD_COMMENT_FAILED" ; export const UPDATE_COMMENT_START = "UPDATE_COMMENT_START" ; export const UPDATE_COMMENT_SUCCESS = "UPDATE_COMMENT_SUCCESS" ; export const UPDATE_COMMENT_FAILED = "UPDATE_COMMENT_FAILED" ; export const REMOVE_COMMENT_START = "REMOVE_COMMENT_START" ; export const REMOVE_COMMENT_SUCCESS = "REMOVE_COMMENT_SUCCESS" ; export const REMOVE_COMMENT_FAILED = "REMOVE_COMMENT_FAILED" ; export const RATE_COMMENT_SUCCESS = "RATE_COMMENT_SUCCESS" ; export const RATE_COMMENT_FAILED = "RATE_COMMENT_FAILED" ; const commentActionCreator = { getComments ( targetType , targetId ) { return ( dispatch ) : void => { dispatch ( { type : LOAD_COMMENTS_START } ) ; fetch ( `/api/comment?targetType=${ targetType }&targetId=${ targetId }` , undefined , "GET" ) . then ( ( json ) => { if ( json && json . data ) { dispatch ( { type : LOAD_COMMENTS_SUCCESS , comments : json . data } ) ; } else { return Promise . reject ( { name : "500 Internal Server Error" , message : "" } ) ; } } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( LOAD_COMMENTS_FAILED , error ) ) ; } ) ; } ; } , addComment ( targetType , targetId , parent , content , mentions ? ) { return ( dispatch ) : void => { dispatch ( { type : ADD_COMMENT_START } ) ; fetch ( `/api/comment/add?targetType=${ targetType }&targetId=${ targetId }${ parent ? "&parent=" + parent : "" }` , { content , mentions } , "POST" , true ) . then ( ( added ) => { if ( added ) { toast ( ) . success ( "toast.comment.add_successfully" ) ; dispatch ( { type : ADD_COMMENT_SUCCESS , targetType : targetType , comment : added } ) ; } else { return Promise . reject ( { name : "500 Internal Server Error" , message : "" } ) ; } } ) . catch ( ( error ) => { toast ( ) . error ( "toast.comment.add_failed" ) ; return dispatch ( actions . handleFetchError ( ADD_COMMENT_FAILED , error ) ) ; } ) ; } ; } , rateComment ( rating , id , user ) { return ( dispatch ) : void => { fetch ( `/api/comment/rate?id=${ id }&rating=${ rating }` , undefined , "GET" , true ) . then ( ( json ) => { dispatch ( { type : RATE_COMMENT_SUCCESS , comment : id , rating : rating , user : user } ) ; } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( RATE_COMMENT_FAILED , error ) ) ; } ) ; } ; } , removeComment ( targetType , id ) { return ( dispatch ) : void => { dispatch ( { type : REMOVE_COMMENT_START } ) ; fetch ( `/api/comment/remove/${ id }` , undefined , "GET" , true ) . then ( ( json ) => { toast ( ) . success ( "toast.comment.delete_successfully" ) ; dispatch ( { type : REMOVE_COMMENT_SUCCESS , targetType : targetType , id : id } ) ; } ) . catch ( ( error ) => { toast ( ) . error ( "toast.comment.delete_failed" ) ; return dispatch ( actions . handleFetchError ( REMOVE_COMMENT_FAILED , error ) ) ; } ) ; } ; } } ; export default commentActionCreator ;