refactor(modal): reduce watchers

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/888/head
Varun Patil 2023-10-23 19:41:24 -07:00
parent 1439160b50
commit 182caed840
5 changed files with 63 additions and 93 deletions

View File

@ -70,25 +70,25 @@ export default defineComponent({
}, },
mounted() { mounted() {
this.routeChange(); this.refresh();
}, },
created() { created() {
utils.bus.on('memories:user-config-changed', this.routeChange); utils.bus.on('memories:user-config-changed', this.refresh);
}, },
beforeDestroy() { beforeDestroy() {
utils.bus.off('memories:user-config-changed', this.routeChange); utils.bus.off('memories:user-config-changed', this.refresh);
}, },
watch: { watch: {
async $route() { async $route() {
this.routeChange(); this.refresh();
}, },
}, },
methods: { methods: {
async routeChange() { async refresh() {
try { try {
this.items = []; this.items = [];
this.loading++; this.loading++;

View File

@ -43,7 +43,7 @@ export default defineComponent({
}, },
watch: { watch: {
src(newSrc, oldSrc) { src() {
this.loadImage(); this.loadImage();
}, },
}, },

View File

@ -39,17 +39,15 @@ export default defineComponent({
data: () => ({ data: () => ({
show: false, show: false,
user: '',
name: '',
}), }),
mounted() { computed: {
this.refreshParams(); name() {
}, return this.$route.params.name;
},
watch: { user() {
$route() { return this.$route.params.user;
this.refreshParams();
}, },
}, },
@ -59,21 +57,12 @@ export default defineComponent({
}, },
open() { open() {
const user = this.$route.params.user || ''; if (this.user !== utils.uid) {
if (this.$route.params.user !== utils.uid) { showError(this.t('memories', 'Only user "{user}" can delete this person', { user: this.user }));
showError(
this.t('memories', 'Only user "{user}" can delete this person', {
user,
}),
);
return; return;
} }
this.show = true;
},
refreshParams() { this.show = true;
this.user = this.$route.params.user;
this.name = this.$route.params.name;
}, },
async save() { async save() {
@ -83,15 +72,11 @@ export default defineComponent({
} else { } else {
await dav.faceRecognitionSetPersonVisibility(this.name, false); await dav.faceRecognitionSetPersonVisibility(this.name, false);
} }
this.$router.push({ name: this.$route.name as string }); this.$router.push({ name: this.$route.name as string }); // "recognize" or "facerecognition"
this.close(); this.close();
} catch (error) { } catch (error) {
console.log(error); console.log(error);
showError( showError(this.t('photos', 'Failed to delete {name}.', { name: this.name }));
this.t('photos', 'Failed to delete {name}.', {
name: this.name,
}),
);
} }
}, },
}, },

View File

@ -8,7 +8,7 @@
<NcTextField <NcTextField
class="field" class="field"
:autofocus="true" :autofocus="true"
:value.sync="name" :value.sync="input"
:label="t('memories', 'Name')" :label="t('memories', 'Name')"
:label-visible="false" :label-visible="false"
:placeholder="t('memories', 'Name')" :placeholder="t('memories', 'Name')"
@ -49,24 +49,20 @@ export default defineComponent({
data: () => ({ data: () => ({
show: false, show: false,
user: '', input: '',
name: '',
oldName: '',
}), }),
mounted() {
this.refreshParams();
},
watch: {
$route() {
this.refreshParams();
},
},
computed: { computed: {
name() {
return this.$route.params.name;
},
user() {
return this.$route.params.user;
},
canSave() { canSave() {
return this.name !== this.oldName && this.name !== '' && isNaN(Number(this.name)); return this.input && this.name !== this.input && isNaN(Number(this.input));
}, },
}, },
@ -76,49 +72,37 @@ export default defineComponent({
}, },
open() { open() {
const user = this.$route.params.user || ''; if (this.user !== utils.uid) {
if (this.$route.params.user !== utils.uid) { showError(this.t('memories', 'Only user "{user}" can update this person', { user: this.user }));
showError(
this.t('memories', 'Only user "{user}" can update this person', {
user,
}),
);
return; return;
} }
this.input = isNaN(Number(this.name)) ? this.name : String();
this.show = true; this.show = true;
}, },
refreshParams() {
this.user = String(this.$route.params.user);
this.name = String(this.$route.params.name);
this.oldName = this.name;
// if name is number then it is blank
if (!isNaN(Number(this.name))) {
this.name = String();
}
},
async save() { async save() {
if (!this.canSave) return; if (!this.canSave) return;
try { try {
if (this.routeIsRecognize) { if (this.routeIsRecognize) {
await dav.recognizeRenameFace(this.user, this.oldName, this.name); await dav.recognizeRenameFace(this.user, this.name, this.input);
} else { } else {
await dav.faceRecognitionRenamePerson(this.oldName, this.name); await dav.faceRecognitionRenamePerson(this.name, this.input);
} }
this.$router.replace({ this.$router.replace({
name: this.$route.name as string, name: this.$route.name as string,
params: { user: this.user, name: this.name }, params: { user: this.user, name: this.input },
}); });
this.close(); this.close();
} catch (error) { } catch (error) {
console.log(error); console.log(error);
showError( showError(
this.t('photos', 'Failed to rename {oldName} to {name}.', { this.t('photos', 'Failed to rename {oldName} to {name}.', {
oldName: this.oldName, oldName: this.name,
name: this.name, name: this.input,
}), }),
); );
} }

View File

@ -59,24 +59,28 @@ export default defineComponent({
}, },
data: () => ({ data: () => ({
user: String(),
name: String(),
list: null as ICluster[] | null, list: null as ICluster[] | null,
fuse: null as Fuse<ICluster> | null, fuse: null as Fuse<ICluster> | null,
search: String(), search: String(),
}), }),
watch: {
$route() {
this.refreshParams();
},
},
mounted() { mounted() {
this.refreshParams(); this.refresh();
}, },
computed: { computed: {
user() {
return this.$route.params.user;
},
name() {
return this.$route.params.name;
},
backend() {
return this.$route.name as 'recognize' | 'facerecognition';
},
filteredList() { filteredList() {
if (!this.list || !this.search || !this.fuse) return this.list || []; if (!this.list || !this.search || !this.fuse) return this.list || [];
return this.fuse.search(this.search).map((r) => r.item); return this.fuse.search(this.search).map((r) => r.item);
@ -84,23 +88,20 @@ export default defineComponent({
}, },
methods: { methods: {
async refreshParams() { async refresh() {
this.user = this.$route.params.user; try {
this.name = this.$route.params.name; this.list = null;
this.list = null; const faces = await dav.getFaceList(this.backend);
this.search = ''; this.list = faces.filter((c: IFace) => c.user_id === this.user && String(c.name || c.cluster_id) !== this.name);
this.fuse = new Fuse(this.list, { keys: ['name'] });
const backend = this.routeIsRecognize ? 'recognize' : this.routeIsFaceRecognition ? 'facerecognition' : null; } catch (e) {
console.assert(backend, '[BUG] Invalid route for FaceList'); showError(this.t('memories', 'Failed to load faces'));
console.error(e);
const faces = await dav.getFaceList(backend!); }
this.list = faces.filter((c: IFace) => c.user_id === this.user && String(c.name || c.cluster_id) !== this.name);
this.fuse = new Fuse(this.list, { keys: ['name'] });
}, },
async addFace() { async addFace() {
let name: string = String(); let name = String();
try { try {
// TODO: use a proper dialog // TODO: use a proper dialog