From 57d928fd9877db7830c639d7462cba92190dc22c Mon Sep 17 00:00:00 2001
From: Konrad Mohrfeldt <konrad.mohrfeldt@farbdev.org>
Date: Fri, 16 Jun 2023 15:24:43 +0200
Subject: [PATCH] feat: add read-only hosts store

refs #127
---
 src/main.js         | 17 ++++++++++++++++-
 src/stores/hosts.ts | 14 ++++++++++++++
 src/types.ts        |  1 +
 3 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 src/stores/hosts.ts

diff --git a/src/main.js b/src/main.js
index 2e02d984..3d130fd8 100644
--- a/src/main.js
+++ b/src/main.js
@@ -1,6 +1,6 @@
 import BootstrapVue from 'bootstrap-vue'
 import { createPinia } from 'pinia'
-import { createApp } from 'vue'
+import { createApp, reactive, watchEffect } from 'vue'
 import VueToast from 'vue-toast-notification'
 import VueLogger from 'vuejs-logger'
 
@@ -15,6 +15,8 @@ import '../node_modules/bootstrap/scss/bootstrap.scss'
 import './assets/custom.scss'
 import './assets/styles/tailwind.css'
 import { shouldLog } from '@/utilities'
+import { useAuthStore } from '@/stores/auth'
+import { useHostStore } from '@/stores/hosts'
 
 const pinia = createPinia()
 const app = createApp(App)
@@ -44,3 +46,16 @@ app.use(TranslationPlugin)
 app.use(VueToast, { position: 'bottom-left' })
 store.$log = app.$log
 app.mount('#app')
+
+const authStore = reactive(useAuthStore())
+const hostStore = useHostStore()
+
+watchEffect(async () => {
+  if (authStore.currentUser) {
+    try {
+      await Promise.allSettled([hostStore.list()])
+    } catch (e) {
+      console.error('unable to load base state from stores', e)
+    }
+  }
+})
diff --git a/src/stores/hosts.ts b/src/stores/hosts.ts
new file mode 100644
index 00000000..c556e04e
--- /dev/null
+++ b/src/stores/hosts.ts
@@ -0,0 +1,14 @@
+import { defineStore } from 'pinia'
+import { steeringAuthInit } from '@/stores/auth'
+import { APIListUnpaginated, APIRetrieve, createExtendableAPI, createSteeringURL } from '@/api'
+import { Host } from '@/types'
+
+export const useHostStore = defineStore('hosts', () => {
+  const endpoint = createSteeringURL.prefix('hosts')
+  const { base, ...api } = createExtendableAPI<Host>(endpoint, steeringAuthInit)
+  return {
+    ...base,
+    ...APIListUnpaginated(api),
+    ...APIRetrieve(api),
+  }
+})
diff --git a/src/types.ts b/src/types.ts
index a14dc112..3b7663ed 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -20,3 +20,4 @@ export type TimeSlot = Required<steeringComponents['schemas']['TimeSlot']>
 export type Show = Required<steeringComponents['schemas']['Show']>
 export type Playlist = Required<tankComponents['schemas']['store.Playlist']>
 export type Schedule = Required<steeringComponents['schemas']['Schedule']>
+export type Host = Required<steeringComponents['schemas']['Host']>
-- 
GitLab