import cx from 'clsx'; import { isEmpty, isNil } from 'lodash-es'; import { useActionState, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { VSegmentedControl } from '../components/VSegmentedControl'; import { SchemeTypeOption, SchemeTypeOptions } from '../models'; import { useCreateScheme } from '../stores/schemes'; import styles from './NewScheme.module.css'; export function NewScheme() { const createScheme = useCreateScheme(); const navigate = useNavigate(); const [schemeType, setSchemeType] = useState('q_scheme'); const [errors, formAction] = useActionState<{ [key: string]: string }, FormData>( (_prevState, formData): { [key: string]: string } => { try { const name = formData.get('name') as string; if (isNil(name) || isEmpty(name)) { throw { name: 'Name is required' }; } const description = (formData.get('description') ?? null) as string | null; const schemeType = (formData.get('type') ?? 'q_scheme') as SchemeTypeOption['value']; const newId = createScheme(name, schemeType, description); navigate(`../${newId}`); } catch (error) { return error as { [key: string]: string }; } return {} as { [key: string]: string }; }, {}, ); return (
setSchemeType(v as SchemeTypeOption['value'])} />
{errors.name &&
{errors.name}
}