Files
marechai/Marechai/Pages/Admin/Details/Machine.razor.cs

112 lines
3.4 KiB
C#
Raw Normal View History

2020-05-27 14:24:47 +01:00
using System;
2020-05-26 02:41:55 +01:00
using System.Collections.Generic;
using System.Threading.Tasks;
2020-05-27 14:24:47 +01:00
using Blazorise;
2020-05-26 02:41:55 +01:00
using Marechai.Database;
2020-05-27 14:24:47 +01:00
using Marechai.Shared;
2020-05-26 02:41:55 +01:00
using Marechai.ViewModels;
using Microsoft.AspNetCore.Components;
namespace Marechai.Pages.Admin.Details
{
public partial class Machine
{
List<CompanyViewModel> _companies;
2020-05-27 14:24:47 +01:00
bool _editing;
2020-05-26 02:41:55 +01:00
List<MachineFamilyViewModel> _families;
bool _loaded;
2020-05-27 14:24:47 +01:00
MachineViewModel _model;
bool _noFamily;
bool _prototype;
bool _unknownIntroduced;
bool _unknownModel;
2020-05-26 02:41:55 +01:00
[Parameter]
public int Id { get; set; }
int Type
{
get => (int)_model.Type;
set => _model.Type = (MachineType)value;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(_loaded)
return;
_loaded = true;
if(Id <= 0)
return;
_companies = await CompaniesService.GetAsync();
_families = await MachineFamiliesService.GetAsync();
_model = await Service.GetAsync(Id);
2020-05-27 14:24:47 +01:00
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/machines/edit/", StringComparison.InvariantCulture);
if(_editing)
SetCheckboxes();
StateHasChanged();
}
void SetCheckboxes()
{
_noFamily = !_model.FamilyId.HasValue;
_prototype = _model.Introduced?.Year == 1000;
_unknownIntroduced = !_model.Introduced.HasValue;
_unknownModel = string.IsNullOrWhiteSpace(_model.Model);
}
void OnEditClicked()
{
_editing = true;
SetCheckboxes();
StateHasChanged();
}
async void OnCancelClicked()
{
_editing = false;
_model = await Service.GetAsync(Id);
SetCheckboxes();
2020-05-26 02:41:55 +01:00
StateHasChanged();
}
2020-05-27 14:24:47 +01:00
async void OnSaveClicked()
{
if(_noFamily)
_model.FamilyId = null;
else if(_model.FamilyId < 0)
return;
if(_unknownIntroduced)
_model.Introduced = null;
else if(_prototype)
_model.Introduced = new DateTime(1000, 1, 1);
else if(_model.Introduced?.Date >= DateTime.UtcNow.Date)
return;
if(_unknownModel)
_model.Model = null;
else if(string.IsNullOrWhiteSpace(_model.Model))
return;
_editing = false;
await Service.UpdateAsync(_model);
_model = await Service.GetAsync(Id);
SetCheckboxes();
StateHasChanged();
}
void ValidateName(ValidatorEventArgs e) =>
Validators.ValidateString(e, L["Name must contain less than 255 characters."], 255);
void ValidateModel(ValidatorEventArgs e) =>
Validators.ValidateString(e, L["Model must contain less than 50 characters."], 50);
void ValidateIntroduced(ValidatorEventArgs e) => Validators.ValidateIntroducedDate(e);
2020-05-26 02:41:55 +01:00
}
}