2026-05-22 16:51:01 +00:00
|
|
|
import { FormEvent, useState } from 'react';
|
2026-06-08 21:19:57 +07:00
|
|
|
import { Eye, EyeOff } from 'lucide-react';
|
2026-05-22 16:51:01 +00:00
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
|
|
|
import { Input } from '@/components/ui/input';
|
|
|
|
|
import { Label } from '@/components/ui/label';
|
|
|
|
|
|
|
|
|
|
type AuthFormProps = {
|
|
|
|
|
mode: 'login' | 'register';
|
|
|
|
|
isSubmitting: boolean;
|
|
|
|
|
error: string | null;
|
|
|
|
|
googleOAuthEnabled: boolean;
|
|
|
|
|
onSubmit: (payload: { name?: string; email: string; password: string }) => Promise<unknown>;
|
2026-05-22 16:55:47 +00:00
|
|
|
onFieldChange?: () => void;
|
2026-05-22 16:51:01 +00:00
|
|
|
};
|
|
|
|
|
|
2026-05-22 16:55:47 +00:00
|
|
|
export function AuthForm({ mode, isSubmitting, error, googleOAuthEnabled, onSubmit, onFieldChange }: AuthFormProps) {
|
2026-05-22 16:51:01 +00:00
|
|
|
const [name, setName] = useState('');
|
|
|
|
|
const [email, setEmail] = useState('');
|
|
|
|
|
const [password, setPassword] = useState('');
|
2026-06-08 21:19:57 +07:00
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
2026-05-22 16:51:01 +00:00
|
|
|
|
|
|
|
|
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
await onSubmit({ name, email, password });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Card className="mx-auto w-full max-w-md">
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>{mode === 'login' ? 'Masuk ke ZeaVis Edu' : 'Buat akun ZeaVis Edu'}</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
{mode === 'login'
|
|
|
|
|
? 'Masuk untuk melihat riwayat diagnosis daun jagung Anda.'
|
|
|
|
|
: 'Daftar untuk menyimpan diagnosis dan mengikuti review pakar.'}
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
|
|
|
|
<form className="space-y-4" onSubmit={handleSubmit}>
|
|
|
|
|
{mode === 'register' && (
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="name">Nama</Label>
|
2026-05-22 16:55:47 +00:00
|
|
|
<Input id="name" value={name} onChange={(event) => {
|
|
|
|
|
setName(event.target.value);
|
|
|
|
|
onFieldChange?.();
|
|
|
|
|
}} required />
|
2026-05-22 16:51:01 +00:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="email">Email</Label>
|
2026-05-22 16:55:47 +00:00
|
|
|
<Input id="email" type="email" value={email} onChange={(event) => {
|
|
|
|
|
setEmail(event.target.value);
|
|
|
|
|
onFieldChange?.();
|
|
|
|
|
}} required />
|
2026-05-22 16:51:01 +00:00
|
|
|
</div>
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="password">Password</Label>
|
2026-06-08 21:19:57 +07:00
|
|
|
<div className="relative">
|
|
|
|
|
<Input id="password" type={showPassword ? "text" : "password"} minLength={8} value={password} onChange={(event) => {
|
|
|
|
|
setPassword(event.target.value);
|
|
|
|
|
onFieldChange?.();
|
|
|
|
|
}} required />
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground focus:outline-none"
|
|
|
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
|
|
|
>
|
|
|
|
|
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2026-05-22 16:51:01 +00:00
|
|
|
</div>
|
2026-05-22 16:55:47 +00:00
|
|
|
{error && <p className="text-sm text-red-600" role="alert">{error}</p>}
|
2026-05-22 16:51:01 +00:00
|
|
|
<Button className="w-full" type="submit" disabled={isSubmitting}>
|
|
|
|
|
{isSubmitting ? 'Memproses...' : mode === 'login' ? 'Masuk' : 'Daftar'}
|
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
{googleOAuthEnabled && (
|
|
|
|
|
<Button className="mt-3 w-full" variant="outline" asChild>
|
|
|
|
|
<a href="/api/v1/auth/google">Masuk dengan Google</a>
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|