'use client'; import { getAuthMethodConfig, updateAuthMethodConfig } from '@/services/admin/authMethod'; 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 { Switch } from '@workspace/ui/components/switch'; import { Textarea } from '@workspace/ui/components/textarea'; 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'; const appleSchema = z.object({ enabled: z.boolean(), config: z .object({ team_id: z.string().optional(), key_id: z.string().optional(), client_id: z.string().optional(), client_secret: z.string().optional(), redirect_url: z.string().optional(), }) .optional(), }); type AppleFormData = z.infer; export default function AppleForm() { const t = useTranslations('auth-control'); const [open, setOpen] = useState(false); const [loading, setLoading] = useState(false); const { data, refetch } = useQuery({ queryKey: ['getAuthMethodConfig', 'apple'], queryFn: async () => { const { data } = await getAuthMethodConfig({ method: 'apple', }); return data.data; }, enabled: open, }); const form = useForm({ resolver: zodResolver(appleSchema), defaultValues: { enabled: false, config: { team_id: '', key_id: '', client_id: '', client_secret: '', redirect_url: '', }, }, }); useEffect(() => { if (data) { form.reset({ enabled: data.enabled || false, config: { team_id: data.config?.team_id || '', key_id: data.config?.key_id || '', client_id: data.config?.client_id || '', client_secret: data.config?.client_secret || '', redirect_url: data.config?.redirect_url || '', }, }); } }, [data, form]); async function onSubmit(values: AppleFormData) { setLoading(true); try { await updateAuthMethodConfig({ ...data, enabled: values.enabled, config: { ...data?.config, ...values.config, }, } as API.UpdateAuthMethodConfigRequest); toast.success(t('common.saveSuccess')); refetch(); setOpen(false); } catch (error) { toast.error(t('common.saveFailed')); } finally { setLoading(false); } } return (

{t('apple.title')}

{t('apple.description')}

{t('apple.title')}
( {t('apple.enable')} {t('apple.enableDescription')} )} /> ( {t('apple.teamId')} {t('apple.teamIdDescription')} )} /> ( {t('apple.keyId')} {t('apple.keyIdDescription')} )} /> ( {t('apple.clientId')} {t('apple.clientIdDescription')} )} /> ( {t('apple.clientSecret')}