finished list input + added proper prefix rendering
This commit is contained in:
@@ -17,6 +17,6 @@ public class PermissionGroup : IPermissionOwner {
|
||||
[Required]
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public virtual IList<Permission> Permissions { get; set; }
|
||||
public virtual List<Permission> Permissions { get; set; }
|
||||
|
||||
}
|
||||
@@ -20,9 +20,9 @@ public class User : IPermissionOwner {
|
||||
[Required]
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public virtual IList<Permission> Permissions { get; set; }
|
||||
public virtual List<Permission> Permissions { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public virtual IList<Token> Tokens { get; set; }
|
||||
public virtual List<Token> Tokens { get; set; }
|
||||
|
||||
}
|
||||
@@ -17,6 +17,8 @@ public interface IAdminPropertyGenerator<TProperty> {
|
||||
IAdminPropertyGenerator<TProperty> Prefix(string prefix);
|
||||
IAdminPropertyGenerator<TProperty> Validator(Func<object, bool> validator);
|
||||
IAdminPropertyGenerator<TProperty> IsSelector<TSelector>();
|
||||
IAdminPropertyGenerator<TProperty> Parser<TModel>(Func<TModel, string, TProperty> parser);
|
||||
IAdminPropertyGenerator<TProperty> ParserForListType<TModel, TInnerProperty>(Func<TModel, string, TInnerProperty> parser);
|
||||
IAdminPropertyGenerator<TProperty> DisplayProperty<TListingProperty>(Expression<Func<TProperty, TListingProperty>> propertyExpression);
|
||||
IAdminPropertyGenerator<TProperty> DisplayPropertyForListType<TInnerProperty>(Expression<Func<TInnerProperty, object>> propertyExpression);
|
||||
|
||||
|
||||
@@ -76,6 +76,16 @@ internal sealed class AdminPropertyGenerator<TProperty>(string name, Type type)
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator<TProperty> Parser<TModel>(Func<TModel, string, TProperty> parser) {
|
||||
_property.Parser = (o, s) => parser.Invoke((TModel)o, s);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator<TProperty> ParserForListType<TModel, TInnerProperty>(Func<TModel, string, TInnerProperty> parser) {
|
||||
_property.Parser = (o, s) => parser.Invoke((TModel)o, s);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator<TProperty> DisplayProperty<TListingProperty>(Expression<Func<TProperty, TListingProperty>> propertyExpression) {
|
||||
var property = AdminPageGenerator<object>.GetPropertyInfo(propertyExpression);
|
||||
_property.DisplayPropertyName = property.Name;
|
||||
|
||||
@@ -23,6 +23,7 @@ public sealed class AdminPageProperty {
|
||||
public Type SelectorType { get; set; }
|
||||
|
||||
public Func<object, bool> Validator { get; set; }
|
||||
public Func<object, string, object> Parser { get; set; }
|
||||
|
||||
public object GetValue(object entry) {
|
||||
return entry.GetType().GetProperty(Name)?.GetValue(entry);
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
@using BlazorStrap.Shared.Components.Modal
|
||||
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
||||
@using BlazorStrap.V5
|
||||
@using CurrieTechnologies.Razor.SweetAlert2
|
||||
@using HopFrame.Database.Repositories
|
||||
@using HopFrame.Security.Claims
|
||||
@using HopFrame.Web.Admin
|
||||
@using HopFrame.Web.Admin.Models
|
||||
@using HopFrame.Web.Admin.Providers
|
||||
@@ -22,7 +25,7 @@
|
||||
|
||||
<BSModalContent>
|
||||
@foreach (var prop in GetEditableProperties()) {
|
||||
@if (!_isEdit && !prop.Editable) continue;
|
||||
@if (!_isEdit && prop.Generated) continue;
|
||||
|
||||
<div class="mb-3">
|
||||
@if (IsListType(prop)) {
|
||||
@@ -42,10 +45,12 @@
|
||||
</BSListGroup>
|
||||
</BSListGroupItem>
|
||||
<BSListGroupItem>
|
||||
<div style="display: flex; gap: 20px">
|
||||
<div>
|
||||
@if (prop.SelectorType is null) {
|
||||
<input type="text" class="form-control" @onchange="v => _inputValues[prop] = (string)v.Value"/>
|
||||
<BSButton Color="BSColor.Secondary">Add</BSButton>
|
||||
<form style="display: flex; gap: 20px" @onsubmit="() => AddListItem(prop)">
|
||||
<input type="text" class="form-control" @onchange="v => _inputValues[prop] = (string)v.Value" required/>
|
||||
<BSButton Color="BSColor.Secondary" IsSubmit="true">Add</BSButton>
|
||||
</form>
|
||||
}
|
||||
else {
|
||||
@*<BSInput InputType="InputType.Select"> TODO: implement selector
|
||||
@@ -69,6 +74,12 @@
|
||||
<input class="form-check-input" type="checkbox" checked="@_values[prop]" @onchange="e => _values[prop] = Convert.ToBoolean(e.Value)">
|
||||
</div>
|
||||
}
|
||||
else if (prop.Prefix is not null && !_isEdit) {
|
||||
<BSInputGroup>
|
||||
<span class="@BS.Input_Group_Text">@prop.Prefix</span>
|
||||
<input type="text" class="form-control" disabled="@IsDisabled(prop)" required="@IsRequired(prop)" value="@GetPropertyValue(prop)" @onchange="e => _values[prop] = prop.Prefix + e.Value"/>
|
||||
</BSInputGroup>
|
||||
}
|
||||
else {
|
||||
<BSLabel>@prop.DisplayName</BSLabel>
|
||||
<input type="@GetInputType(prop)" class="form-control" disabled="@IsDisabled(prop)" required="@IsRequired(prop)" value="@GetPropertyValue(prop)" @onchange="e => _values[prop] = e.Value"/>
|
||||
@@ -86,6 +97,9 @@
|
||||
|
||||
@inject IServiceProvider Provider
|
||||
@inject IAdminPagesProvider PageProvider
|
||||
@inject SweetAlertService Alerts
|
||||
@inject IPermissionRepository Permissions
|
||||
@inject ITokenContext Auth
|
||||
|
||||
@code {
|
||||
[Parameter]
|
||||
@@ -129,15 +143,16 @@
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private bool IsDisabled(AdminPageProperty prop) => !prop.Editable;
|
||||
private bool IsDisabled(AdminPageProperty prop) => (_isEdit && !prop.Editable) || prop.Generated;
|
||||
private bool IsRequired(AdminPageProperty prop) => !_isEdit ? prop.Required : prop.Required && prop.EditDisplayValue;
|
||||
private bool IsSwitch(AdminPageProperty prop) => prop.Type == typeof(bool);
|
||||
|
||||
private bool IsListType(AdminPageProperty prop) {
|
||||
if (!prop.Type.IsGenericType) return false;
|
||||
var generic = prop.Type.GenericTypeArguments[0];
|
||||
var listType = typeof(IList<>).MakeGenericType(generic);
|
||||
return prop.Type.IsAssignableFrom(listType);
|
||||
var gListType = typeof(IList<>).MakeGenericType(generic);
|
||||
var iListType = typeof(List<>).MakeGenericType(generic);
|
||||
return prop.Type.IsAssignableFrom(gListType) || prop.Type.IsAssignableFrom(iListType);
|
||||
}
|
||||
|
||||
private IList<string> GetListPropertyValues(AdminPageProperty prop) {
|
||||
@@ -178,13 +193,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
/*if (type.GetProperties().Any(p => p.GetCustomAttributes(false).Any(a => a is ListingPropertyAttribute))) {
|
||||
var prop = type.GetProperties()
|
||||
.SingleOrDefault(p => p.GetCustomAttributes(false).Any(a => a is ListingPropertyAttribute));
|
||||
|
||||
return MapPropertyValue(prop?.GetValue(value), property);
|
||||
}*/
|
||||
|
||||
if (!string.IsNullOrEmpty(property.DisplayPropertyName) && !isSubProperty) {
|
||||
var prop = type.GetProperties()
|
||||
.SingleOrDefault(p => p.Name == property.DisplayPropertyName);
|
||||
@@ -213,25 +221,76 @@
|
||||
if (value.Key.Validator is null) continue;
|
||||
if (value.Key.Validator?.Invoke(value.Value) == true) continue;
|
||||
Console.WriteLine("INVALID");
|
||||
//TODO: implement validation
|
||||
}
|
||||
}
|
||||
|
||||
private void DeleteListItem(AdminPageProperty prop, int index) {
|
||||
Console.WriteLine(index);
|
||||
var list = prop.GetValue<IList>(_entry);
|
||||
list.RemoveAt(index);
|
||||
}
|
||||
|
||||
private void AddListItem(AdminPageProperty prop) {
|
||||
if (!_inputValues.TryGetValue(prop, out var input)) {
|
||||
Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Error!",
|
||||
Text = "Please enter a value!",
|
||||
Icon = SweetAlertIcon.Error
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var list = prop.GetValue<IList>(_entry);
|
||||
var value = prop.Parser?.Invoke(_entry, input) ?? input;
|
||||
list?.Add(value);
|
||||
}
|
||||
|
||||
private async void Save() {
|
||||
if (_isEdit && _currentPage.Permissions.Update is not null) {
|
||||
if (!await Permissions.HasPermission(Auth.User, _currentPage.Permissions.Update)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Unauthorized!",
|
||||
Text = "You don't have the required permissions to edit an entry!",
|
||||
Icon = SweetAlertIcon.Error
|
||||
});
|
||||
return;
|
||||
}
|
||||
}else if (_currentPage.Permissions.Create is not null) {
|
||||
if (!await Permissions.HasPermission(Auth.User, _currentPage.Permissions.Create)) {
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Unauthorized!",
|
||||
Text = "You don't have the required permissions to add an entry!",
|
||||
Icon = SweetAlertIcon.Error
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var value in _values) {
|
||||
value.Key.SetValue(_entry, value.Value);
|
||||
if (IsListType(value.Key)) continue;
|
||||
value.Key.SetValue(_entry, value.Key.Parser?.Invoke(_entry, (string)value.Value) ?? value.Value);
|
||||
}
|
||||
|
||||
if (!_isEdit) {
|
||||
await _repository.CreateO(_entry);
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "New entry added!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
ShowConfirmButton = false,
|
||||
Timer = 1500
|
||||
});
|
||||
}
|
||||
else {
|
||||
await _repository.UpdateO(_entry);
|
||||
|
||||
await Alerts.FireAsync(new SweetAlertOptions {
|
||||
Title = "Entry updated!",
|
||||
Icon = SweetAlertIcon.Success,
|
||||
ShowConfirmButton = false,
|
||||
Timer = 1500
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
await ReloadDelegate.Invoke();
|
||||
|
||||
@@ -30,8 +30,18 @@ public class HopAdminContext : AdminPagesContext {
|
||||
|
||||
generator.Page<User>().Property(u => u.Permissions)
|
||||
.DisplayInListing(false)
|
||||
.IsSelector<PermissionGroup>()
|
||||
.DisplayPropertyForListType<Permission>(p => p.PermissionName);
|
||||
.DisplayPropertyForListType<Permission>(p => p.PermissionName)
|
||||
.ParserForListType<User, Permission>((user, perm) => new Permission {
|
||||
GrantedAt = DateTime.Now,
|
||||
PermissionName = perm,
|
||||
User = user
|
||||
});
|
||||
|
||||
generator.Page<User>().Property(u => u.CreatedAt)
|
||||
.Generated();
|
||||
|
||||
generator.Page<User>().Property(u => u.Id)
|
||||
.Generated();
|
||||
|
||||
generator.Page<User>().Property(u => u.Tokens)
|
||||
.Ignore();
|
||||
@@ -49,13 +59,19 @@ public class HopAdminContext : AdminPagesContext {
|
||||
.Prefix("group.");
|
||||
|
||||
generator.Page<PermissionGroup>().Property(g => g.IsDefaultGroup)
|
||||
.DisplayName("Default Group")
|
||||
.Sortable(false);
|
||||
|
||||
generator.Page<PermissionGroup>().Property(g => g.CreatedAt)
|
||||
.Editable(false);
|
||||
.Generated();
|
||||
|
||||
generator.Page<PermissionGroup>().Property(g => g.Permissions)
|
||||
.DisplayInListing(false)
|
||||
.DisplayPropertyForListType<Permission>(p => p.PermissionName);
|
||||
.DisplayPropertyForListType<Permission>(p => p.PermissionName)
|
||||
.ParserForListType<PermissionGroup, Permission>((group, perm) => new Permission {
|
||||
GrantedAt = DateTime.Now,
|
||||
PermissionName = perm,
|
||||
Group = group
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -130,9 +130,9 @@
|
||||
_currentSortProperty = _pageData.DefaultSortPropertyName;
|
||||
_currentSortDirection = _pageData.DefaultSortDirection;
|
||||
|
||||
_modelRepository = Provider.GetService(_pageData.RepositoryProvider) as IModelRepository;
|
||||
if (_modelRepository is null)
|
||||
if (_pageData.RepositoryProvider is null)
|
||||
throw new ArgumentException($"AdminPage '{_pageData.Title}' does not specify a model repository!'");
|
||||
_modelRepository = Provider.GetService(_pageData.RepositoryProvider) as IModelRepository;
|
||||
|
||||
_hasEditPermission = await Permissions.HasPermission(Auth.User, _pageData.Permissions.Update);
|
||||
_hasDeletePermission = await Permissions.HasPermission(Auth.User, _pageData.Permissions.Delete);
|
||||
|
||||
Reference in New Issue
Block a user