Files
juwan-frontend/components/login-dialog.tsx
T

96 lines
3.1 KiB
TypeScript

"use client"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { getCurrentUserForLogin } from "@/lib/api"
import { useAuthStore } from "@/store/auth"
import { useLoginDialogStore } from "@/store/login-dialog"
import { standardSchemaResolver } from "@hookform/resolvers/standard-schema"
import { useRouter } from "next/navigation"
import { useForm } from "react-hook-form"
import { z } from "zod"
const loginSchema = z.object({
phone: z.string().min(11, "请输入正确的手机号"),
password: z.string().min(6, "密码至少6位"),
})
interface LoginDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
}
export function LoginDialog({ open, onOpenChange }: LoginDialogProps) {
const router = useRouter()
const { login } = useAuthStore()
const consumePendingAction = useLoginDialogStore((state) => state.consumePendingAction)
const {
register,
handleSubmit,
formState: { errors, isSubmitting },
} = useForm({
resolver: standardSchemaResolver(loginSchema),
})
const onSubmit = async (_data: z.infer<typeof loginSchema>) => {
await new Promise((r) => setTimeout(r, 500))
login(getCurrentUserForLogin(), ["consumer", "player", "owner"])
consumePendingAction()
onOpenChange(false)
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle></DialogTitle>
<DialogDescription></DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="dialog-phone"></Label>
<Input id="dialog-phone" placeholder="请输入手机号" {...register("phone")} />
{errors.phone && <p className="text-xs text-destructive">{errors.phone.message}</p>}
</div>
<div className="space-y-2">
<Label htmlFor="dialog-password"></Label>
<Input
id="dialog-password"
type="password"
placeholder="请输入密码"
{...register("password")}
/>
{errors.password && (
<p className="text-xs text-destructive">{errors.password.message}</p>
)}
</div>
<div className="flex gap-2">
<Button type="submit" className="flex-1" disabled={isSubmitting}>
{isSubmitting ? "登录中..." : "登录"}
</Button>
<Button
type="button"
variant="outline"
className="flex-1"
onClick={() => {
onOpenChange(false)
router.push("/register")
}}
>
</Button>
</div>
</form>
</DialogContent>
</Dialog>
)
}