import { Level } from '@logo-quiz/models' ; import { LevelsActionTypes , REQUEST_LEVELS , REQUEST_LEVELS_SUCCESS , FLUSH_LEVELS , REQUEST_LEVELS_ERROR } from './types' ; import { Dispatch } from 'redux' ; import { fetchLevels as apiFetchLevels } from '../../shared/services' ; export function requestLevels ( ) { return { type : REQUEST_LEVELS } ; } export function requestLevelsSuccess ( levels ) { return { type : REQUEST_LEVELS_SUCCESS , levels } ; } export function requestLevelsError ( error ) { return { type : REQUEST_LEVELS_ERROR , error } ; } export function flushLevels ( ) { return { type : FLUSH_LEVELS } ; } export function fetchLevels ( ) { return function ( dispatch ) { dispatch ( requestLevels ( ) ) ; return apiFetchLevels ( ) . then ( levels => { dispatch ( requestLevelsSuccess ( levels ) ) ; } ) . catch ( error => { dispatch ( requestLevelsError ( error ) ) ; } ) ; } ; }