97 lines
2.7 KiB
TypeScript
97 lines
2.7 KiB
TypeScript
import { Heading, Input, Stack, Textarea } from '@chakra-ui/react';
|
|
import { Button } from '@client/components/ui/button';
|
|
import { Field } from '@client/components/ui/field';
|
|
import { valibotResolver } from '@hookform/resolvers/valibot';
|
|
import { createFileRoute, redirect } from '@tanstack/react-router';
|
|
import { useForm } from 'react-hook-form';
|
|
import * as v from 'valibot';
|
|
|
|
export const Route = createFileRoute('/new/book')({
|
|
component: RouteComponent,
|
|
beforeLoad: ({ context }) => {
|
|
if (!context.isAuthenticated) throw redirect({ to: '/' });
|
|
}
|
|
});
|
|
|
|
function RouteComponent() {
|
|
return (
|
|
<Stack width="100%" flexDirection={'column'} justifyContent={'center'} alignItems={'center'}>
|
|
<Heading>New book</Heading>
|
|
<NewBookForm />
|
|
</Stack>
|
|
);
|
|
}
|
|
|
|
interface FormValues {
|
|
title: string;
|
|
description: string;
|
|
}
|
|
|
|
function NewBookForm() {
|
|
const schema = v.pipe(
|
|
v.object({
|
|
title: v.pipe(v.string(), v.nonEmpty('Title is required')),
|
|
description: v.pipe(v.string(), v.nonEmpty('Password is required'))
|
|
})
|
|
);
|
|
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
formState: { errors }
|
|
} = useForm<FormValues>({ resolver: valibotResolver(schema) });
|
|
|
|
return (
|
|
<form style={{ width: '80%' }} onSubmit={handleSubmit(async values => {})}>
|
|
<Stack>
|
|
<Field label="Title" invalid={errors.title != null} errorText={errors.title?.message}>
|
|
<Input {...register('title')} />
|
|
</Field>
|
|
<Field
|
|
label="Description"
|
|
invalid={errors.description != null}
|
|
errorText={errors.description?.message}
|
|
>
|
|
<Textarea width="100%" lineHeight={4} {...register('description')} />
|
|
</Field>
|
|
<Button type="submit">Create new book</Button>
|
|
</Stack>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
// function LoginForm() {
|
|
// const schema = v.pipe(
|
|
// v.object({
|
|
// email: v.pipe(v.string(), v.nonEmpty('Email is required'), v.email('Invalid email')),
|
|
// password: v.pipe(v.string(), v.nonEmpty('Password is required'))
|
|
// })
|
|
// );
|
|
//
|
|
// const {
|
|
// register,
|
|
// handleSubmit,
|
|
// formState: { errors }
|
|
// } = useForm<FormValues>({ resolver: valibotResolver(schema) });
|
|
//
|
|
// const { login } = useAuth();
|
|
//
|
|
// return (
|
|
// <form
|
|
// >
|
|
// <Stack>
|
|
// <Field label="Email" invalid={errors.email != null} errorText={errors.email?.message}>
|
|
// <Input {...register('email')} />
|
|
// </Field>
|
|
// <Field
|
|
// label="Password"
|
|
// invalid={errors.password != null}
|
|
// errorText={errors.password?.message}
|
|
// >
|
|
// <PasswordInput {...register('password')} />
|
|
// </Field>
|
|
// <Button type="submit">Sign in</Button>
|
|
// </Stack>
|
|
// </form>
|
|
// );
|
|
// }
|