3import { useState } from 'react';
4import { cwaApi, setCWAApiKey } from '@/lib/cwa-gnap-api';
6export default function CWATestPage() {
7 const [apiKey, setApiKey] = useState('');
8 const [results, setResults] = useState<Record<string, any>>({});
9 const [loading, setLoading] = useState<Record<string, boolean>>({});
11 const handleSetApiKey = () => {
13 alert('API Key set successfully');
16 const testEndpoint = async (name: string, apiCall: () => Promise<any>) => {
17 setLoading(prev => ({ ...prev, [name]: true }));
19 const result = await apiCall();
20 setResults(prev => ({ ...prev, [name]: result }));
24 [name]: { error: error instanceof Error ? error.message : 'Unknown error' }
27 setLoading(prev => ({ ...prev, [name]: false }));
31 <div className="min-h-screen bg-bg p-8">
32 <div className="max-w-4xl mx-auto">
33 <h1 className="text-3xl font-bold text-text mb-8">
34 CWA GNAP API Test Dashboard
37 {/* API Key Section */}
38 <div className="bg-surface p-6 rounded-lg shadow-md mb-8">
39 <h2 className="text-xl font-semibold mb-4">Authentication</h2>
40 <div className="flex gap-4">
44 onChange={(e) => setApiKey(e.target.value)}
45 placeholder="Enter FieldpineApiKey (e.g., degviwnlnOGCoiZC8NE27LWFkl2VOVZkSghvZe2M7W)"
46 className="flex-1 p-2 border border-border rounded"
49 onClick={handleSetApiKey}
50 className="px-4 py-2 bg-brand text-white rounded hover:bg-brand"
55 <p className="text-sm text-muted mt-2">
56 Use the FieldpineApiKey cookie value from your browser when authenticated to CWA
60 {/* Test Endpoints Grid */}
61 <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-8">
63 <div className="bg-surface p-4 rounded-lg shadow">
64 <h3 className="font-semibold mb-2">Locations</h3>
66 onClick={() => testEndpoint('locations', () => cwaApi.getLocations())}
67 disabled={loading.locations}
68 className="w-full p-2 bg-success text-white rounded hover:bg-green-600 disabled:opacity-50"
70 {loading.locations ? 'Loading...' : 'Get Locations'}
75 <div className="bg-surface p-4 rounded-lg shadow">
76 <h3 className="font-semibold mb-2">Today's Stats</h3>
78 onClick={() => testEndpoint('stats', () => cwaApi.getStatsToday())}
79 disabled={loading.stats}
80 className="w-full p-2 bg-brand text-white rounded hover:bg-brand disabled:opacity-50"
82 {loading.stats ? 'Loading...' : 'Get Stats'}
87 <div className="bg-surface p-4 rounded-lg shadow">
88 <h3 className="font-semibold mb-2">Sales Totals</h3>
90 onClick={() => testEndpoint('sales', () => cwaApi.getSalesTotals({
93 field9: 'f110,4,month-0'
95 disabled={loading.sales}
96 className="w-full p-2 bg-purple-500 text-white rounded hover:bg-purple-600 disabled:opacity-50"
98 {loading.sales ? 'Loading...' : 'Get Sales'}
102 {/* Server Status */}
103 <div className="bg-surface p-4 rounded-lg shadow">
104 <h3 className="font-semibold mb-2">Server Status</h3>
106 onClick={() => testEndpoint('status', () => cwaApi.getServerStatus())}
107 disabled={loading.status}
108 className="w-full p-2 bg-danger text-white rounded hover:bg-red-600 disabled:opacity-50"
110 {loading.status ? 'Loading...' : 'Get Status'}
114 {/* User Interface */}
115 <div className="bg-surface p-4 rounded-lg shadow">
116 <h3 className="font-semibold mb-2">UI Components</h3>
118 onClick={() => testEndpoint('ui', () => cwaApi.getUserInterface('sshome'))}
119 disabled={loading.ui}
120 className="w-full p-2 bg-warn text-white rounded hover:bg-yellow-600 disabled:opacity-50"
122 {loading.ui ? 'Loading...' : 'Get UI'}
126 {/* Retail Config */}
127 <div className="bg-surface p-4 rounded-lg shadow">
128 <h3 className="font-semibold mb-2">Retail Config</h3>
130 onClick={() => testEndpoint('config', () => cwaApi.getRetailConfig())}
131 disabled={loading.config}
132 className="w-full p-2 bg-indigo-500 text-white rounded hover:bg-indigo-600 disabled:opacity-50"
134 {loading.config ? 'Loading...' : 'Get Config'}
139 {/* Results Section */}
140 <div className="bg-surface p-6 rounded-lg shadow">
141 <h2 className="text-xl font-semibold mb-4">API Responses</h2>
143 {Object.keys(results).length === 0 ? (
144 <p className="text-muted">No API calls made yet. Click a button above to test an endpoint.</p>
146 <div className="space-y-4">
147 {Object.entries(results).map(([name, result]) => (
148 <div key={name} className="border border-border rounded p-4">
149 <h3 className="font-semibold text-lg mb-2 capitalize">{name}</h3>
150 <pre className="bg-surface-2 p-3 rounded text-sm overflow-x-auto">
151 {JSON.stringify(result, null, 2)}
159 {/* Information Section */}
160 <div className="mt-8 bg-info/10 p-6 rounded-lg">
161 <h2 className="text-xl font-semibold mb-4">API Information</h2>
162 <div className="space-y-2 text-sm">
163 <p><strong>Base URL:</strong> https://iig.cwanz.online/GNAP/j</p>
164 <p><strong>Authentication:</strong> FieldpineApiKey cookie</p>
165 <p><strong>Request Method:</strong> GET</p>
166 <p><strong>BUCK Pattern:</strong> /BUCK?3=retailmax.elink.*</p>
169 <div className="mt-4">
170 <h3 className="font-semibold mb-2">Discovered Endpoints:</h3>
171 <ul className="text-sm text-text space-y-1">
172 <li>• BUCK?3=retailmax.elink.locations&101=1</li>
173 <li>• buck?3=retailmax.elink.stats.today</li>
174 <li>• buck?3=retailmax.elink.sale.totals&13=120&7=101&9=f110,4,month-0</li>
175 <li>• buck?3=retailmax.elink.userinterface&100=sshome</li>
176 <li>• buck?3=gds.server.status</li>
177 <li>• membership?id=cartridgeworld</li>
178 <li>• products?99=7</li>
180 <li>• RetailConfig</li>