PersonRolesEditCreatorComponent added

This commit is contained in:
2024-10-09 00:57:56 +02:00
Unverified
parent 18b14e8b40
commit 3bc17393b8
3 changed files with 300 additions and 0 deletions

View File

@@ -0,0 +1,151 @@
@using Blazorise.Extensions
@using Blazorise.Components
<div class="rounded-3 panel panel-regular p-3 @(Class)">
@if (_loaded)
{
<div class="vstack gap-3">
<div class="container-grid">
<div class="row gx-2">
<div class="col align-self-center">
<h3 class="m-0"><strong>Actor roles</strong></h3>
</div>
@if (!_editingMode)
{
<div class="col-auto">
<button type="button" class="btn btn-secondary" disabled="@(!Id.HasValue)" @onclick="@(() => ActivateEdit())">Add</button>
</div>
}
else
{
if (!string.IsNullOrWhiteSpace(_error))
{
<div class="col-auto align-self-center">
<span class="text-danger">@(_error)</span>
</div>
}
<div class="col-auto">
<button type="button" class="btn btn-secondary" @onclick="@(CancelEdit)">Cancel</button>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-secondary" disabled="@(_saving)" @onclick="@(SaveEdit)">
@if (!_saving)
{
<span>Save</span>
}
else
{
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<span>Saving...</span>
}
</button>
</div>
}
</div>
</div>
@if (!_editingMode)
{
if (_roles.IsNullOrEmpty())
{
<span class="text-center">No items</span>
}
else
{
<table class="table table-sm table-transparent">
<thead>
<tr>
<th scope="col">
Media name
</th>
<th scope="col">
Media type
</th>
<th scope="col">
Role type
</th>
<th scope="col">
Role name
</th>
<th class="table-cell-fit" scope="col">
Actions
</th>
</tr>
</thead>
<tbody class="table-group-divider">
@foreach (Guid roleId in _roles.Keys)
{
<tr>
<td class="align-middle">
@(Media[_roles[roleId].Data.MediaId].Title)@(Media[_roles[roleId].Data.MediaId].ReleaseDate.HasValue ? $" ({Media[_roles[roleId].Data.MediaId].ReleaseDate!.Value.Year})" : string.Empty)
</td>
<td class="align-middle">
@(Media[_roles[roleId].Data.MediaId].Type == MediaType.Movie ? $"Movie" : "TV Series")
</td>
<td class="align-middle">
@(_roleTypes[_roles[roleId].Data.TypeId])
</td>
<td class="align-middle table-cell-fit">
<div class="hstack gap-1">
<button class="btn btn-outline-secondary btn-sm" type="button" disabled="@(!Id.HasValue || _roles[roleId].Deleting)" @onclick="@(() => ActivateEdit(roleId))"><i class="fas fa-edit"></i></button>
<button class="btn btn-outline-danger btn-sm" type="button" disabled="@(!Id.HasValue || _roles[roleId].Deleting)" @onclick="@(() => Delete(roleId))">
@if (_roles[roleId].Deleting)
{
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
}
else
{
<i class="fa-solid fa-trash"></i>
}
</button>
</div>
</td>
</tr>
}
</tbody>
</table>
}
}
else
{
<EditForm Model="@(_editedModel)">
<AntiforgeryToken/>
<div class="container-grid">
<div class="row form-group mb-1">
<label for="creatorFormMedia" class="col-1 col-form-label">Media:</label>
<div class="col">
<Autocomplete ElementId="creatorFormMedia"
TItem="MediaResponse"
TValue="long"
Data="@(Media.Values)"
TextField="@(item => item.ReleaseDate.HasValue ? $"{item.Title} ({item.ReleaseDate.Value.Year})" : item.Title)"
ValueField="@(item => item.Id)"
@bind-SelectedValue="@(_editedModel.MediaId)"
Placeholder="Search..."
Filter="AutocompleteFilter.Contains">
<NotFoundContent Context="not_found_context"> Sorry... @not_found_context was not found</NotFoundContent>
</Autocomplete>
</div>
</div>
<div class="row form-group my-1">
<label for="creatorFormType" class="col-1 col-form-label">Type:</label>
<div class="col">
<InputSelect id="creatorFormType" class="form-control" TValue="short" @bind-Value="@(_editedModel.TypeId)">
@foreach (KeyValuePair<short, string> type in _roleTypes)
{
<option value="@(type.Key)">@(type.Value)</option>
}
</InputSelect>
</div>
</div>
</div>
</EditForm>
}
</div>
}
else
{
<LoadingComponent Color="white"/>
}
</div>

View File

@@ -0,0 +1,149 @@
using Microsoft.AspNetCore.Components;
using WatchIt.Common.Model.Media;
using WatchIt.Common.Model.Roles;
using WatchIt.Website.Services.WebAPI.Media;
using WatchIt.Website.Services.WebAPI.Persons;
using WatchIt.Website.Services.WebAPI.Roles;
namespace WatchIt.Website.Components.PersonEditPage;
public partial class PersonRolesEditCreatorComponent : ComponentBase
{
#region SERVICES
[Inject] private IPersonsWebAPIService PersonsWebAPIService { get; set; } = default!;
[Inject] private IMediaWebAPIService MediaWebAPIService { get; set; } = default!;
[Inject] private IRolesWebAPIService RolesWebAPIService { get; set; } = default!;
#endregion
#region PARAMETERS
[Parameter] public required long? Id { get; set; }
[Parameter] public required Dictionary<long, MediaResponse> Media { get; set; }
[Parameter] public string Class { get; set; } = string.Empty;
#endregion
#region FIELDS
private bool _loaded;
private string? _error;
private Dictionary<Guid, (CreatorRoleResponse Data, bool Deleting)> _roles = [];
private Dictionary<short, string> _roleTypes = [];
private Guid? _editedId;
private ICreatorRolePersonRequest? _editedModel;
private bool _editingMode;
private bool _saving;
#endregion
#region PUBLIC METHODS
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
List<Task> endTasks = new List<Task>();
// STEP 0
if (Id.HasValue)
{
endTasks.AddRange(
[
PersonsWebAPIService.GetPersonAllCreatorRoles(Id.Value, successAction: data => _roles = data.ToDictionary(x => x.Id, x => (x, false))),
RolesWebAPIService.GetAllCreatorRoleTypes(successAction: data => _roleTypes = data.ToDictionary(x => x.Id, x => x.Name)),
]);
}
// END
await Task.WhenAll(endTasks);
_roles = _roles.OrderBy(x => Media.First(y => y.Key == x.Value.Data.MediaId).Value.ReleaseDate).ToDictionary(x => x.Key, x => x.Value);
_loaded = true;
StateHasChanged();
}
}
private void CancelEdit()
{
_error = null;
_editingMode = false;
}
private async Task SaveEdit()
{
void SuccessPost(CreatorRoleResponse data)
{
_roles[data.Id] = (data, false);
_roles = _roles.OrderBy(x => Media.First(y => y.Key == x.Value.Data.MediaId).Value.ReleaseDate).ToDictionary(x => x.Key, x => x.Value);
_saving = false;
_editingMode = false;
}
void SuccessPut()
{
CreatorRoleResponse temp = _roles[_editedId!.Value].Data;
temp.MediaId = _editedModel.MediaId;
temp.TypeId = _editedModel.TypeId;
_roles[_editedId!.Value] = (temp, false);
_roles = _roles.OrderBy(x => Media.First(y => y.Key == x.Value.Data.MediaId).Value.ReleaseDate).ToDictionary(x => x.Key, x => x.Value);
_saving = false;
_editingMode = false;
}
void BadRequest(IDictionary<string, string[]> errors)
{
_error = errors.SelectMany(x => x.Value).FirstOrDefault() ?? "Unknown error";
_saving = false;
}
void Unauthorized()
{
_error = "You do not have permission to do this";
_saving = false;
}
_error = null;
_saving = true;
if (_editedId.HasValue)
{
await RolesWebAPIService.PutCreatorRole(_editedId.Value, _editedModel as CreatorRoleUniversalRequest, SuccessPut, BadRequest, Unauthorized);
}
else
{
await PersonsWebAPIService.PostPersonCreatorRole(Id!.Value, _editedModel as CreatorRolePersonRequest, SuccessPost, BadRequest, Unauthorized);
}
}
private void ActivateEdit(Guid? id = null)
{
_editedId = id;
_editedModel = id.HasValue ? new CreatorRoleUniversalRequest(_roles[id.Value].Data) : new CreatorRolePersonRequest()
{
TypeId = _roleTypes.Keys.First()
};
_editingMode = true;
}
private async Task Delete(Guid id)
{
_roles[id] = (_roles[id].Data, true);
await RolesWebAPIService.DeleteCreatorRole(id, () => _roles.Remove(id));
}
#endregion
}