import { mainActionType , mainRequestActionType } from "../actions" ; import { MainModel } from "../models/MainModel" ; import * as cloneDeep from "lodash.clonedeep" ; import { LeftFactory } from "../components/Left" ; import * as Types from "MyTypes" ; const initialState = { handlers : { } , selectedTab : "tab0" , leftArray : [ ] , tabPrimaryKey : 0 , handlerInfo : { } , activeTab : { } , tabInfo : { } , } ; export const mainReducer = ( state = initialState , action ) => { switch ( action . type ) { case mainActionType . GET_TAB_STATE : { return { ... state , activeTab : { ... state . activeTab , ... action . payload , } , } ; } case mainActionType . ADD_NEW_TAB : { const newLeftArray = cloneDeep ( state . leftArray ) ; const newSelectedTab = "tab" + state . tabPrimaryKey ; const newHandlerInfo = cloneDeep ( state . handlerInfo ) ; newHandlerInfo [ newSelectedTab ] = { serverResponse : [ ] , isStreaming : false , responseMetrics : "" , } ; state . handlers [ newSelectedTab ] = null ; const newTabPrimaryKey = state . tabPrimaryKey + 1 ; const leftEle = LeftFactory ( { tabKey : newSelectedTab , getTabState : action . payload . getTabState , updateTabNames : action . payload . updateTabNames , setGRPCResponse : action . payload . setGRPCResponse , } ) ; newLeftArray . push ( leftEle ) ; const newTabInfo = cloneDeep ( state . tabInfo ) ; newTabInfo [ newSelectedTab ] = { name : "New Connection" , activeResponseTab : "server" } ; return { ... state , leftArray : newLeftArray , tabPrimaryKey : newTabPrimaryKey , selectedTab : newSelectedTab , handlerInfo : newHandlerInfo , tabInfo : newTabInfo , } ; } case mainActionType . REMOVE_TAB : { const newLeftArray = cloneDeep ( state . leftArray ) ; let removeIdx ; let newSelectedTab = state . selectedTab ; newLeftArray . forEach ( ( ele , idx ) => { if ( ele . key === action . payload ) { removeIdx = idx ; } } ) ; if ( state . selectedTab === action . payload ) { if ( state . leftArray . length === 1 ) { newSelectedTab = "" ; } else if ( removeIdx === newLeftArray . length - 1 ) { newSelectedTab = state . leftArray [ removeIdx - 1 ] . key . toString ( ) ; } else { newSelectedTab = state . leftArray [ removeIdx + 1 ] . key . toString ( ) ; } } for ( let i = removeIdx ; i < newLeftArray . length - 1 ; i ++ ) { newLeftArray [ i ] = newLeftArray [ i + 1 ] ; } newLeftArray . pop ( ) ; return { ... state , leftArray : newLeftArray , selectedTab : newSelectedTab , } ; } case mainActionType . SELECT_TAB : { const newSelectedTab = action . payload ; return { ... state , selectedTab : newSelectedTab , } ; } case mainActionType . SELECT_RESPONSE_TAB : { const newTabInfo = cloneDeep ( state . tabInfo ) ; newTabInfo [ action . payload . selectedTab ] . activeResponseTab = action . payload . mode ; return { ... state , tabInfo : newTabInfo , } ; } case mainActionType . UPDATE_TAB_NAMES : { const newTabInfo = cloneDeep ( state . tabInfo ) ; newTabInfo [ action . payload . tabKey ] . name = action . payload . val ; return { ... state , tabInfo : newTabInfo , } ; } case mainRequestActionType . SET_GRPC_RESPONSE : { const newHandlerInfo = cloneDeep ( state . handlerInfo ) ; if ( action . payload . response instanceof Error ) { newHandlerInfo [ action . payload . tabKey ] . serverResponse = [ { type : 'read' , payload : action . payload . response . message } ] ; newHandlerInfo [ action . payload . tabKey ] . responseMetrics = { timeStamp : new Date ( ) . toLocaleTimeString ( "en-US" , { hour12 : false } ) , request : `Error` , } ; newHandlerInfo [ action . payload . tabKey ] . isStreaming = false ; } else { newHandlerInfo [ action . payload . tabKey ] . serverResponse = action . payload . response ; } return { ... state , handlerInfo : newHandlerInfo , } ; } case mainActionType . TOGGLE_STREAM : { const newHandlerInfo = cloneDeep ( state . handlerInfo ) ; newHandlerInfo [ state . selectedTab ] . isStreaming = action . payload ; newHandlerInfo [ state . selectedTab ] . serverResponse = { } ; return { ... state , handlerInfo : newHandlerInfo , } ; } case mainRequestActionType . HANDLE_SEND_MESSAGE : { state . handlers [ state . selectedTab ] . write ( state . activeTab . configArguments . arguments ) ; const newHandlerInfo = cloneDeep ( state . handlerInfo ) ; newHandlerInfo [ state . selectedTab ] . responseMetrics = { timeStamp : new Date ( ) . toLocaleTimeString ( "en-US" , { hour12 : false } ) , request : `${ state . activeTab . selectedRequest }: message sent.` , } ; return { ... state , handlerInfo : newHandlerInfo , } ; } case mainRequestActionType . HANDLE_RECIEVE_MESSAGE : { const newHandlerInfo = cloneDeep ( state . handlerInfo ) ; newHandlerInfo [ state . selectedTab ] . responseMetrics = { timeStamp : new Date ( ) . toLocaleTimeString ( "en-US" , { hour12 : false } ) , request : `${ state . activeTab . selectedRequest }: message recieved from server.` , } ; return { ... state , handlerInfo : newHandlerInfo , } ; } case mainRequestActionType . HANDLE_STOP_STREAM : { const newHandlerInfo = cloneDeep ( state . handlerInfo ) ; if ( state . activeTab . requestConfig . callType === "CLIENT_STREAM" ) { state . handlers [ state . selectedTab ] . end ( ) ; } else { state . handlers [ state . selectedTab ] . cancel ( ) ; } let request ; if ( action . payload === "server_end" ) { request = `${ state . activeTab . selectedRequest }: con. terminated by server.` ; } else { request = `${ state . activeTab . selectedRequest }: con. terminated by client.` ; } newHandlerInfo [ state . selectedTab ] . responseMetrics = { timeStamp : new Date ( ) . toLocaleTimeString ( "en-US" , { hour12 : false } ) , request : request , } ; newHandlerInfo [ state . selectedTab ] . isStreaming = false ; return { ... state , handlerInfo : newHandlerInfo , } ; } default : { return state ; } } } ;