diff --git a/BlazorApp/Data/Calendar.cs b/BlazorApp/Data/Calendar.cs index 3d13e5f..93147b4 100644 --- a/BlazorApp/Data/Calendar.cs +++ b/BlazorApp/Data/Calendar.cs @@ -237,6 +237,9 @@ namespace BlazorApp.Data // Extract and parse the ICal data var dataNode = document.GetElementsByTagName("C:calendar-data")[0]?.InnerText; + if (dataNode == null) + return null; + var events = ParseICal(dataNode); if (events.Count == 0) return null; @@ -278,5 +281,24 @@ namespace BlazorApp.Data return newEvent; } + + public async Task DeleteEvent(Event @event) + { + // Only delete when we have an E-Tag + if (@event.ETag == null) + { + Console.WriteLine($"Can not delete event '{@event.Uid}' because it has no ETag"); + return; + } + + // TODO Can probably make `client` a member variable + var client = new HttpClient(); + if (AuthorizationHeader != null) + client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeader); + + // Build a new URL based on the calendar's URL and the event's collection href. + var url = new Uri(new Uri(Url), @event.Href); + await client.DeleteAsync(url); + } } } \ No newline at end of file diff --git a/BlazorApp/Pages/Event.razor b/BlazorApp/Pages/Event.razor index accf618..0d39f1c 100644 --- a/BlazorApp/Pages/Event.razor +++ b/BlazorApp/Pages/Event.razor @@ -1,5 +1,6 @@ @page "/calendars/{calendarId:int}/events/{eventUid}" @inject Data.CalendarService _calendarService +@inject NavigationManager _navigationManager Back @@ -8,7 +9,7 @@ @if (_event == null) {

- Loading... + @(_notFound ? "Event not found" : "Loading...")

} else @@ -43,7 +44,19 @@ else } -

@_event.Description

+ @if (_event.Description != null) + { +

@_event.Description

+ } + + @if (!_confirmDelete) + { + + } + else + { + + } } @code @@ -56,16 +69,36 @@ else private Data.Calendar _calendar; private Data.Event _event; + + private bool _notFound; private bool _editTitle; + private bool _confirmDelete; protected override async Task OnInitializedAsync() { _calendar = await Data.CalendarService.GetCalendarById(CalendarId); _event = await _calendar.GetEventByUid(EventUid); + _notFound = _event == null; } - private async void UpdateEvent() + private async Task UpdateEvent() { _event = await _calendar.UpdateEvent(_event); } + + private async Task DeleteEvent() + { + if (!_confirmDelete) + { + _confirmDelete = true; + await Task.Delay(2000); + } + else + { + await _calendar.DeleteEvent(_event); + _navigationManager.NavigateTo($"/calendars/{CalendarId}"); + } + + _confirmDelete = false; + } } \ No newline at end of file