hata ayıklama ve kod temizliği
This commit is contained in:
@@ -32,23 +32,34 @@ const userSchema = z.object({
|
||||
firstName: z.string().min(2, "Ad en az 2 karakter olmalıdır."),
|
||||
lastName: z.string().min(2, "Soyad en az 2 karakter olmalıdır."),
|
||||
email: z.string().email("Geçerli bir e-posta adresi giriniz."),
|
||||
password: z.string().optional(), // Password is optional on edit
|
||||
password: z.string().optional(),
|
||||
confirmPassword: z.string().optional(),
|
||||
role: z.enum(["admin", "user"]),
|
||||
phone: z.string().optional(),
|
||||
}).refine((data) => {
|
||||
}).superRefine((data, ctx) => {
|
||||
// 1. Password match check
|
||||
if (data.password && data.password !== data.confirmPassword) {
|
||||
return false;
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Şifreler eşleşmiyor.",
|
||||
path: ["confirmPassword"],
|
||||
});
|
||||
}
|
||||
// 2. New user password requirement check (simplified here, but strict would check id)
|
||||
// We handle the "required" part in onSubmit manually for now as per previous logic,
|
||||
// but the matching check is now here.
|
||||
return true
|
||||
}, {
|
||||
message: "Şifreler eşleşmiyor.",
|
||||
path: ["confirmPassword"],
|
||||
})
|
||||
|
||||
// 2. New user password requirement check
|
||||
// If there is no ID (we can't easily check for ID here in schema without context,
|
||||
// but typically empty password on CREATE is invalid unless handled elsewhere.
|
||||
// However, the component logic implies 'initialData' determines edit/create mode.
|
||||
// For pure schema validation, we often make password required for create, optional for edit.
|
||||
// Since we don't pass 'isCreate' to schema, we can enforce minimum length if provided.
|
||||
if (data.password && data.password.length > 0 && data.password.length < 6) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Şifre en az 6 karakter olmalıdır.",
|
||||
path: ["password"],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
type UserFormValues = z.infer<typeof userSchema>
|
||||
|
||||
@@ -113,12 +124,8 @@ export function UserForm({ initialData, mode = "admin" }: UserFormProps) {
|
||||
})
|
||||
} else {
|
||||
// Admin create mode
|
||||
if (!data.password || data.password.length < 6) {
|
||||
toast.error("Yeni kullanıcı için şifre gereklidir (min 6 karakter).")
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
result = await createUser(data.firstName, data.lastName, data.email, data.password, data.role, data.phone)
|
||||
// Password requirement is now handled by Zod Schema
|
||||
result = await createUser(data.firstName, data.lastName, data.email, data.password!, data.role, data.phone)
|
||||
}
|
||||
|
||||
if (result.error) {
|
||||
@@ -136,7 +143,7 @@ export function UserForm({ initialData, mode = "admin" }: UserFormProps) {
|
||||
if (mode === "admin") {
|
||||
router.push("/dashboard/users")
|
||||
}
|
||||
} catch (error) {
|
||||
} catch {
|
||||
toast.error("Bir sorun oluştu.")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
|
||||
Reference in New Issue
Block a user