Please select a template
</option>
<option
- v-for="template in $templates!.value"
- v-show="Array.isArray($templates?.value) && $templates.value.length > 0"
+ v-for="template in $templates"
+ v-show="Array.isArray($templates) && $templates.length > 0"
:key="template"
>
{{ template }}
@click="
() => {
$uiClient
- ?.addChargingStations(state.template, state.numberOfStations, {
+ .addChargingStations(state.template, state.numberOfStations, {
supervisionUrls: state.supervisionUrl.length > 0 ? state.supervisionUrl : undefined,
autoStart: convertToBoolean(state.autoStart),
persistentConfiguration: convertToBoolean(state.persistentConfiguration),
})
.then(() => {
$toast.success('Charging stations successfully added')
- return refreshChargingStations()
- })
- .catch((error: Error) => {
- $toast.error('Error at adding charging stations')
- console.error('Error at adding charging stations:', error)
})
.finally(() => {
resetToggleButtonState('add-charging-stations', true)
$router.push({ name: 'charging-stations' })
})
+ .catch((error: Error) => {
+ $toast.error('Error at adding charging stations')
+ console.error('Error at adding charging stations:', error)
+ })
}
"
>
</template>
<script setup lang="ts">
-import { getCurrentInstance, ref, watch } from 'vue'
+import { ref, watch } from 'vue'
import type { UUIDv4 } from '@/types'
import {
convertToBoolean,
randomUUID,
- refreshChargingStations,
resetToggleButtonState,
+ useTemplates,
+ useUIClient,
} from '@/composables'
const state = ref<{
template: '',
})
-const app = getCurrentInstance()
+const $uiClient = useUIClient()
+const $templates = useTemplates()
-const templates = app?.appContext.config.globalProperties.$templates
-if (templates != null) {
- watch(templates, () => {
- state.value.renderTemplates = randomUUID()
- })
-}
+watch($templates, () => {
+ state.value.renderTemplates = randomUUID()
+})
</script>
<style scoped>
@click="
() => {
$uiClient
- ?.setSupervisionUrl(hashId, state.supervisionUrl)
+ .setSupervisionUrl(hashId, state.supervisionUrl)
.then(() => {
$toast.success('Supervision url successfully set')
- return refreshChargingStations()
- })
- .catch((error: Error) => {
- $toast.error('Error at setting supervision url')
- console.error('Error at setting supervision url:', error)
})
.finally(() => {
resetToggleButtonState(`${props.hashId}-set-supervision-url`, true)
$router.push({ name: 'charging-stations' })
})
+ .catch((error: Error) => {
+ $toast.error('Error at setting supervision url')
+ console.error('Error at setting supervision url:', error)
+ })
}
"
>
import { ref } from 'vue'
import Button from '@/components/buttons/Button.vue'
-import { refreshChargingStations, resetToggleButtonState } from '@/composables'
+import { resetToggleButtonState, useUIClient } from '@/composables'
const props = defineProps<{
chargingStationId: string
const state = ref<{ supervisionUrl: string }>({
supervisionUrl: '',
})
+
+const $uiClient = useUIClient()
</script>
<style scoped>
import { useToast } from 'vue-toast-notification'
import Button from '@/components/buttons/Button.vue'
-import {
- convertToInt,
- refreshChargingStations,
- resetToggleButtonState,
- UIClient,
- useUIClient,
-} from '@/composables'
+import { convertToInt, resetToggleButtonState, UIClient, useUIClient } from '@/composables'
import { type OCPPVersion } from '@/types'
const props = defineProps<{
ocppVersion: ocppVersion.value,
})
$toast.success('Transaction successfully started')
- await refreshChargingStations()
} catch (error) {
$toast.error('Error at starting transaction')
console.error('Error at starting transaction:', error)
-import type { Ref } from 'vue'
+import type { InjectionKey, Ref } from 'vue'
-import { getCurrentInstance } from 'vue'
+import { inject } from 'vue'
import { useToast } from 'vue-toast-notification'
-import type { ChargingStationData, UUIDv4 } from '@/types'
+import type { ChargingStationData, ConfigurationData, UUIDv4 } from '@/types'
import { UIClient } from './UIClient'
+export const configurationKey: InjectionKey<Ref<ConfigurationData>> = Symbol('configuration')
+export const chargingStationsKey: InjectionKey<Ref<ChargingStationData[]>> =
+ Symbol('chargingStations')
+export const templatesKey: InjectionKey<Ref<string[]>> = Symbol('templates')
+export const uiClientKey: InjectionKey<UIClient> = Symbol('uiClient')
+
export const convertToBoolean = (value: unknown): boolean => {
let result = false
if (value != null) {
}
export const useUIClient = (): UIClient => {
+ const injected = inject(uiClientKey, undefined)
+ if (injected != null) return injected
return UIClient.getInstance()
}
-export const useChargingStations = (): Ref<ChargingStationData[]> | undefined => {
- return getCurrentInstance()?.appContext.config.globalProperties.$chargingStations
+export const useConfiguration = (): Ref<ConfigurationData> => {
+ const injected = inject(configurationKey, undefined)
+ if (injected != null) return injected
+ throw new Error('configuration not provided')
+}
+
+export const useChargingStations = (): Ref<ChargingStationData[]> => {
+ const injected = inject(chargingStationsKey, undefined)
+ if (injected != null) return injected
+ throw new Error('chargingStations not provided')
}
-export const refreshChargingStations = async (): Promise<void> => {
- const $chargingStations = useChargingStations()
- if ($chargingStations == null) return
- const response = await useUIClient().listChargingStations()
- $chargingStations.value = response.chargingStations as ChargingStationData[]
+export const useTemplates = (): Ref<string[]> => {
+ const injected = inject(templatesKey, undefined)
+ if (injected != null) return injected
+ throw new Error('templates not provided')
}
export const useExecuteAction = (emit: (event: 'need-refresh') => void) => {
export { UIClient } from './UIClient'
export {
+ chargingStationsKey,
+ configurationKey,
convertToBoolean,
convertToInt,
deleteFromLocalStorage,
getFromLocalStorage,
getLocalStorage,
randomUUID,
- refreshChargingStations,
resetToggleButtonState,
setToLocalStorage,
+ templatesKey,
+ uiClientKey,
useChargingStations,
+ useConfiguration,
useExecuteAction,
+ useTemplates,
useUIClient,
} from './Utils'
import type { ChargingStationData, ConfigurationData, UIServerConfigurationSection } from '@/types'
import App from '@/App.vue'
-import { getFromLocalStorage, setToLocalStorage, UIClient } from '@/composables'
+import {
+ chargingStationsKey,
+ configurationKey,
+ getFromLocalStorage,
+ setToLocalStorage,
+ templatesKey,
+ UIClient,
+ uiClientKey,
+} from '@/composables'
import { router } from '@/router'
import 'vue-toast-notification/dist/theme-bootstrap.css'
if (!Array.isArray(config.uiServer)) {
config.uiServer = [config.uiServer]
}
- app.config.globalProperties.$configuration ??= ref(config)
- if (!Array.isArray(app.config.globalProperties.$templates?.value)) {
- app.config.globalProperties.$templates = ref<string[]>([])
- }
- if (!Array.isArray(app.config.globalProperties.$chargingStations?.value)) {
- app.config.globalProperties.$chargingStations = ref<ChargingStationData[]>([])
- }
+ const configuration = ref(config)
+ const templates = ref<string[]>([])
+ const chargingStations = ref<ChargingStationData[]>([])
if (
getFromLocalStorage<number | undefined>('uiServerConfigurationIndex', undefined) == null ||
getFromLocalStorage('uiServerConfigurationIndex', 0) >
- (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[])
- .length -
- 1
+ (configuration.value.uiServer as UIServerConfigurationSection[]).length - 1
) {
setToLocalStorage('uiServerConfigurationIndex', 0)
}
- app.config.globalProperties.$uiClient ??= UIClient.getInstance(
- (app.config.globalProperties.$configuration.value.uiServer as UIServerConfigurationSection[])[
+ const uiClient = UIClient.getInstance(
+ (configuration.value.uiServer as UIServerConfigurationSection[])[
getFromLocalStorage('uiServerConfigurationIndex', 0)
]
)
+ app.provide(configurationKey, configuration)
+ app.provide(chargingStationsKey, chargingStations)
+ app.provide(templatesKey, templates)
+ app.provide(uiClientKey, uiClient)
app.use(router).use(ToastPlugin).mount('#app')
}
RouterLink: (typeof import('vue-router'))['RouterLink']
RouterView: (typeof import('vue-router'))['RouterView']
}
- interface ComponentCustomProperties {
- $chargingStations: import('vue').Ref<import('@/types').ChargingStationData[]> | undefined
- $configuration: import('vue').Ref<import('@/types').ConfigurationData> | undefined
- $templates: import('vue').Ref<string[]> | undefined
- $uiClient: import('@/composables').UIClient | undefined
- }
}
if (
getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
) {
- $uiClient?.setConfiguration(
- ($configuration!.value.uiServer as UIServerConfigurationSection[])[
- state.uiServerIndex
- ]
+ $uiClient.setConfiguration(
+ ($configuration.uiServer as UIServerConfigurationSection[])[state.uiServerIndex]
)
registerWSEventListeners()
- $uiClient?.registerWSEventListener(
+ $uiClient.registerWSEventListener(
'open',
() => {
setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
},
{ once: true }
)
- $uiClient?.registerWSEventListener(
+ $uiClient.registerWSEventListener(
'error',
() => {
state.uiServerIndex = getFromLocalStorage<number>(
'uiServerConfigurationIndex',
0
)
- $uiClient?.setConfiguration(
- ($configuration!.value.uiServer as UIServerConfigurationSection[])[
+ $uiClient.setConfiguration(
+ ($configuration.uiServer as UIServerConfigurationSection[])[
getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
]
)
/>
</Container>
<CSTable
- v-show="Array.isArray($chargingStations?.value) && $chargingStations.value.length > 0"
+ v-show="Array.isArray($chargingStations) && $chargingStations.length > 0"
:key="state.renderChargingStations"
- :charging-stations="$chargingStations!.value"
+ :charging-stations="$chargingStations"
@need-refresh="
() => {
getChargingStations()
</template>
<script setup lang="ts">
-import { computed, getCurrentInstance, onMounted, onUnmounted, ref, watch } from 'vue'
+import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
import { useToast } from 'vue-toast-notification'
import type {
randomUUID,
setToLocalStorage,
useChargingStations,
+ useConfiguration,
+ useTemplates,
useUIClient,
} from '@/composables'
}
}
-const app = getCurrentInstance()
+const $configuration = useConfiguration()
+const $templates = useTemplates()
+const $chargingStations = useChargingStations()
-const chargingStationsRef = useChargingStations()
-if (chargingStationsRef != null) {
- watch(chargingStationsRef, () => {
- state.value.renderChargingStations = randomUUID()
- })
-}
+watch($chargingStations, () => {
+ state.value.renderChargingStations = randomUUID()
+})
const clearTemplates = (): void => {
- if (app != null) {
- app.appContext.config.globalProperties.$templates!.value = []
- }
+ $templates.value = []
}
const clearChargingStations = (): void => {
- if (chargingStationsRef != null) {
- chargingStationsRef.value = []
- }
+ $chargingStations.value = []
}
const $uiClient = useUIClient()
$uiClient
.listTemplates()
.then((response: ResponsePayload) => {
- if (app != null) {
- app.appContext.config.globalProperties.$templates!.value = response.templates as string[]
- }
+ $templates.value = response.templates as string[]
return undefined
})
.finally(() => {
$uiClient
.listChargingStations()
.then((response: ResponsePayload) => {
- if (chargingStationsRef != null) {
- chargingStationsRef.value = response.chargingStations as ChargingStationData[]
- }
+ $chargingStations.value = response.chargingStations as ChargingStationData[]
return undefined
})
.finally(() => {
const uiServerConfigurations: {
configuration: UIServerConfigurationSection
index: number
-}[] = (
- app!.appContext.config.globalProperties.$configuration!.value
- .uiServer as UIServerConfigurationSection[]
-).map((configuration: UIServerConfigurationSection, index: number) => ({
- configuration,
- index,
-}))
+}[] = ($configuration.value.uiServer as UIServerConfigurationSection[]).map(
+ (configuration: UIServerConfigurationSection, index: number) => ({
+ configuration,
+ index,
+ })
+)
const startSimulator = (): void => {
$uiClient
import { ref } from 'vue'
import AddChargingStations from '@/components/actions/AddChargingStations.vue'
+import { templatesKey, uiClientKey } from '@/composables'
import { toastMock } from '../setup'
import { ButtonStub, createMockUIClient, type MockUIClient } from './helpers'
config: {
globalProperties: {
$router: mockRouter,
- $templates: ref(['template-A.json', 'template-B.json']),
$toast: toastMock,
- $uiClient: mockClient,
} as never,
},
+ provide: {
+ [templatesKey as symbol]: ref(['template-A.json', 'template-B.json']),
+ [uiClientKey as symbol]: mockClient,
+ },
stubs: {
Button: ButtonStub,
},
import type { UIClient } from '@/composables'
-import { useUIClient } from '@/composables'
+import {
+ chargingStationsKey,
+ configurationKey,
+ templatesKey,
+ uiClientKey,
+ useUIClient,
+} from '@/composables'
import { ResponseStatus } from '@/types'
import ChargingStationsView from '@/views/ChargingStationsView.vue'
global: {
config: {
globalProperties: {
- $chargingStations: ref(chargingStations),
- $configuration: ref(configuration),
$route: { name: 'charging-stations', params: {}, query: {} },
$router: { back: vi.fn(), push: vi.fn(), replace: vi.fn() },
- $templates: ref(templates),
- $uiClient: mockClient,
} as never,
},
+ provide: {
+ [chargingStationsKey as symbol]: ref(chargingStations),
+ [configurationKey as symbol]: ref(configuration),
+ [templatesKey as symbol]: ref(templates),
+ [uiClientKey as symbol]: mockClient,
+ },
stubs: {
Container: { name: 'Container', template: '<div><slot /></div>' },
CSTable: true,
import { describe, expect, it, vi } from 'vitest'
import SetSupervisionUrl from '@/components/actions/SetSupervisionUrl.vue'
+import { uiClientKey } from '@/composables'
import { toastMock } from '../setup'
import { TEST_HASH_ID, TEST_STATION_ID } from './constants'
globalProperties: {
$router: mockRouter,
$toast: toastMock,
- $uiClient: mockClient,
} as never,
},
+ provide: {
+ [uiClientKey as symbol]: mockClient,
+ },
stubs: {
Button: ButtonStub,
},