feat: add removing admins

This commit is contained in:
Xyphyn committed 2023-08-28 16:02:18 -07:00
1 parent 41a8c1a1f0
commit 9d0694f138
3 files changed
+39 -79

No files matched your search

+13
View File
@@ -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<T>(
return newArray
}
type Maybe<T> = T | undefined | void | null
export const trycatch = <T>(func: () => T): Maybe<T> => {
try {
return func()
} catch (err) {
toast({
content: err as any,
type: 'error',
})
}
}
+26 -42
View File
@@ -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<any> {
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
}
}
</script>
<h1 class="font-bold text-2xl">Admins</h1>
<p class="text-slate-600 dark:text-zinc-400">
Remove admins on their user page.
</p>
<EditableList>
<EditableList let:action on:action={(e) => removeAdmin(e.detail, false)}>
{#each data.site?.admins ?? [] as admin}
<div class="py-4 flex items-center">
<div class="py-3 flex items-center justify-between">
<UserLink avatar showInstance={false} user={admin.person} />
<Button
on:click={() => action(admin.person.id)}
icon={Trash}
size="square-md"
/>
</div>
{/each}
</EditableList>
<Button
color="danger"
class="mt-auto ml-auto"
size="lg"
loading={leaving}
disabled={leaving}
on:click={() => {
toast({
content: 'Are you sure you want to leave the admin team?',
action: leaveTeam,
duration: 60 * 1000,
})
}}
>
Leave the admin team
</Button>
-37
View File
@@ -246,43 +246,6 @@
<Button size="square-md" on:click={toggleOpen} slot="button">
<ShieldIcon width={16} filled />
</Button>
{#if data.person_view.person.local}
<MenuButton
on:click={() => {
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',
})
})
},
})
}}
>
<Icon slot="icon" mini size="16" src={UserPlus} />
{data.person_view.person.admin
? 'Remove admin'
: 'Appoint as admin'}
</MenuButton>
{/if}
<MenuButton
color="dangerSecondary"
on:click={() =>