refactor: adapt api key list to new sort behavior

This commit is contained in:
Elias Schneider
2025-03-11 20:22:05 +01:00
parent 62915d863a
commit d1b9f3a44e
4 changed files with 22 additions and 45 deletions

View File

@@ -1,16 +1,19 @@
import { ACCESS_TOKEN_COOKIE_NAME } from '$lib/constants'; import { ACCESS_TOKEN_COOKIE_NAME } from '$lib/constants';
import ApiKeyService from '$lib/services/api-key-service'; import ApiKeyService from '$lib/services/api-key-service';
import type { SearchPaginationSortRequest } from '$lib/types/pagination.type';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ cookies }) => { export const load: PageServerLoad = async ({ cookies }) => {
const apiKeyService = new ApiKeyService(cookies.get(ACCESS_TOKEN_COOKIE_NAME)); const apiKeyService = new ApiKeyService(cookies.get(ACCESS_TOKEN_COOKIE_NAME));
const apiKeys = await apiKeyService.list({ const apiKeysRequestOptions: SearchPaginationSortRequest = {
sort: { sort: {
column: 'lastUsedAt', column: 'lastUsedAt',
direction: 'desc' as const direction: 'desc' as const
} }
}); };
return apiKeys; const apiKeys = await apiKeyService.list(apiKeysRequestOptions);
return { apiKeys, apiKeysRequestOptions };
}; };

View File

@@ -2,8 +2,7 @@
import { Button } from '$lib/components/ui/button'; import { Button } from '$lib/components/ui/button';
import * as Card from '$lib/components/ui/card'; import * as Card from '$lib/components/ui/card';
import ApiKeyService from '$lib/services/api-key-service'; import ApiKeyService from '$lib/services/api-key-service';
import type { ApiKey, ApiKeyCreate, ApiKeyResponse } from '$lib/types/api-key.type'; import type { ApiKeyCreate, ApiKeyResponse } from '$lib/types/api-key.type';
import type { Paginated } from '$lib/types/pagination.type';
import { axiosErrorToast } from '$lib/utils/error-util'; import { axiosErrorToast } from '$lib/utils/error-util';
import { LucideMinus } from 'lucide-svelte'; import { LucideMinus } from 'lucide-svelte';
import { slide } from 'svelte/transition'; import { slide } from 'svelte/transition';
@@ -12,7 +11,8 @@
import ApiKeyList from './api-key-list.svelte'; import ApiKeyList from './api-key-list.svelte';
let { data } = $props(); let { data } = $props();
let apiKeys = $state<Paginated<ApiKey>>(data); let apiKeys = $state(data.apiKeys);
let apiKeysRequestOptions = $state(data.apiKeysRequestOptions);
const apiKeyService = new ApiKeyService(); const apiKeyService = new ApiKeyService();
let expandAddApiKey = $state(false); let expandAddApiKey = $state(false);
@@ -24,8 +24,7 @@
apiKeyResponse = response; apiKeyResponse = response;
// After creation, reload the list of API keys // After creation, reload the list of API keys
const updatedKeys = await apiKeyService.list(); apiKeys = await apiKeyService.list(apiKeysRequestOptions);
apiKeys = updatedKeys;
return true; return true;
} catch (e) { } catch (e) {
@@ -33,19 +32,6 @@
return false; return false;
} }
} }
function handleDialogClose(open: boolean) {
if (!open) {
apiKeyResponse = null;
// Refresh the list when dialog closes
apiKeyService
.list()
.then((keys) => {
apiKeys = keys;
})
.catch(axiosErrorToast);
}
}
</script> </script>
<svelte:head> <svelte:head>
@@ -82,8 +68,8 @@
<Card.Title>Manage API Keys</Card.Title> <Card.Title>Manage API Keys</Card.Title>
</Card.Header> </Card.Header>
<Card.Content> <Card.Content>
<ApiKeyList {apiKeys} /> <ApiKeyList {apiKeys} requestOptions={apiKeysRequestOptions} />
</Card.Content> </Card.Content>
</Card.Root> </Card.Root>
<ApiKeyDialog bind:apiKeyResponse onOpenChange={handleDialogClose} /> <ApiKeyDialog bind:apiKeyResponse />

View File

@@ -5,12 +5,16 @@
import type { ApiKeyResponse } from '$lib/types/api-key.type'; import type { ApiKeyResponse } from '$lib/types/api-key.type';
let { let {
apiKeyResponse = $bindable(), apiKeyResponse = $bindable()
onOpenChange
}: { }: {
apiKeyResponse: ApiKeyResponse | null; apiKeyResponse: ApiKeyResponse | null;
onOpenChange: (open: boolean) => void;
} = $props(); } = $props();
function onOpenChange(open: boolean) {
if (!open) {
apiKeyResponse = null;
}
}
</script> </script>
<Dialog.Root open={!!apiKeyResponse} {onOpenChange}> <Dialog.Root open={!!apiKeyResponse} {onOpenChange}>

View File

@@ -11,25 +11,15 @@
import { toast } from 'svelte-sonner'; import { toast } from 'svelte-sonner';
let { let {
apiKeys: initialApiKeys apiKeys,
requestOptions
}: { }: {
apiKeys: Paginated<ApiKey>; apiKeys: Paginated<ApiKey>;
requestOptions: SearchPaginationSortRequest;
} = $props(); } = $props();
let apiKeys = $state<Paginated<ApiKey>>(initialApiKeys);
let requestOptions: SearchPaginationSortRequest | undefined = $state({
sort: {
column: 'lastUsedAt',
direction: 'desc'
}
});
const apiKeyService = new ApiKeyService(); const apiKeyService = new ApiKeyService();
// Update the local state whenever the prop changes
$effect(() => {
apiKeys = initialApiKeys;
});
function formatDate(dateStr: string | undefined) { function formatDate(dateStr: string | undefined) {
if (!dateStr) return 'Never'; if (!dateStr) return 'Never';
return new Date(dateStr).toLocaleString(); return new Date(dateStr).toLocaleString();
@@ -54,11 +44,6 @@
} }
}); });
} }
$effect(() => {
// Initial load uses the server-side data
apiKeys = initialApiKeys;
});
</script> </script>
<AdvancedTable <AdvancedTable
@@ -66,7 +51,6 @@
{requestOptions} {requestOptions}
onRefresh={async (o) => (apiKeys = await apiKeyService.list(o))} onRefresh={async (o) => (apiKeys = await apiKeyService.list(o))}
withoutSearch withoutSearch
defaultSort={{ column: 'lastUsedAt', direction: 'desc' }}
columns={[ columns={[
{ label: 'Name', sortColumn: 'name' }, { label: 'Name', sortColumn: 'name' },
{ label: 'Description' }, { label: 'Description' },