import { fetch , addTask } from 'domain-task' ; import { Action , Reducer , ActionCreator } from 'redux' ; import { AppThunkAction } from './' ; import { adalRequest } from '../adal/AuthenticationContext' ; import axios from 'axios' ; export interface WeatherForecastsState { isLoading : boolean ; startDateIndex : number ; forecasts : WeatherForecast [ ] ; } export interface WeatherForecast { dateFormatted : string ; temperatureC : number ; temperatureF : number ; summary : string ; } interface RequestWeatherForecastsAction { type : 'REQUEST_WEATHER_FORECASTS' ; startDateIndex : number ; } interface ReceiveWeatherForecastsAction { type : 'RECEIVE_WEATHER_FORECASTS' ; startDateIndex : number ; forecasts : WeatherForecast [ ] ; } type KnownAction = RequestWeatherForecastsAction | ReceiveWeatherForecastsAction ; export const actionCreators = { requestWeatherForecasts : ( startDateIndex ) : AppThunkAction < KnownAction > => ( dispatch , getState ) => { if ( startDateIndex !== getState ( ) . weatherForecasts . startDateIndex ) { let fetchTask = axios . get ( `/api/SampleData/WeatherForecasts?startDateIndex=${ startDateIndex }` ) . then ( response => response . data as Promise < WeatherForecast [ ] > ) . then ( data => { dispatch ( { type : 'RECEIVE_WEATHER_FORECASTS' , startDateIndex : startDateIndex , forecasts : data } ) ; } ) ; dispatch ( { type : 'REQUEST_WEATHER_FORECASTS' , startDateIndex : startDateIndex } ) ; } } } ; const unloadedState = { startDateIndex : null , forecasts : [ ] , isLoading : false } ; export const reducer = ( state , action ) => { switch ( action . type ) { case 'REQUEST_WEATHER_FORECASTS' : return { startDateIndex : action . startDateIndex , forecasts : state . forecasts , isLoading : true } ; case 'RECEIVE_WEATHER_FORECASTS' : if ( action . startDateIndex === state . startDateIndex ) { return { startDateIndex : action . startDateIndex , forecasts : action . forecasts , isLoading : false } ; } break ; default : const exhaustiveCheck = action ; } return state || unloadedState ; } ;