import { ConnectionOptions } from '../model/ConnectionOptions' ; import { createReducer } from './lib' ; import { Subscription } from '../../../backend/src/DataSource/MqttSource' ; export interface ConnectionManagerState { connections : { [ s ] : ConnectionOptions ; } ; selected ? : string ; showAdvancedSettings : boolean ; showCertificateSettings : boolean ; } const initialState = { connections : { } , selected : undefined , showAdvancedSettings : false , showCertificateSettings : false , } ; export type Action = SetConnections | SelectConnection | UpdateConnection | AddConnection | DeleteConnection | ToggleAdvancedSettings | ToggleCertificateSettings | 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_TOGGLE_CERTIFICATE_SETTINGS = 'CONNECTION_MANAGER_TOGGLE_CERTIFICATE_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 ] : ConnectionOptions ; } ; } export interface SelectConnection { type : ActionTypes . CONNECTION_MANAGER_SELECT_CONNECTION ; selected : string ; } export interface AddSubscription { type : ActionTypes . CONNECTION_MANAGER_ADD_SUBSCRIPTION ; subscription : Subscription ; connectionId : string ; } export interface DeleteSubscription { type : ActionTypes . CONNECTION_MANAGER_DELETE_SUBSCRIPTION ; subscription : Subscription ; 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 interface ToggleCertificateSettings { type : ActionTypes . CONNECTION_MANAGER_TOGGLE_CERTIFICATE_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_TOGGLE_CERTIFICATE_SETTINGS : toggleCertificateSettings , CONNECTION_MANAGER_DELETE_SUBSCRIPTION : deleteSubscription , CONNECTION_MANAGER_ADD_SUBSCRIPTION : addSubscription , } ) ; function setConnections ( state , action ) { return { ... state , connections : action . connections , } ; } function selectConnection ( state , action ) { return { ... state , selected : action . selected , } ; } function toggleAdvancedSettings ( state , action ) { return { ... state , showAdvancedSettings : ! state . showAdvancedSettings , } ; } function toggleCertificateSettings ( state , action ) { return { ... state , showCertificateSettings : ! state . showCertificateSettings , } ; } function addConnection ( state , action ) { return { ... state , connections : { ... state . connections , [ action . connection . id ] : action . connection , } , } ; } function addSubscription ( state , action ) { 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 , action ) { function subscriptionsEqual ( v1 , v2 ) { return v1 . topic == v2 . topic && v1 . qos == v2 . qos ; } const connection = state . connections [ action . connectionId ] ; const newSubscriptions = connection . subscriptions . filter ( s => ! subscriptionsEqual ( s , action . subscription ) ) ; return { ... state , connections : { ... state . connections , [ action . connectionId ] : { ... connection , subscriptions : newSubscriptions , } , } , } ; } function deleteConnection ( state , action ) { const connections = { ... state . connections } ; delete connections [ action . connectionId ] ; return { ... state , connections , } ; } function updateConnection ( state , action ) { let connection = state . connections [ action . connectionId ] ; let changeSet = action . changeSet ; if ( changeSet . username !== undefined ) { changeSet = { changeSet , username : changeSet . username === '' ? undefined : changeSet . username , } ; } if ( changeSet . password !== undefined ) { changeSet = { changeSet , password : changeSet . password === '' ? undefined : changeSet . password , } ; } connection = { ... connection , ... changeSet , } ; return { ... state , connections : { ... state . connections , [ action . connectionId ] : connection , } , } ; }