import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Switch } from "@/components/ui/switch"; import { Textarea } from "@/components/ui/textarea"; import { useToast } from "@/hooks/use-toast"; import { Send, Globe, AlertCircle, CheckCircle2, Copy } from "lucide-react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { apiRequest } from "@/lib/queryClient"; interface WebhookBapConfig { id: number; name: string; webhookUrl: string; description: string | null; isActive: boolean; createdAt: string; updatedAt: string; } export default function WebhookBAPConfig() { const { toast } = useToast(); const queryClient = useQueryClient(); const [formData, setFormData] = useState({ name: "Configuration BAP", webhookUrl: "", description: "", isActive: true }); const { data: config, isLoading } = useQuery({ queryKey: ['/api/webhook-bap-config'], }); const testMutation = useMutation({ mutationFn: async () => { if (!formData.webhookUrl) { throw new Error("URL webhook requise pour le test"); } const response = await fetch('/api/webhook-bap-config/test', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ webhookUrl: formData.webhookUrl }), credentials: 'include' }); if (!response.ok) { const error = await response.json(); throw new Error(error.error || 'Erreur lors du test'); } return response.json(); }, onSuccess: () => { toast({ title: "Test réussi", description: "Le webhook BAP répond correctement", }); }, onError: (error: any) => { toast({ title: "Échec du test", description: error.message, variant: "destructive", }); }, }); const saveMutation = useMutation({ mutationFn: async (data: typeof formData) => { return apiRequest('/api/webhook-bap-config', 'POST', data); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['/api/webhook-bap-config'] }); toast({ title: "Configuration sauvegardée", description: "La configuration du webhook BAP a été mise à jour", }); }, onError: (error: any) => { toast({ title: "Erreur de sauvegarde", description: error.message, variant: "destructive", }); }, }); useEffect(() => { if (config) { // Gérer à la fois les cas où l'API retourne un objet ou un array const configData = Array.isArray(config) && config.length > 0 ? config[0] : config; if (configData && typeof configData === 'object') { setFormData({ name: configData.name || "Configuration BAP", webhookUrl: configData.webhookUrl || "", description: configData.description || "", isActive: configData.isActive ?? true }); } } }, [config]); const handleSave = () => { if (!formData.webhookUrl.trim()) { toast({ title: "Erreur", description: "L'URL du webhook est requise", variant: "destructive", }); return; } if (!formData.webhookUrl.startsWith('http')) { toast({ title: "Erreur", description: "L'URL doit commencer par http:// ou https://", variant: "destructive", }); return; } saveMutation.mutate(formData); }; const handleTest = () => { if (!formData.webhookUrl.trim()) { toast({ title: "Erreur", description: "L'URL du webhook est requise pour effectuer le test", variant: "destructive", }); return; } testMutation.mutate(); }; const copyUrl = () => { navigator.clipboard.writeText(formData.webhookUrl); toast({ title: "URL copiée", description: "L'URL a été copiée dans le presse-papiers", }); }; if (isLoading) { return (
URL complète du webhook n8n qui recevra les fichiers PDF BAP