refactor: front end
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+117
-79
@@ -1,104 +1,142 @@
|
||||
/**
|
||||
* Providers Store
|
||||
*/
|
||||
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import type { Ref, ComputedRef } from 'vue'
|
||||
import type { ProviderInterface, ProviderRecord, ProviderCapabilitiesInterface } from '../types/provider'
|
||||
import type { SourceSelector } from '../types/common'
|
||||
import { providerService } from '../services/providerService'
|
||||
import { providerService } from '../services'
|
||||
import { ProviderObject } from '../models/provider'
|
||||
import type { SourceSelector } from '../types'
|
||||
|
||||
export const useProvidersStore = defineStore('fileProviders', () => {
|
||||
const providers: Ref<Record<string, ProviderObject>> = ref({})
|
||||
const loading = ref(false)
|
||||
const error: Ref<string | null> = ref(null)
|
||||
const initialized = ref(false)
|
||||
export const useProvidersStore = defineStore('documentsProvidersStore', () => {
|
||||
// State
|
||||
const _providers = ref<Record<string, ProviderObject>>({})
|
||||
const transceiving = ref(false)
|
||||
|
||||
const providerList: ComputedRef<ProviderObject[]> = computed(() =>
|
||||
Object.values(providers.value)
|
||||
)
|
||||
/**
|
||||
* Get count of providers in store
|
||||
*/
|
||||
const count = computed(() => Object.keys(_providers.value).length)
|
||||
|
||||
const providerIds: ComputedRef<string[]> = computed(() =>
|
||||
Object.keys(providers.value)
|
||||
)
|
||||
/**
|
||||
* Check if any providers are present in store
|
||||
*/
|
||||
const has = computed(() => count.value > 0)
|
||||
|
||||
const getProvider = (id: string): ProviderObject | undefined => {
|
||||
return providers.value[id]
|
||||
}
|
||||
/**
|
||||
* Get all providers present in store
|
||||
*/
|
||||
const providers = computed(() => Object.values(_providers.value))
|
||||
|
||||
const hasProvider = (id: string): boolean => {
|
||||
return id in providers.value
|
||||
}
|
||||
|
||||
const isCapable = (providerId: string, capability: keyof ProviderCapabilitiesInterface): boolean => {
|
||||
const provider = providers.value[providerId]
|
||||
return provider ? provider.capable(capability) : false
|
||||
}
|
||||
|
||||
const setProviders = (data: ProviderRecord) => {
|
||||
const hydrated: Record<string, ProviderObject> = {}
|
||||
for (const [id, providerData] of Object.entries(data)) {
|
||||
hydrated[id] = new ProviderObject().fromJson(providerData)
|
||||
/**
|
||||
* Get a specific provider from store, with optional retrieval
|
||||
*
|
||||
* @param identifier - Provider identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
* @returns Provider object or null
|
||||
*/
|
||||
function provider(identifier: string, retrieve: boolean = false): ProviderObject | null {
|
||||
if (retrieve === true && !_providers.value[identifier]) {
|
||||
console.debug(`[Documents Manager][Store] - Force fetching provider "${identifier}"`)
|
||||
fetch(identifier)
|
||||
}
|
||||
providers.value = hydrated
|
||||
initialized.value = true
|
||||
|
||||
return _providers.value[identifier] || null
|
||||
}
|
||||
|
||||
const addProvider = (id: string, provider: ProviderInterface) => {
|
||||
providers.value[id] = new ProviderObject().fromJson(provider)
|
||||
}
|
||||
// Actions
|
||||
|
||||
const removeProvider = (id: string) => {
|
||||
delete providers.value[id]
|
||||
}
|
||||
|
||||
const clearProviders = () => {
|
||||
providers.value = {}
|
||||
initialized.value = false
|
||||
}
|
||||
|
||||
// API actions
|
||||
const fetchProviders = async (sources?: SourceSelector): Promise<void> => {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
/**
|
||||
* Retrieve all or specific providers, optionally filtered by source selector
|
||||
*
|
||||
* @param request - list request parameters
|
||||
*
|
||||
* @returns Promise with provider object list keyed by provider identifier
|
||||
*/
|
||||
async function list(sources?: SourceSelector): Promise<Record<string, ProviderObject>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const data = await providerService.list(sources)
|
||||
setProviders(data)
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to fetch providers'
|
||||
throw e
|
||||
const providers = await providerService.list({ sources })
|
||||
|
||||
// Merge retrieved providers into state
|
||||
_providers.value = { ..._providers.value, ...providers }
|
||||
|
||||
console.debug('[Documents Manager][Store] - Successfully retrieved', Object.keys(providers).length, 'providers')
|
||||
return providers
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to retrieve providers:', error)
|
||||
throw error
|
||||
} finally {
|
||||
loading.value = false
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const checkProviderExtant = async (sources: SourceSelector): Promise<Record<string, boolean>> => {
|
||||
/**
|
||||
* Retrieve a specific provider by identifier
|
||||
*
|
||||
* @param identifier - provider identifier
|
||||
*
|
||||
* @returns Promise with provider object
|
||||
*/
|
||||
async function fetch(identifier: string): Promise<ProviderObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
return await providerService.extant(sources)
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'Failed to check providers'
|
||||
throw e
|
||||
const provider = await providerService.fetch({ identifier })
|
||||
|
||||
// Merge fetched provider into state
|
||||
_providers.value[provider.identifier] = provider
|
||||
|
||||
console.debug('[Documents Manager][Store] - Successfully fetched provider:', provider.identifier)
|
||||
return provider
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to fetch provider:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve provider availability status for a given source selector
|
||||
*
|
||||
* @param sources - source selector to check availability for
|
||||
*
|
||||
* @returns Promise with provider availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await providerService.extant({ sources })
|
||||
|
||||
Object.entries(response).forEach(([providerId, providerStatus]) => {
|
||||
if (providerStatus === false) {
|
||||
delete _providers.value[providerId]
|
||||
}
|
||||
})
|
||||
|
||||
console.debug('[Documents Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'providers')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to check providers:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// Return public API
|
||||
return {
|
||||
// State
|
||||
transceiving: readonly(transceiving),
|
||||
// computed
|
||||
count,
|
||||
has,
|
||||
providers,
|
||||
loading,
|
||||
error,
|
||||
initialized,
|
||||
// Computed
|
||||
providerList,
|
||||
providerIds,
|
||||
// Getters
|
||||
getProvider,
|
||||
hasProvider,
|
||||
isCapable,
|
||||
// Setters
|
||||
setProviders,
|
||||
addProvider,
|
||||
removeProvider,
|
||||
clearProviders,
|
||||
// Actions
|
||||
fetchProviders,
|
||||
checkProviderExtant,
|
||||
provider,
|
||||
// functions
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user