2025-03-03 05:16:31 +08:00
|
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
2025-03-03 08:41:38 +08:00
|
|
|
import { useEffect, useState } from 'react'
|
2025-03-03 05:16:31 +08:00
|
|
|
import { useFetcher, useRouteLoaderData } from 'react-router'
|
|
|
|
|
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
|
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
2025-02-23 15:11:35 +07:00
|
|
|
import { Button } from '~/components/ui/button'
|
2025-03-04 09:00:10 +08:00
|
|
|
import { Combobox } from '~/components/ui/combobox'
|
2025-03-03 08:41:38 +08:00
|
|
|
import { useNewsContext } from '~/contexts/news'
|
2025-03-07 12:11:48 +08:00
|
|
|
import type { loader } from '~/routes/_news'
|
2025-03-03 05:16:31 +08:00
|
|
|
|
2025-03-03 08:41:38 +08:00
|
|
|
export const subscribeSchema = z.object({
|
2025-03-04 09:00:10 +08:00
|
|
|
subscribe_plan: z
|
|
|
|
|
.object({
|
|
|
|
|
id: z.string(),
|
|
|
|
|
code: z.string(),
|
|
|
|
|
name: z.string(),
|
|
|
|
|
})
|
|
|
|
|
.optional()
|
|
|
|
|
.nullable()
|
|
|
|
|
.refine((data) => !!data, {
|
|
|
|
|
message: 'Please select a subscription',
|
|
|
|
|
}),
|
2025-03-03 08:41:38 +08:00
|
|
|
})
|
2025-03-03 05:16:31 +08:00
|
|
|
|
|
|
|
|
export type TSubscribeSchema = z.infer<typeof subscribeSchema>
|
2025-02-21 20:47:15 +07:00
|
|
|
|
|
|
|
|
export default function FormSubscription() {
|
2025-03-03 08:41:38 +08:00
|
|
|
const { setIsSubscribeOpen, setIsSuccessOpen } = useNewsContext()
|
2025-03-03 05:16:31 +08:00
|
|
|
const fetcher = useFetcher()
|
2025-03-03 08:41:38 +08:00
|
|
|
const [error, setError] = useState<string>()
|
2025-03-07 12:11:48 +08:00
|
|
|
const loaderData = useRouteLoaderData<typeof loader>('routes/_news')
|
2025-03-09 09:32:51 +08:00
|
|
|
const { subscriptionsData: subscriptions } = loaderData || {}
|
2025-03-03 05:16:31 +08:00
|
|
|
|
|
|
|
|
const formMethods = useRemixForm<TSubscribeSchema>({
|
|
|
|
|
mode: 'onSubmit',
|
|
|
|
|
fetcher,
|
|
|
|
|
resolver: zodResolver(subscribeSchema),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const { handleSubmit } = formMethods
|
|
|
|
|
|
2025-03-03 08:41:38 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!fetcher.data?.success) {
|
|
|
|
|
setError(fetcher.data?.message)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setError(undefined)
|
|
|
|
|
setIsSubscribeOpen(false)
|
|
|
|
|
setIsSuccessOpen('payment')
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [fetcher])
|
|
|
|
|
|
2025-02-21 20:47:15 +07:00
|
|
|
return (
|
2025-02-22 15:15:04 +07:00
|
|
|
<div className="flex flex-col items-center justify-center">
|
2025-03-03 05:16:31 +08:00
|
|
|
<RemixFormProvider {...formMethods}>
|
|
|
|
|
<fetcher.Form
|
|
|
|
|
method="post"
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
className="w-full max-w-md"
|
2025-03-03 08:41:38 +08:00
|
|
|
action="/actions/subscribe"
|
2025-03-03 05:16:31 +08:00
|
|
|
>
|
2025-03-04 09:00:10 +08:00
|
|
|
<Combobox
|
2025-03-03 05:16:31 +08:00
|
|
|
id="subscribe_plan"
|
|
|
|
|
name="subscribe_plan"
|
|
|
|
|
label="Subscription"
|
|
|
|
|
placeholder="Pilih Subscription"
|
|
|
|
|
options={subscriptions}
|
|
|
|
|
/>
|
|
|
|
|
|
2025-03-03 08:41:38 +08:00
|
|
|
{error && (
|
|
|
|
|
<div className="text-sm text-red-500 capitalize">{error}</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-03-03 05:16:31 +08:00
|
|
|
<Button
|
2025-03-11 06:00:49 +08:00
|
|
|
isLoading={fetcher.state !== 'idle'}
|
|
|
|
|
disabled={fetcher.state !== 'idle'}
|
2025-03-03 05:16:31 +08:00
|
|
|
type="submit"
|
2025-03-11 05:51:16 +08:00
|
|
|
className="mt-5 w-full rounded-md py-2"
|
2025-03-03 05:16:31 +08:00
|
|
|
>
|
2025-02-22 15:15:04 +07:00
|
|
|
Lanjutkan
|
|
|
|
|
</Button>
|
2025-03-03 05:16:31 +08:00
|
|
|
</fetcher.Form>
|
|
|
|
|
</RemixFormProvider>
|
2025-02-21 20:47:15 +07:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|