2025-03-15 15:24:01 +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-15 15:24:01 +08:00
|
|
|
import { useFetcher } from 'react-router'
|
|
|
|
|
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
|
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
|
|
|
|
import { DialogNews } from '~/components/dialog/news'
|
|
|
|
|
import { Button } from '~/components/ui/button'
|
|
|
|
|
import { Input } from '~/components/ui/input'
|
|
|
|
|
import { useNewsContext } from '~/contexts/news'
|
|
|
|
|
|
|
|
|
|
export const loginSchema = z.object({
|
|
|
|
|
email: z.string().email('Email tidak valid'),
|
|
|
|
|
password: z.string().min(6, 'Kata sandi minimal 6 karakter'),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export type TLoginSchema = z.infer<typeof loginSchema>
|
|
|
|
|
|
|
|
|
|
export const DialogLogin = () => {
|
|
|
|
|
const {
|
|
|
|
|
setIsRegisterOpen,
|
|
|
|
|
setIsLoginOpen,
|
|
|
|
|
setIsForgetOpen,
|
|
|
|
|
setIsSubscribeOpen,
|
|
|
|
|
isLoginOpen,
|
|
|
|
|
} = useNewsContext()
|
|
|
|
|
const fetcher = useFetcher()
|
|
|
|
|
|
|
|
|
|
const formMethods = useRemixForm<TLoginSchema>({
|
|
|
|
|
mode: 'onSubmit',
|
|
|
|
|
fetcher,
|
|
|
|
|
resolver: zodResolver(loginSchema),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const { handleSubmit } = formMethods
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-03-15 17:27:49 +08:00
|
|
|
if (!fetcher.data?.success && fetcher.data?.message) {
|
|
|
|
|
toast.error(fetcher.data.message)
|
2025-03-15 15:24:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsLoginOpen(false)
|
|
|
|
|
|
2025-03-15 17:03:06 +08:00
|
|
|
if (fetcher.data?.user.subscribe?.subscribe_plan?.code === 'basic') {
|
2025-03-15 15:24:01 +08:00
|
|
|
setIsSubscribeOpen(true)
|
|
|
|
|
}
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2025-03-15 17:27:49 +08:00
|
|
|
}, [fetcher.data])
|
2025-03-15 15:24:01 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DialogNews
|
|
|
|
|
isOpen={isLoginOpen}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
if (fetcher.state === 'idle') {
|
|
|
|
|
setIsLoginOpen(false)
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
description="Selamat Datang, silakan daftarkan akun Anda untuk melanjutkan!"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-center justify-center">
|
|
|
|
|
<div className="w-full max-w-md">
|
|
|
|
|
<RemixFormProvider {...formMethods}>
|
|
|
|
|
<fetcher.Form
|
|
|
|
|
method="post"
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
className="space-y-4"
|
|
|
|
|
action="/actions/login"
|
|
|
|
|
>
|
|
|
|
|
<Input
|
|
|
|
|
id="email"
|
|
|
|
|
label="Email"
|
|
|
|
|
placeholder="Contoh: legal@legalgo.id"
|
|
|
|
|
name="email"
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Input
|
|
|
|
|
id="password"
|
|
|
|
|
label="Kata Sandi"
|
|
|
|
|
placeholder="Masukkan Kata Sandi"
|
|
|
|
|
name="password"
|
|
|
|
|
type="password"
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className="flex items-center justify-between text-sm">
|
|
|
|
|
<span className="text-gray-600">Lupa Kata Sandi?</span>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setIsLoginOpen(false)
|
|
|
|
|
setIsForgetOpen(true)
|
|
|
|
|
}}
|
|
|
|
|
variant="link"
|
|
|
|
|
size="fit"
|
|
|
|
|
>
|
|
|
|
|
Reset Kata Sandi
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
isLoading={fetcher.state !== 'idle'}
|
|
|
|
|
disabled={fetcher.state !== 'idle'}
|
|
|
|
|
type="submit"
|
|
|
|
|
className="w-full rounded-md py-2"
|
|
|
|
|
>
|
|
|
|
|
Masuk
|
|
|
|
|
</Button>
|
|
|
|
|
</fetcher.Form>
|
|
|
|
|
</RemixFormProvider>
|
|
|
|
|
|
|
|
|
|
{/* Link Daftar */}
|
|
|
|
|
<div className="mt-4 text-center text-sm">
|
|
|
|
|
Belum punya akun?{' '}
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setIsLoginOpen(false)
|
|
|
|
|
setIsRegisterOpen(true)
|
|
|
|
|
}}
|
|
|
|
|
variant="link"
|
|
|
|
|
size="fit"
|
|
|
|
|
>
|
|
|
|
|
Daftar Disini
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</DialogNews>
|
|
|
|
|
)
|
|
|
|
|
}
|