From d77b6041a6c411e994e85dd696b4e38a1b3a2024 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 18 Apr 2023 13:59:51 +0200 Subject: [PATCH] Fetch cobs by selected or default state Signed-off-by: Sebastian Martinez --- httpd-client/lib/project.ts | 12 +++ src/views/projects/Issues.svelte | 129 +++++++++++++++++--------- src/views/projects/Patches.svelte | 144 +++++++++++++++++++----------- src/views/projects/View.svelte | 37 +++----- 4 files changed, 200 insertions(+), 122 deletions(-) diff --git a/httpd-client/lib/project.ts b/httpd-client/lib/project.ts index 17f72f62d..e6ba38b93 100644 --- a/httpd-client/lib/project.ts +++ b/httpd-client/lib/project.ts @@ -370,12 +370,18 @@ export class Client { public async getAllIssues( id: string, + query?: { + page?: number; + perPage?: number; + state?: string; + }, options?: RequestOptions, ): Promise { return this.#fetcher.fetchOk( { method: "GET", path: `projects/${id}/issues`, + query, options, }, issuesSchema, @@ -441,12 +447,18 @@ export class Client { public async getAllPatches( id: string, + query?: { + page?: number; + perPage?: number; + state?: string; + }, options?: RequestOptions, ): Promise { return this.#fetcher.fetchOk( { method: "GET", path: `projects/${id}/patches`, + query, options, }, patchesSchema, diff --git a/src/views/projects/Issues.svelte b/src/views/projects/Issues.svelte index 720d22a35..759fd4407 100644 --- a/src/views/projects/Issues.svelte +++ b/src/views/projects/Issues.svelte @@ -12,48 +12,79 @@ import * as router from "@app/lib/router"; import * as utils from "@app/lib/utils"; import capitalize from "lodash/capitalize"; + import { HttpdClient } from "@httpd-client"; import { sessionStore } from "@app/lib/session"; import HeaderToggleLabel from "@app/views/projects/HeaderToggleLabel.svelte"; import IssueTeaser from "@app/views/projects/Issue/IssueTeaser.svelte"; import Placeholder from "@app/components/Placeholder.svelte"; import TabBar from "@app/components/TabBar.svelte"; + import Loading from "@app/components/Loading.svelte"; + import Button from "@app/components/Button.svelte"; - export let issues: Issue[]; - export let status: IssueStatus; + export let projectId: string; + export let state: IssueStatus; export let baseUrl: BaseUrl; export let issueCounters: { open: number; closed: number }; - let options: Tab[]; + const perPage = 10; - function groupIssues(issues: Issue[]): { - open: Issue[]; - closed: Issue[]; - } { - return issues.reduce( - (acc, issue) => { - acc[issue.state.status].push(issue); - return acc; - }, - { open: [] as Issue[], closed: [] as Issue[] }, - ); + // Keeping it true, to avoid an initial flash + // of EmptyState Placeholder + let refresh = true; + let loading = false; + let page = 0; + let error: any; + let issues: Issue[] = []; + + const api = new HttpdClient(baseUrl); + + async function loadIssues(): Promise { + loading = true; + try { + const response = await api.project.getAllIssues(projectId, { + state, + page, + perPage, + }); + issues = [...issues, ...response]; + page += 1; + } catch (e) { + error = e; + } finally { + loading = false; + refresh = false; + } + } + + function switchState(e: CustomEvent): void { + refresh = true; + // Update state to be used in the query + state = e.detail; + // Reset page to 0 to load the first page for a new state + page = 0; + // Remove all existing patches with old state + issues = []; + loadIssues(); + router.updateProjectRoute({ + search: `state=${state}`, + }); } const stateOptions: IssueStatus[] = ["open", "closed"]; - $: options = stateOptions.map<{ - value: IssueStatus; - title: string; - disabled: boolean; - }>((s: IssueStatus) => ({ + const options = stateOptions.map>(s => ({ value: s, title: `${issueCounters[s]} ${s}`, disabled: issueCounters[s] === 0, })); - $: filteredIssues = groupIssues(issues)[status]; - $: sortedIssues = filteredIssues.sort( - ({ discussion: t1 }, { discussion: t2 }) => - t2[0].timestamp - t1[0].timestamp, - ); + + $: showMoreButton = + !loading && + !error && + issueCounters[state] && + issues.length < issueCounters[state]; + + loadIssues();