import { useState, useEffect } from "react"; import { useMutation, useQuery } from "@tanstack/react-query"; import { queryClient } from "@/lib/queryClient"; import { apiClient } from "@/lib/api"; import { useToast } from "@/hooks/use-toast"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; interface AuthenticationPanelProps { token: string; onSave: () => void; } export default function AuthenticationPanel({ token, onSave }: AuthenticationPanelProps) { const { toast } = useToast(); const [citesToken, setCitesToken] = useState(token || ""); const [iucnToken, setIucnToken] = useState(""); const [activeTab, setActiveTab] = useState("cites"); // Fetch both tokens from the server const { data: tokenData } = useQuery({ queryKey: ['/api/token'], queryFn: async () => { return await apiClient.getTokens(); }, enabled: true, // Always fetch the tokens }); // Update local state when token data is fetched useEffect(() => { if (tokenData) { setCitesToken(tokenData.token || ""); setIucnToken(tokenData.iucnToken || ""); } }, [tokenData]); const saveTokenMutation = useMutation({ mutationFn: async (params: { citesToken: string; iucnToken?: string }) => { return await apiClient.saveToken(params.citesToken, params.iucnToken); }, onSuccess: (response) => { if (response.success) { queryClient.invalidateQueries({ queryKey: ['/api/token'] }); toast({ title: "Success", description: "API tokens have been saved successfully", }); onSave(); } else { toast({ title: "Error", description: response.message || "Failed to save API tokens", variant: "destructive", }); } }, onError: () => { toast({ title: "Error", description: "Failed to save API tokens", variant: "destructive", }); } }); const handleSaveTokens = () => { if (!citesToken.trim()) { toast({ title: "Validation Error", description: "Please enter a CITES API token", variant: "destructive", }); return; } saveTokenMutation.mutate({ citesToken, iucnToken: iucnToken.trim() ? iucnToken : undefined }); }; const handleGetCitesToken = () => { window.open("https://api.speciesplus.net/", "_blank"); }; const handleGetIucnToken = () => { window.open("https://apiv4.iucnredlist.org/api/v4/docs", "_blank"); }; return (

API Authentication

CITES+ API IUCN Red List API
setCitesToken(e.target.value)} />
The CITES+ API provides species listings, distributions, and legislation information.
setIucnToken(e.target.value)} />
The IUCN Red List API provides conservation status, threats, habitats, and conservation measures.
Both tokens are stored securely.
); }