import { WeatherActionType } from './types' ; import type { ReduxAction } from '../' ; import type { WeatherForecastPayload , IWeatherForecastsState } from './types' ; const initialState = { forecasts : [ ] , isLoading : false , startDateIndex : 5 } ; export const reducer = ( state = initialState , action ) : IWeatherForecastsState => { switch ( action . type ) { case WeatherActionType . REQUEST : { const { startDateIndex = 0 } = action . payload as WeatherForecastPayload ; return { ... state , startDateIndex , isLoading : true } ; } case WeatherActionType . RECEIVE : { const { startDateIndex , forecasts = [ ] } = action . payload as WeatherForecastPayload ; if ( startDateIndex === state . startDateIndex ) { return { forecasts , startDateIndex , isLoading : false } ; } return state ; } default : return state ; } } ;