Calendar/src/managers/EventManager.ts
Janus Knudsen 28ed131b9e wip
2025-08-02 23:59:52 +02:00

233 lines
No EOL
6.9 KiB
TypeScript

import { EventBus } from '../core/EventBus';
import { IEventBus, CalendarEvent } from '../types/CalendarTypes';
import { EventTypes } from '../constants/EventTypes';
/**
* EventManager - Administrerer event lifecycle og CRUD operationer
* Håndterer mock data og event synchronization
*/
export class EventManager {
private eventBus: IEventBus;
private events: CalendarEvent[] = [];
constructor(eventBus: IEventBus) {
this.eventBus = eventBus;
this.setupEventListeners();
this.loadMockData();
}
private setupEventListeners(): void {
this.eventBus.on(EventTypes.CALENDAR_INITIALIZED, () => {
this.syncEvents();
});
this.eventBus.on(EventTypes.DATE_CHANGED, () => {
this.syncEvents();
});
this.eventBus.on(EventTypes.VIEW_RENDERED, () => {
this.syncEvents();
});
}
private loadMockData(): void {
// Mock events for current week (July 27 - August 2, 2025)
this.events = [
// Sunday July 27, 2025
{
id: '1',
title: 'Weekend Planning',
start: '2025-07-27T10:00:00',
end: '2025-07-27T11:00:00',
type: 'work',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 60 }
},
// Monday July 28, 2025
{
id: '2',
title: 'Team Standup',
start: '2025-07-28T09:00:00',
end: '2025-07-28T09:30:00',
type: 'meeting',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 30 }
},
{
id: '3',
title: 'Project Kickoff',
start: '2025-07-28T14:00:00',
end: '2025-07-28T15:30:00',
type: 'meeting',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 90 }
},
// Tuesday July 29, 2025
{
id: '4',
title: 'Deep Work Session',
start: '2025-07-29T10:00:00',
end: '2025-07-29T12:00:00',
type: 'work',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 120 }
},
{
id: '5',
title: 'Lunch Meeting',
start: '2025-07-29T12:30:00',
end: '2025-07-29T13:30:00',
type: 'meal',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 60 }
},
// Wednesday July 30, 2025
{
id: '6',
title: 'Client Review',
start: '2025-07-30T15:00:00',
end: '2025-07-30T16:00:00',
type: 'meeting',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 60 }
},
// Thursday July 31, 2025
{
id: '7',
title: 'Sprint Planning',
start: '2025-07-31T09:00:00',
end: '2025-07-31T10:30:00',
type: 'meeting',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 90 }
},
{
id: '8',
title: 'Code Review',
start: '2025-07-31T14:00:00',
end: '2025-07-31T15:00:00',
type: 'work',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 60 }
},
// Friday August 1, 2025
{
id: '9',
title: 'Team Standup',
start: '2025-08-01T09:00:00',
end: '2025-08-01T09:30:00',
type: 'meeting',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 30 }
},
{
id: '10',
title: 'Client Meeting',
start: '2025-08-01T14:00:00',
end: '2025-08-01T15:30:00',
type: 'meeting',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 90 }
},
// Saturday August 2, 2025
{
id: '11',
title: 'Weekend Project',
start: '2025-08-02T10:00:00',
end: '2025-08-02T12:00:00',
type: 'work',
allDay: false,
syncStatus: 'synced',
metadata: { duration: 120 }
}
];
console.log(`EventManager: Loaded ${this.events.length} mock events`);
console.log('EventManager: First event:', this.events[0]);
console.log('EventManager: Last event:', this.events[this.events.length - 1]);
}
private syncEvents(): void {
// Emit events for rendering
this.eventBus.emit(EventTypes.EVENTS_LOADED, {
events: this.events
});
console.log(`EventManager: Synced ${this.events.length} events`);
}
public getEvents(): CalendarEvent[] {
return [...this.events];
}
public getEventById(id: string): CalendarEvent | undefined {
return this.events.find(event => event.id === id);
}
public addEvent(event: Omit<CalendarEvent, 'id'>): CalendarEvent {
const newEvent: CalendarEvent = {
...event,
id: Date.now().toString()
};
this.events.push(newEvent);
this.syncEvents();
this.eventBus.emit(EventTypes.EVENT_CREATED, {
event: newEvent
});
return newEvent;
}
public updateEvent(id: string, updates: Partial<CalendarEvent>): CalendarEvent | null {
const eventIndex = this.events.findIndex(event => event.id === id);
if (eventIndex === -1) return null;
const updatedEvent = { ...this.events[eventIndex], ...updates };
this.events[eventIndex] = updatedEvent;
this.syncEvents();
this.eventBus.emit(EventTypes.EVENT_UPDATED, {
event: updatedEvent
});
return updatedEvent;
}
public deleteEvent(id: string): boolean {
const eventIndex = this.events.findIndex(event => event.id === id);
if (eventIndex === -1) return false;
const deletedEvent = this.events[eventIndex];
this.events.splice(eventIndex, 1);
this.syncEvents();
this.eventBus.emit(EventTypes.EVENT_DELETED, {
event: deletedEvent
});
return true;
}
public refresh(): void {
this.syncEvents();
}
public destroy(): void {
this.events = [];
}
}