import type Collection from '#models/collection'; import { useForm } from '@inertiajs/react'; import { ChangeEvent, FormEvent, useMemo } from 'react'; import FormField from '~/components/common/form/_form_field'; import TextBox from '~/components/common/form/textbox'; import BackToDashboard from '~/components/common/navigation/bask_to_dashboard'; import FormLayout from '~/components/layouts/form_layout'; import useSearchParam from '~/hooks/use_search_param'; import { isValidHttpUrl } from '~/lib/navigation'; export default function CreateLinkPage({ collections, }: { collections: Collection[]; }) { const collectionId = useSearchParam('collectionId') ?? collections[0].id; const { data, setData, post, processing } = useForm({ name: '', description: '', url: '', favorite: false, collectionId: collectionId, }); const canSubmit = useMemo( () => data.name !== '' && isValidHttpUrl(data.url) && data.favorite !== null && data.collectionId !== null && !processing, [data, processing] ); const handleOnCheck = ({ target }: ChangeEvent) => { setData('favorite', !!target.checked); }; const handleSubmit = (e: FormEvent) => { e.preventDefault(); post('/links'); }; return ( ); }