208 lines
7.2 KiB
TypeScript
208 lines
7.2 KiB
TypeScript
"use client"
|
||
|
||
import type React from "react"
|
||
|
||
import { useState } from "react"
|
||
import { useAuth } from "@/contexts/auth-context"
|
||
import { Button } from "@/components/ui/button"
|
||
import { Input } from "@/components/ui/input"
|
||
import { Label } from "@/components/ui/label"
|
||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"
|
||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
|
||
import { Alert, AlertDescription } from "@/components/ui/alert"
|
||
import { CheckCircle, AlertTriangle, Loader2, Eye, EyeOff, Lock } from "lucide-react"
|
||
|
||
interface RegisterDialogProps {
|
||
open: boolean
|
||
onOpenChange: (open: boolean) => void
|
||
}
|
||
|
||
export function RegisterDialog({ open, onOpenChange }: RegisterDialogProps) {
|
||
const { register } = useAuth()
|
||
const [formData, setFormData] = useState({
|
||
name: "",
|
||
email: "",
|
||
password: "",
|
||
confirmPassword: "",
|
||
department: "",
|
||
})
|
||
const [isLoading, setIsLoading] = useState(false)
|
||
const [error, setError] = useState("")
|
||
const [showPassword, setShowPassword] = useState(false)
|
||
const [showConfirmPassword, setShowConfirmPassword] = useState(false)
|
||
const [success, setSuccess] = useState(false)
|
||
|
||
const departments = ["ACBU", "AUBU", "FAB3", "FNBU", "HQBU", "HRBU", "IBU", "ICDU", "ICBU", "ITBU", "MBU1", "MBU5", "PJA", "PBU", "SBG", "SBU", "董事會", "法務室", "關係企業發展", "稽核室", "總經理室"]
|
||
|
||
const handleSubmit = async (e: React.FormEvent) => {
|
||
e.preventDefault()
|
||
setError("")
|
||
|
||
if (formData.password !== formData.confirmPassword) {
|
||
setError("密碼確認不符")
|
||
return
|
||
}
|
||
|
||
if (formData.password.length < 6) {
|
||
setError("密碼至少需要6個字符")
|
||
return
|
||
}
|
||
|
||
setIsLoading(true)
|
||
|
||
try {
|
||
await register({
|
||
name: formData.name,
|
||
email: formData.email,
|
||
password: formData.password,
|
||
department: formData.department,
|
||
})
|
||
setSuccess(true)
|
||
setTimeout(() => {
|
||
setSuccess(false)
|
||
onOpenChange(false)
|
||
setFormData({
|
||
name: "",
|
||
email: "",
|
||
password: "",
|
||
confirmPassword: "",
|
||
department: "",
|
||
})
|
||
}, 2000)
|
||
} catch (err) {
|
||
setError("註冊失敗,請稍後再試")
|
||
} finally {
|
||
setIsLoading(false)
|
||
}
|
||
}
|
||
|
||
return (
|
||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||
<DialogContent className="sm:max-w-md">
|
||
<DialogHeader>
|
||
<DialogTitle>註冊帳號</DialogTitle>
|
||
<DialogDescription>填寫以下資訊創建您的帳號</DialogDescription>
|
||
</DialogHeader>
|
||
|
||
{success ? (
|
||
<div className="text-center py-6">
|
||
<CheckCircle className="w-16 h-16 text-green-500 mx-auto mb-4" />
|
||
<h3 className="text-lg font-semibold text-green-700 mb-2">註冊成功!</h3>
|
||
<p className="text-gray-600">您的帳號已創建,請等待管理員審核。</p>
|
||
</div>
|
||
) : (
|
||
<form onSubmit={handleSubmit} className="space-y-4">
|
||
{error && (
|
||
<Alert variant="destructive">
|
||
<AlertTriangle className="h-4 w-4" />
|
||
<AlertDescription>{error}</AlertDescription>
|
||
</Alert>
|
||
)}
|
||
|
||
<div className="space-y-2">
|
||
<Label htmlFor="name">姓名</Label>
|
||
<Input
|
||
id="name"
|
||
type="text"
|
||
value={formData.name}
|
||
onChange={(e) => setFormData({ ...formData, name: e.target.value })}
|
||
required
|
||
/>
|
||
</div>
|
||
|
||
<div className="space-y-2">
|
||
<Label htmlFor="email">電子郵件</Label>
|
||
<Input
|
||
id="email"
|
||
type="email"
|
||
value={formData.email}
|
||
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
||
required
|
||
/>
|
||
</div>
|
||
|
||
<div className="space-y-2">
|
||
<Label htmlFor="department">部門</Label>
|
||
<Select
|
||
value={formData.department}
|
||
onValueChange={(value) => setFormData({ ...formData, department: value })}
|
||
>
|
||
<SelectTrigger>
|
||
<SelectValue placeholder="選擇部門" />
|
||
</SelectTrigger>
|
||
<SelectContent>
|
||
{departments.map((dept) => (
|
||
<SelectItem key={dept} value={dept}>
|
||
{dept}
|
||
</SelectItem>
|
||
))}
|
||
</SelectContent>
|
||
</Select>
|
||
</div>
|
||
|
||
<div className="space-y-2">
|
||
<Label htmlFor="password">密碼</Label>
|
||
<div className="relative">
|
||
<Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
|
||
<Input
|
||
id="password"
|
||
type={showPassword ? "text" : "password"}
|
||
value={formData.password}
|
||
onChange={(e) => setFormData({ ...formData, password: e.target.value })}
|
||
className="pl-10 pr-10"
|
||
required
|
||
/>
|
||
<button
|
||
type="button"
|
||
onClick={() => setShowPassword(!showPassword)}
|
||
className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
||
>
|
||
{showPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="space-y-2">
|
||
<Label htmlFor="confirmPassword">確認密碼</Label>
|
||
<div className="relative">
|
||
<Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
|
||
<Input
|
||
id="confirmPassword"
|
||
type={showConfirmPassword ? "text" : "password"}
|
||
value={formData.confirmPassword}
|
||
onChange={(e) => setFormData({ ...formData, confirmPassword: e.target.value })}
|
||
className="pl-10 pr-10"
|
||
required
|
||
/>
|
||
<button
|
||
type="button"
|
||
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
|
||
className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
||
>
|
||
{showConfirmPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="flex justify-end space-x-3 pt-4">
|
||
<Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
|
||
取消
|
||
</Button>
|
||
<Button type="submit" disabled={isLoading}>
|
||
{isLoading ? (
|
||
<>
|
||
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
|
||
註冊中...
|
||
</>
|
||
) : (
|
||
"註冊"
|
||
)}
|
||
</Button>
|
||
</div>
|
||
</form>
|
||
)}
|
||
</DialogContent>
|
||
</Dialog>
|
||
)
|
||
}
|