about summary refs log tree commit diff
path: root/MatrixUtils.Web/Shared/PolicyEditorComponents
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-01-31 12:10:03 +0100
committerRory& <root@rory.gay>2024-01-31 12:10:03 +0100
commit83e6d98d2d7586fb518ed1b2097c59ea9b8af223 (patch)
tree995dacaec65725007e6a55c88f597aed1d13145a /MatrixUtils.Web/Shared/PolicyEditorComponents
parentRoom list fixes, migration fix, update available handler (diff)
downloadMatrixUtils-83e6d98d2d7586fb518ed1b2097c59ea9b8af223.tar.xz
New tools, fix room list items
Diffstat (limited to 'MatrixUtils.Web/Shared/PolicyEditorComponents')
-rw-r--r--MatrixUtils.Web/Shared/PolicyEditorComponents/PolicyEditorModal.razor120
1 files changed, 73 insertions, 47 deletions
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
 <ModalWindow Title="@((string.IsNullOrWhiteSpace(PolicyEvent.EventId) ? "Creating new " : "Editing ") + (PolicyEvent.MappedType.GetFriendlyNameOrNull()?.ToLower() ?? "event"))"
              OnCloseClicked="@OnClose" X="60" Y="60" MinWidth="300">
-    @{
-        var policyData = (PolicyEvent.TypedContent as PolicyRuleEventContent)!;
-    }
     @if (string.IsNullOrWhiteSpace(PolicyEvent.EventId)) {
         <span>Policy type:</span>
-        <select @bind="@PolicyEvent.Type">
+        <select @bind="@MappedType">
             <option>Select a value</option>
             @foreach (var (type, mappedType) in PolicyTypes) {
                 <option value="@type">@mappedType.GetFriendlyName().ToLower()</option>
@@ -20,7 +17,6 @@
         </select>
     }
 
-
     @{
         // 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();
     }
-    <table>
-        <thead style="border-bottom: solid #ffffff44 1px;">
-            <tr>
-                <th>Property</th>
-                <th>Value</th>
-            </tr>
-        </thead>
-        <tbody>
-            @foreach (var prop in props) {
+    @if (PolicyData is not null) {
+        <table>
+            <thead style="border-bottom: solid #ffffff44 1px;">
                 <tr>
-                    <td style="padding-right: 8px;">
-                        <span>@prop.GetFriendlyName()</span>
-                        @if (Nullable.GetUnderlyingType(prop.PropertyType) is not null) {
-                            <span style="color: red;">*</span>
-                        }
-                    </td>
-                    @{
-                        var getter = prop.GetGetMethod();
-                        var setter = prop.GetSetMethod();
-                    }
-                    @switch (Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType) {
-                        case Type t when t == typeof(string):
-                            <FancyTextBox Value="@(getter?.Invoke(policyData, null) as string)" ValueChanged="@(e => { Console.WriteLine($"{prop.Name} ({setter is not null}) -> {e}"); setter?.Invoke(policyData, [e]); StateHasChanged(); })"></FancyTextBox>
-                            break;
-                        default:
-                            <p style="color: red;">Unsupported type: @prop.PropertyType</p>
-                            break;
-                    }
+                    <th>Property</th>
+                    <th>Value</th>
                 </tr>
-            }
-        </tbody>
-    </table>
-    <br/>
-    <pre>
-        @PolicyEvent.ToJson(true, false)
-    </pre>
-    <LinkButton OnClick="@(() => { OnClose.Invoke(); return Task.CompletedTask; })">Cancel</LinkButton>
-    <LinkButton OnClick="@(() => { OnSave.Invoke(PolicyEvent); return Task.CompletedTask; })">Save</LinkButton>
-    @* <span>Target entity: </span> *@
-    @* <FancyTextBox @bind-Value="@policyData.Entity"></FancyTextBox><br/> *@
-    @* <span>Reason: </span> *@
-    @* <FancyTextBox @bind-Value="@policyData.Reason"></FancyTextBox> *@
+            </thead>
+            <tbody>
+                @foreach (var prop in props) {
+                    <tr>
+                        <td style="padding-right: 8px;">
+                            <span>@prop.GetFriendlyName()</span>
+                            @if (Nullable.GetUnderlyingType(prop.PropertyType) is not null) {
+                                <span style="color: red;">*</span>
+                            }
+                        </td>
+                        @{
+                            var getter = prop.GetGetMethod();
+                            var setter = prop.GetSetMethod();
+                        }
+                        @switch (Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType) {
+                            case Type t when t == typeof(string):
+                                <FancyTextBox Value="@(getter?.Invoke(PolicyData, null) as string)" ValueChanged="@(e => { Console.WriteLine($"{prop.Name} ({setter is not null}) -> {e}"); setter?.Invoke(PolicyData, [e]); PolicyEvent.TypedContent = PolicyData; StateHasChanged(); })"></FancyTextBox>
+                                break;
+                            default:
+                                <p style="color: red;">Unsupported type: @prop.PropertyType</p>
+                                break;
+                        }
+                    </tr>
+                }
+            </tbody>
+        </table>
+        <br/>
+        <pre>
+            @PolicyEvent.ToJson(true, false)
+        </pre>
+        <LinkButton OnClick="@(() => { OnClose.Invoke(); return Task.CompletedTask; })"> Cancel </LinkButton>
+        <LinkButton OnClick="@(() => { OnSave.Invoke(PolicyEvent); return Task.CompletedTask; })"> Save </LinkButton>
+        @* <span>Target entity: </span> *@
+        @* <FancyTextBox @bind-Value="@policyData.Entity"></FancyTextBox><br/> *@
+        @* <span>Reason: </span> *@
+        @* <FancyTextBox @bind-Value="@policyData.Reason"></FancyTextBox> *@
+    }
+    else {
+        <p>Policy data is null</p>
+    }
 </ModalWindow>
 
 @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<StateEventResponse> OnSave { get; set; }
 
+    public PolicyRuleEventContent? PolicyData { get; set; }
+
     private static FrozenSet<Type> KnownPolicyTypes = StateEvent.KnownStateEventTypes.Where(x => x.IsAssignableTo(typeof(PolicyRuleEventContent))).ToFrozenSet();
 
     private static Dictionary<string, Type> PolicyTypes = KnownPolicyTypes
         .ToDictionary(x => x.GetCustomAttributes<MatrixEventAttribute>().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