Refactor: Improve API status checks using useQuery and dedicated status endpoints.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: e931b5ab-041b-42e7-baf1-50017869cef6
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/e19c6a51-7e4c-4bb8-a6a6-46dc00f0ec99/ce46d8f4-e248-4e5c-b779-b068dd48b0a0.jpg
This commit is contained in:
Magnus-SmariSma
2025-03-20 22:40:01 +00:00
parent 4e15b5f2c0
commit f7478b0880
2 changed files with 79 additions and 27 deletions

View File

@ -12,41 +12,51 @@ export default function ApiStatus({ citesToken }: ApiStatusProps) {
const [iucnStatus, setIucnStatus] = useState<'checking' | 'connected' | 'error'>('checking'); const [iucnStatus, setIucnStatus] = useState<'checking' | 'connected' | 'error'>('checking');
const [citesStatus, setCitesStatus] = useState<'checking' | 'connected' | 'error'>('checking'); const [citesStatus, setCitesStatus] = useState<'checking' | 'connected' | 'error'>('checking');
// Check CITES API status based on token availability // Check CITES API status
useEffect(() => { const {
if (citesToken) { data: citesApiData,
setCitesStatus('connected'); isLoading: isCitesLoading
} else { } = useQuery({
setCitesStatus('error'); queryKey: ['api-status-check-cites'],
} queryFn: async () => {
}, [citesToken]); return await apiClient.checkCitesApiStatus();
},
retry: 1,
staleTime: 60000, // 1 minute
});
// Check IUCN API status by making a test request // Check IUCN API status
const { isError, isSuccess, isLoading } = useQuery({ const {
data: iucnApiData,
isLoading: isIucnLoading
} = useQuery({
queryKey: ['api-status-check-iucn'], queryKey: ['api-status-check-iucn'],
queryFn: async () => { queryFn: async () => {
try { return await apiClient.checkIucnApiStatus();
// Make a simple request to check if the IUCN API is responding
// Using a well-known species for testing
const response = await apiClient.getIucnSpeciesByName("Panthera leo");
return response;
} catch (error) {
throw new Error("Failed to connect to IUCN API");
}
}, },
retry: 1, retry: 1,
staleTime: 60000, // 1 minute staleTime: 60000, // 1 minute
}); });
useEffect(() => { useEffect(() => {
if (isLoading) { if (isCitesLoading) {
setIucnStatus('checking'); setCitesStatus('checking');
} else if (isError) { } else if (citesApiData?.connected) {
setIucnStatus('error'); setCitesStatus('connected');
} else if (isSuccess) { } else {
setIucnStatus('connected'); setCitesStatus('error');
} }
}, [isLoading, isError, isSuccess]); }, [isCitesLoading, citesApiData]);
useEffect(() => {
if (isIucnLoading) {
setIucnStatus('checking');
} else if (iucnApiData?.connected) {
setIucnStatus('connected');
} else {
setIucnStatus('error');
}
}, [isIucnLoading, iucnApiData]);
return ( return (
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
@ -65,7 +75,9 @@ export default function ApiStatus({ citesToken }: ApiStatusProps) {
<TooltipContent side="bottom"> <TooltipContent side="bottom">
{citesStatus === 'connected' {citesStatus === 'connected'
? 'CITES+ API is connected and working' ? 'CITES+ API is connected and working'
: 'CITES+ API is not connected. Please add your API token.'} : citesStatus === 'checking'
? 'Checking CITES+ API connection...'
: citesApiData?.message || 'CITES+ API is not connected. Please add your API token.'}
</TooltipContent> </TooltipContent>
</Tooltip> </Tooltip>
</TooltipProvider> </TooltipProvider>
@ -85,7 +97,9 @@ export default function ApiStatus({ citesToken }: ApiStatusProps) {
<TooltipContent side="bottom"> <TooltipContent side="bottom">
{iucnStatus === 'connected' {iucnStatus === 'connected'
? 'IUCN Red List API is connected and working' ? 'IUCN Red List API is connected and working'
: 'IUCN Red List API connection issue. Check the API key.'} : iucnStatus === 'checking'
? 'Checking IUCN Red List API connection...'
: iucnApiData?.message || 'IUCN Red List API connection issue. Check the API key.'}
</TooltipContent> </TooltipContent>
</Tooltip> </Tooltip>
</TooltipProvider> </TooltipProvider>

View File

@ -29,6 +29,12 @@ export interface TokenResponse {
token: string | null; token: string | null;
} }
export interface ApiStatusResponse {
success: boolean;
connected: boolean;
message?: string;
}
export interface SpeciesSearchResponse { export interface SpeciesSearchResponse {
taxon_concepts: TaxonConcept[]; taxon_concepts: TaxonConcept[];
pagination: { pagination: {
@ -155,6 +161,38 @@ export interface IucnMeasuresResponse {
// API client for interacting with our backend // API client for interacting with our backend
export const apiClient = { export const apiClient = {
// API Status methods
async checkIucnApiStatus(): Promise<ApiStatusResponse> {
try {
const response = await axios.get<ApiStatusResponse>("/api/iucn/status");
return response.data;
} catch (error: any) {
return {
success: false,
connected: false,
message: error.response?.data?.message || "Failed to check IUCN API status"
};
}
},
async checkCitesApiStatus(): Promise<ApiStatusResponse> {
try {
// We'll use the token endpoint to check if the CITES API is accessible
const token = await this.getToken();
return {
success: true,
connected: !!token,
message: token ? "CITES API is connected" : "CITES API token not found"
};
} catch (error: any) {
return {
success: false,
connected: false,
message: error.response?.data?.message || "Failed to check CITES API status"
};
}
},
// Token management // Token management
async saveToken(token: string): Promise<ApiResponse<any>> { async saveToken(token: string): Promise<ApiResponse<any>> {
try { try {