Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/team management #25

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions src/iRLeagueManager.Web/Components/Settings/EditTeamModal.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
@inherits MvvmComponentBase
@using iRLeagueApiCore.Common.Enums
@using iRLeagueApiCore.Common.Models
@using iRLeagueApiCore.Common.Models.Members
@using iRLeagueManager.Web.ViewModels
@inject TeamViewModel vm

<EditForm Model=vm OnValidSubmit=Submit>
<StatusResultValidator @ref=ResultValidator />
<div class="input-group-list mb-3">
<InputGroup Label="Name">
<InputText class="form-control" @bind-Value=vm.Name/>
</InputGroup>
<InputGroup Label="Color">
<InputText class="form-control" @bind-Value=vm.TeamColor />
</InputGroup>
</div>
<div>
<div class="row g-0">
<div class="input-group-text" style="border-bottom-left-radius:0; border-bottom-right-radius:0; border-bottom: none">
<label>Team Members</label>
</div>
</div>
<div>
<BlazoredTypeahead
@bind-Values=vm.Members
class="form-control p-0"
style="border-top-right-radius:0; border-top-left-radius:0;"
EnableDropDown=true
SearchMethod=SearchMembers
MinimumLength=1
MaximumSuggestions=100
DeleteItemsOnBackspace=false
placeholder="Search driver by name...">

<SelectedTemplate Context=member>
@member.FirstName @member.LastName
</SelectedTemplate>
<ResultTemplate Context=member>
@member.FirstName @member.LastName
</ResultTemplate>
<NotFoundTemplate Context=name>
<span>Driver <b>@name</b> Not found!</span>
</NotFoundTemplate>
</BlazoredTypeahead>
</div>
</div>
<FormValidationMessage TValue=string Message=@ResultValidator?.ErrorMessage />
<CancelSubmitButtons ModelState=vm SubmitText="Save" OnCancel=Cancel/>
</EditForm>

@code {
[CascadingParameter]
public BlazoredModalInstance ModalInstance { get; set; } = default!;
[CascadingParameter]
public IModalService ModalService { get; set; } = default!;

private TeamModel model = default!;
[Parameter]
public TeamModel Model
{
get => model;
set
{
if (EqualityComparer<TeamModel>.Default.Equals(model, value) == false)
{
model = value;
vm.SetModel(model);
}
}
}
[Parameter]
public IEnumerable<MemberInfoModel> LeagueMembers { get; set; } = default!;

[Parameter]
public Func<TeamViewModel, CancellationToken, Task<StatusResult>>? OnSubmit { get; set; }

private CancellationTokenSource cts = new();

private StatusResultValidator? ResultValidator { get; set; }

protected override void OnParametersSet()
{
_ = ModalInstance ?? throw BlazorParameterNullException.New(this, ModalInstance);
_ = ModalService ?? throw BlazorParameterNullException.New(this, ModalService);
_ = Model ?? throw BlazorParameterNullException.New(this, Model);
_ = LeagueMembers ?? throw BlazorParameterNullException.New(this, LeagueMembers);
}

private async Task<IEnumerable<MemberInfoModel>> SearchMembers(string searchString)
{
if (string.IsNullOrEmpty(searchString))
{
return await Task.FromResult(LeagueMembers);
}

var terms = searchString.ToLower().Split(',', ' ', ';')
.Where(x => string.IsNullOrWhiteSpace(x) == false)
.ToArray();
return await Task.FromResult(LeagueMembers
.Where(x => MatchMemberSearchTerms(x, terms)));
}

private bool MatchMemberSearchTerms(MemberInfoModel member, params string[] terms)
{
var searchName = member.FirstName + member.LastName;
return terms.Any(x => searchName.Contains(x, StringComparison.OrdinalIgnoreCase));
}

private async Task Submit()
{
var success = true;
if (OnSubmit is not null)
{
var status = await OnSubmit(vm, cts.Token);
success &= status.IsSuccess;
ResultValidator?.ValidateResult(status);
}
if (success)
{
var result = ModalResult.Ok(vm.GetModel());
await ModalInstance.CloseAsync(result);
}
}

private async Task Cancel()
{
if (ModalInstance != null)
{
await ModalInstance.CancelAsync();
}
}

protected override void Dispose(bool disposing)
{
if (disposing == false)
{
cts.Cancel();
cts.Dispose();
}
base.Dispose(disposing);
}
}
28 changes: 28 additions & 0 deletions src/iRLeagueManager.Web/Components/Settings/TeamPreview.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@using iRLeagueManager.Web.ViewModels

<div @attributes=AdditionalAttributes>
<div>
<h5 style="color: @Team.TeamColor;">@Team.Name</h5>
</div>
<h6>Team Members</h6>
<div class="d-flex">
@foreach(var member in Team.Members)
{
<div class="badge bg-secondary me-1 mb-1">@member.FirstName @member.LastName</div>
}
</div>
</div>


@code {
[Parameter(CaptureUnmatchedValues=true)]
public IDictionary<string, object>? AdditionalAttributes { get; set; }

[Parameter, EditorRequired]
public TeamViewModel Team { get; set; } = default!;

protected override void OnParametersSet()
{
_ = Team ?? throw BlazorParameterNullException.New(this, Team);
}
}
100 changes: 100 additions & 0 deletions src/iRLeagueManager.Web/Components/Settings/TeamSettings.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
@using iRLeagueApiCore.Common.Enums
@using iRLeagueApiCore.Common.Models
@using iRLeagueApiCore.Common.Models.Members
@using iRLeagueManager.Web.ViewModels
@inherits MvvmComponentBase
@inject TeamsViewModel vm
@inject LeagueApiService apiService
@inject ILogger<ResultSettings> logger

<div @attributes=AdditionalAttributes>
<div class="card my-2 pb-0">
<div class="card-header">
Teams
</div>
<div class="list-group-flush">
@foreach((var team, var index) in @Bind(vm, x => x.Teams).Select((x, i) => (x, i)))
{
<div class="list-group-item list-group-item-action d-flex justify-content-between align-items-start" type="button" @onclick=@(() => OnTeamClick(team))>
<TeamPreview Team=team />
<button class="btn btn-outline-danger" @onclick=@(() => OnDeleteTeamClick(team)) @onclick:stopPropagation="true">
<span class="oi oi-trash"/>
</button>
</div>
}
</div>
</div>
<button class="btn btn-outline-secondary" @onclick=OnAddTeamClick>Add Team</button>
</div>

@code {
[CascadingParameter]
public IModalService ModalService { get; set; } = default!;

[Parameter(CaptureUnmatchedValues = true)]
public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }

private async Task OnTeamClick(TeamViewModel config)
{
var parameters = new ModalParameters()
.Add(nameof(EditTeamModal.Model), config.CopyModel())
.Add(nameof(EditTeamModal.LeagueMembers), GetMembersWithoutTeam(vm.Members))
.Add(nameof(EditTeamModal.OnSubmit), new Func<TeamViewModel, CancellationToken, Task<StatusResult>>(
async (team, cancellationToken) => await team.SaveChangesAsync(cancellationToken)));
var options = new ModalOptions()
{
DisableBackgroundCancel = true,
};
var result = await ModalService.Show<EditTeamModal>("Edit Team", parameters, options).Result;
if (result.Confirmed && result.Data is TeamModel model)
{
config.SetModel(model);
}
}

private async Task OnAddTeamClick()
{
var parameters = new ModalParameters()
.Add(nameof(EditTeamModal.Model), new TeamModel())
.Add(nameof(EditTeamModal.LeagueMembers), GetMembersWithoutTeam(vm.Members))
.Add(nameof(EditTeamModal.OnSubmit), new Func<TeamViewModel, CancellationToken, Task<StatusResult>>(
async (team, cancellationToken) => await vm.AddTeam(team.GetModel(), cancellationToken)));
var options = new ModalOptions()
{
DisableBackgroundCancel = true,
};
var result = await ModalService.Show<EditTeamModal>("Add new Team", parameters, options).Result;
}

private IEnumerable<MemberInfoModel> GetMembersWithoutTeam(IEnumerable<MemberInfoModel> members)
{
return members
.Where(member => vm.Teams.Any(team => team.Members.Any(teamMember => member.MemberId == teamMember.MemberId)) == false)
.ToList();
}

private async Task OnDeleteTeamClick(TeamViewModel team)
{
var parameters = new ModalParameters()
.Add(nameof(ConfirmModal.Text), $"Really delete Team \"{team.Name}\"?")
.Add(nameof(ConfirmModal.ButtonTypes), ButtonTypes.YesNo);
var result = await ModalService.Show<ConfirmModal>("Delete Result Config", parameters).Result;
if (result.Confirmed)
{
await vm.DeleteTeam(team.GetModel());
}
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender == false)
{
return;
}
if (apiService.CurrentLeague != null)
{
await vm.LoadFromLeagueAsync();
}
await InvokeAsync(StateHasChanged);
}
}
28 changes: 19 additions & 9 deletions src/iRLeagueManager.Web/Pages/Settings/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@

<div class="container-md">
<LoadingHeader><h3>Settings</h3></LoadingHeader>
<AuthorizeView Roles=@GetRoleString(LeagueRoles.Admin)>
<AuthorizeView Roles=@GetRoleString(LeagueRoles.Admin, LeagueRoles.Organizer)>
<Authorized>
<div>
<ul class="nav nav-tabs">
<AuthorizeView Roles=@GetRoleString(LeagueRoles.Admin)>
<Authorized Context=AdminContext>
<li class="nav-item">
<NavLink class="nav-link" href="@GetTabLink(0)">League</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link" href="@GetTabLink(1)">Users</NavLink>
</li>
</Authorized>
</AuthorizeView>
<li class="nav-item">
<NavLink class="nav-link" href="@GetTabLink(0)">League</NavLink>
<NavLink class="nav-link" href="@GetTabLink(2)">Results</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link" href="@GetTabLink(1)">Users</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link" href="@GetTabLink(2)">Results</NavLink>
<NavLink class="nav-link" href="@GetTabLink(3)">Teams</NavLink>
</li>
</ul>

Expand All @@ -29,13 +36,16 @@
@switch (selectedTabIndex)
{
case 0:
<LeagueSettings class="container-md px-0 mb-2 mx-0" />
<LeagueSettings class="container px-0 mb-2 mx-0" />
break;
case 1:
<UserSettings class="container-md px-0 mb-2 mx-0" />
<UserSettings class="container px-0 mb-2 mx-0" />
break;
case 2:
<ResultSettings class="container-md px-0 mb-2 mx-0" />
<ResultSettings class="container px-0 mb-2 mx-0" />
break;
case 3:
<TeamSettings class="container px-0 mb-2 mx-0" />
break;
default:
<p><em>Index out of bounds!</em></p>
Expand Down
2 changes: 2 additions & 0 deletions src/iRLeagueManager.Web/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public static IServiceCollection AddViewModels(this IServiceCollection services)
services.TryAddTransient<PointRuleViewModel>();
services.TryAddTransient<SortOptionsViewModel>();
services.TryAddTransient<ResultFiltersViewModel>();
services.TryAddTransient<TeamViewModel>();
services.TryAddScoped<ResultsPageViewModel>();
services.TryAddScoped<ResultConfigSettingsViewModel>();
services.TryAddScoped<ReviewsPageViewModel>();
services.TryAddScoped<EventListViewModel>();
services.TryAddScoped<LeagueUsersViewModel>();
services.TryAddScoped<StandingsPageViewModel>();
services.TryAddScoped<ResultConfigSettingsViewModel>();
services.TryAddScoped<TeamsViewModel>();
return services;
}
}
53 changes: 53 additions & 0 deletions src/iRLeagueManager.Web/ViewModels/TeamViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using iRLeagueApiCore.Common.Models;
using iRLeagueApiCore.Common.Models.Members;
using iRLeagueManager.Web.Data;
using iRLeagueManager.Web.Extensions;

namespace iRLeagueManager.Web.ViewModels;

public sealed class TeamViewModel : LeagueViewModelBase<TeamViewModel, TeamModel>
{
public TeamViewModel(ILoggerFactory loggerFactory, LeagueApiService apiService) :
this(loggerFactory, apiService, new())
{
}

public TeamViewModel(ILoggerFactory loggerFactory, LeagueApiService apiService, TeamModel model) :
base(loggerFactory, apiService, model)
{
}

public long TeamId => model.TeamId;
public string Name { get => model.Name; set => SetP(model.Name, value => model.Name = value, value); }
public string Profile { get => model.Profile; set => SetP(model.Profile, value => model.Profile = value, value); }
public string TeamColor { get => model.TeamColor; set => SetP(model.TeamColor, value => model.TeamColor = value, value); }
public string TeamHomepage { get => model.TeamHomepage; set => SetP(model.TeamHomepage, value => model.TeamHomepage = value, value); }
public IList<MemberInfoModel> Members { get => (IList<MemberInfoModel>)model.Members; set => SetP(model.Members, value => model.Members = value, value); }

public async Task<StatusResult> SaveChangesAsync(CancellationToken cancellationToken = default)
{
if (ApiService.CurrentLeague is null)
{
return LeagueNullResult();
}

try
{
Loading = true;
var request = ApiService.CurrentLeague
.Teams()
.WithId(TeamId)
.Put(model, cancellationToken);
var result = await request;
if (result.Success && result.Content is not null)
{
SetModel(result.Content);
}
return result.ToStatusResult();
}
finally
{
Loading = false;
}
}
}
Loading