'use client'; import { getCurrencyConfig, updateCurrencyConfig } from '@/services/admin/system'; import { zodResolver } from '@hookform/resolvers/zod'; import { useQuery } from '@tanstack/react-query'; import { Button } from '@workspace/ui/components/button'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@workspace/ui/components/form'; import { ScrollArea } from '@workspace/ui/components/scroll-area'; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, SheetTrigger, } from '@workspace/ui/components/sheet'; import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input'; import { Icon } from '@workspace/ui/custom-components/icon'; import { useTranslations } from 'next-intl'; import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { toast } from 'sonner'; import { z } from 'zod'; // Constants const EXCHANGE_RATE_HOST_URL = 'https://exchangerate.host'; const currencySchema = z.object({ access_key: z.string().optional(), currency_unit: z.string().min(1), currency_symbol: z.string().min(1), }); type CurrencyFormData = z.infer; export default function CurrencyConfig() { const t = useTranslations('system'); const [open, setOpen] = useState(false); const [loading, setLoading] = useState(false); const { data, refetch } = useQuery({ queryKey: ['getCurrencyConfig'], queryFn: async () => { const { data } = await getCurrencyConfig(); return data.data; }, enabled: open, // Only request data when the modal is open }); const form = useForm({ resolver: zodResolver(currencySchema), defaultValues: { access_key: '', currency_unit: 'USD', currency_symbol: '$', }, }); useEffect(() => { if (data) { form.reset(data); } }, [data, form]); async function onSubmit(values: CurrencyFormData) { setLoading(true); try { await updateCurrencyConfig(values as API.CurrencyConfig); toast.success(t('common.saveSuccess')); refetch(); setOpen(false); } catch (error) { toast.error(t('common.saveFailed')); } finally { setLoading(false); } } return (

{t('currency.title')}

{t('currency.description')}

{t('currency.title')}
( {t('currency.accessKey')} {t('currency.accessKeyDescription', { url: EXCHANGE_RATE_HOST_URL })} )} /> ( {t('currency.currencyUnit')} {t('currency.currencyUnitDescription')} )} /> ( {t('currency.currencySymbol')} {t('currency.currencySymbolDescription')} )} />
); }