Allow to change company logo year in admin view.

This commit is contained in:
2020-05-28 16:02:51 +01:00
parent 3cb555db18
commit a7d44c8940
7 changed files with 170 additions and 53 deletions

View File

@@ -1,43 +0,0 @@
@model Marechai.Database.Models.CompanyLogo
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Company logo</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Company" class="control-label">
</label>
<select asp-for="CompanyId" class="form-control" asp-items="ViewBag.CompanyId">
</select>
</div>
<div class="form-group">
<label asp-for="Year" class="control-label">
</label>
<input asp-for="Year" class="form-control" />
<span asp-validation-for="Year" class="text-danger">
</span>
</div>
<div class="form-group">
<label asp-for="Guid" class="control-label">
</label>
<input asp-for="Guid" class="form-control" readonly />
<span asp-validation-for="Guid" class="text-danger">
</span>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Save" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</div>
</form>
</div>
</div>

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1269</Version>
<Version>3.0.99.1275</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>

View File

@@ -337,9 +337,10 @@
<tbody>
@foreach (var logo in _logos)
{
bool logoFound = File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", logo.Guid + ".svg"));
<tr>
<td>
@if (File.Exists(Path.Combine(Host.WebRootPath, "assets/logos", logo.Guid + ".svg")))
@if (logoFound)
{
<picture>
<source type="image/svg+xml" srcset="/assets/logos/@(logo.Guid).svg">
@@ -364,8 +365,11 @@
}
</td>
<td>
<a class="btn btn-primary">@L["Change year"]</a>
<Button Color="Color.Danger" Clicked="() => {ShowModal(logo.Id);}">@L["Delete"]</Button>
@if (logoFound)
{
<Button Color="Color.Success" Clicked="() => {ShowLogoYearModal(logo.Id);}">@L["Change year"]</Button>
}
<Button Color="Color.Danger" Clicked="() => {ShowDeleteModal(logo.Id);}">@L["Delete"]</Button>
</td>
</tr>
}
@@ -373,19 +377,62 @@
</table>
}
<Modal @ref="_frmDelete" IsCentered="true" Closing="@ModalClosing">
<Modal @ref="_frmDelete" IsCentered="true" Closing="@DeleteModalClosing">
<ModalBackdrop/>
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@L["Delete logo"]</ModalTitle>
<CloseButton Clicked="@HideModal"/>
<CloseButton Clicked="@HideDeleteModal"/>
</ModalHeader>
<ModalBody>
<Text>@string.Format(@L["Are you sure you want to delete the company logo introduced in {0}?"], _currentLogo?.Year)</Text>
</ModalBody>
<ModalFooter>
<Button Color="Color.Primary" Clicked="@HideModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
<Button Color="Color.Primary" Clicked="@HideDeleteModal" Disabled="@_deleteInProgress">@L["Cancel"]</Button>
<Button Color="Color.Danger" Clicked="@ConfirmDelete" Disabled="@_deleteInProgress">@L["Delete"]</Button>
</ModalFooter>
</ModalContent>
</Modal>
<Modal @ref="_frmLogoYear" IsCentered="true" Closing="@LogoYearModalClosing">
<ModalBackdrop/>
<ModalContent Centered="true">
<ModalHeader>
<ModalTitle>@L["Change logo year"]</ModalTitle>
<CloseButton Clicked="@HideLogoYearModal"/>
</ModalHeader>
<ModalBody>
@if (_currentLogo != null)
{
<div class="text-center">
<picture>
<source type="image/svg+xml" srcset="/assets/logos/@(_currentLogo.Guid).svg">
<source type="image/webp" srcset="/assets/logos/webp/1x/@(_currentLogo.Guid).webp, /assets/logos/webp/2x/@(_currentLogo.Guid).webp 2x, /assets/logos/webp/3x/@(_currentLogo.Guid).webp 3x">
<img srcset="/assets/logos/png/1x/@(_currentLogo.Guid).png, /assets/logos/png/2x/@(_currentLogo.Guid).png 2x, /assets/logos/png/3x/@(_currentLogo.Guid).png 3x" src="/assets/logos/png/1x@(_currentLogo.Guid).png" alt="" height="auto" width="auto" style="max-height: 256px; max-width: 256px" />
</picture>
</div>
<Field>
<FieldLabel>@L["Year logo came in use"]</FieldLabel>
<Check TValue="bool" @bind-Checked="@_unknownLogoYear">@L["Unknown (logo year)"]</Check>
@if (!_unknownLogoYear)
{
<Validation Validator="@ValidateLogoYear">
<NumericEdit TValue="int?" Decimals="0" @bind-Value="@_currentLogoYear">
<Feedback>
<ValidationError>@L["Please enter a valid year."]</ValidationError>
</Feedback>
</NumericEdit>
</Validation>
}
</Field>
}
</ModalBody>
<ModalFooter>
<Button Color="Color.Primary" Clicked="@HideLogoYearModal" Disabled="@_yearChangeInProgress">@L["Cancel"]</Button>
@if (_currentLogo != null)
{
<Button Color="Color.Success" Clicked="@ConfirmLogoYear" Disabled="@_yearChangeInProgress">@L["Save"]</Button>
}
</ModalFooter>
</ModalContent>
</Modal>

View File

@@ -17,9 +17,11 @@ namespace Marechai.Pages.Admin.Details
List<Iso31661Numeric> _countries;
bool _creating;
CompanyLogo _currentLogo;
int? _currentLogoYear;
bool _deleteInProgress;
bool _editing;
Modal _frmDelete;
Modal _frmLogoYear;
bool _loaded;
List<CompanyLogo> _logos;
CompanyViewModel _model;
@@ -28,12 +30,15 @@ namespace Marechai.Pages.Admin.Details
bool _unknownCountry;
bool _unknownFacebook;
bool _unknownFounded;
bool _unknownLogoYear;
bool _unknownPostalCode;
bool _unknownProvince;
bool _unknownSold;
bool _unknownSoldTo;
bool _unknownTwitter;
bool _unknownWebsite;
bool _yearChangeInProgress;
[Parameter]
public int Id { get; set; }
@@ -237,13 +242,13 @@ namespace Marechai.Pages.Admin.Details
void ValidateFacebook(ValidatorEventArgs e) =>
Validators.ValidateString(e, L["Facebook username must be smaller than 256 characters."], 256);
void ShowModal(int itemId)
void ShowDeleteModal(int itemId)
{
_currentLogo = _logos.FirstOrDefault(n => n.Id == itemId);
_frmDelete.Show();
}
void HideModal() => _frmDelete.Hide();
void HideDeleteModal() => _frmDelete.Hide();
async void ConfirmDelete()
{
@@ -268,6 +273,55 @@ namespace Marechai.Pages.Admin.Details
StateHasChanged();
}
void ModalClosing(ModalClosingEventArgs obj) => _currentLogo = null;
void DeleteModalClosing(ModalClosingEventArgs e) => _currentLogo = null;
void ShowLogoYearModal(int itemId)
{
_currentLogo = _logos.FirstOrDefault(n => n.Id == itemId);
_currentLogoYear = _currentLogo?.Year;
_unknownLogoYear = _currentLogoYear is null;
_frmLogoYear.Show();
}
void HideLogoYearModal() => _frmLogoYear.Hide();
async void ConfirmLogoYear()
{
if(_currentLogo is null)
return;
_yearChangeInProgress = true;
// Yield thread to let UI to update
await Task.Yield();
await CompanyLogosService.ChangeYearAsync(_currentLogo.Id, _unknownLogoYear ? null : _currentLogoYear);
_logos = await CompanyLogosService.GetByCompany(Id);
_yearChangeInProgress = false;
_frmLogoYear.Hide();
// Yield thread to let UI to update
await Task.Yield();
// Tell we finished loading
StateHasChanged();
}
void LogoYearModalClosing(ModalClosingEventArgs e)
{
_currentLogo = null;
_currentLogoYear = null;
}
void ValidateLogoYear(ValidatorEventArgs e)
{
if(!(e.Value is int item) ||
item <= 1000 ||
item > DateTime.UtcNow.Year)
e.Status = ValidationStatus.Error;
else
e.Status = ValidationStatus.Success;
}
}
}

View File

@@ -162,4 +162,8 @@
<value>Unknown</value>
<comment>Unknown, referring to another company name</comment>
</data>
<data name="Unknown (logo year)" xml:space="preserve">
<value>Unknown</value>
<comment>Unknown, referring to a company logo year</comment>
</data>
</root>

View File

@@ -402,4 +402,48 @@
<value>Por favor introduce una fecha de venta, fusión, bancarrota o renombre válida.</value>
<comment>Please enter a valid sold/merge/bankruptcy date.</comment>
</data>
<data name="Unknown (logo year)" xml:space="preserve">
<value>Desconocido</value>
<comment>Unknown, referring to a company logo year</comment>
</data>
<data name="Upload new logo" xml:space="preserve">
<value>Subir nuevo logo</value>
<comment>Upload new logo</comment>
</data>
<data name="Logos" xml:space="preserve">
<value>Logos</value>
<comment>Logos</comment>
</data>
<data name="Logo" xml:space="preserve">
<value>Logo</value>
<comment>Logo</comment>
</data>
<data name="Year logo came in use" xml:space="preserve">
<value>Año el logo empezó a usarse</value>
<comment>Year logo came in use</comment>
</data>
<data name="Cannot find logo file" xml:space="preserve">
<value>No se pudo encontrar el archivo del logo</value>
<comment>Cannot find logo file</comment>
</data>
<data name="Change year" xml:space="preserve">
<value>Cambiar año</value>
<comment>Change year</comment>
</data>
<data name="Delete logo" xml:space="preserve">
<value>Borrar logo</value>
<comment>Delete logo</comment>
</data>
<data name="Are you sure you want to delete the company logo introduced in {0}?" xml:space="preserve">
<value>¿Estás seguro de que quieres borrar el logo que la compañía empezó a utilizar en {0}?</value>
<comment>Are you sure you want to delete the company logo introduced in {0}?</comment>
</data>
<data name="Change logo year" xml:space="preserve">
<value>Cambiar año del logo</value>
<comment>Change logo year</comment>
</data>
<data name="Please enter a valid year." xml:space="preserve">
<value>Por favor introduce un año válido.</value>
<comment>Please enter a valid year.</comment>
</data>
</root>

View File

@@ -73,5 +73,16 @@ namespace Marechai.Services
if(File.Exists(Path.Combine(_webRootPath, "assets/logos/thumbs/png/3x", logo.Guid + ".png")))
File.Delete(Path.Combine(_webRootPath, "assets/logos/thumbs/png/3x", logo.Guid + ".png"));
}
public async Task ChangeYearAsync(int id, int? year)
{
CompanyLogo logo = await _context.CompanyLogos.Where(l => l.Id == id).FirstOrDefaultAsync();
if(logo is null)
return;
logo.Year = year;
await _context.SaveChangesAsync();
}
}
}