Add validation when adding calendar items

This commit is contained in:
Sijmen 2021-07-04 12:28:21 +02:00
parent 64fb38e1ad
commit 6c696c7821
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
2 changed files with 27 additions and 22 deletions

View file

@ -28,10 +28,11 @@ namespace BlazorApp.Data
public string Password { get; set; }
public string Name { get; set; }
[Required] public string Name { get; set; }
private string AuthorizationHeader =>
$"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))}";
private string AuthorizationHeader => Username == null || Password == null
? null
: $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))}";
private static DateTime _parseDateTime(string s)
{
@ -119,7 +120,9 @@ namespace BlazorApp.Data
public async Task<List<Event>> GetEvents()
{
var client = new HttpClient();
if (AuthorizationHeader != null)
client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeader);
var body = await client.GetStringAsync(Url);
var events = ParseICal(body);
@ -145,6 +148,7 @@ namespace BlazorApp.Data
public async Task<Event> GetEventByUid(string uid)
{
var client = new HttpClient();
if (AuthorizationHeader != null)
client.DefaultRequestHeaders.Add("Authorization", AuthorizationHeader);
var request = new HttpRequestMessage(new HttpMethod("REPORT"), Url);

View file

@ -18,16 +18,18 @@ else
{
<li>
<a href="/calendars/@calendar.Id">@calendar.Name</a>
(<a href="#" @onclick="() => DeleteCalendar(calendar)">
(
<a href="#" @onclick="() => DeleteCalendar(calendar)">
@if (_confirmDeleteId == calendar.Id)
{
<span>Confirm deletion?</span>
@:Confirm deletion?
}
else
{
<span class="oi oi-x"></span>
}
</a>)
</a>
)
</li>
}
</ul>
@ -37,6 +39,9 @@ else
<h2>Add New Calendar</h2>
<EditForm Model="@_calendarModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator/>
<ValidationSummary/>
<div class="form-group">
<label for="url">URL</label>
<InputText id="url" class="form-control" placeholder="Enter URL" @bind-Value="_calendarModel.Url"/>
@ -46,24 +51,21 @@ else
<label for="username">Username</label>
<InputText
id="username" class="form-control" placeholder="Enter username"
@bind-Value="_calendarModel.Username"
/>
@bind-Value="_calendarModel.Username"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<InputText
type="password" id="password" class="form-control" placeholder="Enter password"
@bind-Value="_calendarModel.Password"
/>
@bind-Value="_calendarModel.Password"/>
</div>
<div class="form-group">
<label for="name">Name</label>
<InputText
type="text" id="name" class="form-control" placeholder="Enter name"
@bind-Value="_calendarModel.Name"
/>
@bind-Value="_calendarModel.Name"/>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
@ -95,7 +97,6 @@ else
{
await CalendarService.CreateCalendar(_calendarModel);
_calendarModel = null;
_calendars = await CalendarService.GetCalendars();
}