LANCommander/LANCommander.Server/UI/Components/ScriptEditorDialog.razor

70 lines
1.8 KiB
Text
Raw Normal View History

@inherits FeedbackComponent<ScriptEditorOptions, Script>
@inject ScriptService ScriptService
@inject IMessageService MessageService
2024-01-04 18:23:54 -06:00
@inject ILogger<ScriptEditorDialog> Logger
<ScriptTextEditor
@bind-Script="@Script"
ArchiveId="@Options.ArchiveId"
AllowedTypes="@Options.AllowedTypes"
OnSave="Save" />
@code {
Guid Id = Guid.NewGuid();
2025-02-04 20:58:08 -06:00
Script Script = new();
protected override async Task OnInitializedAsync()
{
if (Options.ScriptId != Guid.Empty)
Script = await ScriptService.GetAsync(Options.ScriptId);
else if (Options.GameId != Guid.Empty)
Script = new Script()
{
GameId = Options.GameId
};
else if (Options.RedistributableId != Guid.Empty)
Script = new Script()
{
RedistributableId = Options.RedistributableId
};
else if (Options.ServerId != Guid.Empty)
Script = new Script()
{
ServerId = Options.ServerId
};
}
public override async Task OnFeedbackOkAsync(ModalClosingEventArgs args)
{
var success = await Save();
if (success)
await base.OkCancelRefWithResult!.OnOk(Script);
else
args.Reject();
}
async Task<bool> Save()
{
try
{
if (Script.Id == Guid.Empty)
Script = await ScriptService.AddAsync(Script);
else
Script = await ScriptService.UpdateAsync(Script);
MessageService.Success("Script saved!");
return true;
}
catch (Exception ex)
{
MessageService.Error("Script could not be saved!");
Logger.LogError(ex, "Script could not be saved!");
return false;
}
}
}