优化组件页面代码
This commit is contained in:
@@ -67,9 +67,7 @@ export function BalanceDialog({
|
||||
|
||||
const onSubmit = async (data: BalanceFormValues) => {
|
||||
if (!currentUser) return
|
||||
|
||||
setIsLoading(true)
|
||||
|
||||
try {
|
||||
const result = await getBalance({
|
||||
user_id: currentUser.id,
|
||||
@@ -85,9 +83,8 @@ export function BalanceDialog({
|
||||
toast.error(result.message || "修改余额失败")
|
||||
}
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "网络错误,请稍后重试"
|
||||
toast.error(message)
|
||||
const message = error instanceof Error ? error.message : error
|
||||
toast.error(`网络错误,请稍后重试: ${message}`)
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
@@ -155,7 +152,7 @@ export function BalanceDialog({
|
||||
取消
|
||||
</Button>
|
||||
<Button type="submit" disabled={isLoading}>
|
||||
{isLoading ? "保存中..." : "保存"}
|
||||
{isLoading ? "保存中" : "保存"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
|
||||
@@ -35,9 +35,12 @@ import type { ProductDiscount } from "@/models/product_discount"
|
||||
// 表单验证规则
|
||||
const addUserSchema = z
|
||||
.object({
|
||||
username: z.string().min(1, "账号不能为空"),
|
||||
password: z.string().min(6, "密码至少6位"),
|
||||
confirmPassword: z.string().min(1, "请确认密码"),
|
||||
username: z.string().optional(),
|
||||
password: z
|
||||
.string()
|
||||
.optional()
|
||||
.refine(val => !val || val.length >= 6, { message: "密码至少6位" }),
|
||||
confirmPassword: z.string().optional(),
|
||||
phone: z.string().regex(/^1[3-9]\d{9}$/, "请输入正确的手机号格式"),
|
||||
email: z
|
||||
.string()
|
||||
@@ -53,10 +56,18 @@ const addUserSchema = z
|
||||
contact_qq: z.string().optional(),
|
||||
contact_wechat: z.string().optional(),
|
||||
})
|
||||
.refine(data => data.password === data.confirmPassword, {
|
||||
message: "两次输入的密码不一致",
|
||||
path: ["confirmPassword"],
|
||||
})
|
||||
.refine(
|
||||
data => {
|
||||
if (data.password) {
|
||||
return data.password === data.confirmPassword
|
||||
}
|
||||
return true
|
||||
},
|
||||
{
|
||||
message: "两次输入的密码不一致",
|
||||
path: ["confirmPassword"],
|
||||
},
|
||||
)
|
||||
|
||||
export type AddUserFormValues = z.infer<typeof addUserSchema>
|
||||
|
||||
@@ -147,9 +158,9 @@ export function AddUserDialog({
|
||||
|
||||
const onSubmit = handleSubmit(async data => {
|
||||
const payload = {
|
||||
username: data.username,
|
||||
password: data?.password,
|
||||
phone: data.phone,
|
||||
username: data?.username,
|
||||
password: data?.password,
|
||||
email: data?.email || "",
|
||||
name: data?.name,
|
||||
admin_id: data.admin_id ? Number(data.admin_id) : undefined,
|
||||
@@ -228,7 +239,10 @@ export function AddUserDialog({
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid}>
|
||||
<FieldLabel>用户密码</FieldLabel>
|
||||
<FieldLabel>
|
||||
用户密码
|
||||
<span className="text-gray-400 text-xs">(选填)</span>
|
||||
</FieldLabel>
|
||||
<Input
|
||||
{...field}
|
||||
type="password"
|
||||
|
||||
@@ -88,7 +88,6 @@ export default function UserPage() {
|
||||
)
|
||||
|
||||
const table = useDataTable<Cust>(fetchUsers)
|
||||
console.log(table, "客户管理table")
|
||||
|
||||
const onFilter = handleSubmit(data => {
|
||||
const result: FilterValues = {}
|
||||
@@ -106,10 +105,6 @@ export default function UserPage() {
|
||||
table.refresh()
|
||||
}, [table])
|
||||
|
||||
const handleAddUserSuccess = () => {
|
||||
refreshTable()
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<form onSubmit={onFilter} className="bg-white p-4">
|
||||
@@ -222,14 +217,7 @@ export default function UserPage() {
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
reset({
|
||||
account: "",
|
||||
name: "",
|
||||
identified: "all",
|
||||
enabled: "all",
|
||||
created_at_start: "",
|
||||
created_at_end: "",
|
||||
})
|
||||
reset()
|
||||
setFilters({})
|
||||
table.pagination.onPageChange(1)
|
||||
}}
|
||||
@@ -386,7 +374,7 @@ export default function UserPage() {
|
||||
<AddUserDialog
|
||||
open={isAddDialogOpen}
|
||||
onOpenChange={setIsAddDialogOpen}
|
||||
onSuccess={handleAddUserSuccess}
|
||||
onSuccess={refreshTable}
|
||||
/>
|
||||
|
||||
<UpdateDialog
|
||||
|
||||
@@ -37,7 +37,7 @@ import type { ProductDiscount } from "@/models/product_discount"
|
||||
const editUserSchema = z
|
||||
.object({
|
||||
id: z.number(),
|
||||
username: z.string().min(2, "用户名至少2个字符"),
|
||||
username: z.string().optional(),
|
||||
email: z.string().email("邮箱格式不正确").optional().or(z.literal("")),
|
||||
password: z.string().optional(),
|
||||
confirmPassword: z.string().optional(),
|
||||
@@ -230,7 +230,7 @@ export function UpdateDialog({
|
||||
control={control}
|
||||
render={({ field, fieldState }) => (
|
||||
<Field data-invalid={fieldState.invalid}>
|
||||
<FieldLabel>用户名 *</FieldLabel>
|
||||
<FieldLabel>用户名</FieldLabel>
|
||||
<Input {...field} placeholder="请输入用户名" />
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
@@ -258,7 +258,7 @@ export function UpdateDialog({
|
||||
<Input
|
||||
{...field}
|
||||
type="password"
|
||||
placeholder="留空则保持不变,修改请输入新密码(至少6位)"
|
||||
placeholder="选填,修改请输入新密码(至少6位)"
|
||||
/>
|
||||
<FieldError>{fieldState.error?.message}</FieldError>
|
||||
</Field>
|
||||
|
||||
Reference in New Issue
Block a user