Header fixed

This commit is contained in:
Brieuc Dubois 2024-01-07 19:46:19 +01:00
parent 5e272a9bcc
commit e7b34fc1b4
9 changed files with 122 additions and 108 deletions

View File

@ -1,4 +1,4 @@
import type Card from '$lib/types/Card'; import Card from '$lib/types/Card';
import Project from '$lib/types/Project'; import Project from '$lib/types/Project';
import ProjectTag from '$lib/types/ProjectTag'; import ProjectTag from '$lib/types/ProjectTag';
import View from '$lib/types/View'; import View from '$lib/types/View';
@ -65,15 +65,15 @@ async function delete_(projectId: number): Promise<boolean> {
return true; return true;
} }
async function getCards(projectId: number): Promise<Card[]> { async function getCards(project: Project): Promise<Card[]> {
const response = await api.get(`/v1/projects/${projectId}/cards`); const response = await api.get(`/v1/projects/${project.id}/cards`);
if (response.status !== status.OK) { if (response.status !== status.OK) {
processError(response, 'Failed to fetch cards'); processError(response, 'Failed to fetch cards');
return Promise.reject(); return [];
} }
return parseCards(response.data); return Card.parseAll(response.data, project);
} }
async function getTags(project: Project): Promise<ProjectTag[]> { async function getTags(project: Project): Promise<ProjectTag[]> {
@ -95,9 +95,7 @@ async function getViews(project: Project): Promise<View[]> {
return []; return [];
} }
const views: View[] = View.parseAll(response.data, project); return View.parseAll(response.data, project);
return views;
} }
export default { export default {

View File

@ -1,27 +1,28 @@
<script lang="ts"> <script lang="ts">
import Menu from '$lib/components/menu/Menu.svelte'; import Menu from '$lib/components/menu/Menu.svelte';
import type ProjectTag from '$lib/types/ProjectTag';
export let isOpen = false; export let isOpen = false;
export let choices: { id: number; value: string }[] = []; export let choices: ProjectTag[] = [];
export let onChoice = (id: number) => {}; export let onChoice = (projectTag: ProjectTag) => {};
export let currentChoice: number | null; export let currentChoice: ProjectTag | null;
</script> </script>
<Menu bind:isOpen> <Menu bind:isOpen>
{#each choices as choice} {#each choices as choice}
<div <div
class="menu-item" class="menu-item"
on:click={() => onChoice(choice.id)} on:click={() => onChoice(choice)}
role="button" role="button"
tabindex="0" tabindex="0"
on:keypress={(e) => { on:keypress={(e) => {
if (e.key === 'Enter') { if (e.key === 'Enter') {
onChoice(choice.id); onChoice(choice);
} }
}} }}
> >
<span>{choice.value}</span> <span>{choice.title}</span>
{#if currentChoice === choice.id} {#if currentChoice === choice}
<span class="mark"></span> <span class="mark"></span>
{/if} {/if}
</div> </div>

View File

@ -1,10 +1,11 @@
<script lang="ts"> <script lang="ts">
import Menu from '$lib/components/menu/Menu.svelte'; import Menu from '$lib/components/menu/Menu.svelte';
import type ProjectTag from '$lib/types/ProjectTag';
export let isOpen = false; export let isOpen = false;
export let choices: { id: number; value: string }[] = []; export let choices: ProjectTag[] = [];
export let onChoice = (id: number) => {}; export let onChoice = (projectTag: ProjectTag) => {};
export let currentChoice: number | null; export let currentChoice: ProjectTag | null;
export let currentDirection: number | null; export let currentDirection: number | null;
</script> </script>
@ -12,17 +13,17 @@
{#each choices as choice} {#each choices as choice}
<div <div
class="menu-item" class="menu-item"
on:click={() => onChoice(choice.id)} on:click={() => onChoice(choice)}
role="button" role="button"
tabindex="0" tabindex="0"
on:keypress={(e) => { on:keypress={(e) => {
if (e.key === 'Enter') { if (e.key === 'Enter') {
onChoice(choice.id); onChoice(choice);
} }
}} }}
> >
<span>{choice.value}</span> <span>{choice.title}</span>
{#if currentChoice === choice.id} {#if currentChoice === choice}
<span class="mark"> <span class="mark">
{#if currentDirection === 1} {#if currentDirection === 1}

View File

@ -1,49 +1,35 @@
<script lang="ts"> <script lang="ts">
import GroupMenu from '$lib/components/menu/GroupMenu.svelte'; import GroupMenu from '$lib/components/menu/GroupMenu.svelte';
import SortMenu from '$lib/components/menu/SortMenu.svelte'; import SortMenu from '$lib/components/menu/SortMenu.svelte';
import cards from '$lib/stores/cards';
import currentView from '$lib/stores/currentView'; import currentView from '$lib/stores/currentView';
import projectTags from '$lib/stores/projectTags'; import Card from '$lib/types/Card';
import views from '$lib/stores/views';
import type Project from '$lib/types/Project'; import type Project from '$lib/types/Project';
import type View from '$lib/types/View'; import type ProjectTag from '$lib/types/ProjectTag';
import { projectTags } from '$lib/types/ProjectTag';
import { get } from 'svelte/store';
export let project: Project; export let project: Project;
export let view: View;
let groupMenuOpen = false; let groupMenuOpen = false;
let sortMenuOpen = false; let sortMenuOpen = false;
async function setGroup(id: number): Promise<boolean> { async function setGroup(projectTag: ProjectTag): Promise<boolean> {
if ($currentView == null) return false; const view = get(currentView);
if (!view) return false;
const view = { const res = await view.setPrimaryTag(projectTag);
...$currentView,
primary_tag_id: id
};
const res = await views.edit(view); if (res) currentView.reload();
if (res) currentView.set(view);
return res; return res;
} }
async function setSort(id: number): Promise<boolean> { async function setSort(projectTag: ProjectTag): Promise<boolean> {
if ($currentView == null) return false; const view = get(currentView);
if (!view) return false;
const view = { const res = await view.setSortTag(projectTag, view.sortDirection ? -view.sortDirection : 1);
...$currentView,
sort_tag_id: id,
sort_direction: $currentView.sort_direction
? $currentView.sort_tag_id === id
? -$currentView.sort_direction
: 1
: 1
};
const res = await views.edit(view); if (res) currentView.reload();
if (res) currentView.set(view);
return res; return res;
} }
@ -55,39 +41,36 @@
<div> <div>
<button <button
on:click={() => (groupMenuOpen = !groupMenuOpen)} on:click={() => (groupMenuOpen = !groupMenuOpen)}
class:defined={$currentView?.primary_tag_id}>Group</button class:defined={$currentView?.primaryTag}>Group</button
> >
<GroupMenu <GroupMenu
bind:isOpen={groupMenuOpen} bind:isOpen={groupMenuOpen}
choices={Object.values($projectTags).map((tag) => ({ id: tag.id, value: tag.title }))} choices={$projectTags}
onChoice={async (id) => { onChoice={async (projectTag) => {
if (!(await setGroup(id))) return; if (!(await setGroup(projectTag))) return;
groupMenuOpen = false; groupMenuOpen = false;
}} }}
currentChoice={view?.primary_tag_id} currentChoice={$currentView?.primaryTag || null}
/> />
</div> </div>
<button class:disabled={true}>Sub-group</button> <button class:disabled={true}>Sub-group</button>
<button class:disabled={true}>Filter</button> <button class:disabled={true}>Filter</button>
<div> <div>
<button <button on:click={() => (sortMenuOpen = !sortMenuOpen)} class:defined={$currentView?.sortTag}>
on:click={() => (sortMenuOpen = !sortMenuOpen)} Sort
class:defined={$currentView?.sort_tag_id}>Sort</button </button>
>
<SortMenu <SortMenu
bind:isOpen={sortMenuOpen} bind:isOpen={sortMenuOpen}
choices={Object.values($projectTags) choices={$projectTags}
.filter((tag) => tag.id !== view?.primary_tag_id) onChoice={async (projectTag) => {
.map((tag) => ({ id: tag.id, value: tag.title }))} if (!(await setSort(projectTag))) return;
onChoice={async (id) => {
if (!(await setSort(id))) return;
sortMenuOpen = false; sortMenuOpen = false;
}} }}
currentChoice={view?.sort_tag_id} currentChoice={$currentView?.sortTag || null}
currentDirection={view?.sort_direction} currentDirection={$currentView?.sortDirection || null}
/> />
</div> </div>
<button id="newButton" on:click={async () => cards.add(project.id, [])}>New</button> <button id="newButton" on:click={async () => Card.create(project)}>New</button>
</nav> </nav>
</header> </header>

View File

@ -1,35 +1,16 @@
<script lang="ts"> <script lang="ts">
import cards from '$lib/stores/cards';
import currentView from '$lib/stores/currentView'; import currentView from '$lib/stores/currentView';
import projectTags from '$lib/stores/projectTags';
import type Project from '$lib/types/Project'; import type Project from '$lib/types/Project';
import type View from '$lib/types/View';
import { onMount } from 'svelte';
import Column from './Column.svelte';
import Header from './Header.svelte'; import Header from './Header.svelte';
export let project: Project; export let project: Project;
let view: View | null = null;
onMount(async () => {
await cards.init(project.id);
if (!(await projectTags.init(project.id))) {
return;
}
currentView.subscribe((v) => {
view = v;
});
});
</script> </script>
{#if project} {#if project}
<section> <section>
{#if view} {#if $currentView}
<Header {project} {view} /> <Header {project} />
{#if cards} {#if $cards}
<div class="grid"> <div class="grid">
{#if view.primary_tag_id} {#if view.primary_tag_id}
{#each $projectTags[view.primary_tag_id].options as option} {#each $projectTags[view.primary_tag_id].options as option}

View File

@ -5,5 +5,6 @@ const { subscribe, set, update } = writable(null as View | null);
export default { export default {
subscribe, subscribe,
set set,
reload: () => update((v) => v)
}; };

View File

@ -1,25 +1,27 @@
import cardsApi from '$lib/api/cardsApi'; import cardsApi from '$lib/api/cardsApi';
import { get, writable } from 'svelte/store'; import { get, writable } from 'svelte/store';
import CardTag from './CardTag'; import CardTag from './CardTag';
import Project from './Project';
import { toastAlert } from '$lib/utils/toasts';
const cards = writable([] as Card[]); export const cards = writable([] as Card[]);
export default class Card { export default class Card {
private _id: number; private _id: number;
private _project_id: number; private _project: Project;
private _title: string; private _title: string;
private _content: string; private _content: string;
private _tags: CardTag[]; private _tags: CardTag[];
private constructor( private constructor(
id: number, id: number,
project_id: number, project: Project,
title: string, title: string,
content: string, content: string,
tags: CardTag[] tags: CardTag[]
) { ) {
this._id = id; this._id = id;
this._project_id = project_id; this._project = project;
this._title = title; this._title = title;
this._content = content; this._content = content;
this._tags = tags; this._tags = tags;
@ -29,8 +31,8 @@ export default class Card {
return this._id; return this._id;
} }
get project_id(): number { get project(): Project {
return this._project_id; return this._project;
} }
get title(): string { get title(): string {
@ -45,10 +47,6 @@ export default class Card {
return this._tags; return this._tags;
} }
static getAll(): Card[] {
return get(cards);
}
static fromId(id: number): Card | null { static fromId(id: number): Card | null {
for (const card of get(cards)) { for (const card of get(cards)) {
if (card.id === id) { if (card.id === id) {
@ -59,12 +57,12 @@ export default class Card {
return null; return null;
} }
static async create(project_id: number): Promise<Card | null> { static async create(project: Project): Promise<Card | null> {
const id = await cardsApi.create(project_id); const id = await cardsApi.create(project.id);
if (!id) return null; if (!id) return null;
const card = new Card(id, project_id, 'Untilted', '', []); const card = new Card(id, project, 'Untilted', '', []);
cards.update((cards) => [...cards, card]); cards.update((cards) => [...cards, card]);
@ -81,12 +79,21 @@ export default class Card {
return true; return true;
} }
static parse(json: any): Card | null { static parse(json: any): Card | null;
static parse(json: any, project: Project | null | undefined): Card | null;
static parse(json: any, project?: Project | null | undefined): Card | null {
if (json === null) { if (json === null) {
return null; return null;
} }
const card = new Card(json.id, json.project_id, json.title, json.content, []); if (!project) project = Project.fromId(json.project_id);
if (!project) {
toastAlert('Failed to parse card: project not found');
return null;
}
const card = new Card(json.id, project, json.title, json.content, []);
card._tags = CardTag.parseAll(json.tags, card); card._tags = CardTag.parseAll(json.tags, card);
@ -101,7 +108,10 @@ export default class Card {
return card; return card;
} }
static parseAll(json: any): Card[] { static parseAll(json: any): Card[];
static parseAll(json: any, project: Project | null): Card[];
static parseAll(json: any, project?: Project | null): Card[] {
if (json === null) { if (json === null) {
return []; return [];
} }
@ -109,7 +119,7 @@ export default class Card {
const cards: Card[] = []; const cards: Card[] = [];
for (const jsonCard of json) { for (const jsonCard of json) {
const card = this.parse(jsonCard); const card = this.parse(jsonCard, project);
if (!card) continue; if (!card) continue;
cards.push(card); cards.push(card);
} }

View File

@ -61,6 +61,43 @@ export default class View {
return this._sortDirection; return this._sortDirection;
} }
async setPrimaryTag(projectTag: ProjectTag): Promise<boolean> {
const response = await viewsApi.update(
this.id,
this.project.id,
projectTag.id,
this.secondaryTag?.id || null,
this.title,
this.sortTag?.id || null,
this.sortDirection || null
);
if (!response) return false;
this._pimaryTag = projectTag;
return true;
}
async setSortTag(projectTag: ProjectTag, direction: number): Promise<boolean> {
const response = await viewsApi.update(
this.id,
this.project.id,
this.primaryTag?.id || null,
this.secondaryTag?.id || null,
this.title,
projectTag.id,
direction
);
if (!response) return false;
this._sortTag = projectTag;
this._sortDirection = direction;
return true;
}
static async create(project: Project) { static async create(project: Project) {
const id = await viewsApi.create(project); const id = await viewsApi.create(project);

View File

@ -5,6 +5,7 @@
import type Project from '$lib/types/Project'; import type Project from '$lib/types/Project';
import { SvelteToast } from '@zerodevx/svelte-toast'; import { SvelteToast } from '@zerodevx/svelte-toast';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import ProjectComponent from '$lib/components/project/Project.svelte';
let projectId: number = +$page.params.project; let projectId: number = +$page.params.project;
@ -19,13 +20,14 @@
await projectsApi.getTags(project); await projectsApi.getTags(project);
await projectsApi.getViews(project); await projectsApi.getViews(project);
await projectsApi.getCards(project);
}); });
</script> </script>
{#if project} {#if project}
<div> <div>
<Sidebar {project} /> <Sidebar {project} />
<!-- <ProjectComponent {project} /> --> <ProjectComponent {project} />
</div> </div>
<SvelteToast /> <SvelteToast />
{/if} {/if}