import { Dispatch } from 'redux' ; import * as actions from './actions' ; import { ResultsActions , Results } from './types' ; import service from '../../service' ; export async function getAndSetResultsById ( dispatch , resultsId ) { dispatch ( actions . setLoadingValue ( true ) ) ; const result = await service . getResultsById ( resultsId ) . then ( ( response ) => response . singleResultsById ) . then ( ( results ) => { if ( results ) { dispatch ( actions . setResults ( results ) ) ; return results ; } return undefined ; } ) . catch ( console . error ) ; dispatch ( actions . setLoadingValue ( false ) ) ; return result ; } export async function addToResultsHistoryById ( dispatch , resultsId ) { dispatch ( actions . setLoadingValue ( true ) ) ; await service . getResultsById ( resultsId ) . then ( ( response ) => response . singleResultsById ) . then ( ( results ) => { if ( results ) { dispatch ( actions . addToResultsHistory ( results ) ) ; } else { throw new Error ( 'Results not found' ) ; } } ) . catch ( console . error ) ; dispatch ( actions . setLoadingValue ( false ) ) ; } export function addAndSetResults ( dispatch , results ) { dispatch ( actions . setLoadingValue ( true ) ) ; return service . addResults ( results ) . then ( ( response ) => response . addResults ) . then ( ( addResults ) => addResults . id ) . catch ( console . error ) . finally ( ( ) => dispatch ( actions . setLoadingValue ( false ) ) ) ; }