blazor-calendar/BlazorApp/Pages/Index.razor

114 lines
3.1 KiB
Plaintext
Raw Normal View History

2021-07-03 12:19:53 +00:00
@page "/"
@using BlazorApp.Data
@inject CalendarService _calendarService
@if (_calendars == null)
{
<p>
<em>Loading...</em>
</p>
}
else
{
<h1>Calendars</h1>
<ul>
@foreach (var calendar in _calendars)
{
<li>
<a href="/calendars/@calendar.Id">@calendar.Name</a>
(
<a href="#" @onclick="() => DeleteCalendar(calendar)">
2021-07-04 10:16:51 +00:00
@if (_confirmDeleteId == calendar.Id)
{
@:Confirm deletion?
2021-07-04 10:16:51 +00:00
}
else
{
<span class="oi oi-x"></span>
}
</a>
)
2021-07-03 12:19:53 +00:00
</li>
}
</ul>
2021-07-04 10:16:51 +00:00
@if (_calendarModel != null)
2021-07-03 12:19:53 +00:00
{
<h2>Add New Calendar</h2>
2021-07-04 10:16:51 +00:00
<EditForm Model="@_calendarModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator/>
<ValidationSummary/>
2021-07-03 12:19:53 +00:00
<div class="form-group">
<label for="url">URL</label>
<InputText id="url" class="form-control" placeholder="Enter URL" @bind-Value="_calendarModel.Url"/>
2021-07-03 12:19:53 +00:00
</div>
<div class="form-group">
<label for="username">Username</label>
<InputText
2021-07-04 10:16:51 +00:00
id="username" class="form-control" placeholder="Enter username"
@bind-Value="_calendarModel.Username"/>
2021-07-03 12:19:53 +00:00
</div>
2021-07-04 10:16:51 +00:00
2021-07-03 12:19:53 +00:00
<div class="form-group">
<label for="password">Password</label>
2021-07-04 10:16:51 +00:00
<InputText
type="password" id="password" class="form-control" placeholder="Enter password"
@bind-Value="_calendarModel.Password"/>
2021-07-03 12:19:53 +00:00
</div>
2021-07-04 10:16:51 +00:00
2021-07-03 12:19:53 +00:00
<div class="form-group">
<label for="name">Name</label>
2021-07-04 10:16:51 +00:00
<InputText
type="text" id="name" class="form-control" placeholder="Enter name"
@bind-Value="_calendarModel.Name"/>
2021-07-03 12:19:53 +00:00
</div>
2021-07-04 10:16:51 +00:00
2021-07-03 12:19:53 +00:00
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
}
else
{
<button class="btn btn-primary" @onclick="ShowAddCalendar">Add Calendar</button>
}
}
@code
{
private List<Data.Calendar> _calendars;
2021-07-04 10:16:51 +00:00
private int? _confirmDeleteId = null;
private Data.Calendar _calendarModel = null;
2021-07-03 12:19:53 +00:00
protected override async Task OnInitializedAsync()
{
_calendars = await CalendarService.GetCalendars();
}
private void ShowAddCalendar()
{
2021-07-04 10:16:51 +00:00
_calendarModel = new Data.Calendar();
2021-07-03 12:19:53 +00:00
}
2021-07-04 10:16:51 +00:00
private async Task HandleValidSubmit()
2021-07-03 12:19:53 +00:00
{
2021-07-04 10:16:51 +00:00
await CalendarService.CreateCalendar(_calendarModel);
_calendarModel = null;
_calendars = await CalendarService.GetCalendars();
}
private async Task DeleteCalendar(Data.Calendar calendar)
{
if (_confirmDeleteId != calendar.Id)
{
_confirmDeleteId = calendar.Id;
return;
}
await CalendarService.DeleteCalendar(calendar);
_calendars = await CalendarService.GetCalendars();
_confirmDeleteId = null;
2021-07-03 12:19:53 +00:00
}
}