feat(documents): prefill sender and receiver from URL params on new document page
When navigating from the conversations page via the 'New document in this correspondence' link, the senderId and receiverId query params are now read in the server load, resolved to person names, and used to pre-populate the sender typeahead and receiver multi-select on the form. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,10 +5,12 @@ import { parseBackendError, getErrorMessage } from '$lib/errors';
|
|||||||
|
|
||||||
export async function load({
|
export async function load({
|
||||||
fetch,
|
fetch,
|
||||||
locals
|
locals,
|
||||||
|
url
|
||||||
}: {
|
}: {
|
||||||
fetch: typeof globalThis.fetch;
|
fetch: typeof globalThis.fetch;
|
||||||
locals: App.Locals;
|
locals: App.Locals;
|
||||||
|
url: URL;
|
||||||
}) {
|
}) {
|
||||||
const canWrite =
|
const canWrite =
|
||||||
locals.user?.groups?.some((g: { permissions: string[] }) =>
|
locals.user?.groups?.some((g: { permissions: string[] }) =>
|
||||||
@@ -16,14 +18,41 @@ export async function load({
|
|||||||
) ?? false;
|
) ?? false;
|
||||||
if (!canWrite) throw error(403, 'Forbidden');
|
if (!canWrite) throw error(403, 'Forbidden');
|
||||||
|
|
||||||
const api = createApiClient(fetch);
|
const senderId = url.searchParams.get('senderId') || '';
|
||||||
const personsResult = await api.GET('/api/persons');
|
const receiverId = url.searchParams.get('receiverId') || '';
|
||||||
|
|
||||||
if (!personsResult.response.ok) {
|
const api = createApiClient(fetch);
|
||||||
return { persons: [] };
|
|
||||||
|
let initialSenderName = '';
|
||||||
|
let initialReceivers: { id: string; firstName: string; lastName: string }[] = [];
|
||||||
|
|
||||||
|
const requests: Promise<void>[] = [];
|
||||||
|
|
||||||
|
if (senderId) {
|
||||||
|
requests.push(
|
||||||
|
api.GET('/api/persons/{id}', { params: { path: { id: senderId } } }).then(({ data }) => {
|
||||||
|
if (data) initialSenderName = `${data.firstName} ${data.lastName}`;
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { persons: personsResult.data };
|
if (receiverId) {
|
||||||
|
requests.push(
|
||||||
|
api.GET('/api/persons/{id}', { params: { path: { id: receiverId } } }).then(({ data }) => {
|
||||||
|
if (data)
|
||||||
|
initialReceivers = [{ id: data.id!, firstName: data.firstName, lastName: data.lastName }];
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [personsResult] = await Promise.all([api.GET('/api/persons'), ...requests]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
persons: personsResult.response.ok ? personsResult.data : [],
|
||||||
|
initialSenderId: senderId,
|
||||||
|
initialSenderName,
|
||||||
|
initialReceivers
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ import PersonTypeahead from '$lib/components/PersonTypeahead.svelte';
|
|||||||
import PersonMultiSelect from '$lib/components/PersonMultiSelect.svelte';
|
import PersonMultiSelect from '$lib/components/PersonMultiSelect.svelte';
|
||||||
import { m } from '$lib/paraglide/messages.js';
|
import { m } from '$lib/paraglide/messages.js';
|
||||||
|
|
||||||
let { form } = $props();
|
let { data, form } = $props();
|
||||||
|
|
||||||
let tags: string[] = $state([]);
|
let tags: string[] = $state([]);
|
||||||
let senderId = $state('');
|
let senderId = $state(data.initialSenderId);
|
||||||
let selectedReceivers: { id: string; firstName: string; lastName: string }[] = $state([]);
|
let selectedReceivers: { id: string; firstName: string; lastName: string }[] = $state(
|
||||||
|
data.initialReceivers
|
||||||
|
);
|
||||||
|
|
||||||
let dateDisplay = $state('');
|
let dateDisplay = $state('');
|
||||||
let dateIso = $state('');
|
let dateIso = $state('');
|
||||||
@@ -120,7 +122,12 @@ function handleDateInput(e: Event) {
|
|||||||
|
|
||||||
<!-- Absender -->
|
<!-- Absender -->
|
||||||
<div>
|
<div>
|
||||||
<PersonTypeahead name="senderId" label={m.form_label_sender()} bind:value={senderId} />
|
<PersonTypeahead
|
||||||
|
name="senderId"
|
||||||
|
label={m.form_label_sender()}
|
||||||
|
bind:value={senderId}
|
||||||
|
initialName={data.initialSenderName}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Empfänger -->
|
<!-- Empfänger -->
|
||||||
|
|||||||
Reference in New Issue
Block a user