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,9 +233,7 @@ 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);
this.updateLoading(1);
for await (const favIds of dav.favoriteFilesByIds(Array.from(selection.keys()), val)) { for await (const favIds of dav.favoriteFilesByIds(Array.from(selection.keys()), val)) {
favIds.forEach(id => { favIds.forEach(id => {
const photo = selection.get(id); const photo = selection.get(id);
@ -239,9 +249,6 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
}); });
} }
this.clearSelection(); this.clearSelection();
} finally {
this.updateLoading(-1);
}
} }
/** /**
@ -254,17 +261,10 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
} }
} }
try {
this.updateLoading(1);
for await (const delIds of dav.deleteFilesByIds(Array.from(selection.keys()))) { for await (const delIds of dav.deleteFilesByIds(Array.from(selection.keys()))) {
const delPhotos = delIds.map(id => selection.get(id)); const delPhotos = delIds.map(id => selection.get(id));
this.delete(delPhotos); this.delete(delPhotos);
} }
} catch (error) {
console.error(error);
} finally {
this.updateLoading(-1);
}
} }
/** /**
@ -301,8 +301,6 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
} }
} }
try {
this.updateLoading(1);
for await (let delIds of dav.archiveFilesByIds(Array.from(selection.keys()), !this.routeIsArchive())) { for await (let delIds of dav.archiveFilesByIds(Array.from(selection.keys()), !this.routeIsArchive())) {
delIds = delIds.filter(x => x); delIds = delIds.filter(x => x);
if (delIds.length === 0) { if (delIds.length === 0) {
@ -311,11 +309,6 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
const delPhotos = delIds.map(id => selection.get(id)); const delPhotos = delIds.map(id => selection.get(id));
this.delete(delPhotos); this.delete(delPhotos);
} }
} catch (error) {
console.error(error);
} finally {
this.updateLoading(-1);
}
} }
/** Archive is not allowed only on folder routes */ /** Archive is not allowed only on folder routes */
@ -339,17 +332,10 @@ export default class SelectionHandler extends Mixins(GlobalMixin) {
} }
// Run query // Run query
try {
this.updateLoading(1);
for await (let delIds of dav.removeFaceImages(user, name, Array.from(selection.keys()))) { for await (let delIds of dav.removeFaceImages(user, name, Array.from(selection.keys()))) {
const delPhotos = delIds.filter(x => x).map(id => selection.get(id)); const delPhotos = delIds.filter(x => x).map(id => selection.get(id));
this.delete(delPhotos); this.delete(delPhotos);
} }
} catch (error) {
console.error(error);
} finally {
this.updateLoading(-1);
}
} }
} }
</script> </script>

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;
} }