import { FLUSH_LOGO , GUESS_LETTER , QuizLetter , LogoActionTypes , REMOVE_LETTER_FROM_GUESS , REQUEST_LOGO , REQUEST_LOGO_SUCCESS , VERIFY_LOGO , VERIFY_LOGO_SUCCESS } from './types' ; import { Logo , LogoVerifyResponse } from '@logo-quiz/models' ; import { Dispatch } from 'redux' ; import { validateLogo as apiValidateLogo } from '../../shared/services' ; import { fetchLogo as apiFetchLogo } from '../../shared/services' ; export function guessLetter ( letter ) { return { type : GUESS_LETTER , letter } ; } export function removeLetterFromGuess ( letter ) { return { type : REMOVE_LETTER_FROM_GUESS , letter } ; } export function flushLogo ( ) { return { type : FLUSH_LOGO } ; } export function requestLogo ( id ) { return { type : REQUEST_LOGO , id } ; } export function requestLogoSuccess ( logo ) { return { type : REQUEST_LOGO_SUCCESS , logo } ; } export function requestLogoError ( error ) { return { type : 'REQUEST_LOGO_ERROR' , error } ; } export function verifyLogo ( id , guess ) { return { type : VERIFY_LOGO , id , guess } ; } export function verifyLogoSuccess ( data ) { return { type : VERIFY_LOGO_SUCCESS , data } ; } export function fetchLogo ( id ) { return function ( dispatch ) { dispatch ( requestLogo ( id ) ) ; return apiFetchLogo ( id ) . then ( logo => { dispatch ( requestLogoSuccess ( logo ) ) ; } ) . catch ( error => { dispatch ( requestLogoError ( error ) ) ; } ) ; } ; } export function validateLogo ( id , guess ) { return function ( dispatch ) { dispatch ( verifyLogo ( id , guess ) ) ; return apiValidateLogo ( id , guess ) . then ( status => { dispatch ( verifyLogoSuccess ( status ) ) ; } ) . catch ( error => { } ) ; } ; }