import AppClient from '../httpClients/appClient' ; import { ThunkAction , ThunkDispatch } from 'redux-thunk' ; import { Action } from 'redux' ; import { ErrorRes , ShipReq , ShipRes } from '../types/appApiTypes' ; import { fetchItemAction } from './fetchItemAction' ; import { AppResponseError } from '../errors/AppResponseError' ; import { AppState } from '../index' ; import { SnackBarAction } from './actionTypes' ; export const POST_SHIPPED_START = 'POST_SHIPPED_START' ; export const POST_SHIPPED_SUCCESS = 'POST_SHIPPED_SUCCESS' ; export const POST_SHIPPED_FAIL = 'POST_SHIPPED_FAIL' ; export type PostShippedActions = PostShippedStartAction | PostShippedSuccessAction | PostShippedFailAction ; type ThunkResult < R > = ThunkAction < R , AppState , undefined , PostShippedActions > ; export function postShippedAction ( itemId ) { return ( dispatch ) => { Promise . resolve ( ) . then ( ( ) => { dispatch ( postShippedStartAction ( ) ) ; } ) . then ( ( ) => { return AppClient . post ( '/ship' , { item_id : itemId , } as ShipReq ) ; } ) . 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 ( postShippedSuccessAction ( ) ) ; } ) . then ( ( ) => { dispatch ( fetchItemAction ( itemId . toString ( ) ) ) ; } ) . catch ( ( err ) => { dispatch ( postShippedFailAction ( err . message ) ) ; } ) ; } ; } export interface PostShippedStartAction extends Action < typeof POST_SHIPPED_START > { } export function postShippedStartAction ( ) { return { type : POST_SHIPPED_START , } ; } export interface PostShippedSuccessAction extends Action < typeof POST_SHIPPED_SUCCESS > { } export function postShippedSuccessAction ( ) { return { type : POST_SHIPPED_SUCCESS , } ; } export interface PostShippedFailAction extends SnackBarAction < typeof POST_SHIPPED_FAIL > { } export function postShippedFailAction ( error ) { return { type : POST_SHIPPED_FAIL , snackBarMessage : error , variant : 'error' , } ; }