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

View File

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

View File

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

View File

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

View File

@ -1,35 +1,16 @@
<script lang="ts">
import cards from '$lib/stores/cards';
import currentView from '$lib/stores/currentView';
import projectTags from '$lib/stores/projectTags';
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';
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>
{#if project}
<section>
{#if view}
<Header {project} {view} />
{#if cards}
{#if $currentView}
<Header {project} />
{#if $cards}
<div class="grid">
{#if view.primary_tag_id}
{#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 {
subscribe,
set
set,
reload: () => update((v) => v)
};

View File

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

View File

@ -61,6 +61,43 @@ export default class View {
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) {
const id = await viewsApi.create(project);

View File

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