import { LikeAction , LikesActionTypes , } from '../../types/redux/likes-reducer-types' ; import { Dispatch } from 'redux' ; import { getAllLikesFromDB , persistNewLikeToDB , } from '../../frontend-rest-client/rest/likes' ; import { getErrorMessage } from '../../utils/getErrorMessage' ; Action function returns { function } export const getAllLikes = ( ) => { return async ( dispatch ) : Promise < void > => { dispatch ( { type : LikesActionTypes . LOAD_ALL_LIKES } ) ; try { const { data } = await getAllLikesFromDB ( ) ; dispatch ( { type : LikesActionTypes . LIKES_DID_LOAD , payload : data . totalLikes , } ) ; } catch ( error ) { dispatch ( { type : LikesActionTypes . LIKES_LOAD_ERROR , payload : getErrorMessage ( error ) , } ) ; } } ; } ; Action function returns { function } export const postLike = ( ) => { return async ( dispatch ) : Promise < void > => { dispatch ( { type : LikesActionTypes . PERSIST_LIKE } ) ; try { const { data } = await persistNewLikeToDB ( ) ; dispatch ( { type : LikesActionTypes . LIKE_WAS_PERSISTED , payload : data . success , } ) ; } catch ( error ) { dispatch ( { type : LikesActionTypes . LIKE_PERSIST_ERROR , payload : getErrorMessage ( error ) , } ) ; } } ; } ; Action function returns { function } export const clearLikeState = ( ) => { return async ( dispatch ) : Promise < void > => { dispatch ( { type : LikesActionTypes . CLEAR_STATE } ) ; } ; } ;