53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import type { Person, NewPerson } from '../types';
|
|
|
|
// Get APP_NAME from environment or use default
|
|
const APP_NAME = import.meta.env.VITE_APP_NAME || 'ResumeFormatter';
|
|
const API_BASE_URL = `/${APP_NAME}/api`;
|
|
|
|
// Function to get all people
|
|
export const getPeople = async (): Promise<Person[]> => {
|
|
console.log('Fetching people from API...');
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/people/`);
|
|
if (!response.ok) {
|
|
throw new Error(`API error: ${response.status}`);
|
|
}
|
|
const data = await response.json();
|
|
console.log('... Data loaded:', data);
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Error fetching people:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
// Function to add a new person
|
|
export const addPerson = async (newPerson: NewPerson): Promise<Person> => {
|
|
console.log('Adding person to API:', newPerson);
|
|
|
|
if (!newPerson.firstName || !newPerson.lastName || !newPerson.linkedinUrl) {
|
|
throw new Error('All fields are required.');
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/people/`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(newPerson),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`API error: ${response.status}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
console.log('... Person added:', data);
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Error adding person:', error);
|
|
throw error;
|
|
}
|
|
};
|