import { action } from 'typesafe-actions' ; import { Axios } from '../../utils' ; import { ISubmitEntryParams , SubmitEntryStateTypes } from './submitEntryTypes' ; import { getAuthHeader , wait } from '../../utils' ; import { AsyncAction } from '../storeTypes' ; export const submitEntryRequest = ( ) => action ( SubmitEntryStateTypes . REQUEST ) ; export const submitEntrySuccess = ( ) => action ( SubmitEntryStateTypes . SUCCESS ) ; export const submitEntryFailure = ( payload ) => action ( SubmitEntryStateTypes . FAILURE , payload ) ; export const submitEntryClear = ( ) => action ( SubmitEntryStateTypes . CLEAR ) ; export const submitNewEntry = ( params , isEdit ? ) => async ( dispatch , getState ) => { try { const method = isEdit ? 'put' : 'post' ; dispatch ( submitEntryRequest ( ) ) ; await wait ( 500 ) ; await Axios [ method ] ( '/api/entry' , params , getAuthHeader ( getState ) ) ; dispatch ( submitEntrySuccess ( ) ) ; } catch ( error ) { const { error : errorMessage , reject_reason , status = 'error' , } = error . response . data ; dispatch ( submitEntryFailure ( { error : errorMessage , reject_reason , status } ) ) ; } } ;