From 83e6d98d2d7586fb518ed1b2097c59ea9b8af223 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 31 Jan 2024 12:10:03 +0100 Subject: New tools, fix room list items --- .../PolicyEditorComponents/PolicyEditorModal.razor | 120 +++++++++++++-------- 1 file changed, 73 insertions(+), 47 deletions(-) (limited to 'MatrixUtils.Web/Shared/PolicyEditorComponents') diff --git a/MatrixUtils.Web/Shared/PolicyEditorComponents/PolicyEditorModal.razor b/MatrixUtils.Web/Shared/PolicyEditorComponents/PolicyEditorModal.razor index 4fd151d..1bd00d1 100644 --- a/MatrixUtils.Web/Shared/PolicyEditorComponents/PolicyEditorModal.razor +++ b/MatrixUtils.Web/Shared/PolicyEditorComponents/PolicyEditorModal.razor @@ -7,12 +7,9 @@ @using LibMatrix.EventTypes - @{ - var policyData = (PolicyEvent.TypedContent as PolicyRuleEventContent)!; - } @if (string.IsNullOrWhiteSpace(PolicyEvent.EventId)) { Policy type: - @foreach (var (type, mappedType) in PolicyTypes) { @@ -20,7 +17,6 @@ } - @{ // enumerate all properties with friendly name var props = PolicyEvent.MappedType.GetProperties(BindingFlags.Public | BindingFlags.Instance) @@ -29,64 +25,94 @@ .ToFrozenSet(); var propNames = props.Select(x => x.GetFriendlyNameOrNull() ?? x.GetJsonPropertyName()!).ToFrozenSet(); } - - - - - - - - - @foreach (var prop in props) { + @if (PolicyData is not null) { +
PropertyValue
+ - - @{ - var getter = prop.GetGetMethod(); - var setter = prop.GetSetMethod(); - } - @switch (Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType) { - case Type t when t == typeof(string): - {e}"); setter?.Invoke(policyData, [e]); StateHasChanged(); })"> - break; - default: -

Unsupported type: @prop.PropertyType

- break; - } + + - } - -
- @prop.GetFriendlyName() - @if (Nullable.GetUnderlyingType(prop.PropertyType) is not null) { - * - } - PropertyValue
-
-
-        @PolicyEvent.ToJson(true, false)
-    
- Cancel - Save - @* Target entity: *@ - @*
*@ - @* Reason: *@ - @* *@ + + + @foreach (var prop in props) { + + + @prop.GetFriendlyName() + @if (Nullable.GetUnderlyingType(prop.PropertyType) is not null) { + * + } + + @{ + var getter = prop.GetGetMethod(); + var setter = prop.GetSetMethod(); + } + @switch (Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType) { + case Type t when t == typeof(string): + {e}"); setter?.Invoke(PolicyData, [e]); PolicyEvent.TypedContent = PolicyData; StateHasChanged(); })"> + break; + default: +

Unsupported type: @prop.PropertyType

+ break; + } + + } + + +
+
+            @PolicyEvent.ToJson(true, false)
+        
+ Cancel + Save + @* Target entity: *@ + @*
*@ + @* Reason: *@ + @* *@ + } + else { +

Policy data is null

+ }
@code { [Parameter] - public StateEventResponse? PolicyEvent { get; set; } + 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; + } + } + } + + } \ No newline at end of file -- cgit 1.5.1