|
| 1 | +<script lang="ts"> |
| 2 | + import { useAuth } from '#queries/auth'; |
| 3 | + import { useUsersQuery } from '#queries/admin_users'; |
| 4 | + import * as Table from '$lib/components/ui/table'; |
| 5 | + import * as Card from '$lib/components/ui/card'; |
| 6 | + import * as Pagination from '$lib/components/ui/pagination'; |
| 7 | + import { Button } from '$lib/components/ui/button'; |
| 8 | + import { Trash2, UserPlus } from 'lucide-svelte'; |
| 9 | + import { toast } from 'svelte-sonner'; |
| 10 | + import CreateUserDialog from './create_user_dialog.svelte'; |
| 11 | + import DeleteUserDialog from './delete_user_dialog.svelte'; |
| 12 | +
|
| 13 | + let currentPage = $state(1); |
| 14 | + const pageSize = 20; |
| 15 | +
|
| 16 | + const { user } = useAuth(); |
| 17 | + const { users } = useUsersQuery(() => currentPage, pageSize); |
| 18 | +
|
| 19 | + let isCreateDialogOpen = $state(false); |
| 20 | + let isDeleteDialogOpen = $state(false); |
| 21 | + let userToDelete = $state<string | null>(null); |
| 22 | +
|
| 23 | + let totalItems = $derived(users.data?.total_items ?? 0); |
| 24 | +
|
| 25 | + function requestDelete(userId: string) { |
| 26 | + if (userId === user.data?.id) { |
| 27 | + toast.error('Cannot delete yourself.'); |
| 28 | + return; |
| 29 | + } |
| 30 | + userToDelete = userId; |
| 31 | + isDeleteDialogOpen = true; |
| 32 | + } |
| 33 | +</script> |
| 34 | + |
| 35 | +<div class="flex items-center justify-between space-y-2 pb-6"> |
| 36 | + <div> |
| 37 | + <h2 class="text-3xl font-bold tracking-tight">Users</h2> |
| 38 | + <p class="text-muted-foreground">Manage system users.</p> |
| 39 | + </div> |
| 40 | + |
| 41 | + <div> |
| 42 | + <Button onclick={() => (isCreateDialogOpen = true)}> |
| 43 | + <UserPlus class="mr-2 h-4 w-4" /> |
| 44 | + Create User |
| 45 | + </Button> |
| 46 | + </div> |
| 47 | +</div> |
| 48 | + |
| 49 | +<Card.Root> |
| 50 | + <Card.Content class="p-0"> |
| 51 | + <Table.Root> |
| 52 | + <Table.Header> |
| 53 | + <Table.Row> |
| 54 | + <Table.Head>Username</Table.Head> |
| 55 | + <Table.Head>Email</Table.Head> |
| 56 | + <Table.Head class="text-right">Actions</Table.Head> |
| 57 | + </Table.Row> |
| 58 | + </Table.Header> |
| 59 | + <Table.Body> |
| 60 | + {#if users.isLoading} |
| 61 | + <Table.Row> |
| 62 | + <Table.Cell colspan={3} class="py-8 text-center text-muted-foreground"> |
| 63 | + Loading users... |
| 64 | + </Table.Cell> |
| 65 | + </Table.Row> |
| 66 | + {:else if users.error} |
| 67 | + <Table.Row> |
| 68 | + <Table.Cell colspan={3} class="py-8 text-center text-muted-foreground"> |
| 69 | + Failed to load users: {users.error.message} |
| 70 | + </Table.Cell> |
| 71 | + </Table.Row> |
| 72 | + {:else if !users.data?.items || users.data.items.length === 0} |
| 73 | + <Table.Row> |
| 74 | + <Table.Cell colspan={3} class="py-8 text-center text-muted-foreground"> |
| 75 | + No users found. |
| 76 | + </Table.Cell> |
| 77 | + </Table.Row> |
| 78 | + {:else} |
| 79 | + {#each users.data.items as u} |
| 80 | + <Table.Row> |
| 81 | + <Table.Cell>{u.username}</Table.Cell> |
| 82 | + <Table.Cell>{u.email || '-'}</Table.Cell> |
| 83 | + <Table.Cell class="text-right"> |
| 84 | + <Button |
| 85 | + variant="ghost" |
| 86 | + size="icon" |
| 87 | + disabled={u.id === user.data?.id} |
| 88 | + onclick={() => requestDelete(u.id)} |
| 89 | + > |
| 90 | + <Trash2 class="h-4 w-4 cursor-pointer text-destructive" /> |
| 91 | + </Button> |
| 92 | + </Table.Cell> |
| 93 | + </Table.Row> |
| 94 | + {/each} |
| 95 | + {/if} |
| 96 | + </Table.Body> |
| 97 | + </Table.Root> |
| 98 | + </Card.Content> |
| 99 | +</Card.Root> |
| 100 | + |
| 101 | +<div class="flex items-center justify-end py-4"> |
| 102 | + <Pagination.Root count={totalItems} perPage={pageSize} bind:page={currentPage}> |
| 103 | + {#snippet children({ pages, currentPage })} |
| 104 | + <Pagination.Content> |
| 105 | + <Pagination.Item> |
| 106 | + <Pagination.PrevButton /> |
| 107 | + </Pagination.Item> |
| 108 | + {#each pages as page (page.key)} |
| 109 | + {#if page.type === 'ellipsis'} |
| 110 | + <Pagination.Item> |
| 111 | + <Pagination.Ellipsis /> |
| 112 | + </Pagination.Item> |
| 113 | + {:else} |
| 114 | + <Pagination.Item> |
| 115 | + <Pagination.Link {page} isActive={currentPage === page.value}> |
| 116 | + {page.value} |
| 117 | + </Pagination.Link> |
| 118 | + </Pagination.Item> |
| 119 | + {/if} |
| 120 | + {/each} |
| 121 | + <Pagination.Item> |
| 122 | + <Pagination.NextButton /> |
| 123 | + </Pagination.Item> |
| 124 | + </Pagination.Content> |
| 125 | + {/snippet} |
| 126 | + </Pagination.Root> |
| 127 | +</div> |
| 128 | + |
| 129 | +<CreateUserDialog bind:open={isCreateDialogOpen} /> |
| 130 | + |
| 131 | +<DeleteUserDialog bind:open={isDeleteDialogOpen} bind:userId={userToDelete} /> |
0 commit comments