1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-22 08:42:44 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Web/Shared/InstanceStopDialog.razor
2022-12-30 03:26:06 +01:00

71 lines
2.4 KiB
Plaintext

@using Phantom.Server.Services.Instances
@using Phantom.Server.Services.Audit
@using System.ComponentModel.DataAnnotations
@using Phantom.Common.Data.Minecraft
@using Phantom.Common.Data.Replies
@inherits PhantomComponent
@inject IJSRuntime Js;
@inject InstanceManager InstanceManager;
@inject AuditLog AuditLog
<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 {
[Parameter, EditorRequired]
public Guid InstanceGuid { get; set; }
[Parameter, EditorRequired]
public string ModalId { get; set; } = string.Empty;
[Parameter]
public bool Disabled { get; set; }
private readonly StopInstanceFormModel form = new ();
private sealed class StopInstanceFormModel : FormModel {
[Range(minimum: 0, maximum: 300, ErrorMessage = "Stop delay must be between 0 and 300 seconds.")]
public ushort StopInSeconds { get; set; } = 0;
}
private async Task StopInstance(EditContext context) {
await form.SubmitModel.StartSubmitting();
if (!await CheckPermission(Permission.ControlInstances)) {
form.SubmitModel.StopSubmitting("You do not have permission to stop instances.");
return;
}
var result = await InstanceManager.StopInstance(InstanceGuid, new MinecraftStopStrategy(form.StopInSeconds));
if (result.Is(StopInstanceResult.StopInitiated)) {
await AuditLog.AddInstanceStoppedEvent(InstanceGuid, form.StopInSeconds);
await Js.InvokeVoidAsync("closeModal", ModalId);
form.SubmitModel.StopSubmitting();
}
else {
form.SubmitModel.StopSubmitting(result.ToSentence(StopInstanceResultExtensions.ToSentence));
}
}
}