Add event deletion

This commit is contained in:
Sijmen 2021-07-06 00:04:24 +02:00
parent 3c2c33736b
commit cc0eba7042
2 changed files with 58 additions and 3 deletions

View File

@ -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);
}
}
}

View File

@ -1,5 +1,6 @@
@page "/calendars/{calendarId:int}/events/{eventUid}"
@inject Data.CalendarService _calendarService
@inject NavigationManager _navigationManager
<a href="/calendars/@CalendarId">
<span class="oi oi-arrow-left"></span> Back
@ -8,7 +9,7 @@
@if (_event == null)
{
<p>
<em>Loading...</em>
<em>@(_notFound ? "Event not found" : "Loading...")</em>
</p>
}
else
@ -43,7 +44,19 @@ else
</div>
}
<p class="event-description">@_event.Description</p>
@if (_event.Description != null)
{
<p class="event-description">@_event.Description</p>
}
@if (!_confirmDelete)
{
<button class="btn btn-outline-danger" @onclick="DeleteEvent">Delete</button>
}
else
{
<button class="btn btn-danger" @onclick="DeleteEvent">Are you sure?</button>
}
}
@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;
}
}