Files
juip-web/src/components/composites/dialogs/editInfoDialog.tsx

161 lines
4.2 KiB
TypeScript
Raw Normal View History

import { zodResolver } from "@hookform/resolvers/zod"
import { useEffect, useState } from "react"
import { Controller, useForm } from "react-hook-form"
2026-09-04 13:21:47 +08:00
import { toast } from "sonner"
import { z } from "zod"
2026-09-04 13:21:47 +08:00
import { updateUserInfo } from "@/api/user"
2026-07-30 13:55:28 +08:00
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { FieldError } from "@/components/ui/field"
2026-07-30 13:55:28 +08:00
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
export type EditInfoValues = {
2026-09-04 13:21:47 +08:00
qq: string
wechat: string
taobao: string
email: string
}
const editInfoSchema = z.object({
2026-09-04 13:21:47 +08:00
qq: z.string(),
wechat: z.string(),
taobao: z.string(),
email: z
.string()
.refine(
v => !v || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v),
"请输入正确的邮箱",
),
})
2026-07-30 13:55:28 +08:00
interface EditInfoDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
initialData?: EditInfoValues
2026-09-04 13:21:47 +08:00
onSaved?: () => void
}
const DEFAULT_INITIAL_DATA: EditInfoValues = {
qq: "",
2026-09-04 13:21:47 +08:00
wechat: "",
taobao: "",
email: "",
2026-07-30 13:55:28 +08:00
}
export function EditInfoDialog({
open,
onOpenChange,
initialData = DEFAULT_INITIAL_DATA,
2026-09-04 13:21:47 +08:00
onSaved,
2026-07-30 13:55:28 +08:00
}: EditInfoDialogProps) {
2026-09-04 13:21:47 +08:00
const [submitting, setSubmitting] = useState(false)
2026-07-30 13:55:28 +08:00
const form = useForm<EditInfoValues>({
resolver: zodResolver(editInfoSchema),
defaultValues: initialData,
})
2026-07-30 13:55:28 +08:00
useEffect(() => {
if (open) {
form.reset(initialData)
}
}, [open, initialData, form])
2026-09-04 13:21:47 +08:00
const onSubmit = async (data: EditInfoValues) => {
setSubmitting(true)
const res = await updateUserInfo({
QQ: data.qq,
Wx: data.wechat,
TaoBao: data.taobao,
Email: data.email,
})
setSubmitting(false)
if (!res.success) {
toast.error(res.message)
return
}
toast.success("保存成功")
2026-07-30 13:55:28 +08:00
onOpenChange(false)
2026-09-04 13:21:47 +08:00
onSaved?.()
2026-07-30 13:55:28 +08:00
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
2026-09-03 14:37:18 +08:00
<DialogContent className="max-h-[85dvh] overflow-y-auto sm:max-w-120 p-6">
2026-07-30 13:55:28 +08:00
<DialogHeader>
<DialogTitle className="text-xl font-semibold text-center">
编辑信息
</DialogTitle>
</DialogHeader>
<form className="space-y-4 mt-2" onSubmit={form.handleSubmit(onSubmit)}>
2026-07-30 13:55:28 +08:00
<div className="space-y-1.5">
2026-09-04 13:21:47 +08:00
<Label className="text-sm text-gray-600">QQ号</Label>
<Controller
2026-09-04 13:21:47 +08:00
name="qq"
control={form.control}
2026-09-04 13:21:47 +08:00
render={({ field }) => (
<Input {...field} className="h-11 bg-gray-50 border-gray-200" />
)}
2026-07-30 13:55:28 +08:00
/>
</div>
<div className="space-y-1.5">
2026-09-04 13:21:47 +08:00
<Label className="text-sm text-gray-600">微信号</Label>
<Controller
2026-09-04 13:21:47 +08:00
name="wechat"
control={form.control}
render={({ field }) => (
<Input {...field} className="h-11 bg-gray-50 border-gray-200" />
)}
2026-07-30 13:55:28 +08:00
/>
</div>
<div className="space-y-1.5">
2026-09-04 13:21:47 +08:00
<Label className="text-sm text-gray-600">淘宝会员名</Label>
<Controller
2026-09-04 13:21:47 +08:00
name="taobao"
control={form.control}
2026-09-04 13:21:47 +08:00
render={({ field }) => (
<Input {...field} className="h-11 bg-gray-50 border-gray-200" />
)}
/>
2026-07-30 13:55:28 +08:00
</div>
<div className="space-y-1.5">
<Label className="text-sm text-gray-600">邮箱</Label>
<Controller
name="email"
control={form.control}
render={({ field, fieldState }) => (
<>
<Input
{...field}
className="h-11 bg-gray-50 border-gray-200"
/>
<FieldError>{fieldState.error?.message}</FieldError>
</>
)}
2026-07-30 13:55:28 +08:00
/>
</div>
<Button
type="submit"
2026-09-04 13:21:47 +08:00
disabled={submitting}
2026-07-30 13:55:28 +08:00
className="w-full h-11 mt-2 bg-blue-600 hover:bg-blue-700 text-white font-medium"
>
2026-09-04 13:21:47 +08:00
{submitting ? "保存中..." : "保存"}
2026-07-30 13:55:28 +08:00
</Button>
</form>
2026-07-30 13:55:28 +08:00
</DialogContent>
</Dialog>
)
}