feat(frontend): add /documents page.server.ts — search load function with all filter params
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
91
frontend/src/routes/documents/+page.server.ts
Normal file
91
frontend/src/routes/documents/+page.server.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import { createApiClient } from '$lib/api.server';
|
||||
import type { components } from '$lib/generated/api';
|
||||
|
||||
type DocumentSearchItem = components['schemas']['DocumentSearchItem'];
|
||||
|
||||
const VALID_SORTS = ['DATE', 'TITLE', 'SENDER', 'RECEIVER', 'UPLOAD_DATE', 'RELEVANCE'] as const;
|
||||
type ValidSort = (typeof VALID_SORTS)[number];
|
||||
const VALID_DIRS = ['asc', 'desc'] as const;
|
||||
type ValidDir = (typeof VALID_DIRS)[number];
|
||||
|
||||
export async function load({ url, fetch }) {
|
||||
const q = url.searchParams.get('q') || '';
|
||||
const from = url.searchParams.get('from') || '';
|
||||
const to = url.searchParams.get('to') || '';
|
||||
const senderId = url.searchParams.get('senderId') || '';
|
||||
const receiverId = url.searchParams.get('receiverId') || '';
|
||||
const tags = url.searchParams.getAll('tag');
|
||||
const rawSort = url.searchParams.get('sort') ?? 'DATE';
|
||||
const sort: ValidSort = (VALID_SORTS as readonly string[]).includes(rawSort)
|
||||
? (rawSort as ValidSort)
|
||||
: 'DATE';
|
||||
const rawDir = url.searchParams.get('dir') ?? 'desc';
|
||||
const dir: ValidDir = (VALID_DIRS as readonly string[]).includes(rawDir)
|
||||
? (rawDir as ValidDir)
|
||||
: 'desc';
|
||||
const tagQ = url.searchParams.get('tagQ') || '';
|
||||
const tagOp = url.searchParams.get('tagOp') === 'OR' ? 'OR' : 'AND';
|
||||
|
||||
const api = createApiClient(fetch);
|
||||
|
||||
try {
|
||||
const result = await api.GET('/api/documents/search', {
|
||||
params: {
|
||||
query: {
|
||||
q: q || undefined,
|
||||
from: from || undefined,
|
||||
to: to || undefined,
|
||||
senderId: senderId || undefined,
|
||||
receiverId: receiverId || undefined,
|
||||
tag: tags.length ? tags : undefined,
|
||||
tagQ: tagQ && !tags.length ? tagQ : undefined,
|
||||
tagOp: tagOp === 'OR' ? 'OR' : undefined,
|
||||
sort,
|
||||
dir: dir || undefined
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (result.response.status === 401) {
|
||||
throw redirect(302, '/login');
|
||||
}
|
||||
|
||||
const items: DocumentSearchItem[] = (result.data?.items ?? []) as DocumentSearchItem[];
|
||||
const total: number = result.data?.total ?? 0;
|
||||
|
||||
return {
|
||||
items,
|
||||
total,
|
||||
q,
|
||||
from,
|
||||
to,
|
||||
senderId,
|
||||
receiverId,
|
||||
tags,
|
||||
sort,
|
||||
dir,
|
||||
tagQ,
|
||||
tagOp,
|
||||
error: null as string | null
|
||||
};
|
||||
} catch (e) {
|
||||
if ((e as { status?: number }).status) throw e;
|
||||
console.error('Error loading documents:', e);
|
||||
return {
|
||||
items: [] as DocumentSearchItem[],
|
||||
total: 0,
|
||||
q,
|
||||
from,
|
||||
to,
|
||||
senderId,
|
||||
receiverId,
|
||||
tags,
|
||||
sort,
|
||||
dir,
|
||||
tagQ,
|
||||
tagOp,
|
||||
error: 'Daten konnten nicht geladen werden.' as string | null
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user