Fix real-time sync for projectTag & project_id in ProjectTag

This commit is contained in:
Brieuc Dubois 2024-01-24 01:42:42 +01:00
parent cc7862b0d4
commit 5eb4ab53c6
2 changed files with 31 additions and 11 deletions

View File

@ -1,6 +1,7 @@
// import type TagOption from './TagOption'; // import type TagOption from './TagOption';
import projectTagsApi from '$lib/api/projectTagsApi'; import projectTagsApi from '$lib/api/projectTagsApi';
import { toastAlert } from '$lib/utils/toasts';
import { get, writable } from 'svelte/store'; import { get, writable } from 'svelte/store';
import Project from './Project'; import Project from './Project';
import TagOption from './TagOption'; import TagOption from './TagOption';
@ -69,11 +70,10 @@ export default class ProjectTag {
} }
static async create(project: Project, title: string, type: number): Promise<ProjectTag | null> { static async create(project: Project, title: string, type: number): Promise<ProjectTag | null> {
const response = await projectTagsApi.create(project.id, title, type); const id = await projectTagsApi.create(project.id, title, type);
if (!id) return null;
if (!response) return null; const projectTag = new ProjectTag(id, project, title, type, []);
const projectTag = new ProjectTag(0, project, title, type, []);
projectTags.update((projectTags) => [...projectTags, projectTag]); projectTags.update((projectTags) => [...projectTags, projectTag]);
@ -81,7 +81,14 @@ export default class ProjectTag {
} }
async delete(): Promise<boolean> { async delete(): Promise<boolean> {
return await projectTagsApi.delete(this.id); const res = await projectTagsApi.delete(this.id);
if (!res) return false;
projectTags.update((projectTags) =>
projectTags.filter((projectTag) => projectTag.id !== this.id)
);
return true;
} }
async update(title: string, type: number): Promise<boolean> { async update(title: string, type: number): Promise<boolean> {
@ -112,12 +119,18 @@ export default class ProjectTag {
static parse(json: any, project: Project | null | undefined): ProjectTag | null; static parse(json: any, project: Project | null | undefined): ProjectTag | null;
static parse(json: any, project?: Project | null): ProjectTag | null { static parse(json: any, project?: Project | null): ProjectTag | null {
if (!json) return null; if (!json) {
toastAlert('Failed to parse ProjectTag');
return null;
}
if (!project) project = Project.fromId(json.project); if (!project) project = Project.fromId(json.project_id);
if (!project) return null; if (!project) {
toastAlert('Failed to parse ProjectTag: project not found');
return null;
}
const projectTag = new ProjectTag(json.id, json.project, json.title, json.type, []); const projectTag = new ProjectTag(json.id, project, json.title, json.type, []);
const options = TagOption.parseAll(json.options, projectTag); const options = TagOption.parseAll(json.options, projectTag);

View File

@ -1,4 +1,5 @@
import tagsOptions from '$lib/api/tagsOptions'; import tagsOptions from '$lib/api/tagsOptions';
import { toastAlert } from '$lib/utils/toasts';
import ProjectTag from './ProjectTag'; import ProjectTag from './ProjectTag';
export default class TagOption { export default class TagOption {
@ -50,10 +51,16 @@ export default class TagOption {
static parse(json: any, projectTag: ProjectTag | null | undefined): TagOption | null; static parse(json: any, projectTag: ProjectTag | null | undefined): TagOption | null;
static parse(json: any, projectTag?: ProjectTag | null | undefined): TagOption | null { static parse(json: any, projectTag?: ProjectTag | null | undefined): TagOption | null {
if (!json) return null; if (!json) {
toastAlert('Failed to parse TagOption');
return null;
}
if (!projectTag) projectTag = ProjectTag.fromId(json.tag_id); if (!projectTag) projectTag = ProjectTag.fromId(json.tag_id);
if (!projectTag) return null; if (!projectTag) {
toastAlert('Failed to parse TagOption: ProjectTag not found');
return null;
}
return new TagOption(json.id, projectTag, json.value); return new TagOption(json.id, projectTag, json.value);
} }