WellNuo/admin/lib/api.js
Sergei bda883d34d Add Beneficiary Detail Page with tabs and status components
- Create BeneficiaryDetailPage with Overview, Sensors, and Activity tabs
- Add StatusBadge and SensorStatusBadge UI components
- Add Tabs component (Tabs, TabsList, TabsTrigger, TabsContent)
- Add getBeneficiary API method
- Include comprehensive tests for all new components
- Update ESLint config with root:true to prevent config inheritance

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-01 08:26:31 -08:00

76 lines
2.3 KiB
JavaScript

const API_URL = process.env.NEXT_PUBLIC_API_URL || 'https://wellnuo.smartlaunchhub.com';
export async function apiRequest(endpoint, options = {}) {
const token = typeof window !== 'undefined' ? localStorage.getItem('adminToken') : null;
const headers = {
'Content-Type': 'application/json',
...options.headers,
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const res = await fetch(`${API_URL}${endpoint}`, {
...options,
headers,
});
if (res.status === 401) {
if (typeof window !== 'undefined') {
localStorage.removeItem('adminToken');
window.location.href = '/admin/login';
}
throw new Error('Unauthorized');
}
const data = await res.json();
if (!res.ok) {
throw new Error(data.error || 'Request failed');
}
return data;
}
// Auth
export const requestOTP = (email) =>
apiRequest('/api/auth/request-otp', {
method: 'POST',
body: JSON.stringify({ email }),
});
export const verifyOTP = (email, code) =>
apiRequest('/api/auth/verify-otp', {
method: 'POST',
body: JSON.stringify({ email, code }),
});
export const getMe = () => apiRequest('/api/auth/me');
// Admin endpoints
export const getStats = () => apiRequest('/api/admin/stats');
export const getOrders = (status) => apiRequest(`/api/admin/orders${status ? `?status=${status}` : ''}`);
export const getOrder = (id) => apiRequest(`/api/admin/orders/${id}`);
export const updateOrder = (id, data) =>
apiRequest(`/api/admin/orders/${id}`, {
method: 'PATCH',
body: JSON.stringify(data),
});
export const getUsers = () => apiRequest('/api/admin/users');
export const getUser = (id) => apiRequest(`/api/admin/users/${id}`);
export const getBeneficiaries = () => apiRequest('/api/admin/beneficiaries');
export const getBeneficiary = (id) => apiRequest(`/api/admin/beneficiaries/${id}`);
export const getSubscriptions = () => apiRequest('/api/admin/subscriptions');
// Deployments
export const getDeployments = () => apiRequest('/api/admin/deployments');
export const getDeployment = (id) => apiRequest(`/api/admin/deployments/${id}`);
// Devices
export const getDevices = (wellId) => apiRequest(`/api/admin/devices${wellId ? `?well_id=${wellId}` : ''}`);
export const getDevice = (id) => apiRequest(`/api/admin/devices/${id}`);