From 97cd0ef387b54392852368147eb51cea354bc804 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 5 Mar 2024 15:02:46 +0100 Subject: [PATCH] refactor(ui): cleanup initial data fetching code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../actions/AddChargingStations.vue | 1 + ui/web/src/views/ChargingStationsView.vue | 104 +++++++++--------- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/ui/web/src/components/actions/AddChargingStations.vue b/ui/web/src/components/actions/AddChargingStations.vue index 8946e82a..5eb51f35 100644 --- a/ui/web/src/components/actions/AddChargingStations.vue +++ b/ui/web/src/components/actions/AddChargingStations.vue @@ -121,6 +121,7 @@ const state = ref<{ }) const app = getCurrentInstance() + const uiClient = app?.appContext.config.globalProperties.$uiClient const $toast = useToast() diff --git a/ui/web/src/views/ChargingStationsView.vue b/ui/web/src/views/ChargingStationsView.vue index 0468c597..109803f4 100644 --- a/ui/web/src/views/ChargingStationsView.vue +++ b/ui/web/src/views/ChargingStationsView.vue @@ -13,13 +13,13 @@ if ( getFromLocalStorage('uiServerConfigurationIndex', 0) !== state.uiServerIndex ) { - app?.appContext.config.globalProperties.$uiClient.setConfiguration( + uiClient.setConfiguration( app?.appContext.config.globalProperties.$configuration.uiServer[ state.uiServerIndex ] ) initializeWSEventListeners() - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener( + uiClient.registerWSEventListener( 'open', () => { setToLocalStorage('uiServerConfigurationIndex', state.uiServerIndex) @@ -29,14 +29,14 @@ }, { once: true } ) - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener( + uiClient.registerWSEventListener( 'error', () => { state.uiServerIndex = getFromLocalStorage( 'uiServerConfigurationIndex', 0 ) - app?.appContext.config.globalProperties.$uiClient.setConfiguration( + uiClient.setConfiguration( app?.appContext.config.globalProperties.$configuration.uiServer[ getFromLocalStorage('uiServerConfigurationIndex', 0) ] @@ -157,8 +157,6 @@ const simulatorButtonMessage = computed( `${state.value.simulatorState?.started === true ? 'Stop' : 'Start'} Simulator${state.value.simulatorState?.version != null ? ` (${state.value.simulatorState.version})` : ''}` ) -const app = getCurrentInstance() - const clearToggleButtons = (): void => { for (const key in getLocalStorage()) { if (key.includes('toggle-button')) { @@ -169,6 +167,8 @@ const clearToggleButtons = (): void => { state.value.renderAddChargingStations = randomUUID() } +const app = getCurrentInstance() + const clearChargingStations = (): void => { if (app != null) { app.appContext.config.globalProperties.$chargingStations = [] @@ -178,6 +178,8 @@ const clearChargingStations = (): void => { const uiClient = app?.appContext.config.globalProperties.$uiClient +const $toast = useToast() + const getSimulatorState = (): void => { if (state.value.gettingSimulatorState === false) { state.value.gettingSimulatorState = true @@ -197,55 +199,29 @@ const getSimulatorState = (): void => { } } -const initializeWSEventListeners = () => { - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => { - getSimulatorState() - if (state.value.gettingTemplates === false) { - state.value.gettingTemplates = true - uiClient - .listTemplates() - .then((response: ResponsePayload) => { - if (app != null) { - app.appContext.config.globalProperties.$templates = response.templates - } - }) - .catch((error: Error) => { - if (app != null) { - app.appContext.config.globalProperties.$templates = [] - } - $toast.error('Error at fetching charging station templates') - console.error('Error at fetching charging station templates:', error) - }) - .finally(() => { - state.value.gettingTemplates = false - }) - } - getChargingStations() - }) - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener( - 'error', - clearChargingStations - ) - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener( - 'close', - clearChargingStations - ) +const getTemplates = (): void => { + if (state.value.gettingTemplates === false) { + state.value.gettingTemplates = true + uiClient + .listTemplates() + .then((response: ResponsePayload) => { + if (app != null) { + app.appContext.config.globalProperties.$templates = response.templates + } + }) + .catch((error: Error) => { + if (app != null) { + app.appContext.config.globalProperties.$templates = [] + } + $toast.error('Error at fetching charging station templates') + console.error('Error at fetching charging station templates:', error) + }) + .finally(() => { + state.value.gettingTemplates = false + }) + } } -onMounted(() => { - initializeWSEventListeners() -}) - -const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] = - app?.appContext.config.globalProperties.$configuration.uiServer.map( - (configuration: UIServerConfigurationSection, index: number) => ({ - index, - configuration - }) - ) - -const $toast = useToast() - const getChargingStations = (): void => { if (state.value.gettingChargingStations === false) { state.value.gettingChargingStations = true @@ -270,6 +246,28 @@ const getChargingStations = (): void => { } } +const initializeWSEventListeners = () => { + uiClient.registerWSEventListener('open', () => { + getSimulatorState() + getTemplates() + getChargingStations() + }) + uiClient.registerWSEventListener('error', clearChargingStations) + uiClient.registerWSEventListener('close', clearChargingStations) +} + +onMounted(() => { + initializeWSEventListeners() +}) + +const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] = + app?.appContext.config.globalProperties.$configuration.uiServer.map( + (configuration: UIServerConfigurationSection, index: number) => ({ + index, + configuration + }) + ) + const startSimulator = (): void => { uiClient .startSimulator() -- 2.34.1