From 8fab2d03bce82e4dee798ebffb1e93c557f62a4b Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Thu, 7 Mar 2024 23:16:22 -0500 Subject: feat: Update authentication route and add comments to exported members - The authentication route in the API has been updated to use a new router setup function. - Comments have been added to all exported members of the `auth.go` module in the internal controllers package. --- fe/src/http.ts | 60 ++++++++++++++++++++++++++++++++++++++++++++++ fe/src/lib/DataView.svelte | 53 +++++++++++++++++++++++++++++++++++----- fe/src/lib/Table.svelte | 13 +++++----- 3 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 fe/src/http.ts (limited to 'fe/src') diff --git a/fe/src/http.ts b/fe/src/http.ts new file mode 100644 index 0000000..cc5a906 --- /dev/null +++ b/fe/src/http.ts @@ -0,0 +1,60 @@ +export default class HttpClient { + private static instance: HttpClient; + baseURL: string; + + private constructor(baseURL: string) { + this.baseURL = baseURL; + } + + private getURL(endpoint: string): URL { + return new URL(endpoint, this.baseURL) + } + + public static getInstance(): HttpClient { + if (!HttpClient.instance) { + const baseUrl = import.meta.env?.VITE_API_BASE_URL ?? 'http://localhost:8080/api/v1'; + HttpClient.instance = new HttpClient(baseUrl); + } + + return HttpClient.instance; + } + + async get({ endpoint }: IHttpParameters): Promise { + const url = this.getURL(endpoint); + const response = await fetch(url, { + method: 'GET', + headers: headers, + }); + return response.json(); + } + + async post({ endpoint }: IHttpParameters): Promise { + const url = this.getURL(endpoint); + const response = await fetch(url, { + method: 'POST', + body: JSON.stringify(body), + headers: headers, + }); + return response.json(); + } + + async patch({ endpoint, authenticated, headers }: IHttpParameters): Promise { + const url = this.getURL(endpoint); + if (authenticated) { + + } + const response: Response = await fetch(url) + } + + async delete({ endpoint, authenticated }: IHttpParameters): Promise { + const url = this.getURL(endpoint); + if (authenticated) { } + const response = await fetch() + } +} + +interface IHttpParameters { + endpoint: string; + authenticated: boolean; + headers: Headers +} diff --git a/fe/src/lib/DataView.svelte b/fe/src/lib/DataView.svelte index 7d62a43..0a6b81b 100644 --- a/fe/src/lib/DataView.svelte +++ b/fe/src/lib/DataView.svelte @@ -1,5 +1,6 @@ - - + + - + diff --git a/fe/src/lib/Table.svelte b/fe/src/lib/Table.svelte index d1cd7da..621157e 100644 --- a/fe/src/lib/Table.svelte +++ b/fe/src/lib/Table.svelte @@ -5,6 +5,10 @@ export let omit: string[] = ["id"]; export let title: string | undefined = undefined; + export let sortBy: string = 'date'; + + type SortComparator = (a, b) => number + function getDataKeys(data: any[]): string[] { if (!data || data.length === 0) return []; return Object.keys(data[0]) @@ -16,11 +20,8 @@ return Object.entries(row).filter((r) => !omit.includes(r[0])); } - - let limitedData: Array = []; - - if (data && (data as any[]).length > 0) { - limitedData = (data as any[]).slice(0, 4); + function sort(arr: Array>, fn: SortComparator = (a , b) => new Date(b[sortBy]) - new Date(a[sortBy])) { + return arr.sort(fn) } const formatter = new Intl.DateTimeFormat("en", { @@ -62,7 +63,7 @@ {/if} {#if data} - {#each limitedData as row} + {#each sort(data) as row} {#each getRow(row) as datum} {formatDatum(datum)} -- cgit v1.1