"use client"; import { useForm } from "react-hook-form"; import { useMutation, useQuery } from "@tanstack/react-query"; import { toast } from "sonner"; import { communicationApi } from "@/modules/communication/services/communication-api"; import { useTenantId } from "@/hooks/useTenantId"; import { CommunicationPageLoader, CommunicationPageError } from "@/modules/communication/pages/shared"; import { PageHeader, Card, CardContent, Button, FormField, Input, Textarea, Select } from "@/components/ds"; import { CommunicationBreadcrumbs } from "@/modules/communication/components/CommunicationBreadcrumbs"; import { useCommunicationPermissions } from "@/modules/communication/hooks/useCommunicationPermissions"; import { PermissionDeniedState } from "@/src/shared/ui"; export function SendMessagePage() { const { tenantId } = useTenantId(); const perms = useCommunicationPermissions(); const form = useForm({ defaultValues: { channel: "sms", to_address: "", body: "", template_key: "", priority: "normal", correlation_id: "", scheduled_at: "", }, }); const templatesQ = useQuery({ queryKey: ["communication", tenantId, "templates-select"], queryFn: () => communicationApi.templates.list(tenantId!), enabled: !!tenantId, }); const sendM = useMutation({ mutationFn: (body: Record) => communicationApi.messages.send(tenantId!, body), onSuccess: (data) => { toast.success(`${data.length} پیام در صف قرار گرفت`); form.reset({ channel: "sms", priority: "normal", to_address: "", body: "", template_key: "", correlation_id: "", scheduled_at: "" }); }, onError: (e: Error) => toast.error(e.message), }); if (!tenantId || templatesQ.isLoading) return ; if (!perms.can("communication.messages.send")) return ; return (
{ const body: Record = { channel: d.channel, to_address: d.to_address, priority: d.priority, process_immediately: !d.scheduled_at, }; if (d.body) body.body = d.body; if (d.template_key) body.template_key = d.template_key; if (d.correlation_id) body.correlation_id = d.correlation_id; if (d.scheduled_at) body.scheduled_at = new Date(d.scheduled_at).toISOString(); sendM.mutate(body); })} >