Initial commit
This commit is contained in:
169
src/stores/collectionsStore.ts
Normal file
169
src/stores/collectionsStore.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { collectionService } from '../services'
|
||||
import type { CollectionInterface, CollectionCreateRequest } from '../types'
|
||||
import { CollectionObject, CollectionPropertiesObject } from '../models/collection'
|
||||
|
||||
export const useCollectionsStore = defineStore('mail-collections', {
|
||||
state: () => ({
|
||||
collections: {} as Record<string, Record<string, Record<string, CollectionObject>>>,
|
||||
loading: false,
|
||||
error: null as string | null,
|
||||
}),
|
||||
|
||||
actions: {
|
||||
async loadCollections(sources?: any) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await collectionService.list({ sources })
|
||||
|
||||
// Response is already in nested object format: provider -> service -> collection
|
||||
// Transform to CollectionObject instances
|
||||
const transformed: Record<string, Record<string, Record<string, CollectionObject>>> = {}
|
||||
|
||||
for (const [providerId, providerData] of Object.entries(response)) {
|
||||
transformed[providerId] = {}
|
||||
|
||||
for (const [serviceId, collections] of Object.entries(providerData as any)) {
|
||||
transformed[providerId][serviceId] = {}
|
||||
|
||||
// Collections come as an object keyed by identifier
|
||||
for (const [collectionId, collection] of Object.entries(collections as any)) {
|
||||
// Create CollectionObject instance with provider and service set
|
||||
const collectionData = {
|
||||
...collection,
|
||||
provider: providerId,
|
||||
service: serviceId,
|
||||
} as CollectionInterface
|
||||
|
||||
transformed[providerId][serviceId][collectionId] = new CollectionObject().fromJson(collectionData)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.collections = transformed
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
async getCollection(provider: string, service: string | number, collectionId: string | number) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await collectionService.fetch({
|
||||
provider,
|
||||
service,
|
||||
collection: collectionId
|
||||
})
|
||||
|
||||
// Create CollectionObject instance
|
||||
const collectionObject = new CollectionObject().fromJson(response)
|
||||
|
||||
// Update in store
|
||||
if (!this.collections[provider]) {
|
||||
this.collections[provider] = {}
|
||||
}
|
||||
if (!this.collections[provider][String(service)]) {
|
||||
this.collections[provider][String(service)] = {}
|
||||
}
|
||||
this.collections[provider][String(service)][String(collectionId)] = collectionObject
|
||||
|
||||
return collectionObject
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
async createCollection(params: {
|
||||
provider: string
|
||||
service: string | number
|
||||
collection?: string | number | null
|
||||
properties: CollectionPropertiesObject
|
||||
}): Promise<CollectionObject> {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
// Prepare request data from CollectionPropertiesObject
|
||||
const requestData: CollectionCreateRequest = {
|
||||
provider: params.provider,
|
||||
service: params.service,
|
||||
collection: params.collection ?? null,
|
||||
properties: {
|
||||
'@type': 'mail.collection',
|
||||
label: params.properties.label,
|
||||
role: params.properties.role ?? null,
|
||||
rank: params.properties.rank ?? 0,
|
||||
subscribed: params.properties.subscribed ?? true,
|
||||
},
|
||||
}
|
||||
|
||||
// Call service to create collection
|
||||
const response = await collectionService.create(requestData)
|
||||
|
||||
// Create CollectionObject instance
|
||||
const collectionObject = new CollectionObject().fromJson(response)
|
||||
|
||||
// Update store with new collection
|
||||
const provider = response.provider
|
||||
const service = String(response.service)
|
||||
const identifier = String(response.identifier)
|
||||
|
||||
if (!this.collections[provider]) {
|
||||
this.collections[provider] = {}
|
||||
}
|
||||
if (!this.collections[provider][service]) {
|
||||
this.collections[provider][service] = {}
|
||||
}
|
||||
|
||||
this.collections[provider][service][identifier] = collectionObject
|
||||
|
||||
return collectionObject
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
getters: {
|
||||
collectionList: (state) => {
|
||||
const list: CollectionObject[] = []
|
||||
Object.values(state.collections).forEach(providerCollections => {
|
||||
Object.values(providerCollections).forEach(serviceCollections => {
|
||||
Object.values(serviceCollections).forEach(collection => {
|
||||
list.push(collection)
|
||||
})
|
||||
})
|
||||
})
|
||||
return list
|
||||
},
|
||||
|
||||
collectionCount: (state) => {
|
||||
let count = 0
|
||||
Object.values(state.collections).forEach(providerCollections => {
|
||||
Object.values(providerCollections).forEach(serviceCollections => {
|
||||
count += Object.keys(serviceCollections).length
|
||||
})
|
||||
})
|
||||
return count
|
||||
},
|
||||
|
||||
hasCollections: (state) => {
|
||||
return Object.values(state.collections).some(providerCollections =>
|
||||
Object.values(providerCollections).some(serviceCollections =>
|
||||
Object.keys(serviceCollections).length > 0
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user