'use client'; import useGlobalStore from '@/config/use-global'; import { updateUserNotify } from '@/services/user/user'; import { zodResolver } from '@hookform/resolvers/zod'; import { Button } from '@workspace/airo-ui/components/button'; import { Card } from '@workspace/airo-ui/components/card'; import { useEffect } from 'react'; import { AiroButton } from '@workspace/airo-ui/components/AiroButton'; import { Form, FormControl, FormField, FormItem, FormLabel, } from '@workspace/airo-ui/components/form'; import { Switch } from '@workspace/airo-ui/components/switch'; import { useTranslations } from 'next-intl'; import { useForm } from 'react-hook-form'; import { toast } from 'sonner'; import { z } from 'zod'; const FormSchema = z.object({ enable_balance_notify: z.boolean().default(false), enable_login_notify: z.boolean().default(false), enable_subscribe_notify: z.boolean().default(false), enable_trade_notify: z.boolean().default(false), }); export default function NotifySettings() { const t = useTranslations('profile'); const { user, getUserInfo } = useGlobalStore(); const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { enable_balance_notify: user?.enable_balance_notify ?? false, enable_login_notify: user?.enable_login_notify ?? false, enable_subscribe_notify: user?.enable_subscribe_notify ?? false, enable_trade_notify: user?.enable_trade_notify ?? false, }, }); // 监听 user 变化,更新表单 useEffect(() => { if (user) { form.reset({ enable_balance_notify: user.enable_balance_notify ?? false, enable_login_notify: user.enable_login_notify ?? false, enable_subscribe_notify: user.enable_subscribe_notify ?? false, enable_trade_notify: user.enable_trade_notify ?? false, }); } }, [user, form]); async function onSubmit(data: z.infer) { await updateUserNotify(data); toast.success(t('notify.updateSuccess')); await getUserInfo(); } return (
{t('notify.notificationSettings')}
{' '} {t('notify.description')}
{[ { name: 'enable_balance_notify', label: 'balanceChange' }, { name: 'enable_login_notify', label: 'login' }, { name: 'enable_subscribe_notify', label: 'subscribe' }, { name: 'enable_trade_notify', label: 'finance' }, ].map(({ name, label }) => ( ( {t(`notify.${label}`)} )} /> ))}
); }