import { call , put } from 'redux-saga/effects' ; import { ReturnHitRequest , ReturnHitFailure , returnHitFailure , ReturnHitSuccess , returnHitSuccess } from '../actions/return' ; import { sendReturnHitRequest } from '../api/returnHit' ; import { generateReturnToast , showWaitingToast , updateTopRightToaster } from '../utils/toaster' ; import { getHitAuthToken } from '../api/getHitAuthToken' ; export function * returnHit ( action ) { const { title , hitId } = action . queueItem ; const toasterKey = showWaitingToast ( `Returning HIT...` ) ; try { const authToken = yield call ( getHitAuthToken , action . queueItem ) ; if ( ! authToken ) { updateTopRightToaster ( toasterKey , generateReturnToast ( false , title ) ) ; return yield put < ReturnHitFailure > ( returnHitFailure ( ) ) ; } const successful = yield call ( sendReturnHitRequest , action . queueItem , authToken ) ; updateTopRightToaster ( toasterKey , generateReturnToast ( successful , title ) ) ; return successful ? yield put < ReturnHitSuccess > ( returnHitSuccess ( hitId ) ) : yield put < ReturnHitFailure > ( returnHitFailure ( ) ) ; } catch ( e ) { console . warn ( e ) ; updateTopRightToaster ( toasterKey , generateReturnToast ( false , title ) ) ; yield put < ReturnHitFailure > ( returnHitFailure ( ) ) ; } }