2026-08-18 13:33:22 +08:00
|
|
|
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"
|
2026-08-18 13:33:22 +08:00
|
|
|
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"
|
2026-08-18 13:33:22 +08:00
|
|
|
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"
|
|
|
|
|
|
2026-08-18 13:33:22 +08:00
|
|
|
export type EditInfoValues = {
|
2026-09-04 13:21:47 +08:00
|
|
|
qq: string
|
|
|
|
|
wechat: string
|
2026-09-08 16:19:40 +08:00
|
|
|
// taobao: string
|
2026-08-18 13:33:22 +08:00
|
|
|
email: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const editInfoSchema = z.object({
|
2026-09-04 13:21:47 +08:00
|
|
|
qq: z.string(),
|
|
|
|
|
wechat: z.string(),
|
2026-09-08 16:19:40 +08:00
|
|
|
// taobao: z.string(),
|
2026-08-18 13:33:22 +08:00
|
|
|
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
|
2026-08-18 13:33:22 +08:00
|
|
|
initialData?: EditInfoValues
|
2026-09-04 13:21:47 +08:00
|
|
|
onSaved?: () => void
|
2026-08-18 13:33:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const DEFAULT_INITIAL_DATA: EditInfoValues = {
|
|
|
|
|
qq: "",
|
2026-09-04 13:21:47 +08:00
|
|
|
wechat: "",
|
2026-09-08 16:19:40 +08:00
|
|
|
// taobao: "",
|
2026-09-04 13:21:47 +08:00
|
|
|
email: "",
|
2026-07-30 13:55:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function EditInfoDialog({
|
|
|
|
|
open,
|
|
|
|
|
onOpenChange,
|
2026-08-18 13:33:22 +08:00
|
|
|
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
|
|
|
|
2026-08-18 13:33:22 +08:00
|
|
|
const form = useForm<EditInfoValues>({
|
|
|
|
|
resolver: zodResolver(editInfoSchema),
|
|
|
|
|
defaultValues: initialData,
|
|
|
|
|
})
|
2026-07-30 13:55:28 +08:00
|
|
|
|
2026-08-18 13:33:22 +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,
|
2026-09-08 16:19:40 +08:00
|
|
|
// TaoBao: data.taobao,
|
2026-09-04 13:21:47 +08:00
|
|
|
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>
|
|
|
|
|
|
2026-08-18 13:33:22 +08:00
|
|
|
<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>
|
2026-08-18 13:33:22 +08:00
|
|
|
<Controller
|
2026-09-04 13:21:47 +08:00
|
|
|
name="qq"
|
2026-08-18 13:33:22 +08:00
|
|
|
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-08-18 13:33:22 +08:00
|
|
|
)}
|
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>
|
2026-08-18 13:33:22 +08:00
|
|
|
<Controller
|
2026-09-04 13:21:47 +08:00
|
|
|
name="wechat"
|
2026-08-18 13:33:22 +08:00
|
|
|
control={form.control}
|
|
|
|
|
render={({ field }) => (
|
2026-08-26 16:55:22 +08:00
|
|
|
<Input {...field} className="h-11 bg-gray-50 border-gray-200" />
|
2026-08-18 13:33:22 +08:00
|
|
|
)}
|
2026-07-30 13:55:28 +08:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-09-08 16:19:40 +08:00
|
|
|
{/* <div className="space-y-1.5">
|
2026-09-04 13:21:47 +08:00
|
|
|
<Label className="text-sm text-gray-600">淘宝会员名</Label>
|
2026-08-18 13:33:22 +08:00
|
|
|
<Controller
|
2026-09-04 13:21:47 +08:00
|
|
|
name="taobao"
|
2026-08-18 13:33:22 +08:00
|
|
|
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-08-18 13:33:22 +08:00
|
|
|
)}
|
|
|
|
|
/>
|
2026-09-08 16:19:40 +08:00
|
|
|
</div> */}
|
2026-07-30 13:55:28 +08:00
|
|
|
|
|
|
|
|
<div className="space-y-1.5">
|
|
|
|
|
<Label className="text-sm text-gray-600">邮箱</Label>
|
2026-08-18 13:33:22 +08:00
|
|
|
<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
|
2026-08-18 13:33:22 +08:00
|
|
|
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>
|
2026-08-18 13:33:22 +08:00
|
|
|
</form>
|
2026-07-30 13:55:28 +08:00
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|