feat: implement subscription form with validation and fetcher integration
This commit is contained in:
parent
5cfad29302
commit
722966f50e
@ -1,29 +1,65 @@
|
|||||||
|
import { zodResolver } from '@hookform/resolvers/zod'
|
||||||
|
import { useFetcher, useRouteLoaderData } from 'react-router'
|
||||||
|
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
||||||
|
import { z } from 'zod'
|
||||||
|
|
||||||
import { Button } from '~/components/ui/button'
|
import { Button } from '~/components/ui/button'
|
||||||
|
import { Select } from '~/components/ui/select'
|
||||||
|
import type { loader } from '~/routes/_layout'
|
||||||
|
|
||||||
|
export const subscribeSchema = z
|
||||||
|
.object({
|
||||||
|
email: z.string().email('Email tidak valid'),
|
||||||
|
password: z.string().min(6, 'Kata sandi minimal 6 karakter'),
|
||||||
|
rePassword: z.string().min(6, 'Kata sandi minimal 6 karakter'),
|
||||||
|
phone: z.string().min(10, 'No telepon tidak valid'),
|
||||||
|
subscribe_plan: z.string().min(1, 'Pilih salah satu subscription'),
|
||||||
|
})
|
||||||
|
.refine((field) => field.password === field.rePassword, {
|
||||||
|
message: 'Kata sandi tidak sama',
|
||||||
|
path: ['rePassword'],
|
||||||
|
})
|
||||||
|
|
||||||
|
export type TSubscribeSchema = z.infer<typeof subscribeSchema>
|
||||||
|
|
||||||
export default function FormSubscription() {
|
export default function FormSubscription() {
|
||||||
|
const fetcher = useFetcher()
|
||||||
|
const loaderData = useRouteLoaderData<typeof loader>('routes/_layout')
|
||||||
|
const subscriptions = loaderData?.subscriptionsData
|
||||||
|
|
||||||
|
const formMethods = useRemixForm<TSubscribeSchema>({
|
||||||
|
mode: 'onSubmit',
|
||||||
|
fetcher,
|
||||||
|
resolver: zodResolver(subscribeSchema),
|
||||||
|
})
|
||||||
|
|
||||||
|
const { handleSubmit } = formMethods
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col items-center justify-center">
|
<div className="flex flex-col items-center justify-center">
|
||||||
<div className="w-full max-w-md">
|
<RemixFormProvider {...formMethods}>
|
||||||
<form>
|
<fetcher.Form
|
||||||
{/* Subscribe*/}
|
method="post"
|
||||||
<div className="mb-4">
|
onSubmit={handleSubmit}
|
||||||
<label
|
className="w-full max-w-md"
|
||||||
htmlFor="subscription"
|
action="/actions/register"
|
||||||
className="mb-1 block text-gray-700"
|
>
|
||||||
>
|
<Select
|
||||||
Subscription
|
id="subscribe_plan"
|
||||||
</label>
|
name="subscribe_plan"
|
||||||
<select className="focus:inheriten w-full rounded-md border border-[#DFDFDF] p-2">
|
label="Subscription"
|
||||||
<option selected>Subscription</option>
|
placeholder="Pilih Subscription"
|
||||||
</select>
|
options={subscriptions}
|
||||||
</div>
|
/>
|
||||||
|
|
||||||
{/* Tombol Masuk */}
|
<Button
|
||||||
<Button className="mt-5 w-full rounded-md bg-[#2E2F7C] py-2 text-white transition hover:bg-blue-800">
|
type="submit"
|
||||||
|
className="mt-5 w-full rounded-md bg-[#2E2F7C] py-2 text-white transition hover:bg-blue-800"
|
||||||
|
>
|
||||||
Lanjutkan
|
Lanjutkan
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</fetcher.Form>
|
||||||
</div>
|
</RemixFormProvider>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user