blazor-calendar/BlazorApp/Pages/Event.razor

145 lines
3.5 KiB
Plaintext
Raw Normal View History

2021-07-03 12:19:53 +00:00
@page "/calendars/{calendarId:int}/events/{eventUid}"
@using System.Globalization
2021-07-03 12:19:53 +00:00
@inject Data.CalendarService _calendarService
2021-07-05 22:04:24 +00:00
@inject NavigationManager _navigationManager
2021-07-03 12:19:53 +00:00
2021-07-04 11:38:52 +00:00
<a href="/calendars/@CalendarId">
<span class="oi oi-arrow-left"></span> Back
</a>
2021-07-03 12:19:53 +00:00
@if (_event == null)
{
<p>
2021-07-05 22:04:24 +00:00
<em>@(_notFound ? "Event not found" : "Loading...")</em>
2021-07-03 12:19:53 +00:00
</p>
}
else
{
<h1>Event</h1>
<div>
<strong>Summary: </strong>
@if (_editing != Editing.Summary)
{
<span class="property-editable" @onclick="() => _editing = Editing.Summary">
@_event.Summary
</span>
}
else
{
<div>
<input @bind="@_event.Summary"/>
<button @onclick="UpdateEvent">Save</button>
</div>
}
</div>
<div>
<strong>Start: </strong>
@if (_editing != Editing.DtStart)
{
<span class="property-editable" @onclick="() => _editing = Editing.DtStart">
@(_event.DtStart?.ToString(DtFormat) ?? "Unknown")
</span>
}
else
{
<input @bind="@_event.DtStart" @bind:format="@DtFormat"/>
<button @onclick="UpdateEvent">Save</button>
}
</div>
<div>
<strong>End: </strong>
@if (_editing != Editing.CalculatedEnd)
{
<span class="property-editable" @onclick="() => _editing = Editing.CalculatedEnd">
@(_event.CalculatedEnd?.ToString(DtFormat) ?? "Unknown")
</span>
}
else
{
<input @bind="@_event.CalculatedEnd" @bind:format="@DtFormat"/>
<button @onclick="UpdateEvent">Save</button>
}
</div>
@if (_editing != Editing.Description)
{
<p class="event-description" @onclick="() => _editing = Editing.Description">
@(_event.Description ?? "No description")
</p>
}
else
{
<div>
<textarea rows="30" cols="100" @bind="@_event.Description"></textarea>
<button @onclick="UpdateEvent">Save</button>
</div>
}
2021-07-05 22:04:24 +00:00
@if (!_confirmDelete)
{
<button class="btn btn-outline-danger" @onclick="DeleteEvent">Delete</button>
}
else
{
<button class="btn btn-danger" @onclick="DeleteEvent">Are you sure?</button>
}
2021-07-03 12:19:53 +00:00
}
@code
{
[Parameter]
public int CalendarId { get; set; }
[Parameter]
public string EventUid { get; set; }
const string DtFormat = "dd-MM-yyyy HH:mm:ss";
private Data.Calendar _calendar;
2021-07-03 12:19:53 +00:00
private Data.Event _event;
2021-07-05 22:04:24 +00:00
private bool _notFound;
private bool _confirmDelete;
2021-07-03 12:19:53 +00:00
private enum Editing
{
Summary,
DtStart,
CalculatedEnd,
Description,
}
private Editing? _editing;
2021-07-03 12:19:53 +00:00
protected override async Task OnInitializedAsync()
{
_calendar = await Data.CalendarService.GetCalendarById(CalendarId);
_event = await _calendar.GetEventByUid(EventUid);
2021-07-05 22:04:24 +00:00
_notFound = _event == null;
}
2021-07-05 22:04:24 +00:00
private async Task UpdateEvent()
{
_event = await _calendar.UpdateEvent(_event);
_editing = null;
2021-07-03 12:19:53 +00:00
}
2021-07-05 22:04:24 +00:00
private async Task DeleteEvent()
{
if (!_confirmDelete)
{
_confirmDelete = true;
await Task.Delay(2000);
}
else
{
await _calendar.DeleteEvent(_event);
_navigationManager.NavigateTo($"/calendars/{CalendarId}");
}
_confirmDelete = false;
}
2021-07-03 12:19:53 +00:00
}