161 lines
4.3 KiB
TypeScript
161 lines
4.3 KiB
TypeScript
|
|
import { Proposal, User } from '../types';
|
|
import { getApiBaseUrl } from '../config';
|
|
|
|
export const apiService = {
|
|
async fetchProposals(): Promise<Proposal[]> {
|
|
const apiUrl = getApiBaseUrl();
|
|
if (!apiUrl) return [];
|
|
|
|
try {
|
|
const response = await fetch(`${apiUrl}/proposals`);
|
|
if (!response.ok) throw new Error('Network response was not ok');
|
|
const data = await response.json();
|
|
return Array.isArray(data) ? data : [];
|
|
} catch (error) {
|
|
console.error('Error fetching proposals:', error);
|
|
return [];
|
|
}
|
|
},
|
|
|
|
async saveProposal(proposal: Proposal): Promise<boolean> {
|
|
const apiUrl = getApiBaseUrl();
|
|
if (!apiUrl) return false;
|
|
|
|
try {
|
|
console.log('[API Debug] Saving proposal:', { id: proposal.id, status: proposal.status, title: proposal.title });
|
|
|
|
const response = await fetch(`${apiUrl}/proposals`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(proposal)
|
|
});
|
|
|
|
const responseData = await response.json();
|
|
console.log('[API Debug] Save response:', responseData);
|
|
|
|
if (!response.ok) {
|
|
console.error('[API Debug] Save failed:', responseData);
|
|
throw new Error('Failed to save proposal');
|
|
}
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error saving proposal:', error);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
|
|
|
|
async getSettings(): Promise<any> {
|
|
const apiUrl = getApiBaseUrl();
|
|
if (!apiUrl) return null;
|
|
|
|
try {
|
|
const response = await fetch(`${apiUrl}/settings`);
|
|
if (!response.ok) throw new Error('Network response was not ok');
|
|
const data = await response.json();
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Error fetching settings:', error);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
async saveSetting(key: string, value: any): Promise<boolean> {
|
|
const apiUrl = getApiBaseUrl();
|
|
if (!apiUrl) return false;
|
|
|
|
try {
|
|
const response = await fetch(`${apiUrl}/settings`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ key, value })
|
|
});
|
|
|
|
if (!response.ok) throw new Error('Failed to save setting');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error saving setting:', error);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
// User CRUD operations
|
|
async fetchUsers(): Promise<User[]> {
|
|
const apiUrl = getApiBaseUrl();
|
|
if (!apiUrl) return [];
|
|
|
|
try {
|
|
const response = await fetch(`${apiUrl}/users`);
|
|
if (!response.ok) throw new Error('Network response was not ok');
|
|
const data = await response.json();
|
|
return Array.isArray(data) ? data : [];
|
|
} catch (error) {
|
|
console.error('Error fetching users:', error);
|
|
return [];
|
|
}
|
|
},
|
|
|
|
async loginUser(email: string, password: string): Promise<User | null> {
|
|
const apiUrl = getApiBaseUrl();
|
|
if (!apiUrl) return null;
|
|
|
|
try {
|
|
const response = await fetch(`${apiUrl}/users/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
console.error('Login failed:', error);
|
|
return null;
|
|
}
|
|
|
|
const data = await response.json();
|
|
return data.user || null;
|
|
} catch (error) {
|
|
console.error('Error logging in:', error);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
async saveUser(user: User): Promise<boolean> {
|
|
const apiUrl = getApiBaseUrl();
|
|
if (!apiUrl) return false;
|
|
|
|
try {
|
|
const response = await fetch(`${apiUrl}/users`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(user)
|
|
});
|
|
|
|
if (!response.ok) throw new Error('Failed to save user');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error saving user:', error);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
async deleteUser(userId: string): Promise<boolean> {
|
|
const apiUrl = getApiBaseUrl();
|
|
if (!apiUrl) return false;
|
|
|
|
try {
|
|
const response = await fetch(`${apiUrl}/users/${userId}`, {
|
|
method: 'DELETE'
|
|
});
|
|
|
|
if (!response.ok) throw new Error('Failed to delete user');
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error deleting user:', error);
|
|
return false;
|
|
}
|
|
}
|
|
};
|