From 5e272a9bcc8968193819d300b40e386d75e57979 Mon Sep 17 00:00:00 2001 From: Bhasher Date: Sun, 7 Jan 2024 01:47:02 +0100 Subject: [PATCH] Sidebar fixed --- frontend/src/lib/api/projectsApi.ts | 23 +++-- frontend/src/lib/api/viewsApi.ts | 30 ++++--- .../src/lib/components/project/Sidebar.svelte | 84 +++++++++---------- frontend/src/lib/types/Card.ts | 8 +- frontend/src/lib/types/Project.ts | 6 +- frontend/src/lib/types/ProjectTag.ts | 17 ++-- frontend/src/lib/types/View.ts | 71 ++++++++++++++-- frontend/src/routes/[project]/+page.svelte | 18 ++-- 8 files changed, 174 insertions(+), 83 deletions(-) diff --git a/frontend/src/lib/api/projectsApi.ts b/frontend/src/lib/api/projectsApi.ts index 6d15f05..49de037 100644 --- a/frontend/src/lib/api/projectsApi.ts +++ b/frontend/src/lib/api/projectsApi.ts @@ -1,6 +1,7 @@ import type Card from '$lib/types/Card'; import Project from '$lib/types/Project'; import ProjectTag from '$lib/types/ProjectTag'; +import View from '$lib/types/View'; import api, { processError } from '$lib/utils/api'; import { parseCards } from '$lib/utils/parser'; import status from '$lib/utils/status'; @@ -29,7 +30,7 @@ async function create(title: string): Promise { return response.data.id; } -async function get(projectId: number): Promise { +async function get(projectId: number): Promise { const response = await api.get(`/v1/projects/${projectId}`); if (response.status !== status.OK) { @@ -37,7 +38,7 @@ async function get(projectId: number): Promise { return null; } - return response.data; + return Project.parse(response.data); } async function update(projectId: number, title: string): Promise { @@ -83,9 +84,20 @@ async function getTags(project: Project): Promise { return []; } - const projectTags: ProjectTag[] = ProjectTag.parseAll(response.data, project); + return ProjectTag.parseAll(response.data, project); +} - return projectTags; +async function getViews(project: Project): Promise { + const response = await api.get(`/v1/projects/${project.id}/views`); + + if (response.status !== status.OK) { + processError(response, 'Failed to fetch views'); + return []; + } + + const views: View[] = View.parseAll(response.data, project); + + return views; } export default { @@ -95,5 +107,6 @@ export default { delete: delete_, getAll, getCards, - getTags + getTags, + getViews }; diff --git a/frontend/src/lib/api/viewsApi.ts b/frontend/src/lib/api/viewsApi.ts index 518f299..0b0b359 100644 --- a/frontend/src/lib/api/viewsApi.ts +++ b/frontend/src/lib/api/viewsApi.ts @@ -4,7 +4,7 @@ import api, { processError } from '$lib/utils/api'; import status from '$lib/utils/status'; async function create(project: Project): Promise { - const response = await api.post('/views', { + const response = await api.post('/v1/views', { project: project.id }); @@ -16,17 +16,25 @@ async function create(project: Project): Promise { return response.data.id; } -async function update(view: View): Promise { - const response = await api.put(`/views/${view.id}`, { - project: view.project.id, - primary_tag_id: view.primaryTag?.id, - secondary_tag_id: view.secondaryTag?.id, - title: view.title, - sort_tag_id: view.sortTag?.id, - sort_direction: view.sortDirection +async function update( + id: number, + projectId: number, + primaryTagId: number | null, + secondaryTagId: number | null, + title: string, + sortTagId: number | null, + sortDirection: number | null +): Promise { + const response = await api.put(`/v1/views/${id}`, { + project_id: projectId, + primary_tag_id: primaryTagId, + secondary_tag_id: secondaryTagId, + title: title, + sort_tag_id: sortTagId, + sort_direction: sortDirection }); - if (response.status !== status.OK) { + if (response.status !== status.NoContent) { processError(response, 'Failed to update view'); return false; } @@ -35,7 +43,7 @@ async function update(view: View): Promise { } async function delete_(viewId: number): Promise { - const response = await api.delete(`/views/${viewId}`); + const response = await api.delete(`/v1/views/${viewId}`); if (response.status !== status.OK) { processError(response, 'Failed to delete view'); diff --git a/frontend/src/lib/components/project/Sidebar.svelte b/frontend/src/lib/components/project/Sidebar.svelte index 68ab2c5..2af1e89 100644 --- a/frontend/src/lib/components/project/Sidebar.svelte +++ b/frontend/src/lib/components/project/Sidebar.svelte @@ -1,52 +1,44 @@ @@ -60,7 +52,7 @@

{project.title}

{#if views}
    - {#each get(views) as view} + {#each $views as view}
  • currentView.set(view)} @@ -74,26 +66,29 @@ class:active={$currentView === view} > - saveView(view)} - id="viewTitle-{view.id}" - on:keydown={(e) => { - if (e.key === 'Enter') { - e.currentTarget.blur(); - } - }} - /> + {#if viewEdit && viewEdit === view} + { + if (e.key === 'Enter') { + e.currentTarget.blur(); + } + }} + /> + {:else} + {view.title} + {/if}