Skip to content

Commit

Permalink
feat: select resource from system
Browse files Browse the repository at this point in the history
  • Loading branch information
aguacongas committed Jun 4, 2020
1 parent c98eb07 commit 5cac5ff
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/Aguacongas.TheIdServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public void Configure(IApplicationBuilder app)
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCulture.Select(c => new CultureInfo(c)).ToList();
options.SupportedUICultures = options.SupportedCultures;
options.FallBackToParentCultures = true;
})
.UseSerilogRequestLogging();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

namespace Aguacongas.TheIdServer.BlazorApp.Components
{
public abstract class AutoCompleteModel<T> : ComponentBase, IDisposable
{
private Expression<Func<object>> _fieldExpression;
private FieldIdentifier _fieldIdentifier;

[Inject]
Expand All @@ -29,6 +31,17 @@ public abstract class AutoCompleteModel<T> : ComponentBase, IDisposable
[Parameter]
public EventCallback<T> ValueChanged { get; set; }

[Parameter]
public Expression<Func<object>> FieldExpression
{
get => _fieldExpression;
set
{
_fieldExpression = value;
_fieldIdentifier = FieldIdentifier.Create(value);
}
}

[CascadingParameter]
EditContext EditContext { get; set; }

Expand All @@ -52,18 +65,21 @@ protected string FieldClass
=> EditContext.FieldCssClass(_fieldIdentifier);


protected Task SetSelectedValue(string value)
protected async Task SetSelectedValue(string value)
{
SetValue(value);
EditContext.NotifyFieldChanged(_fieldIdentifier);
FilteredValues = null;
return ValueChanged.InvokeAsync(Entity);
await ValueChanged.InvokeAsync(Entity).ConfigureAwait(false);
EditContext.NotifyFieldChanged(_fieldIdentifier);
}

protected override void OnParametersSet()
{
base.OnParametersSet();
_fieldIdentifier = new FieldIdentifier(Entity, PropertyName);
if (_fieldExpression == null)
{
_fieldIdentifier = new FieldIdentifier(Entity, PropertyName);
}
}

protected override void OnInitialized()
Expand All @@ -89,7 +105,7 @@ protected Task Filter()
_cancellationTokenSource = new CancellationTokenSource();
var token = _cancellationTokenSource.Token;

return Task.Delay(250, token)
return Task.Delay(200, token)
.ContinueWith(async task =>
{
if (task.IsCanceled)
Expand All @@ -106,7 +122,7 @@ protected Task Filter()
}, TaskScheduler.Default);
}

protected Task OnInputChanged(ChangeEventArgs e)
protected virtual Task OnInputChanged(ChangeEventArgs e)
{
SelectedValue = e.Value as string;
SetValue(SelectedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
else
{
<div id="@Id" class="input-group input-group-sm mb-3" data-toggle="dropdown">
<input type="text" class="form-control new-claim" placeholder="@Localizer["claim type"]"
<input type="text" class="form-control new-claim @FieldClass" placeholder="@Localizer["claim type"]"
@oninput="OnInputChanged" />
<div class="input-group-append" @onclick="() => SetSelectedValue(SelectedValue)">
<span class="input-group-text oi oi-plus" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ else
{
<div id="@Id" class="input-group input-group-sm mb-3" data-toggle="dropdown">
<input type="text" class="form-control new-claim @FieldClass" placeholder="@Localizer["grant type"]" value="@SelectedValue"
@oninput="OnInputChanged" @onfocus='() => OnInputChanged(new ChangeEventArgs { Value = "" })' />
@oninput="OnInputChanged" @onfocus="() => OnInputChanged(new ChangeEventArgs { Value = string.Empty })" />
<div class="input-group-append">
<span class="input-group-text oi oi-plus" />
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@using System.Globalization
@inherits AutoCompleteModel<CultureInfo>

<div class="input-group" data-toggle="dropdown">
<input id="@Id" type="text" class="form-control @FieldClass" placeholder="@Localizer["culture"]" value="@Entity.Name"
@oninput="OnInputChanged" @onfocus="() => OnInputChanged(new ChangeEventArgs { Value = Entity.Name })" />
</div>
<div class="dropdown-menu m-0">
@if (_filterValues != null)
{
foreach (var value in _filterValues)
{
<button class="dropdown-item m-0 p-0 pl-1 pr-1" type="button" @onclick="() => SetSelectedValue(value.Name)">
@value.Name
<div>
<em><small>@value.DisplayName</small></em>
</div>
</button>
}
}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;

namespace Aguacongas.TheIdServer.BlazorApp.Components
{
public partial class CultureInfos
{
private IEnumerable<CultureInfo> _filterValues;
protected override bool IsReadOnly => false;

protected override string PropertyName => "Name";

protected override Task<IEnumerable<string>> GetFilteredValues(string term)
{
term = term ?? string.Empty;
_filterValues = CultureInfo.GetCultures(CultureTypes.AllCultures)
.Where(c => c.Name.Contains(term, StringComparison.OrdinalIgnoreCase) || c.DisplayName.Contains(term, StringComparison.OrdinalIgnoreCase))
.Take(5);

return Task.FromResult(_filterValues.Select(c => c.Name));
}

protected override void SetValue(string inputValue)
{
var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures)
.FirstOrDefault(c => c.Name == inputValue);
if (cultureInfo != null)
{
Entity = cultureInfo;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
@inject IStringLocalizerAsync<EntityResourceTranslatation> Localizer

<div class="col" for="culture">
<AuthorizeText Id="@($"culture{Resource.CultureId}")" Placeholder="@Localizer["culture"]" @bind-Value="@Resource.CultureId" />
</div>
<div class="col" for="culture">
@if (Resource.Id == null)
{
<CultureInfos Entity="@_cultureInfo" ValueChanged="CultureSelected" FieldExpression="FieldExpression" />
}
else
{
<div class="col-form-label">
@Resource.CultureId <em><small>@Localizer[_cultureInfo?.DisplayName ?? string.Empty]</small></em>
</div>
}
</div>
<div class="col-lg-9 col-sm-12">
@if (Resource.ResourceKind == Entity.EntityResourceKind.DisplayName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,44 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System;
using System.Globalization;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Aguacongas.TheIdServer.BlazorApp.Components
{
public partial class EntityResourceTranslatation
{
private CultureInfo _cultureInfo = CultureInfo.InvariantCulture;

[Parameter]
public IEntityResource Resource { get; set; }

[Parameter]
public EventCallback<IEntityResource> DeleteResource { get; set; }

[Parameter]
public Expression<Func<object>> FieldExpression { get; set; }

protected override void OnInitialized()
{
Console.WriteLine($"Resource = {Resource}");
Localizer.OnResourceReady = () => InvokeAsync(StateHasChanged);
if (Resource.CultureId != null)
{
_cultureInfo = CultureInfo.GetCultureInfo(Resource.CultureId);
}
base.OnInitialized();
}

private Task OnDeleteClick(MouseEventArgs args)
{
return DeleteResource.InvokeAsync(Resource);
}

private void CultureSelected(CultureInfo cultureInfo)
{
_cultureInfo = cultureInfo;
Resource.CultureId = cultureInfo.Name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
@foreach (var resource in Collection.Where(r => r.ResourceKind == ResourceKind))
{
<div @key="@resource" class="form-group row">
<EntityResourceTranslatation Resource="resource" DeleteResource="OnDeleteResource" />
<EntityResourceTranslatation Resource="resource" DeleteResource="OnDeleteResource" FieldExpression="() => resource.CultureId" />
</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class EntityResourcesComponentBase<T> : ComponentBase where T:IE
protected Task OnDeleteResource(IEntityResource resource)
{
Collection.Remove((T)resource);
HandleModificationState.EntityDeleted(resource);
HandleModificationState.EntityDeleted((T)resource);
return Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ else
<div class="col">
@if (IsNew)
{
<AuthorizeText Id="id" Placeholder="@Localizer["culture name"]" @bind-Value="@Model.Id" />
<AuthorizeView>
<Authorized Context="culture">
<CultureInfos Entity="_culture" ValueChanged="CultureSelected" FieldExpression="() => Model.Id" />
</Authorized>
</AuthorizeView>
}
else
{
<h3>@Model.Id</h3>
<h3>@Model.Id <small><em>@Info.DisplayName</em></small></h3>
}
</div>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,6 +14,7 @@ namespace Aguacongas.TheIdServer.BlazorApp.Pages
public partial class Culture
{
private readonly GridState _gridState = new GridState();
private CultureInfo _culture = CultureInfo.InvariantCulture;
private bool _hasMore;
private CancellationTokenSource _cancellationTokenSource;
private PageRequest _pageRequest;
Expand All @@ -23,6 +25,7 @@ public Task ScrollBottomReach()
return LoadMoreAsync();
}

private CultureInfo Info => CultureInfo.GetCultureInfo(Id);
protected override string Expand => null;

protected override bool NonEditable => false;
Expand Down Expand Up @@ -184,5 +187,12 @@ private async Task LoadMoreAsync()
_hasMore = resourceList.Count < page.Count;
StateHasChanged();
}

private void CultureSelected(CultureInfo culture)
{
Console.WriteLine($"culture {culture}");
_culture = culture;
Model.Id = culture.Name;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using Microsoft.AspNetCore.Identity
@using System.Globalization

@page "/cultures"
@inherits EntitiesModel<Entity.Culture>

Expand Down Expand Up @@ -29,7 +30,12 @@ else
<EntitiesGrid Items="EntityList" TableClass="table table-hover"
RowClicked="(Entity.Culture item) => OnRowClicked(item)">
<RowTemplate>
<td>@context.Id</td>
<td>
@context.Id
<div>
<em>@CultureInfo.GetCultureInfo(context.Id).DisplayName</em>
</div>
</td>
</RowTemplate>
</EntitiesGrid>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<ExternalClaimTransformation>()
.HasIndex(e => new { e.Scheme, e.FromClaimType })
.IsUnique(true);
modelBuilder.Entity<ApiLocalizedResource>()
.HasOne(e => e.Api)
.WithMany(a => a.Resources)
.HasForeignKey(e => e.ApiId)
.IsRequired(true)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ApiScopeLocalizedResource>()
.HasOne(e => e.ApiScope)
.WithMany(a => a.Resources)
.HasForeignKey(e => e.ApiScopeId)
.IsRequired(true)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ClientLocalizedResource>()
.HasOne(e => e.Client)
.WithMany(a => a.Resources)
.HasForeignKey(e => e.ClientId)
.IsRequired(true)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<IdentityLocalizedResource>()
.HasOne(e => e.Identity)
.WithMany(a => a.Resources)
.HasForeignKey(e => e.IdentityId)
.IsRequired(true)
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<SchemeDefinition>(b =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public class LocalizedResource : LocalizedResourceBase, IAuditable
/// </value>
public string Location { get; set; }

/// <summary>
/// Gets or sets the culture.
/// </summary>
/// <value>
/// The culture.
/// </value>
public virtual Culture Culture { get; set; }

}

/// <summary>
Expand Down Expand Up @@ -67,14 +75,6 @@ public abstract class LocalizedResourceBase : IAuditable
[Required]
public string CultureId { get; set; }

/// <summary>
/// Gets or sets the culture.
/// </summary>
/// <value>
/// The culture.
/// </value>
public virtual Culture Culture { get; set; }

/// <summary>
/// Gets or sets the created at.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.AspNetCore.Components.Testing/TestHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void WaitForNextRender(Action trigger = null)
public void WaitForNoRender()
{
var task = Renderer.NextRender;
while(task.Wait(200))
while(task.Wait(500))
{
task = Renderer.NextRender;
}
Expand Down

0 comments on commit 5cac5ff

Please sign in to comment.