feat: add removing admins
This commit is contained in:
1 parent
41a8c1a1f0
commit
9d0694f138
3 files changed
+39
-79
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
|
import { toast } from '$lib/components/ui/toasts/toasts.js'
|
||||||
import { userSettings } from '$lib/settings.js'
|
import { userSettings } from '$lib/settings.js'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
@@ -81,3 +82,15 @@ export function moveItem<T>(
|
|||||||
|
|
||||||
return newArray
|
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',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,63 +6,47 @@
|
|||||||
import EditableList from '$lib/components/ui/list/EditableList.svelte'
|
import EditableList from '$lib/components/ui/list/EditableList.svelte'
|
||||||
import { toast } from '$lib/components/ui/toasts/toasts.js'
|
import { toast } from '$lib/components/ui/toasts/toasts.js'
|
||||||
import { getClient } from '$lib/lemmy.js'
|
import { getClient } from '$lib/lemmy.js'
|
||||||
|
import { trycatch } from '$lib/util.js'
|
||||||
|
import { Trash } from 'svelte-hero-icons'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
|
|
||||||
let leaving = false
|
async function removeAdmin(id: number, confirm: boolean): Promise<any> {
|
||||||
|
if (!confirm)
|
||||||
async function leaveTeam() {
|
return toast({
|
||||||
if (!$profile?.jwt || !$profile?.user) return
|
content: 'Are you sure you want to remove that admin?',
|
||||||
|
action: () => removeAdmin(id, true),
|
||||||
leaving = true
|
|
||||||
$profile.user.local_user_view.person.admin = false
|
|
||||||
|
|
||||||
try {
|
|
||||||
await getClient().leaveAdmin({
|
|
||||||
auth: $profile.jwt,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (!$profile?.jwt) return
|
||||||
|
|
||||||
|
const result = await trycatch(() =>
|
||||||
|
getClient().addAdmin({
|
||||||
|
added: false,
|
||||||
|
auth: $profile!.jwt!,
|
||||||
|
person_id: id,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
if (result) {
|
||||||
toast({
|
toast({
|
||||||
content: 'You have left the admin team.',
|
content: 'Removed that admin.',
|
||||||
type: 'success',
|
type: 'success',
|
||||||
})
|
})
|
||||||
|
|
||||||
goto('/')
|
|
||||||
} catch (err) {
|
|
||||||
toast({
|
|
||||||
content: err as any,
|
|
||||||
type: 'error',
|
|
||||||
})
|
|
||||||
} finally {
|
|
||||||
leaving = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1 class="font-bold text-2xl">Admins</h1>
|
<h1 class="font-bold text-2xl">Admins</h1>
|
||||||
<p class="text-slate-600 dark:text-zinc-400">
|
<EditableList let:action on:action={(e) => removeAdmin(e.detail, false)}>
|
||||||
Remove admins on their user page.
|
|
||||||
</p>
|
|
||||||
<EditableList>
|
|
||||||
{#each data.site?.admins ?? [] as admin}
|
{#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} />
|
<UserLink avatar showInstance={false} user={admin.person} />
|
||||||
|
<Button
|
||||||
|
on:click={() => action(admin.person.id)}
|
||||||
|
icon={Trash}
|
||||||
|
size="square-md"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
</EditableList>
|
</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>
|
|
||||||
@@ -246,43 +246,6 @@
|
|||||||
<Button size="square-md" on:click={toggleOpen} slot="button">
|
<Button size="square-md" on:click={toggleOpen} slot="button">
|
||||||
<ShieldIcon width={16} filled />
|
<ShieldIcon width={16} filled />
|
||||||
</Button>
|
</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
|
<MenuButton
|
||||||
color="dangerSecondary"
|
color="dangerSecondary"
|
||||||
on:click={() =>
|
on:click={() =>
|
||||||
|
|||||||
Reference in new issue
Block a user