X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=ui%2Fweb%2Fsrc%2Fviews%2FChargingStationsView.vue;h=56200d2eccd672b4ab0012dad6f77efe1720caed;hb=f4b3f35d653138a946d74d5f3c313275ee9c03b2;hp=6f589ccc6a8030136f4449ac5de1a2e64daaf78e;hpb=c63085548339556d66ced562e9649a84d0544ff4;p=e-mobility-charging-stations-simulator.git diff --git a/ui/web/src/views/ChargingStationsView.vue b/ui/web/src/views/ChargingStationsView.vue index 6f589ccc..56200d2e 100644 --- a/ui/web/src/views/ChargingStationsView.vue +++ b/ui/web/src/views/ChargingStationsView.vue @@ -13,35 +13,35 @@ if ( getFromLocalStorage('uiServerConfigurationIndex', 0) !== state.uiServerIndex ) { - app?.appContext.config.globalProperties.$uiClient.setConfiguration( - app?.appContext.config.globalProperties.$configuration.uiServer[ + $uiClient.setConfiguration( + ($configuration.value.uiServer as UIServerConfigurationSection[])[ state.uiServerIndex ] ) - initializeWSEventListeners() - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener( + registerWSEventListeners() + $uiClient.registerWSEventListener( 'open', () => { setToLocalStorage('uiServerConfigurationIndex', state.uiServerIndex) clearToggleButtons() - $router.currentRoute.value.name !== 'charging-stations' && + $route.name !== 'charging-stations' && $router.push({ name: 'charging-stations' }) }, { once: true } ) - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener( + $uiClient.registerWSEventListener( 'error', () => { state.uiServerIndex = getFromLocalStorage( 'uiServerConfigurationIndex', 0 ) - app?.appContext.config.globalProperties.$uiClient.setConfiguration( - app?.appContext.config.globalProperties.$configuration.uiServer[ + $uiClient.setConfiguration( + ($configuration.value.uiServer as UIServerConfigurationSection[])[ getFromLocalStorage('uiServerConfigurationIndex', 0) ] ) - initializeWSEventListeners() + registerWSEventListeners() }, { once: true } ) @@ -62,17 +62,12 @@ - {{ state.simulatorState?.started === true ? 'Stop' : 'Start' }} Simulator - {{ state.simulatorState?.version != null ? ` (${state.simulatorState?.version})` : '' }} + {{ simulatorButtonMessage }} -import { getCurrentInstance, onMounted, ref } from 'vue' +import { computed, getCurrentInstance, onMounted, onUnmounted, ref, watch } from 'vue' import { useToast } from 'vue-toast-notification' import CSTable from '@/components/charging-stations/CSTable.vue' -import type { ResponsePayload, SimulatorState, UIServerConfigurationSection } from '@/types' +import type { + ChargingStationData, + ResponsePayload, + SimulatorState, + UIServerConfigurationSection +} from '@/types' import Container from '@/components/Container.vue' import ReloadButton from '@/components/buttons/ReloadButton.vue' import { @@ -131,131 +128,170 @@ import { getFromLocalStorage, getLocalStorage, randomUUID, - setToLocalStorage + setToLocalStorage, + useUIClient } from '@/composables' import ToggleButton from '@/components/buttons/ToggleButton.vue' +const simulatorState = ref(undefined) + +const simulatorButtonClass = computed(() => + simulatorState.value?.started === true ? 'simulator-stop-button' : 'simulator-start-button' +) +const simulatorButtonMessage = computed( + () => + `${simulatorState.value?.started === true ? 'Stop' : 'Start'} Simulator${simulatorState.value?.version != null ? ` (${simulatorState.value.version})` : ''}` +) + const state = ref<{ renderSimulator: `${string}-${string}-${string}-${string}-${string}` renderAddChargingStations: `${string}-${string}-${string}-${string}-${string}` renderChargingStations: `${string}-${string}-${string}-${string}-${string}` - loading: boolean - simulatorState?: SimulatorState + gettingSimulatorState: boolean + gettingTemplates: boolean + gettingChargingStations: boolean uiServerIndex: number }>({ renderSimulator: randomUUID(), renderAddChargingStations: randomUUID(), renderChargingStations: randomUUID(), - loading: false, + gettingSimulatorState: false, + gettingTemplates: false, + gettingChargingStations: false, uiServerIndex: getFromLocalStorage('uiServerConfigurationIndex', 0) }) -const app = getCurrentInstance() - const clearToggleButtons = (): void => { for (const key in getLocalStorage()) { if (key.includes('toggle-button')) { deleteFromLocalStorage(key) } } + state.value.renderChargingStations = randomUUID() + state.value.renderAddChargingStations = randomUUID() } -const clearChargingStations = (): void => { - app!.appContext.config.globalProperties.$chargingStations = [] +const app = getCurrentInstance() + +watch(app!.appContext.config.globalProperties.$chargingStations, () => { state.value.renderChargingStations = randomUUID() +}) + +watch(simulatorState, () => { + state.value.renderSimulator = randomUUID() +}) + +const clearTemplates = (): void => { + if (app != null) { + app.appContext.config.globalProperties.$templates.value = [] + } } -const uiClient = app?.appContext.config.globalProperties.$uiClient +const clearChargingStations = (): void => { + if (app != null) { + app.appContext.config.globalProperties.$chargingStations.value = [] + } +} + +const uiClient = useUIClient() + +const $toast = useToast() 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() - }) + if (state.value.gettingSimulatorState === false) { + state.value.gettingSimulatorState = true + uiClient + .simulatorState() + .then((response: ResponsePayload) => { + simulatorState.value = 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.gettingSimulatorState = false + }) + } } -const initializeWSEventListeners = () => { - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => { - getSimulatorState() +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 + app.appContext.config.globalProperties.$templates.value = response.templates as string[] } }) .catch((error: Error) => { - if (app != null) { - app.appContext.config.globalProperties.$templates = [] - } + clearTemplates() $toast.error('Error at fetching charging station templates') console.error('Error at fetching charging station templates:', error) }) .finally(() => { - state.value.renderAddChargingStations = randomUUID() + state.value.gettingTemplates = false }) - loadChargingStations(() => { - state.value.renderChargingStations = randomUUID() - }) - }) - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener( - 'error', - clearChargingStations - ) - app?.appContext.config.globalProperties.$uiClient.registerWSEventListener( - 'close', - clearChargingStations - ) + } } -onMounted(() => { - initializeWSEventListeners() -}) - -const uiServerConfigurations: { configuration: UIServerConfigurationSection; index: number }[] = - app?.appContext.config.globalProperties.$configuration.uiServer.map( - (configuration: UIServerConfigurationSection, index: number) => ({ - configuration, - index - }) - ) - -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) => { if (app != null) { - app.appContext.config.globalProperties.$chargingStations = response.chargingStations + app.appContext.config.globalProperties.$chargingStations.value = + response.chargingStations as ChargingStationData[] } }) .catch((error: Error) => { - if (app != null) { - app.appContext.config.globalProperties.$chargingStations = [] - } + clearChargingStations() $toast.error('Error at fetching charging stations') console.error('Error at fetching charging stations:', error) }) .finally(() => { - if (renderCallback != null) { - renderCallback() - } - state.value.loading = false + state.value.gettingChargingStations = false }) } } +const getData = (): void => { + getSimulatorState() + getTemplates() + getChargingStations() +} + +const registerWSEventListeners = () => { + uiClient.registerWSEventListener('open', getData) + uiClient.registerWSEventListener('error', clearChargingStations) + uiClient.registerWSEventListener('close', clearChargingStations) +} + +const unregisterWSEventListeners = () => { + uiClient.unregisterWSEventListener('open', getData) + uiClient.unregisterWSEventListener('error', clearChargingStations) + uiClient.unregisterWSEventListener('close', clearChargingStations) +} + +onMounted(() => { + registerWSEventListeners() +}) + +onUnmounted(() => { + unregisterWSEventListeners() +}) + +const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] = ( + app?.appContext.config.globalProperties.$configuration.value + .uiServer as UIServerConfigurationSection[] +).map((configuration: UIServerConfigurationSection, index: number) => ({ + index, + configuration +})) + const startSimulator = (): void => { uiClient .startSimulator() @@ -274,9 +310,7 @@ const stopSimulator = (): void => { uiClient .stopSimulator() .then(() => { - if (app != null) { - app.appContext.config.globalProperties.$chargingStations = [] - } + clearChargingStations() $toast.success('Simulator successfully stopped') }) .catch((error: Error) => { @@ -300,6 +334,7 @@ const stopSimulator = (): void => { #ui-server-container { display: flex; justify-content: center; + border-style: outset; } #ui-server-selector { @@ -323,11 +358,19 @@ const stopSimulator = (): void => { background-color: green; } +.simulator-start-button:hover { + background-color: rgb(0, 98, 0); +} + .simulator-stop-button { color: ivory; background-color: red; } +.simulator-stop-button:hover { + background-color: rgb(225, 0, 0); +} + #action-button { flex: none; } @@ -347,8 +390,9 @@ const stopSimulator = (): void => { } #action { + min-width: max-content; color: ivory; background-color: black; - padding: 1%; + padding: 0.8%; }