From 9d0694f1382b9aa9b799a73c3912c1f3eeae8297 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Mon, 28 Aug 2023 16:02:18 -0700 Subject: [PATCH] feat: add removing admins --- src/lib/util.ts | 13 ++++++ src/routes/admin/team/+page.svelte | 68 ++++++++++++------------------ src/routes/u/[name]/+page.svelte | 37 ---------------- 3 files changed, 39 insertions(+), 79 deletions(-) diff --git a/src/lib/util.ts b/src/lib/util.ts index ccc13d35..ba84dbc7 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -1,4 +1,5 @@ import { goto } from '$app/navigation' +import { toast } from '$lib/components/ui/toasts/toasts.js' import { userSettings } from '$lib/settings.js' import { get } from 'svelte/store' @@ -81,3 +82,15 @@ export function moveItem( return newArray } + +type Maybe = T | undefined | void | null +export const trycatch = (func: () => T): Maybe => { + try { + return func() + } catch (err) { + toast({ + content: err as any, + type: 'error', + }) + } +} diff --git a/src/routes/admin/team/+page.svelte b/src/routes/admin/team/+page.svelte index 27357dc0..c1b8fc83 100644 --- a/src/routes/admin/team/+page.svelte +++ b/src/routes/admin/team/+page.svelte @@ -6,63 +6,47 @@ import EditableList from '$lib/components/ui/list/EditableList.svelte' import { toast } from '$lib/components/ui/toasts/toasts.js' import { getClient } from '$lib/lemmy.js' + import { trycatch } from '$lib/util.js' + import { Trash } from 'svelte-hero-icons' export let data - let leaving = false - - async function leaveTeam() { - if (!$profile?.jwt || !$profile?.user) return - - leaving = true - $profile.user.local_user_view.person.admin = false - - try { - await getClient().leaveAdmin({ - auth: $profile.jwt, + async function removeAdmin(id: number, confirm: boolean): Promise { + if (!confirm) + return toast({ + content: 'Are you sure you want to remove that admin?', + action: () => removeAdmin(id, true), }) + if (!$profile?.jwt) return + + const result = await trycatch(() => + getClient().addAdmin({ + added: false, + auth: $profile!.jwt!, + person_id: id, + }) + ) + + if (result) { toast({ - content: 'You have left the admin team.', + content: 'Removed that admin.', type: 'success', }) - - goto('/') - } catch (err) { - toast({ - content: err as any, - type: 'error', - }) - } finally { - leaving = false } }

Admins

-

- Remove admins on their user page. -

- + removeAdmin(e.detail, false)}> {#each data.site?.admins ?? [] as admin} -
+
+
{/each} - diff --git a/src/routes/u/[name]/+page.svelte b/src/routes/u/[name]/+page.svelte index 2a467399..3a9cfbf0 100644 --- a/src/routes/u/[name]/+page.svelte +++ b/src/routes/u/[name]/+page.svelte @@ -246,43 +246,6 @@ - {#if data.person_view.person.local} - { - toast({ - content: `Are you sure you want to ${ - data.person_view.person.admin ? 'remove' : 'appoint' - } ${data.person_view.person.name} as an admin?`, - duration: 99999 * 1000, - type: 'info', - action: async () => { - if (!$profile?.jwt) return - await getClient() - .addAdmin({ - auth: $profile.jwt, - added: !data.person_view.person.admin, - person_id: data.person_view.person.id, - }) - .then(() => { - data.person_view.person.admin = - !data.person_view.person.admin - }) - .catch(() => { - toast({ - content: 'Failed to modify that user.', - type: 'error', - }) - }) - }, - }) - }} - > - - {data.person_view.person.admin - ? 'Remove admin' - : 'Appoint as admin'} - - {/if}