From d04697ae6335f934d59b1343de19c0b1c2a53eca Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sat, 19 Aug 2023 11:19:00 -0700 Subject: [PATCH] albums: auto select new album in picker (fix #770) Signed-off-by: Varun Patil --- src/components/modal/AlbumPicker.vue | 31 +++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/components/modal/AlbumPicker.vue b/src/components/modal/AlbumPicker.vue index 4903e35e..862dff83 100644 --- a/src/components/modal/AlbumPicker.vue +++ b/src/components/modal/AlbumPicker.vue @@ -119,15 +119,26 @@ export default defineComponent({ }, methods: { - albumCreatedHandler() { + async albumCreatedHandler({ album }: { album: { basename: string } }) { this.showAlbumCreationForm = false; - this.loadAlbums(); + await this.loadAlbums(true); + + // select the newly created album + const newAlbum = this.albums.find((a) => a.name === album.basename); + if (newAlbum) { + this.selection.add(newAlbum); + this.forceUpdate(); + } }, - async loadAlbums() { + async loadAlbums(preserveSelection: boolean = false) { try { this.loadingAlbums = true; + // FIXME: preserve deselection too; but then this is only + // applicable for single photo selection ... at least for now + const prevSel = new Set(Array.from(this.selection).map((a) => a.album_id)); + // get all albums this.albums = await dav.getAlbums(); @@ -143,10 +154,16 @@ export default defineComponent({ this.initSelection = new Set(this.albums.filter((a) => selIds.has(a.album_id))); this.selection = new Set(this.initSelection); } + + // restore selection + if (preserveSelection) { + this.albums.filter((a) => prevSel.has(a.album_id)).forEach(this.selection.add, this.selection); + } } catch (e) { console.error(e); } finally { this.loadingAlbums = false; + this.forceUpdate(); } }, @@ -165,13 +182,17 @@ export default defineComponent({ this.deselection.delete(album); } - this.$forceUpdate(); // sets do not trigger reactivity - (this.$refs.albumsList)?.$forceUpdate(); + this.forceUpdate(); }, submit() { this.$emit('select', Array.from(this.selection), Array.from(this.deselection)); }, + + forceUpdate() { + this.$forceUpdate(); // sets do not trigger reactivity + (this.$refs.albumsList)?.$forceUpdate(); + }, }, });