41 lines
924 B
TypeScript
41 lines
924 B
TypeScript
import { userstate } from './shared.svelte';
|
|
import type { SelectEntry } from '@server/pt-api';
|
|
|
|
export async function fetchEntries(retry: boolean = false): Promise<SelectEntry[]> {
|
|
try {
|
|
const response = await fetch('/api/entries', {
|
|
method: 'GET',
|
|
mode: 'no-cors',
|
|
});
|
|
|
|
if (!response.ok) {
|
|
if (response.status === 401) {
|
|
userstate.checkIsLoggedIn();
|
|
}
|
|
if (retry) {
|
|
return fetchEntries(false);
|
|
}
|
|
throw new Error('Network response was not ok');
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!Array.isArray(data)) {
|
|
throw new Error('Invalid data format');
|
|
}
|
|
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Error fetching entries:', error);
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
export const formatter = new Intl.DateTimeFormat(undefined, {
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|