"use client"; import { useEffect, useState } from "react"; import { useParams } from "next/navigation"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { toast } from "@app/hooks/useToast"; import { useTranslations } from "next-intl"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { SettingsContainer, SettingsSection, SettingsSectionHeader, SettingsSectionTitle, SettingsSectionDescription, SettingsSectionBody, SettingsSectionFooter, SettingsSectionForm } from "@app/components/Settings"; import { Button } from "@app/components/ui/button"; import { Input } from "@app/components/ui/input"; import { Textarea } from "@app/components/ui/textarea"; import { Save } from "lucide-react"; const updateTemplateSchema = z.object({ name: z.string().min(1, "Name is required"), description: z.string().optional() }); type UpdateTemplateForm = z.infer; export default function GeneralPage() { const params = useParams(); const t = useTranslations(); const api = createApiClient(useEnvContext()); const [template, setTemplate] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const { register, handleSubmit, setValue, formState: { errors } } = useForm({ resolver: zodResolver(updateTemplateSchema) }); useEffect(() => { const fetchTemplate = async () => { if (!params.orgId || !params.templateId) return; try { const response = await api.get( `/org/${params.orgId}/rule-templates/${params.templateId}` ); setTemplate(response.data.data); setValue("name", response.data.data.name); setValue("description", response.data.data.description || ""); } catch (error) { toast({ title: t("ruleTemplateErrorLoad"), description: formatAxiosError(error, t("ruleTemplateErrorLoadDescription")), variant: "destructive" }); } finally { setLoading(false); } }; fetchTemplate(); }, [params.orgId, params.templateId, setValue, t]); const onSubmit = async (data: UpdateTemplateForm) => { if (!params.orgId || !params.templateId) return; setSaving(true); try { await api.put( `/org/${params.orgId}/rule-templates/${params.templateId}`, data ); toast({ title: "Template Updated", description: "Template details have been updated successfully. Changes to template rules will automatically propagate to all assigned resources.", variant: "default" }); } catch (error) { toast({ title: t("ruleTemplateErrorUpdate"), description: formatAxiosError(error, t("ruleTemplateErrorUpdateDescription")), variant: "destructive" }); } finally { setSaving(false); } }; if (loading) { return (
Loading...
); } if (!template) { return (
Template not found
); } return ( {t("templateDetails")} Update the name and description for this rule template.
{errors.name && (

{errors.name.message}

)}