From 26cf7d996edab284f7f616ab32702a6a979cad1f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 5 Mar 2024 14:29:11 +0100 Subject: [PATCH] refactor(ui): cleanup data fetching and display refreshing logic MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- ui/web/src/views/ChargingStationsView.vue | 95 +++++++++++++---------- 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/ui/web/src/views/ChargingStationsView.vue b/ui/web/src/views/ChargingStationsView.vue index de7cd746..0468c597 100644 --- a/ui/web/src/views/ChargingStationsView.vue +++ b/ui/web/src/views/ChargingStationsView.vue @@ -93,8 +93,8 @@ ({ renderSimulator: randomUUID(), renderAddChargingStations: randomUUID(), renderChargingStations: randomUUID(), - loading: false, + gettingSimulatorState: false, + gettingTemplates: false, + gettingChargingStations: false, uiServerIndex: getFromLocalStorage('uiServerConfigurationIndex', 0) }) @@ -161,53 +165,62 @@ const clearToggleButtons = (): void => { deleteFromLocalStorage(key) } } + state.value.renderChargingStations = randomUUID() + state.value.renderAddChargingStations = randomUUID() } const clearChargingStations = (): void => { - app!.appContext.config.globalProperties.$chargingStations = [] + if (app != null) { + app.appContext.config.globalProperties.$chargingStations = [] + } state.value.renderChargingStations = randomUUID() } const uiClient = app?.appContext.config.globalProperties.$uiClient const getSimulatorState = (): void => { - uiClient - .simulatorState() - .then((response: ResponsePayload) => { - state.value.simulatorState = response.state as SimulatorState - }) - .catch((error: Error) => { - $toast.error('Error at fetching simulator state') - console.error('Error at fetching simulator state:', error) - }) - .finally(() => { - state.value.renderSimulator = randomUUID() - }) -} - -const initializeWSEventListeners = () => { - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => { - getSimulatorState() + if (state.value.gettingSimulatorState === false) { + state.value.gettingSimulatorState = true uiClient - .listTemplates() + .simulatorState() .then((response: ResponsePayload) => { - if (app != null) { - app.appContext.config.globalProperties.$templates = response.templates - } + state.value.simulatorState = response.state as SimulatorState }) .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) + $toast.error('Error at fetching simulator state') + console.error('Error at fetching simulator state:', error) }) .finally(() => { - state.value.renderAddChargingStations = randomUUID() + state.value.renderSimulator = randomUUID() + state.value.gettingSimulatorState = false }) - loadChargingStations(() => { - state.value.renderChargingStations = randomUUID() - }) + } +} + +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', @@ -233,9 +246,9 @@ const uiServerConfigurations: { index: number; configuration: UIServerConfigurat const $toast = useToast() -const loadChargingStations = (renderCallback?: () => void): void => { - if (state.value.loading === false) { - state.value.loading = true +const getChargingStations = (): void => { + if (state.value.gettingChargingStations === false) { + state.value.gettingChargingStations = true uiClient .listChargingStations() .then((response: ResponsePayload) => { @@ -251,10 +264,8 @@ const loadChargingStations = (renderCallback?: () => void): void => { console.error('Error at fetching charging stations:', error) }) .finally(() => { - if (renderCallback != null) { - renderCallback() - } - state.value.loading = false + state.value.renderChargingStations = randomUUID() + state.value.gettingChargingStations = false }) } } -- 2.34.1