import ThreadActionCreator from "../models/client/ThreadActionCreator" ; import GetThreadsResponse from "../models/response/GetThreadsResponse.d" ; import { Dispatch } from "redux" ; import fetch from "../shared/fetch" ; import actions from "./common" ; import { getToast as toast } from "../shared/toast" ; import Thread from "../models/Thread" ; export const GET_THREADS_START = "GET_THREADS_START" ; export const GET_THREADS_SUCCESS = "GET_THREADS_SUCCESS" ; export const GET_THREADS_FAILED = "GET_THREADS_FAILED" ; export const ADD_THREAD_START = "ADD_THREAD_START" ; export const ADD_THREAD_SUCCESS = "ADD_THREAD_SUCCESS" ; export const ADD_THREAD_FAILED = "ADD_THREAD_FAILED" ; export const UPDATE_THREAD_START = "UPDATE_THREAD_START" ; export const UPDATE_THREAD_SUCCESS = "UPDATE_THREAD_SUCCESS" ; export const UPDATE_THREAD_FAILED = "UPDATE_THREAD_FAILED" ; export const REMOVE_THREAD_START = "REMOVE_THREAD_START" ; export const REMOVE_THREAD_SUCCESS = "REMOVE_THREAD_SUCCESS" ; export const REMOVE_THREAD_FAILED = "REMOVE_THREAD_FAILED" ; export const RATE_THREAD_SUCCESS = "RATE_THREAD_SUCCESS" ; export const RATE_THREAD_FAILED = "RATE_THREAD_FAILED" ; const threadActionCreator = { getThreads ( pageIndex , pageSize ) { return ( dispatch ) : void => { dispatch ( { type : GET_THREADS_START } ) ; fetch ( `/api/thread?pageIndex=${ pageIndex }&pageSize=${ pageSize }` , undefined , "GET" , true ) . then ( ( json ) => { if ( json && json . data && json . totalCount >= 0 ) { dispatch ( { type : GET_THREADS_SUCCESS , threads : json . data , pageIndex : pageIndex , totalCount : json . totalCount } ) ; } else { return Promise . reject ( { name : "500 Internal Server Error" , message : "" } ) ; } } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( GET_THREADS_FAILED , error ) ) ; } ) ; } ; } , addThread ( title , content , author , mentions ? ) { return ( dispatch ) : void => { dispatch ( { type : ADD_THREAD_START } ) ; fetch ( `/api/thread/add` , { title , content , author , mentions } , "POST" , true ) . then ( ( json ) => { if ( json ) { toast ( ) . success ( "toast.thread.add_successfully" ) ; dispatch ( { type : ADD_THREAD_SUCCESS , thread : json } ) ; } else { return Promise . reject ( { name : "500 Internal Server Error" , message : "Broken data." } ) ; } } ) . catch ( ( error ) => { toast ( ) . error ( "toast.thread.add_failed" ) ; return dispatch ( actions . handleFetchError ( ADD_THREAD_FAILED , error ) ) ; } ) ; } ; } , rateThread ( rating , id , user ) { return ( dispatch ) : void => { fetch ( `/api/thread/rate?id=${ id }&rating=${ rating }` , undefined , "GET" , true ) . then ( ( json ) => { dispatch ( { type : RATE_THREAD_SUCCESS , thread : id , rating : rating , user : user } ) ; } ) . catch ( ( error ) => { dispatch ( actions . handleFetchError ( RATE_THREAD_FAILED , error ) ) ; } ) ; } ; } , removeThread ( id ) { return ( dispatch ) : void => { dispatch ( { type : REMOVE_THREAD_START } ) ; fetch ( `/api/thread/remove/${ id }` , undefined , "GET" , true ) . then ( ( json ) => { toast ( ) . success ( "toast.thread.delete_successfully" ) ; dispatch ( { type : REMOVE_THREAD_SUCCESS , redirectTask : { redirected : false , to : "/thread" } } ) ; } ) . catch ( ( error ) => { toast ( ) . error ( "toast.thread.delete_failed" ) ; return dispatch ( actions . handleFetchError ( REMOVE_THREAD_FAILED , error ) ) ; } ) ; } ; } } ; export default threadActionCreator ;