2import { useState } from "react";
3import { fieldpine } from "@/lib/fieldpine";
5export default function ApiTest() {
6 const [testResults, setTestResults] = useState<string>("");
7 const [loading, setLoading] = useState(false);
9 const testEnvironmentVariables = () => {
11 setTestResults("=== ENVIRONMENT VARIABLES TEST ===\n");
16 // Test environment variables
17 results.push("Environment Variables:");
18 results.push(`NEXT_PUBLIC_FIELDPINE_BASE_URL: ${process.env.NEXT_PUBLIC_FIELDPINE_BASE_URL || 'Not set'}`);
19 results.push(`NEXT_PUBLIC_FIELDPINE_RETAILER_ID: ${process.env.NEXT_PUBLIC_FIELDPINE_RETAILER_ID || 'Not set'}`);
20 results.push(`NEXT_PUBLIC_APP_ENV: ${process.env.NEXT_PUBLIC_APP_ENV || 'Not set'}`);
21 results.push(`NEXT_PUBLIC_DEMO_MODE: ${process.env.NEXT_PUBLIC_DEMO_MODE || 'Not set'}`);
23 // Test API URL construction
24 const baseUrl = process.env.NEXT_PUBLIC_FIELDPINE_BASE_URL || "https://www.fieldpine.com";
25 const retailerId = process.env.NEXT_PUBLIC_FIELDPINE_RETAILER_ID || "1_2_3_4";
26 const apiUrl = `${baseUrl}/RetailAPI_${retailerId}`;
28 results.push("\n=== API CONFIGURATION ===");
29 results.push(`Base URL: ${baseUrl}`);
30 results.push(`Retailer ID: ${retailerId}`);
31 results.push(`Full API URL: ${apiUrl}`);
33 // Test fieldpine instance
34 results.push("\n=== FIELDPINE INSTANCE TEST ===");
36 const testUrl = fieldpine.buildUrl('/products', { limit: 1 });
37 results.push(`Built URL: ${testUrl}`);
38 results.push("✅ Fieldpine instance working");
40 results.push(`❌ Fieldpine instance error: ${error}`);
43 setTestResults(results.join('\n'));
45 setTestResults(`❌ Error: ${error}`);
51 const testApiCall = async () => {
53 setTestResults("=== API CALL TEST ===\n");
56 // Set demo authentication
57 fieldpine.setAuth("demo-api-key", "1_2_3_4");
60 results.push("Testing API call to /products endpoint...");
62 const url = fieldpine.buildUrl('/products', { limit: 2 });
63 results.push(`URL: ${url}`);
67 const response = await fieldpine.getProducts({ limit: 2 });
68 results.push("✅ API call successful");
69 results.push(`Response type: ${typeof response}`);
70 results.push(`Response: ${JSON.stringify(response, null, 2)}`);
71 } catch (apiError: any) {
72 results.push(`❌ API call failed: ${apiError.message}`);
73 results.push(`Error details: ${JSON.stringify(apiError, null, 2)}`);
75 // Try a direct fetch to see the raw response
77 const directResponse = await fetch(url);
78 results.push(`\nDirect fetch status: ${directResponse.status}`);
79 results.push(`Direct fetch ok: ${directResponse.ok}`);
80 const text = await directResponse.text();
81 results.push(`Direct fetch response: ${text.substring(0, 500)}`);
82 } catch (fetchError: any) {
83 results.push(`Direct fetch also failed: ${fetchError.message}`);
87 setTestResults(results.join('\n'));
88 } catch (error: any) {
89 setTestResults(`❌ Error: ${error.message}`);
95 const clearResults = () => {
100 <div className="p-6 bg-white rounded-lg shadow">
101 <h2 className="text-2xl font-bold mb-4">API Test Console</h2>
103 <div className="space-y-4">
104 <div className="flex flex-wrap gap-2">
106 onClick={testEnvironmentVariables}
108 className="px-4 py-2 bg-brand text-white rounded hover:opacity-90 disabled:opacity-50"
110 Test Environment Variables
114 onClick={testApiCall}
116 className="px-4 py-2 bg-success text-white rounded hover:opacity-90 disabled:opacity-50"
122 onClick={clearResults}
124 className="px-4 py-2 bg-surface border-2 border-border text-text rounded hover:bg-surface-2 disabled:opacity-50"
131 <div className="text-blue-600 font-medium">
137 <div className="mt-4">
138 <h3 className="font-semibold mb-2">Test Results:</h3>
139 <pre className="bg-gray-100 p-4 rounded text-sm overflow-auto max-h-96 whitespace-pre-wrap">