2025-03-03 05:16:31 +08:00
|
|
|
import { zodResolver } from '@hookform/resolvers/zod'
|
2025-03-15 17:25:08 +08:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
import toast from 'react-hot-toast'
|
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-03-15 15:24:01 +08:00
|
|
|
import { DialogNews } from '~/components/dialog/news'
|
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
|
|
|
|
2025-03-15 15:24:01 +08:00
|
|
|
export const DialogSubscribePlan = () => {
|
|
|
|
|
const { setIsSubscribeOpen, setIsSuccessOpen, isSubscribeOpen } =
|
|
|
|
|
useNewsContext()
|
2025-03-03 05:16:31 +08:00
|
|
|
const fetcher = useFetcher()
|
2025-03-07 12:11:48 +08:00
|
|
|
const loaderData = useRouteLoaderData<typeof loader>('routes/_news')
|
2025-03-13 06:13:43 +08:00
|
|
|
const { subscribePlanData: subscribePlan } = 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) {
|
2025-03-15 17:25:08 +08:00
|
|
|
toast.error(fetcher.data?.message)
|
2025-03-03 08:41:38 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsSubscribeOpen(false)
|
|
|
|
|
setIsSuccessOpen('payment')
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [fetcher])
|
|
|
|
|
|
2025-02-21 20:47:15 +07:00
|
|
|
return (
|
2025-03-15 15:24:01 +08:00
|
|
|
<DialogNews
|
|
|
|
|
isOpen={isSubscribeOpen}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
if (fetcher.state === 'idle') {
|
|
|
|
|
setIsSubscribeOpen(false)
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
description="Selamat Datang, silakan Pilih Subscribe Plan Anda untuk melanjutkan!"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex flex-col items-center justify-center">
|
|
|
|
|
<RemixFormProvider {...formMethods}>
|
|
|
|
|
<fetcher.Form
|
|
|
|
|
method="post"
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
className="w-full max-w-md"
|
|
|
|
|
action="/actions/subscribe"
|
|
|
|
|
>
|
|
|
|
|
<Combobox
|
|
|
|
|
id="subscribe_plan"
|
|
|
|
|
name="subscribe_plan"
|
|
|
|
|
label="Subscribe Plan"
|
|
|
|
|
placeholder="Pilih Subscribe Plan"
|
|
|
|
|
options={subscribePlan}
|
|
|
|
|
/>
|
2025-03-03 05:16:31 +08:00
|
|
|
|
2025-03-15 15:24:01 +08:00
|
|
|
<Button
|
|
|
|
|
isLoading={fetcher.state !== 'idle'}
|
|
|
|
|
disabled={fetcher.state !== 'idle'}
|
|
|
|
|
type="submit"
|
|
|
|
|
className="mt-5 w-full rounded-md py-2"
|
|
|
|
|
>
|
|
|
|
|
Lanjutkan
|
|
|
|
|
</Button>
|
|
|
|
|
</fetcher.Form>
|
|
|
|
|
</RemixFormProvider>
|
|
|
|
|
</div>
|
|
|
|
|
</DialogNews>
|
2025-02-21 20:47:15 +07:00
|
|
|
)
|
|
|
|
|
}
|