WellNuo/admin/lib/api.js
Sergei d530695b8b Add Add Beneficiary Flow for web admin
- Add AddBeneficiaryModal component with form fields for name, address, phone
- Add createBeneficiary and updateBeneficiary API methods in admin/lib/api.js
- Update Beneficiaries page with Add button and modal integration
- Add comprehensive tests for AddBeneficiaryModal (15 test cases)

Features:
- Modal with form validation (name required)
- Keyboard support (Escape to close, Enter to submit)
- Click outside to dismiss
- Loading state during submission
- Error handling and display
- Form resets on modal reopen
- Automatic redirect to beneficiary detail after creation
2026-02-01 08:32:59 -08:00

86 lines
2.5 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 createBeneficiary = (data) =>
apiRequest('/api/me/beneficiaries', {
method: 'POST',
body: JSON.stringify(data),
});
export const updateBeneficiary = (id, data) =>
apiRequest(`/api/me/beneficiaries/${id}`, {
method: 'PATCH',
body: JSON.stringify(data),
});
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}`);