import { IPageCountByContact , IRootAction , ITask } from '@models' ; import * as types from './types' ; export interface ITasksState { [ taskId ] : ITask ; } export interface ITasksGetByContactState { error : Error ; isFetching : boolean ; lastUpdated : number ; fetchedPageCount : IPageCountByContact ; } const getByContactInitialState = { error : null , isFetching : false , lastUpdated : null , fetchedPageCount : { } , } ; export const tasksReducer = ( state = { } , action ) : ITasksState => { switch ( action . type ) { case types . GET_TASKS_BY_CONTACT_SUCCESS : const tasks = { ... state } ; action . payload . tasks . forEach ( item => { if ( ! tasks [ item . id ] ) { tasks [ item . id ] = item ; } } ) ; return tasks ; } return state ; } ; export const getByContactReducer = ( state = getByContactInitialState , action ) : ITasksGetByContactState => { switch ( action . type ) { case types . GET_TASKS_BY_CONTACT_FETCHED : return { ... state , error : null , isFetching : true , fetchedPageCount : { [ action . payload . contactId ] : ( state . fetchedPageCount [ action . payload . contactId ] || 0 ) + 1 , } , } ; case types . GET_TASKS_BY_CONTACT_SUCCESS : return { ... state , isFetching : false , lastUpdated : + new Date ( ) , } ; case types . GET_TASKS_BY_CONTACT_FAILED : return { ... state , isFetching : false , error : action . payload . error , } ; } return state ; } ;