+import {
+ getFromLocalStorage,
+ getLocalStorage,
+ randomUUID,
+ removeFromLocalStorage,
+ setToLocalStorage
+} from '@/composables'
+import ToggleButton from '@/components/buttons/ToggleButton.vue'
+
+const app = getCurrentInstance()
+
+const clearToggleButtons = (): void => {
+ for (const key in getLocalStorage()) {
+ if (key.includes('toggle-button')) {
+ removeFromLocalStorage(key)
+ }
+ }
+}
+
+const clearChargingStations = (): void => {
+ clearToggleButtons()
+ app!.appContext.config.globalProperties.$chargingStations = []
+ state.value.renderAddChargingStations = randomUUID()
+ state.value.renderChargingStations = randomUUID()
+}
+
+const initializeWSEventListeners = () => {
+ app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
+ 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)
+ })
+ loadChargingStations(() => {
+ state.value.renderAddChargingStations = randomUUID()
+ state.value.renderChargingStations = randomUUID()
+ })
+ })
+ app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
+ 'error',
+ clearChargingStations
+ )
+ app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
+ 'close',
+ clearChargingStations
+ )
+}