54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
|
|
import { CalendarEvent } from '../types/CalendarTypes';
|
||
|
|
import { IEventRepository } from './IEventRepository';
|
||
|
|
|
||
|
|
interface RawEventData {
|
||
|
|
id: string;
|
||
|
|
title: string;
|
||
|
|
start: string | Date;
|
||
|
|
end: string | Date;
|
||
|
|
type: string;
|
||
|
|
color?: string;
|
||
|
|
allDay?: boolean;
|
||
|
|
[key: string]: unknown;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* MockEventRepository - Loads event data from local JSON file
|
||
|
|
*
|
||
|
|
* This repository implementation fetches mock event data from a static JSON file.
|
||
|
|
* Used for development and testing before backend API is available.
|
||
|
|
*
|
||
|
|
* Data Source: data/mock-events.json
|
||
|
|
*/
|
||
|
|
export class MockEventRepository implements IEventRepository {
|
||
|
|
private readonly dataUrl = 'data/mock-events.json';
|
||
|
|
|
||
|
|
public async loadEvents(): Promise<CalendarEvent[]> {
|
||
|
|
try {
|
||
|
|
const response = await fetch(this.dataUrl);
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error(`Failed to load mock events: ${response.status} ${response.statusText}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const rawData: RawEventData[] = await response.json();
|
||
|
|
|
||
|
|
return this.processCalendarData(rawData);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Failed to load event data:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private processCalendarData(data: RawEventData[]): CalendarEvent[] {
|
||
|
|
return data.map((event): CalendarEvent => ({
|
||
|
|
...event,
|
||
|
|
start: new Date(event.start),
|
||
|
|
end: new Date(event.end),
|
||
|
|
type: event.type,
|
||
|
|
allDay: event.allDay || false,
|
||
|
|
syncStatus: 'synced' as const
|
||
|
|
}));
|
||
|
|
}
|
||
|
|
}
|