import AppClient from '../httpClients/appClient' ; import { ThunkAction , ThunkDispatch } from 'redux-thunk' ; import { Action } from 'redux' ; import { ErrorRes , BumpReq , BumpRes } from '../types/appApiTypes' ; import { AppResponseError } from '../errors/AppResponseError' ; import { AppState } from '../index' ; import { SnackBarAction } from './actionTypes' ; export const POST_BUMP_START = 'POST_BUMP_START' ; export const POST_BUMP_SUCCESS = 'POST_BUMP_SUCCESS' ; export const POST_BUMP_FAIL = 'POST_BUMP_FAIL' ; export type PostBumpActions = PostBumpStartAction | PostBumpSuccessAction | PostBumpFailAction ; type ThunkResult < R > = ThunkAction < R , AppState , undefined , PostBumpActions > ; export function postBumpAction ( itemId ) { return ( dispatch ) => { Promise . resolve ( ) . then ( ( ) => { dispatch ( postBumpStartAction ( ) ) ; } ) . then ( ( ) => { return AppClient . post ( '/bump' , { item_id : itemId } as BumpReq ) ; } ) . then ( async ( response ) => { if ( response . status !== 200 ) { const errRes = await response . json ( ) ; throw new AppResponseError ( errRes . error , response ) ; } return await response . json ( ) ; } ) . then ( ( body ) => { dispatch ( postBumpSuccessAction ( ) ) ; } ) . catch ( ( err ) => { dispatch ( postBumpFailAction ( err . message ) ) ; } ) ; } ; } export interface PostBumpStartAction extends Action < typeof POST_BUMP_START > { } export function postBumpStartAction ( ) { return { type : POST_BUMP_START , } ; } export interface PostBumpSuccessAction extends SnackBarAction < typeof POST_BUMP_SUCCESS > { } export function postBumpSuccessAction ( ) { return { type : POST_BUMP_SUCCESS , snackBarMessage : 'BUMPに成功しました' , variant : 'success' , } ; } export interface PostBumpFailAction extends SnackBarAction < typeof POST_BUMP_FAIL > { } export function postBumpFailAction ( error ) { return { type : POST_BUMP_FAIL , snackBarMessage : error , variant : 'error' , } ; }