refactor: front end
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
/**
|
||||
* Collections Store
|
||||
*/
|
||||
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { collectionService } from '../services/collectionService'
|
||||
import type {
|
||||
SourceSelector,
|
||||
ListFilter,
|
||||
ListSort,
|
||||
CollectionMutableProperties,
|
||||
CollectionDeleteResponse,
|
||||
} from '../types'
|
||||
import { CollectionObject } from '../models/collection'
|
||||
|
||||
export const useCollectionsStore = defineStore('documentsCollectionsStore', () => {
|
||||
// State
|
||||
const _collections = ref<Record<string, CollectionObject>>({})
|
||||
const transceiving = ref(false)
|
||||
|
||||
// Getters
|
||||
const count = computed(() => Object.keys(_collections.value).length)
|
||||
const has = computed(() => count.value > 0)
|
||||
const collections = computed(() => Object.values(_collections.value))
|
||||
|
||||
const collectionsByService = computed(() => {
|
||||
const groups: Record<string, CollectionObject[]> = {}
|
||||
|
||||
Object.values(_collections.value).forEach((collection) => {
|
||||
const serviceKey = `${collection.provider}:${collection.service}`
|
||||
const serviceCollections = (groups[serviceKey] ??= [])
|
||||
serviceCollections.push(collection)
|
||||
})
|
||||
|
||||
return groups
|
||||
})
|
||||
|
||||
function identifierKey(provider: string, service: string | number | null, identifier: string | number | null): string {
|
||||
return `${provider}:${service ?? ''}:${identifier ?? ''}`
|
||||
}
|
||||
|
||||
function collection(provider: string, service: string | number, identifier: string | number, retrieve: boolean = false): CollectionObject | null {
|
||||
const key = identifierKey(provider, service, identifier)
|
||||
if (retrieve === true && !_collections.value[key]) {
|
||||
console.debug(`[Documents Manager][Store] - Force fetching collection "${key}"`)
|
||||
fetch(provider, service, identifier)
|
||||
}
|
||||
|
||||
return _collections.value[key] || null
|
||||
}
|
||||
|
||||
function collectionsForService(provider: string, service: string | number): CollectionObject[] {
|
||||
const serviceKeyPrefix = `${provider}:${service}:`
|
||||
return Object.entries(_collections.value)
|
||||
.filter(([key]) => key.startsWith(serviceKeyPrefix))
|
||||
.map(([_, collectionObj]) => collectionObj)
|
||||
}
|
||||
|
||||
function clearService(provider: string, service: string | number): void {
|
||||
const serviceKeyPrefix = `${provider}:${service}:`
|
||||
Object.keys(_collections.value)
|
||||
.filter((key) => key.startsWith(serviceKeyPrefix))
|
||||
.forEach((key) => {
|
||||
delete _collections.value[key]
|
||||
})
|
||||
}
|
||||
|
||||
function clearAll(): void {
|
||||
_collections.value = {}
|
||||
}
|
||||
|
||||
// Actions
|
||||
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort): Promise<Record<string, CollectionObject>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.list({ sources, filter, sort })
|
||||
|
||||
const hydrated: Record<string, CollectionObject> = {}
|
||||
Object.entries(response).forEach(([providerId, providerServices]) => {
|
||||
Object.entries(providerServices).forEach(([serviceId, serviceCollections]) => {
|
||||
Object.entries(serviceCollections).forEach(([collectionId, collectionObj]) => {
|
||||
const key = identifierKey(providerId, serviceId, collectionId)
|
||||
hydrated[key] = collectionObj
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
_collections.value = { ..._collections.value, ...hydrated }
|
||||
|
||||
console.debug('[Documents Manager][Store] - Successfully retrieved', Object.keys(hydrated).length, 'collections')
|
||||
return hydrated
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to retrieve collections:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function fetch(provider: string, service: string | number, identifier: string | number): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.fetch({ provider, service, collection: identifier })
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[Documents Manager][Store] - Successfully fetched collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to fetch collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function extant(sources: SourceSelector) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.extant({ sources })
|
||||
console.debug('[Documents Manager][Store] - Successfully checked collection availability')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to check collection availability:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function create(
|
||||
provider: string,
|
||||
service: string | number,
|
||||
collection: string | number | null,
|
||||
properties: CollectionMutableProperties,
|
||||
): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.create({ provider, service, collection, properties })
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[Documents Manager][Store] - Successfully created collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to create collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function update(
|
||||
provider: string,
|
||||
service: string | number,
|
||||
identifier: string | number,
|
||||
properties: CollectionMutableProperties,
|
||||
): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.update({ provider, service, identifier, properties })
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[Documents Manager][Store] - Successfully updated collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to update collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(provider: string, service: string | number, identifier: string | number): Promise<CollectionDeleteResponse> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.delete({ provider, service, identifier })
|
||||
if (response.success) {
|
||||
const key = identifierKey(provider, service, identifier)
|
||||
delete _collections.value[key]
|
||||
}
|
||||
|
||||
console.debug('[Documents Manager][Store] - Successfully deleted collection:', `${provider}:${service}:${identifier}`)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Documents Manager][Store] - Failed to delete collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
transceiving: readonly(transceiving),
|
||||
count,
|
||||
has,
|
||||
collections,
|
||||
collectionsByService,
|
||||
collection,
|
||||
collectionsForService,
|
||||
clearService,
|
||||
clearAll,
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
create,
|
||||
update,
|
||||
delete: remove,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user