- Implemented functions to format dates between YYYY-MM-DD and DD.MM.YYYY - Added functions to get today's date in ISO format - Created functions to round time to the nearest 15 minutes - Developed a function to format time as HH:MM - Added a function to format duration in HH:MM:SS - Implemented a toast notification system with auto-remove functionality - Added functions to get day of the week and month names in German
181 lines
4.2 KiB
JavaScript
181 lines
4.2 KiB
JavaScript
/**
|
|
* API Functions
|
|
* Backend communication layer
|
|
*/
|
|
|
|
/**
|
|
* Fetch entries from backend
|
|
*/
|
|
async function fetchEntries(fromDate = null, toDate = null) {
|
|
try {
|
|
let url = '/api/entries';
|
|
const params = new URLSearchParams();
|
|
|
|
if (fromDate) params.append('from', fromDate);
|
|
if (toDate) params.append('to', toDate);
|
|
|
|
if (params.toString()) {
|
|
url += '?' + params.toString();
|
|
}
|
|
|
|
const response = await fetch(url);
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to fetch entries');
|
|
}
|
|
|
|
const entries = await response.json();
|
|
return entries;
|
|
} catch (error) {
|
|
console.error('Error fetching entries:', error);
|
|
showNotification('Fehler beim Laden der Einträge', 'error');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a new entry
|
|
*/
|
|
async function createEntry(date, startTime, endTime, pauseMinutes, location) {
|
|
try {
|
|
const body = {
|
|
date: formatDateISO(date),
|
|
startTime,
|
|
endTime,
|
|
location: location || 'office'
|
|
};
|
|
|
|
// Only include pauseMinutes if explicitly provided (not empty)
|
|
if (pauseMinutes !== null && pauseMinutes !== undefined && pauseMinutes !== '') {
|
|
body.pauseMinutes = parseInt(pauseMinutes);
|
|
}
|
|
|
|
const response = await fetch('/api/entries', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(body)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Failed to create entry');
|
|
}
|
|
|
|
const entry = await response.json();
|
|
return entry;
|
|
} catch (error) {
|
|
console.error('Error creating entry:', error);
|
|
showNotification(error.message || 'Fehler beim Erstellen des Eintrags', 'error');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update an existing entry
|
|
*/
|
|
async function updateEntry(id, date, startTime, endTime, pauseMinutes, location) {
|
|
try {
|
|
const body = {
|
|
date: formatDateISO(date),
|
|
startTime,
|
|
endTime,
|
|
location: location || 'office'
|
|
};
|
|
|
|
// Only include pauseMinutes if explicitly provided (not empty)
|
|
if (pauseMinutes !== null && pauseMinutes !== undefined && pauseMinutes !== '') {
|
|
body.pauseMinutes = parseInt(pauseMinutes);
|
|
}
|
|
|
|
const response = await fetch(`/api/entries/${id}`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(body)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
throw new Error(error.error || 'Failed to update entry');
|
|
}
|
|
|
|
const entry = await response.json();
|
|
return entry;
|
|
} catch (error) {
|
|
console.error('Error updating entry:', error);
|
|
showNotification(error.message || 'Fehler beim Aktualisieren des Eintrags', 'error');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete an entry
|
|
*/
|
|
async function deleteEntry(id) {
|
|
try {
|
|
const response = await fetch(`/api/entries/${id}`, {
|
|
method: 'DELETE'
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to delete entry');
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error deleting entry:', error);
|
|
showNotification('Fehler beim Löschen des Eintrags', 'error');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get a setting by key
|
|
*/
|
|
async function getSetting(key) {
|
|
try {
|
|
const response = await fetch(`/api/settings/${key}`);
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 404) {
|
|
return null; // Setting doesn't exist yet
|
|
}
|
|
throw new Error('Failed to get setting');
|
|
}
|
|
|
|
const data = await response.json();
|
|
return data.value;
|
|
} catch (error) {
|
|
console.error('Error getting setting:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set a setting
|
|
*/
|
|
async function setSetting(key, value) {
|
|
try {
|
|
const response = await fetch('/api/settings', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ key, value })
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to set setting');
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error setting setting:', error);
|
|
showNotification('Fehler beim Speichern der Einstellung', 'error');
|
|
return false;
|
|
}
|
|
}
|