2025-03-11 14:16:42 -05:00
|
|
|
<script lang="ts">
|
|
|
|
|
import FormInput from '$lib/components/form/form-input.svelte';
|
|
|
|
|
import { Button } from '$lib/components/ui/button';
|
2025-03-20 19:57:41 +01:00
|
|
|
import { m } from '$lib/paraglide/messages';
|
2025-03-11 14:16:42 -05:00
|
|
|
import type { ApiKeyCreate } from '$lib/types/api-key.type';
|
|
|
|
|
import { createForm } from '$lib/utils/form-util';
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
|
|
|
|
let {
|
|
|
|
|
callback
|
|
|
|
|
}: {
|
|
|
|
|
callback: (apiKey: ApiKeyCreate) => Promise<boolean>;
|
|
|
|
|
} = $props();
|
|
|
|
|
|
|
|
|
|
let isLoading = $state(false);
|
|
|
|
|
|
|
|
|
|
// Set default expiration to 30 days from now
|
|
|
|
|
const defaultExpiry = new Date();
|
|
|
|
|
defaultExpiry.setDate(defaultExpiry.getDate() + 30);
|
|
|
|
|
|
|
|
|
|
const apiKey = {
|
|
|
|
|
name: '',
|
|
|
|
|
description: '',
|
|
|
|
|
expiresAt: defaultExpiry
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
|
name: z
|
|
|
|
|
.string()
|
2025-03-20 19:57:41 +01:00
|
|
|
.min(3, m.name_must_be_at_least_3_characters())
|
|
|
|
|
.max(50, m.name_cannot_exceed_50_characters()),
|
2025-03-11 14:16:42 -05:00
|
|
|
description: z.string().default(''),
|
2025-03-20 19:57:41 +01:00
|
|
|
expiresAt: z.date().min(new Date(), m.expiration_date_must_be_in_the_future())
|
2025-03-11 14:16:42 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { inputs, ...form } = createForm<typeof formSchema>(formSchema, apiKey);
|
|
|
|
|
|
|
|
|
|
async function onSubmit() {
|
|
|
|
|
const data = form.validate();
|
|
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
|
|
const apiKeyData: ApiKeyCreate = {
|
|
|
|
|
name: data.name,
|
|
|
|
|
description: data.description,
|
|
|
|
|
expiresAt: data.expiresAt
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
isLoading = true;
|
|
|
|
|
const success = await callback(apiKeyData);
|
|
|
|
|
if (success) form.reset();
|
|
|
|
|
isLoading = false;
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<form onsubmit={onSubmit}>
|
|
|
|
|
<div class="grid grid-cols-1 items-start gap-5 md:grid-cols-2">
|
|
|
|
|
<FormInput
|
2025-03-20 19:57:41 +01:00
|
|
|
label={m.name()}
|
2025-03-11 14:16:42 -05:00
|
|
|
bind:input={$inputs.name}
|
2025-03-20 19:57:41 +01:00
|
|
|
description={m.name_to_identify_this_api_key()}
|
2025-03-11 14:16:42 -05:00
|
|
|
/>
|
|
|
|
|
<FormInput
|
2025-03-20 19:57:41 +01:00
|
|
|
label={m.expires_at()}
|
2025-03-11 14:16:42 -05:00
|
|
|
type="date"
|
2025-03-20 19:57:41 +01:00
|
|
|
description={m.when_this_api_key_will_expire()}
|
2025-03-11 14:16:42 -05:00
|
|
|
bind:input={$inputs.expiresAt}
|
|
|
|
|
/>
|
|
|
|
|
<div class="col-span-1 md:col-span-2">
|
|
|
|
|
<FormInput
|
2025-03-20 19:57:41 +01:00
|
|
|
label={m.description()}
|
|
|
|
|
description={m.optional_description_to_help_identify_this_keys_purpose()}
|
2025-03-11 14:16:42 -05:00
|
|
|
bind:input={$inputs.description}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="mt-5 flex justify-end">
|
2025-03-20 19:57:41 +01:00
|
|
|
<Button {isLoading} type="submit">{m.save()}</Button>
|
2025-03-11 14:16:42 -05:00
|
|
|
</div>
|
|
|
|
|
</form>
|