Skip to content

Commit

Permalink
Simplify Date input
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhiyuan-Amos committed Jan 9, 2022
1 parent acb57ce commit 6a4135d
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 122 deletions.
16 changes: 2 additions & 14 deletions Client/Pages/Image/CreateImage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,8 @@
<label class="mb-0 form-label">
Date
</label>
<input @bind="@CreateUpdateImageStateContainer.Day"
@bind:event="oninput"
style="text-align: center; width: 4ch;"
class="px-1">
/
<input @bind="@CreateUpdateImageStateContainer.Month"
@bind:event="oninput"
style="text-align: center; width: 4ch;"
class="px-1">
/
<input @bind="@CreateUpdateImageStateContainer.Year"
@bind:event="oninput"
style="text-align: center; width: 4ch;"
class="px-1">
<input type="date"
@bind="@CreateUpdateImageStateContainer.Date">
</div>
<div class="m-3">
<label class="mb-0 form-label">
Expand Down
19 changes: 2 additions & 17 deletions Client/Pages/Image/CreateImage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,13 @@ public partial class CreateImage
[Inject] private NavigationManager? NavigationManager { get; init; }
[Inject] private IJSRuntime? Js { get; init; }

private bool IsSaveEnabled
{
get
{
try
{
CreateUpdateImageStateContainer.GetDate();
}
catch (Exception e) when (e is ArgumentOutOfRangeException or FormatException)
{
return false;
}

return CreateUpdateImageStateContainer?.Data != null;
}
}
private bool IsSaveEnabled => CreateUpdateImageStateContainer?.Data != null;

protected override void OnInitialized() => CreateUpdateImageStateContainer = new();

private async Task Save()
{
var date = CreateUpdateImageStateContainer.GetDate();
var date = CreateUpdateImageStateContainer.Date;
var toPersist = new ImageModel(Guid.NewGuid(),
new(date.Year, date.Month, date.Day),
CreateUpdateImageStateContainer.Data, CreateUpdateImageStateContainer.IsFavourite);
Expand Down
17 changes: 2 additions & 15 deletions Client/Pages/Image/UpdateImage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,8 @@
<label class="mb-0 form-label">
Date
</label>
<input @bind="@CreateUpdateImageStateContainer.Day"
@bind:event="oninput"
style="text-align: center; width: 4ch;"
class="px-1">
/
<input @bind="@CreateUpdateImageStateContainer.Month"
@bind:event="oninput"
style="text-align: center; width: 4ch;"
class="px-1">
/
<input @bind="@CreateUpdateImageStateContainer.Year"
@bind:event="oninput"
style="text-align: center; width: 4ch;"
class="px-1">
<input type="date"
@bind="@CreateUpdateImageStateContainer.Date">
</div>
<div class="m-3">
<label class="mb-0 form-label">
Expand All @@ -36,7 +24,6 @@
src="data:image/bmp;base64, @(Convert.ToBase64String(CreateUpdateImageStateContainer.Data))"/>
<div class="d-grid px-3">
<button class="btn btn-secondary"
disabled="@(!IsSaveEnabled)"
@onclick="@Save"
type="button">
Save
Expand Down
18 changes: 1 addition & 17 deletions Client/Pages/Image/UpdateImage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,6 @@ public partial class UpdateImage
[Inject] private IJSRuntime Js { get; init; }
[Inject] private DoneStateContainer DoneStateContainer { get; init; }

private bool IsSaveEnabled
{
get
{
try
{
CreateUpdateImageStateContainer.GetDate();
return true;
}
catch (Exception e) when (e is ArgumentOutOfRangeException or FormatException)
{
return false;
}
}
}

protected override void OnInitialized()
{
if (!DoneStateContainer.TryGetImage(ImageId, out _imageModel))
Expand All @@ -56,7 +40,7 @@ private async Task Delete()

private async Task Save()
{
var date = CreateUpdateImageStateContainer.GetDate();
var date = CreateUpdateImageStateContainer.Date;
var updatedDateTime = new DateTime(date.Year, date.Month, date.Day);
var toPersist = new ImageModel(_imageModel.Id, updatedDateTime,
CreateUpdateImageStateContainer.Data, CreateUpdateImageStateContainer.IsFavourite);
Expand Down
62 changes: 3 additions & 59 deletions Client/States/Image/CreateUpdateImageStateContainer.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,21 @@
using System.Text.RegularExpressions;

namespace Couple.Client.States.Image;

public class CreateUpdateImageStateContainer
{
private string _day;

private string _month;

private string _year;

public CreateUpdateImageStateContainer()
{
var today = DateTime.Today;
Day = today.Day.ToString();
Month = today.Month.ToString();
Year = today.Year.ToString()[2..];
Date = DateOnly.FromDateTime(DateTime.Now);
IsFavourite = false;
}

public CreateUpdateImageStateContainer(DateOnly date, bool isFavourite, byte[] data)
{
Day = date.Day.ToString();
Month = date.Month.ToString();
Year = date.Year.ToString()[2..];
Date = date;
IsFavourite = isFavourite;
Data = data;
}

public string Day
{
get => _day;
set
{
if (value.Length != 0 && !Regex.IsMatch(value, "^[0-9]{1,2}$"))
{
return;
}

_day = value;
}
}

public string Month
{
get => _month;
set
{
if (value.Length != 0 && !Regex.IsMatch(value, "^[0-9]{1,2}$"))
{
return;
}

_month = value;
}
}

public string Year
{
get => _year;
set
{
if (value.Length != 0 && !Regex.IsMatch(value, "^[0-9]{1,2}$"))
{
return;
}

_year = value;
}
}

public DateOnly Date { get; set; }
public bool IsFavourite { get; set; }
public byte[] Data { get; set; }

public DateOnly GetDate() => new(int.Parse($"20{Year}"), int.Parse(Month), int.Parse(Day));
}

0 comments on commit 6a4135d

Please sign in to comment.