import { Dispatch } from 'redux' ; import { CommentAction , CommentActionTypes , } from '../../types/redux/comments-reducer-types' ; import { getAllApprovedCommentsFromDB , persistNewCommentToDB , } from '../../frontend-rest-client/rest/comments' ; import { getErrorMessage } from '../../utils/getErrorMessage' ; Action function returns { function } export const loadAllApprovedComments = ( ) => { return async ( dispatch ) : Promise < void > => { dispatch ( { type : CommentActionTypes . LOAD_ALL_COMMENTS } ) ; try { const { data } = await getAllApprovedCommentsFromDB ( ) ; dispatch ( { type : CommentActionTypes . COMMENTS_DID_LOAD , payload : { total : data . total , comments : data . comments } , } ) ; } catch ( error ) { if ( error instanceof Error ) { dispatch ( { type : CommentActionTypes . COMMENTS_LOAD_ERROR , payload : error . message , } ) ; } } } ; } ; Action function returns { function } export const uploadNewComment = ( newComment ) => { return async ( dispatch ) : Promise < void > => { dispatch ( { type : CommentActionTypes . PERSIST_COMMENT } ) ; try { const { data } = await persistNewCommentToDB ( newComment ) ; dispatch ( { type : CommentActionTypes . COMMENT_WAS_PERSISTED , payload : data . success , } ) ; } catch ( error ) { dispatch ( { type : CommentActionTypes . COMMENT_PERSIST_ERROR , payload : getErrorMessage ( error ) , } ) ; } } ; } ;