import { combineReducers } from "redux" ; import { getType , StateType } from "typesafe-actions" ; import { Actions , actions } from "./actions" ; import { GraphHistory , History , Mocks , Sessions , SmockerError } from "./types" ; const loadingSessions = ( state = false , action ) => { const { fetchSessions , newSession , updateSession , uploadSessions , reset , } = actions ; switch ( action . type ) { case getType ( fetchSessions . request ) : case getType ( newSession . request ) : case getType ( updateSession . request ) : case getType ( uploadSessions . request ) : case getType ( reset . request ) : { return true ; } case getType ( fetchSessions . success ) : case getType ( fetchSessions . failure ) : case getType ( newSession . success ) : case getType ( newSession . failure ) : case getType ( updateSession . success ) : case getType ( updateSession . failure ) : case getType ( uploadSessions . success ) : case getType ( uploadSessions . failure ) : case getType ( reset . success ) : case getType ( reset . failure ) : { return false ; } default : return state ; } } ; const uploadingSessions = ( state = false , action ) => { const { uploadSessions } = actions ; switch ( action . type ) { case getType ( uploadSessions . request ) : { return true ; } case getType ( uploadSessions . success ) : case getType ( uploadSessions . failure ) : { return false ; } default : return state ; } } ; const sessionList = ( state = [ ] , action ) => { const { fetchSessions , newSession , updateSession , uploadSessions , reset , } = actions ; switch ( action . type ) { case getType ( reset . success ) : { return [ ] ; } case getType ( fetchSessions . success ) : case getType ( uploadSessions . success ) : { return [ ... action . payload ] ; } case getType ( newSession . success ) : { return [ ... state , action . payload ] ; } case getType ( updateSession . success ) : { return state . map ( ( session ) => session . id === action . payload . id ? action . payload : session ) ; } default : return state ; } } ; const sessionError = ( state = null , action ) => { const { fetchSessions , newSession , updateSession , uploadSessions , reset , } = actions ; switch ( action . type ) { case getType ( fetchSessions . failure ) : case getType ( newSession . failure ) : case getType ( updateSession . failure ) : case getType ( uploadSessions . failure ) : case getType ( reset . failure ) : { return action . payload ; } case getType ( fetchSessions . success ) : case getType ( newSession . success ) : case getType ( updateSession . success ) : case getType ( uploadSessions . success ) : case getType ( reset . success ) : { return null ; } default : return state ; } } ; const selectedSession = ( state = "" , action ) => { const { fetchSessions , newSession , selectSession , updateSession , uploadSessions , reset , } = actions ; switch ( action . type ) { case getType ( selectSession ) : { return action . payload ; } case getType ( reset . success ) : { return "" ; } case getType ( newSession . success ) : case getType ( updateSession . success ) : { return action . payload . id ; } case getType ( fetchSessions . success ) : case getType ( uploadSessions . success ) : { if ( ! state ) { return "" ; } return action . payload . filter ( ( session ) => session . id === state ) . length === 0 ? "" : state ; } default : return state ; } } ; const sessions = combineReducers ( { loading : loadingSessions , uploading : uploadingSessions , list : sessionList , error : sessionError , selected : selectedSession , } ) ; const loadingHistory = ( state = false , action ) => { const { fetchHistory , summarizeHistory , reset } = actions ; switch ( action . type ) { case getType ( fetchHistory . request ) : case getType ( summarizeHistory . request ) : case getType ( reset . request ) : { return true ; } case getType ( fetchHistory . success ) : case getType ( fetchHistory . failure ) : case getType ( summarizeHistory . success ) : case getType ( summarizeHistory . failure ) : case getType ( reset . success ) : case getType ( reset . failure ) : { return false ; } default : return state ; } } ; const entryList = ( state = [ ] , action ) => { const { fetchHistory , reset } = actions ; switch ( action . type ) { case getType ( reset . success ) : { return [ ] ; } case getType ( fetchHistory . success ) : { return action . payload ; } default : return state ; } } ; const entryError = ( state = null , action ) => { const { fetchHistory , reset } = actions ; switch ( action . type ) { case getType ( fetchHistory . failure ) : case getType ( reset . failure ) : { return action . payload ; } case getType ( fetchHistory . success ) : case getType ( reset . success ) : { return null ; } default : return state ; } } ; const historyGraph = ( state = [ ] , action ) => { const { summarizeHistory , reset } = actions ; switch ( action . type ) { case getType ( summarizeHistory . success ) : { return action . payload ; } case getType ( summarizeHistory . failure ) : case getType ( reset . success ) : { return [ ] ; } default : return state ; } } ; const history = combineReducers ( { loading : loadingHistory , list : entryList , error : entryError , graph : historyGraph , } ) ; const loadingMocks = ( state = false , action ) => { const { fetchMocks , addMocks , lockMocks , unlockMocks , reset } = actions ; switch ( action . type ) { case getType ( fetchMocks . request ) : case getType ( addMocks . request ) : case getType ( lockMocks . request ) : case getType ( unlockMocks . request ) : case getType ( reset . request ) : { return true ; } case getType ( fetchMocks . success ) : case getType ( fetchMocks . failure ) : case getType ( addMocks . success ) : case getType ( addMocks . failure ) : case getType ( lockMocks . success ) : case getType ( lockMocks . failure ) : case getType ( unlockMocks . success ) : case getType ( unlockMocks . failure ) : case getType ( reset . success ) : case getType ( reset . failure ) : { return false ; } default : return state ; } } ; const mockList = ( state = [ ] , action ) => { const { fetchMocks , lockMocks , unlockMocks , reset } = actions ; switch ( action . type ) { case getType ( reset . success ) : { return [ ] ; } case getType ( fetchMocks . success ) : { return action . payload ; } case getType ( lockMocks . success ) : case getType ( unlockMocks . success ) : { return state . map ( ( mock ) => { for ( const modifiedMock of action . payload ) { if ( mock . state . id === modifiedMock . state . id ) { return modifiedMock ; } } return mock ; } ) ; } default : return state ; } } ; const mockEditor = ( state = [ false , "" ] , action ) => { const { openMockEditor } = actions ; switch ( action . type ) { case getType ( openMockEditor ) : return [ action . payload [ 0 ] , action . payload [ 1 ] ] ; default : return state ; } } ; const mocksError = ( state = null , action ) => { const { fetchMocks , addMocks , lockMocks , unlockMocks , reset } = actions ; switch ( action . type ) { case getType ( fetchMocks . failure ) : case getType ( addMocks . failure ) : case getType ( lockMocks . failure ) : case getType ( unlockMocks . failure ) : case getType ( reset . failure ) : { return action . payload ; } case getType ( fetchMocks . success ) : case getType ( addMocks . success ) : case getType ( lockMocks . success ) : case getType ( unlockMocks . success ) : case getType ( reset . success ) : { return null ; } default : return state ; } } ; const mocks = combineReducers ( { loading : loadingMocks , list : mockList , editor : mockEditor , error : mocksError , } ) ; const reducers = combineReducers ( { sessions , history , mocks , } ) ; export type AppState = StateType < typeof reducers > ; export default reducers ;