import { useState } from 'react'; import { useStore } from '@tanstack/react-form'; import { useAppForm, Form } from '@/components/form'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import ApiKeyModal from '@/components/dashboard/ApiKeyModal'; import { useCreateAccountApiKey } from '@/api/account/api-keys/queries'; function CreateApiKeyForm() { const [apiKey, setApiKey] = useState(''); const createApiKey = useCreateAccountApiKey(); const form = useAppForm({ defaultValues: { description: '', allowedIps: '' }, onSubmit: async ({ value, formApi }) => { try { const created = await createApiKey.mutateAsync({ body: { description: value.description, allowed_ips: value.allowedIps.length > 0 ? value.allowedIps.split('\n') : [], }, }); formApi.reset(); setApiKey(`${created.attributes.identifier}${created.meta?.secret_token ?? ''}`); } catch { // Error toast is handled by the mutation. } }, }); const isSubmitting = useStore(form.store, (state) => state.isSubmitting); return ( <> 0} onClose={() => setApiKey('')} apiKey={apiKey} />
value.length >= 4 ? undefined : 'A description of at least 4 characters must be provided.', }} > {(field) => ( )}
{(field) => ( )}
Create
); } export default CreateApiKeyForm;