@using LibMatrix.EventTypes.Spec.State.Policy
@using System.Reflection
@using ArcaneLibs.Attributes
@using ArcaneLibs.Extensions
@using LibMatrix
@using System.Collections.Frozen
@using LibMatrix.EventTypes
@if (string.IsNullOrWhiteSpace(PolicyEvent.EventId)) {
Policy type:
}
@{
// enumerate all properties with friendly name
var props = PolicyEvent.MappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => (x.GetFriendlyNameOrNull() ?? x.GetJsonPropertyNameOrNull()) is not null)
.Where(x => x.GetCustomAttribute() is null)
.ToFrozenSet();
var propNames = props.Select(x => x.GetFriendlyNameOrNull() ?? x.GetJsonPropertyName()!).ToFrozenSet();
}
@if (PolicyData is not null) {
Property
Value
@foreach (var prop in props) {
var isNullable = Nullable.GetUnderlyingType(prop.PropertyType) is not null;
@prop.GetFriendlyName()
@if (Nullable.GetUnderlyingType(prop.PropertyType) is null) {
*
}
@{
var getter = prop.GetGetMethod();
var setter = prop.GetSetMethod();
if (getter is null) {
Missing property getter: @prop.Name
}
else {
switch (Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType) {
case Type t when t == typeof(string):
{e}"); setter?.Invoke(PolicyData, [e]); PolicyEvent.TypedContent = PolicyData; StateHasChanged(); })">
break;
case Type t when t == typeof(DateTime):
if (!isNullable) {
@* {e}"); setter?.Invoke(PolicyData, [e]); PolicyEvent.TypedContent = PolicyData; StateHasChanged(); })"> *@
}
else {
var value = getter?.Invoke(PolicyData, null) as DateTime?;
if (value is null) {
}
else {
var notNullValue = Nullable.GetValueRefOrDefaultRef(ref value);
Console.WriteLine($"Value: {value?.ToString() ?? "null"}");
{e}"); setter?.Invoke(PolicyData, [e]); PolicyEvent.TypedContent = PolicyData; StateHasChanged(); })">
}
}
break;
default:
Unsupported type: @prop.PropertyType
break;
}
}
}
}
JSON data
@PolicyEvent.ToJson(true, true)
Cancel Save
}
else {
Policy data is null
}
@code {
[Parameter]
public StateEventResponse? PolicyEvent {
get => _policyEvent;
set {
if (value is not null && value != _policyEvent)
PolicyData = (value.TypedContent as PolicyRuleEventContent)!;
_policyEvent = value;
if (string.IsNullOrWhiteSpace(value.StateKey))
value.StateKey = Guid.NewGuid().ToString();
}
}
[Parameter]
public required Action OnClose { get; set; }
[Parameter]
public required Action OnSave { get; set; }
public PolicyRuleEventContent? PolicyData { get; set; }
private static FrozenSet KnownPolicyTypes = StateEvent.KnownStateEventTypes.Where(x => x.IsAssignableTo(typeof(PolicyRuleEventContent))).ToFrozenSet();
private static Dictionary PolicyTypes = KnownPolicyTypes
.ToDictionary(x => x.GetCustomAttributes().First(y => !string.IsNullOrWhiteSpace(y.EventName)).EventName, x => x);
private StateEventResponse? _policyEvent;
private string? MappedType {
get => _policyEvent?.Type;
set {
if (value is not null && PolicyTypes.ContainsKey(value)) {
PolicyEvent.Type = value;
PolicyEvent.TypedContent ??= Activator.CreateInstance(PolicyTypes[value]) as PolicyRuleEventContent;
PolicyData = PolicyEvent.TypedContent as PolicyRuleEventContent;
PolicyData.Recommendation ??= "m.ban";
}
}
}
}