From 67d275f15bf08ab84bbeb8ad74b24d98a53b862c Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 29 May 2023 01:27:17 +0200 Subject: [PATCH] feat: add editing and deleting flags --- .../src/routes/settings/flags/+page.svelte | 34 ++++++--- .../src/routes/settings/flags/Flag.svelte | 70 ++++++++++++++++++- 2 files changed, 92 insertions(+), 12 deletions(-) diff --git a/frontend/src/routes/settings/flags/+page.svelte b/frontend/src/routes/settings/flags/+page.svelte index 01e1dfa..f808aa1 100644 --- a/frontend/src/routes/settings/flags/+page.svelte +++ b/frontend/src/routes/settings/flags/+page.svelte @@ -7,7 +7,7 @@ import { addToast } from "$lib/toast"; import { encode } from "base64-arraybuffer"; import unknownFlag from "./unknown_flag.png"; - import { apiFetchClient } from "$lib/api/fetch"; + import { apiFetchClient, fastFetchClient } from "$lib/api/fetch"; import ErrorAlert from "$lib/components/ErrorAlert.svelte"; const MAX_FLAG_BYTES = 500_000; @@ -18,11 +18,13 @@ let error: APIError | null = null; let filtered: PrideFlag[]; - $: filtered = search - ? data.flags.filter((flag) => - flag.name.toLocaleLowerCase().includes(search.toLocaleLowerCase()), - ) - : data.flags; + $: filtered = filterFlags(search, data.flags); + + const filterFlags = (search: string, flags: PrideFlag[]) => { + return search + ? flags.filter((flag) => flag.name.toLocaleLowerCase().includes(search.toLocaleLowerCase())) + : flags; + }; // NEW FLAG UPLOADING CODE let modalOpen = false; @@ -81,6 +83,20 @@ error = e as APIError; } }; + + // DELETE FLAG CODE + const deleteFlag = async (id: string) => { + try { + await fastFetchClient(`/users/@me/flags/${id}`, "DELETE"); + + error = null; + + addToast({ header: "Deleted flag", body: "Successfully deleted flag!" }); + data.flags = data.flags.filter((entry) => entry.id !== id); + } catch (e) { + error = e as APIError; + } + };

Pride flags ({data.flags.length})

@@ -98,13 +114,13 @@
- {#each filtered as flag} - + {#each filtered as flag (flag.id)} + {:else} {#if data.flags.length === 0} You haven't uploaded any flags yet, press the button above to do so. {:else} - There are no flags matching your search {search} + There are no flags matching your search {search}. {/if} {/each}
diff --git a/frontend/src/routes/settings/flags/Flag.svelte b/frontend/src/routes/settings/flags/Flag.svelte index 3ed78e3..eab228f 100644 --- a/frontend/src/routes/settings/flags/Flag.svelte +++ b/frontend/src/routes/settings/flags/Flag.svelte @@ -1,15 +1,75 @@ - + + Edit {flag.name} flag + +

+ + +

+

+ +