feat: collection move
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -4,9 +4,9 @@
|
||||
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { collectionService } from '../services'
|
||||
import { collectionService, entityService } from '../services'
|
||||
import { CollectionObject, CollectionPropertiesObject } from '../models/collection'
|
||||
import type { SourceSelector, ListFilter, ListSort } from '../types'
|
||||
import type { SourceSelector, ListFilter, ListSort, CollectionIdentifier, CollectionMoveResponse } from '../types'
|
||||
|
||||
export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
|
||||
const ROOT_IDENTIFIER = '__root__'
|
||||
@@ -425,6 +425,51 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move collections to another target collection.
|
||||
*
|
||||
* Updates local store keys for successfully moved collections when they are
|
||||
* already present in cache.
|
||||
*
|
||||
* @param target - target collection identifier
|
||||
* @param source - source collection identifier
|
||||
*
|
||||
* @returns Promise with move results keyed by source identifier
|
||||
*/
|
||||
async function move(target: CollectionIdentifier, source: CollectionIdentifier): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.move({ target, source })
|
||||
|
||||
if (!(response instanceof CollectionObject)) {
|
||||
console.warn('[Mail Manager][Store] - Move failed. Received unexpected response from move operation:', response)
|
||||
throw new Error('Failed to move collection: unexpected response from move operation')
|
||||
}
|
||||
|
||||
const sourceCollection = _collections.value[source]
|
||||
|
||||
if (sourceCollection) {
|
||||
deindexCollection(sourceCollection)
|
||||
}
|
||||
|
||||
delete _collections.value[source]
|
||||
|
||||
const movedCollection = response
|
||||
const movedKey = identifierKey(movedCollection.provider, movedCollection.service, movedCollection.identifier)
|
||||
|
||||
_collections.value[movedKey] = movedCollection
|
||||
indexCollection(movedCollection)
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully moved collection:', source, ' to ', movedKey)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to move collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// Return public API
|
||||
return {
|
||||
// State (readonly)
|
||||
@@ -445,5 +490,6 @@ export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
|
||||
create,
|
||||
update,
|
||||
delete: remove,
|
||||
move,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user