1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-05-09 06:34:02 +02:00

Minor web form refactoring and fixes

This commit is contained in:
chylex 2022-10-25 04:56:00 +02:00
parent fd0097214b
commit c582aefb05
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
8 changed files with 56 additions and 45 deletions

View File

@ -1,4 +1,5 @@
<div id="@Id" class="modal fade" tabindex="-1">
@using Phantom.Server.Web.Components.Utils
<div id="@Id" class="modal fade" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
@ -39,9 +40,7 @@
public RenderFragment? Footer { get; set; }
protected override void OnParametersSet() {
if (TitleText != null && Title != null) {
throw new InvalidOperationException("Cannot set both TitleText and Title at the same time.");
}
BlazorUtils.RequireEitherParameterIsSet(TitleText, Title);
}
}

View File

@ -18,9 +18,7 @@ public sealed class FormButtonSubmit : ComponentBase {
public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }
protected override void OnParametersSet() {
if (Form == null && Model == null) {
throw new InvalidOperationException("Either the Form or Model parameter must be set.");
}
BlazorUtils.RequireEitherParameterIsSet(Form, Model);
}
protected override void BuildRenderTree(RenderTreeBuilder builder) {

View File

@ -1,4 +1,5 @@
@if (Label != null) {
@using Phantom.Server.Web.Components.Utils
@if (Label != null) {
<label for="@Id" class="form-label">@Label</label>
}
else if (LabelFragment != null) {
@ -16,4 +17,8 @@ else if (LabelFragment != null) {
[Parameter]
public RenderFragment? LabelFragment { get; set; }
protected override void OnParametersSet() {
BlazorUtils.RequireEitherParameterIsSet(Label, LabelFragment);
}
}

View File

@ -1,4 +1,5 @@
@if (messageLines.Length > 0) {
@using Phantom.Server.Web.Components.Utils
@if (messageLines.Length > 0) {
<div class="text-danger my-2 w-100">
@for (int i = 0; i < messageLines.Length; i++) {
@messageLines[i]
@ -23,12 +24,10 @@
private string[] messageLines = Array.Empty<string>();
protected override void OnParametersSet() {
var model = Form?.Model.SubmitModel ?? Model;
if (model == null) {
throw new InvalidOperationException("Either the Form or Model parameter must be set.");
}
BlazorUtils.RequireEitherParameterIsSet(Form, Model);
var message = model.SubmitError ?? Message;
var model = Form?.Model.SubmitModel ?? Model;
var message = model?.SubmitError ?? Message;
messageLines = message?.Split('\n') ?? Array.Empty<string>();
}

View File

@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace Phantom.Server.Web.Components.Utils;
@ -27,4 +28,20 @@ static class BlazorUtils {
public static bool CombineBooleansWithOr(IReadOnlyDictionary<string, object>? additionalAttributes, string attributeName, bool value) {
return value || (additionalAttributes is not null && additionalAttributes.TryGetValue(attributeName, out var @bool) && @bool is bool and true);
}
public static void RequireEitherParameterIsSet<T1, T2>(
T1 parameterValue1,
T2 parameterValue2,
[CallerArgumentExpression(nameof(parameterValue1))]
string parameterName1 = "",
[CallerArgumentExpression(nameof(parameterValue2))]
string parameterName2 = ""
) {
if (parameterValue1 is null && parameterValue2 is null) {
throw new InvalidOperationException($"Either {parameterName1} or {parameterName2} must be set.");
}
else if (parameterValue1 is not null && parameterValue2 is not null) {
throw new InvalidOperationException($"Either {parameterName1} or {parameterName2} must be set, but not both.");
}
}
}

View File

@ -41,7 +41,7 @@ else {
</div>
</div>
<InstanceStopForm InstanceGuid="InstanceGuid" ModalId="stop-instance" Disabled="@(!Instance.Status.CanStop())" />
<InstanceStopDialog InstanceGuid="InstanceGuid" ModalId="stop-instance" Disabled="@(!Instance.Status.CanStop())" />
}
@code {

View File

@ -18,13 +18,13 @@
<div style="max-width: 400px;">
<div class="row">
<div class="mb-3">
<FormTextInput Id="account-username" Label="Username" @bind-Value="form.Username" />
<FormTextInput Id="account-username" Label="Username" @bind-Value="form.Username" autocomplete="off" />
</div>
</div>
<div class="row">
<div class="mb-3">
<FormTextInput Id="account-password" Label="Password" Type="FormTextInputType.Password" @bind-Value="form.Password" />
<FormTextInput Id="account-password" Label="Password" Type="FormTextInputType.Password" @bind-Value="form.Password" autocomplete="new-password" />
</div>
</div>

View File

@ -7,34 +7,27 @@
@inject InstanceManager InstanceManager;
@inject AuditLog AuditLog
<div id="@ModalId" class="modal fade" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<Form Model="form" OnSubmit="StopInstance">
<div class="modal-header">
<h5 class="modal-title">Stop Instance</h5>
</div>
<div class="modal-body">
<FormSelectInput Id="stop-in-seconds" Label="Stop In..." @bind-Value="form.StopInSeconds">
<option value="0">Immediately</option>
<option value="10">10 Seconds</option>
<option value="30">30 Seconds</option>
<option value="60">1 Minute</option>
<option value="120">2 Minutes</option>
<option value="180">3 Minutes</option>
<option value="240">4 Minutes</option>
<option value="300">5 Minutes</option>
</FormSelectInput>
</div>
<div class="modal-footer">
<FormSubmitError />
<FormButtonSubmit Label="Stop Instance" class="btn btn-danger" disabled="@Disabled" />
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
</div>
</Form>
</div>
</div>
</div>
<Form Model="form" OnSubmit="StopInstance">
<Modal Id="@ModalId" TitleText="Stop Instance">
<Body>
<FormSelectInput Id="stop-in-seconds" Label="Stop In..." @bind-Value="form.StopInSeconds">
<option value="0">Immediately</option>
<option value="10">10 Seconds</option>
<option value="30">30 Seconds</option>
<option value="60">1 Minute</option>
<option value="120">2 Minutes</option>
<option value="180">3 Minutes</option>
<option value="240">4 Minutes</option>
<option value="300">5 Minutes</option>
</FormSelectInput>
</Body>
<Footer>
<FormSubmitError />
<FormButtonSubmit Label="Stop Instance" class="btn btn-danger" disabled="@Disabled" />
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
</Footer>
</Modal>
</Form>
@code {