From f65271c472eb2d9db3403cabdbb1b01eb10af55b Mon Sep 17 00:00:00 2001
From: Konrad Mohrfeldt <km@roko.li>
Date: Thu, 30 May 2024 11:57:20 +0200
Subject: [PATCH] refactor: create custom paginated store for hosts
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Hosts shouldn’t be fetched all at once like it’s the case for stores
like categories and languages because there might be a lot of hosts in
the database.
---
 src/stores/hosts.ts | 29 +++++++++++++++++++++++++++++
 src/stores/index.ts | 18 ++----------------
 2 files changed, 31 insertions(+), 16 deletions(-)
 create mode 100644 src/stores/hosts.ts

diff --git a/src/stores/hosts.ts b/src/stores/hosts.ts
new file mode 100644
index 00000000..b2370cf1
--- /dev/null
+++ b/src/stores/hosts.ts
@@ -0,0 +1,29 @@
+import {
+  APIListPaginated,
+  APIRemove,
+  APIRetrieve,
+  APIUpdate,
+  createExtendableAPI,
+} from '@rokoli/bnb/drf'
+import { defineStore } from 'pinia'
+
+import { createSteeringURL } from '@/api'
+import { components } from '@/steering-types'
+import { steeringAuthInit } from '@/stores/auth'
+import { KeysFrom, Host } from '@/types'
+
+type ReadonlyAttrs = KeysFrom<Host, 'id'>
+type HostUpdateData = Omit<Host, ReadonlyAttrs>
+type HostPartialUpdateData = components['schemas']['PatchedHost']
+
+export const useHostStore = defineStore('hosts', () => {
+  const endpoint = createSteeringURL.prefix('hosts')
+  const { api, base } = createExtendableAPI<Host>(endpoint, steeringAuthInit)
+  return {
+    ...base,
+    ...APIListPaginated(api),
+    ...APIRetrieve(api),
+    ...APIUpdate<Host, HostUpdateData, HostPartialUpdateData>(api),
+    ...APIRemove(api),
+  }
+})
diff --git a/src/stores/index.ts b/src/stores/index.ts
index 8ce6e37b..a318496f 100644
--- a/src/stores/index.ts
+++ b/src/stores/index.ts
@@ -1,18 +1,10 @@
 import { createSteeringURL } from '@/api'
-import {
-  Category,
-  FundingCategory,
-  Host,
-  Language,
-  LinkType,
-  MusicFocus,
-  Topic,
-  Type,
-} from '@/types'
+import { Category, FundingCategory, Language, LinkType, MusicFocus, Topic, Type } from '@/types'
 import { steeringAuthInit } from '@/stores/auth'
 import { createUnpaginatedAPIStore } from '@/stores/util'
 
 export { useAuthStore, useUserStore } from '@/stores/auth'
+export { useHostStore } from '@/stores/hosts'
 export { useImageStore } from '@/stores/images'
 export { useLicenseStore } from '@/stores/licenses'
 export { useNoteStore } from '@/stores/notes'
@@ -61,12 +53,6 @@ export const useMusicFocusStore = createUnpaginatedAPIStore<MusicFocus>(
   steeringAuthInit,
   { syncOnAuth: true },
 )
-export const useHostStore = createUnpaginatedAPIStore<Host>(
-  'hosts',
-  createSteeringURL.prefix('hosts'),
-  steeringAuthInit,
-  { syncOnAuth: true },
-)
 
 export const useLinkTypeStore = createUnpaginatedAPIStore<LinkType>(
   'linkTypes',
-- 
GitLab