Fix real-time sync for filters

This commit is contained in:
Brieuc Dubois 2024-01-24 01:05:20 +01:00
parent 77a0c33c18
commit 7770d3b6cc
2 changed files with 18 additions and 18 deletions

View File

@ -285,23 +285,27 @@ export default class View {
return views;
}
parseFilter(json: any) {
const filter = Filter.parse(json, this);
static parseFilter(json: any) {
const filter = Filter.parse(json);
if (!filter) return;
this._filters = [...this._filters, filter];
filter.view._filters = [...filter.view._filters, filter];
}
parseFilterUpdate(json: any) {
const filter = this._filters.find((f) => f.id === json.id);
static parseFilterUpdate(json: any) {
const view = get(views).find((v) => v._filters.map((f) => f.id).includes(json.id));
if (!view) return;
const filter = view._filters.find((f) => f.id === json.id);
if (!filter) return;
filter.parseUpdate(json);
}
parseFilterDelete(id: number) {
this._filters = this._filters.filter((f) => f.id !== id);
static parseFilterDelete(id: number) {
const view = get(views).find((v) => v._filters.map((f) => f.id).includes(id));
if (!view) return;
view._filters = view._filters.filter((f) => f.id !== id);
}
}

View File

@ -1,3 +1,4 @@
import currentView from '$lib/stores/currentView';
import Card, { cards } from '$lib/types/Card';
import Project, { projects } from '$lib/types/Project';
import ProjectTag, { projectTags } from '$lib/types/ProjectTag';
@ -241,22 +242,17 @@ function applyProjectTagOption(data: any) {
}
function applyFilter(data: any) {
const view = View.fromId(data.view_id);
if (!view) {
toastWarning('Failed to parse filter update: view not found');
return;
}
if (data.action === 'create') {
view.parseFilter(data.data);
View.parseFilter(data.data);
} else if (data.action === 'update') {
view.parseFilterUpdate(data.changes);
View.parseFilterUpdate(data.changes);
} else if (data.action === 'delete') {
view.parseFilterDelete(data.id);
View.parseFilterDelete(data.id);
} else {
toastWarning('Failed to parse filter update: unknown action');
return;
}
views.reload(view);
views.reload();
currentView.reload();
}