import { combineReducers } from 'redux' ; import { createSelector } from 'reselect' ; import { ActionType , State } from '../ducks' ; interface TrackedQuery { contextJSON : string ; id : string ; name : string ; queryJSON : string ; variablesJSON : string ; } const TRACKED_QUERIES_ADD = 'TRACKED_QUERIES_ADD' ; const TRACKED_QUERIES_REMOVE = 'TRACKED_QUERIES_REMOVE' ; interface TrackedQueriesAdd { payload : TrackedQuery ; type : typeof TRACKED_QUERIES_ADD ; } interface TrackedQueriesRemove { payload : string ; type : typeof TRACKED_QUERIES_REMOVE ; } export type TrackedQueriesActionType = TrackedQueriesAdd | TrackedQueriesRemove ; export const trackedQueriesAdd = ( trackedQuery ) : TrackedQueriesAdd => ( { payload : trackedQuery , type : TRACKED_QUERIES_ADD , } ) ; export const trackedQueriesRemove = ( id ) : TrackedQueriesRemove => ( { payload : id , type : TRACKED_QUERIES_REMOVE , } ) ; interface TrackedQueriesById { [ key ] : TrackedQuery ; } type TrackedQueriesIds = string [ ] ; export interface TrackedQueriesState { byId : TrackedQueriesById ; ids : TrackedQueriesIds ; } const byId = ( state = { } , action ) => { let newState ; switch ( action . type ) { case TRACKED_QUERIES_ADD : newState = { ... state , [ action . payload . id ] : action . payload , } ; return newState ; case TRACKED_QUERIES_REMOVE : newState = { ... state , } ; delete newState [ action . payload ] ; return newState ; default : return state ; } } ; const ids = ( state = [ ] , action ) => { let newState ; switch ( action . type ) { case TRACKED_QUERIES_ADD : newState = [ ... state , action . payload . id ] ; return newState ; case TRACKED_QUERIES_REMOVE : newState = state . filter ( id => id !== action . payload ) ; return newState ; default : return state ; } } ; export default combineReducers ( { byId , ids , } ) ; const getTrackedQueriesIds = ( state ) => state . trackedQueries . ids ; const getTrackedQueriesById = ( state ) => state . trackedQueries . byId ; export const getTrackedQueries = createSelector ( getTrackedQueriesIds , getTrackedQueriesById , ( pIds , pById ) => pIds . map ( o => pById [ o ] ) ) ;