import { Component, inject, input } from '@angular/core'; import { CalendarEventDto, EventExceptionDto } from '../../../models/events-in-range-dto.model'; import { SingleEventDashboardEventDetailsView, } from '../single-event-dashboard-event-details-view/single-event-dashboard-event-details-view'; import { Button } from '@rschneider/ng-daisyui'; import { SvgIcons } from '../../../../../svg-icons'; import { SafeHtmlPipe } from '../../../../../pipes/safe-html-pipe'; import { CalendarService } from '../../../services/calendar.service'; import { CreateExceptionDto } from '../../../models/event-exception.model'; export type ACTIVATION_TYPE = 'cancel' | 'activate'; @Component({ selector: 'app-single-event-dashboard-event-cancel', imports: [ SingleEventDashboardEventDetailsView, Button, SafeHtmlPipe, ], templateUrl: './single-event-dashboard-event-activation.component.html', styleUrl: './single-event-dashboard-event-activation.component.css', }) export class SingleEventDashboardEventActivation { mode = input('cancel'); calendarService = inject(CalendarService); event = input(); onAction = input.required<(msg: string) => void>(); protected readonly SvgIcons = SvgIcons; protected setEventOccurrenceActivation(activated: boolean) { const event = this.event(); console.info('setEventOccurrenceActivation', event); const eventId = this.event()?.id!; const startTime = this.event()?.startTime!; let payload: CreateExceptionDto | undefined = undefined; const eventException = event?.exceptions?.length ? event.exceptions[0] : undefined; if (eventException) { payload = { ...eventException, originalStartTime: new Date(eventException.originalStartTime), newStartTime: eventException.newStartTime ? new Date(eventException.newStartTime) : undefined, newEndTime: eventException.newEndTime ? new Date(eventException.newEndTime) :undefined, isCancelled: !activated, }; } else { payload = { originalStartTime: new Date(startTime), isCancelled: !activated, }; } this.calendarService.applyException(eventId, payload ).subscribe( { next: () => { this.onAction()('close'); }, error: err => { alert('Failed to change event'); }, }, ); } protected cancelEventOccurrence() { this.setEventOccurrenceActivation(false); } protected activateEventOccurrence() { this.setEventOccurrenceActivation(true); } protected closeDialog() { this.onAction()('close'); } }