refactor: selection manager loading

old-stable24
Varun Patil 2022-10-18 17:01:04 -07:00
parent d73242badc
commit 789186160d
2 changed files with 40 additions and 54 deletions

View File

@ -17,7 +17,7 @@
<NcActions :inline="1"> <NcActions :inline="1">
<NcActionButton v-for="action of getActions()" :key="action.name" <NcActionButton v-for="action of getActions()" :key="action.name"
:aria-label="action.name" :aria-label="action.name"
@click="action.callback(selection)"> @click="click(action)">
{{ action.name }} {{ action.name }}
<template #icon> <component :is="action.icon" :size="20" /> </template> <template #icon> <component :is="action.icon" :size="20" /> </template>
</NcActionButton> </NcActionButton>
@ -127,6 +127,18 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
]; ];
} }
/** Click on an action */
private async click(action: ISelectionAction) {
try {
this.updateLoading(1);
await action.callback(this.selection);
} catch (error) {
console.error(error);
} finally {
this.updateLoading(-1);
}
}
/** Get the actions list */ /** Get the actions list */
private getActions(): ISelectionAction[] { private getActions(): ISelectionAction[] {
return this.defaultActions.filter(a => !a.if || a.if()); return this.defaultActions.filter(a => !a.if || a.if());
@ -221,27 +233,22 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
* Favorite the currently selected photos * Favorite the currently selected photos
*/ */
private async favoriteSelection(selection: Selection) { private async favoriteSelection(selection: Selection) {
try { const val = !this.allSelectedFavorites(selection);
const val = !this.allSelectedFavorites(selection); for await (const favIds of dav.favoriteFilesByIds(Array.from(selection.keys()), val)) {
this.updateLoading(1); favIds.forEach(id => {
for await (const favIds of dav.favoriteFilesByIds(Array.from(selection.keys()), val)) { const photo = selection.get(id);
favIds.forEach(id => { if (!photo) {
const photo = selection.get(id); return;
if (!photo) { }
return;
}
if (val) { if (val) {
photo.flag |= this.c.FLAG_IS_FAVORITE; photo.flag |= this.c.FLAG_IS_FAVORITE;
} else { } else {
photo.flag &= ~this.c.FLAG_IS_FAVORITE; photo.flag &= ~this.c.FLAG_IS_FAVORITE;
} }
}); });
}
this.clearSelection();
} finally {
this.updateLoading(-1);
} }
this.clearSelection();
} }
/** /**
@ -254,16 +261,9 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
} }
} }
try { for await (const delIds of dav.deleteFilesByIds(Array.from(selection.keys()))) {
this.updateLoading(1); const delPhotos = delIds.map(id => selection.get(id));
for await (const delIds of dav.deleteFilesByIds(Array.from(selection.keys()))) { this.delete(delPhotos);
const delPhotos = delIds.map(id => selection.get(id));
this.delete(delPhotos);
}
} catch (error) {
console.error(error);
} finally {
this.updateLoading(-1);
} }
} }
@ -301,20 +301,13 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
} }
} }
try { for await (let delIds of dav.archiveFilesByIds(Array.from(selection.keys()), !this.routeIsArchive())) {
this.updateLoading(1); delIds = delIds.filter(x => x);
for await (let delIds of dav.archiveFilesByIds(Array.from(selection.keys()), !this.routeIsArchive())) { if (delIds.length === 0) {
delIds = delIds.filter(x => x); continue
if (delIds.length === 0) {
continue
}
const delPhotos = delIds.map(id => selection.get(id));
this.delete(delPhotos);
} }
} catch (error) { const delPhotos = delIds.map(id => selection.get(id));
console.error(error); this.delete(delPhotos);
} finally {
this.updateLoading(-1);
} }
} }
@ -339,16 +332,9 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
} }
// Run query // Run query
try { for await (let delIds of dav.removeFaceImages(user, name, Array.from(selection.keys()))) {
this.updateLoading(1); const delPhotos = delIds.filter(x => x).map(id => selection.get(id));
for await (let delIds of dav.removeFaceImages(user, name, Array.from(selection.keys()))) { this.delete(delPhotos);
const delPhotos = delIds.filter(x => x).map(id => selection.get(id));
this.delete(delPhotos);
}
} catch (error) {
console.error(error);
} finally {
this.updateLoading(-1);
} }
} }
} }

View File

@ -171,7 +171,7 @@ export type ISelectionAction = {
/** Icon component */ /** Icon component */
icon: VueConstructor; icon: VueConstructor;
/** Action to perform */ /** Action to perform */
callback: (selection: Map<number, IPhoto>) => void; callback: (selection: Map<number, IPhoto>) => Promise<void>;
/** Condition to check for including */ /** Condition to check for including */
if?: () => boolean; if?: () => boolean;
} }