Add connection profiles (#63)

* Add connection setup

* Refactor

* Fix lifecycle
This commit is contained in:
Thomas Nordquist
2019-02-16 05:36:02 -08:00
committed by GitHub
parent f316d5699d
commit 93ea829987
19 changed files with 1225 additions and 418 deletions

View File

@@ -0,0 +1,174 @@
import { Action } from 'redux'
import { ConnectionOptions } from '../model/ConnectionOptions'
import { createReducer } from './lib'
export interface ConnectionManagerState {
connections: {[s:string]: ConnectionOptions},
selected?: string
showAdvancedSettings: boolean
}
const initialState: ConnectionManagerState = {
connections: {},
selected: undefined,
showAdvancedSettings: false,
}
export type Action = SetConnections | SelectConnection | UpdateConnection | AddConnection | DeleteConnection | ToggleAdvancedSettings | DeleteSubscription | AddSubscription
export enum ActionTypes {
CONNECTION_MANAGER_SET_CONNECTIONS = 'CONNECTION_MANAGER_SET_CONNECTIONS',
CONNECTION_MANAGER_SELECT_CONNECTION = 'CONNECTION_MANAGER_SELECT_CONNECTION',
CONNECTION_MANAGER_UPDATE_CONNECTION = 'CONNECTION_MANAGER_UPDATE_CONNECTION',
CONNECTION_MANAGER_ADD_CONNECTION = 'CONNECTION_MANAGER_ADD_CONNECTION',
CONNECTION_MANAGER_DELETE_CONNECTION = 'CONNECTION_MANAGER_DELETE_CONNECTION',
CONNECTION_MANAGER_TOGGLE_ADVANCED_SETTINGS = 'CONNECTION_MANAGER_TOGGLE_ADVANCED_SETTINGS',
CONNECTION_MANAGER_ADD_SUBSCRIPTION = 'CONNECTION_MANAGER_ADD_SUBSCRIPTION',
CONNECTION_MANAGER_DELETE_SUBSCRIPTION = 'CONNECTION_MANAGER_DELETE_SUBSCRIPTION',
}
export interface SetConnections {
type: ActionTypes.CONNECTION_MANAGER_SET_CONNECTIONS
connections: {[s:string]: ConnectionOptions}
}
export interface SelectConnection {
type: ActionTypes.CONNECTION_MANAGER_SELECT_CONNECTION
selected: string
}
export interface AddSubscription {
type: ActionTypes.CONNECTION_MANAGER_ADD_SUBSCRIPTION
subscription: string
connectionId: string
}
export interface DeleteSubscription {
type: ActionTypes.CONNECTION_MANAGER_DELETE_SUBSCRIPTION
subscription: string
connectionId: string
}
export interface UpdateConnection {
type: ActionTypes.CONNECTION_MANAGER_UPDATE_CONNECTION
connectionId: string
changeSet: any
}
export interface AddConnection {
type: ActionTypes.CONNECTION_MANAGER_ADD_CONNECTION
connection: ConnectionOptions
}
export interface DeleteConnection {
type: ActionTypes.CONNECTION_MANAGER_DELETE_CONNECTION
connectionId: string
}
export interface ToggleAdvancedSettings {
type: ActionTypes.CONNECTION_MANAGER_TOGGLE_ADVANCED_SETTINGS
}
export const connectionManagerReducer = createReducer(initialState, {
CONNECTION_MANAGER_SET_CONNECTIONS: setConnections,
CONNECTION_MANAGER_SELECT_CONNECTION: selectConnection,
CONNECTION_MANAGER_UPDATE_CONNECTION: updateConnection,
CONNECTION_MANAGER_ADD_CONNECTION: addConnection,
CONNECTION_MANAGER_DELETE_CONNECTION: deleteConnection,
CONNECTION_MANAGER_TOGGLE_ADVANCED_SETTINGS: toggleAdvancedSettings,
CONNECTION_MANAGER_DELETE_SUBSCRIPTION: deleteSubscription,
CONNECTION_MANAGER_ADD_SUBSCRIPTION: addSubscription,
})
function setConnections(state: ConnectionManagerState, action: SetConnections): ConnectionManagerState {
return {
...state,
connections: action.connections,
}
}
function selectConnection(state: ConnectionManagerState, action: SelectConnection): ConnectionManagerState {
return {
...state,
selected: action.selected,
}
}
function toggleAdvancedSettings(state: ConnectionManagerState, action: ToggleAdvancedSettings): ConnectionManagerState {
return {
...state,
showAdvancedSettings: !state.showAdvancedSettings,
}
}
function addConnection(state: ConnectionManagerState, action: AddConnection): ConnectionManagerState {
return {
...state,
connections: {
...state.connections,
[action.connection.id]: action.connection,
},
}
}
function addSubscription(state: ConnectionManagerState, action: AddSubscription): ConnectionManagerState {
const connection = state.connections[action.connectionId]
const alreadyExists = connection.subscriptions.indexOf(action.subscription) !== -1
if (alreadyExists) {
return state
}
const newSubscriptions = connection.subscriptions.slice()
newSubscriptions.push(action.subscription)
return {
...state,
connections: {
...state.connections,
[action.connectionId]: {
...connection,
subscriptions: newSubscriptions,
},
},
}
}
function deleteSubscription(state: ConnectionManagerState, action: AddSubscription): ConnectionManagerState {
const connection = state.connections[action.connectionId]
const newSubscriptions = connection.subscriptions.filter(s => s !== action.subscription)
return {
...state,
connections: {
...state.connections,
[action.connectionId]: {
...connection,
subscriptions: newSubscriptions,
},
},
}
}
function deleteConnection(state: ConnectionManagerState, action: DeleteConnection): ConnectionManagerState {
const connections = { ...state.connections }
delete connections[action.connectionId]
return {
...state,
connections,
}
}
function updateConnection(state: ConnectionManagerState, action: UpdateConnection): ConnectionManagerState {
let connection = state.connections[action.connectionId]
connection = {
...connection,
...action.changeSet,
}
return {
...state,
connections: {
...state.connections,
[action.connectionId]: connection,
},
}
}

View File

@@ -5,6 +5,7 @@ import { PublishState, publishReducer } from './Publish'
import { ConnectionState, connectionReducer } from './Connection'
import { SettingsState, settingsReducer } from './Settings'
import { TreeState, treeReducer } from './Tree'
import { ConnectionManagerState, connectionManagerReducer } from './ConnectionManager'
export enum ActionTypes {
showUpdateNotification = 'SHOW_UPDATE_NOTIFICATION',
@@ -23,6 +24,7 @@ export interface AppState {
settings: SettingsState,
publish: PublishState
connection: ConnectionState
connectionManager: ConnectionManagerState
}
export interface TooBigOfState {
@@ -67,6 +69,7 @@ const reducer = combineReducers({
connection: connectionReducer,
settings: settingsReducer,
tree: treeReducer,
connectionManager: connectionManagerReducer,
})
export default reducer