Updated settings change route

This commit is contained in:
2025-01-13 12:27:35 +02:00
parent 49fe637455
commit 7ebe3e7014
10 changed files with 194 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
@inject DialogService DialogService
@inject NotificationService NotificationService
<div style="display: flex; justify-content: center; align-items: center; height: 100vh; background-color: transparent;">
<RadzenCard Style="padding: 2rem; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); width: 50%; max-width: 600px;">
<RadzenStack Orientation="Orientation.Vertical" Gap="20px" Style="color: white;">
@@ -9,21 +9,24 @@
Name="token"
Placeholder="Enter your token here ..."
Style="width: 100%;"
bind-Value="@TokenString" />
Value="@TokenString"
ValueChanged="str => TokenString = str"/>
<RadzenLabel Text="Specify the bot's prefix:" />
<RadzenTextBox
id="prefix"
Name="prefix"
Placeholder="Enter your prefix here ..."
Style="width: 100%;"
bind-Value="@PrefixString" />
Value="@PrefixString"
ValueChanged="str => PrefixString = str" />
<RadzenLabel Text="Enter server IDs (separated by semicolons):" />
<RadzenTextBox
id="server"
Name="server"
Placeholder="Enter server IDs here ..."
Style="width: 100%;"
bind-Value="@ServerIdsString" />
Value="@ServerIdsString"
ValueChanged="str => ServerIdsString = str" />
<RadzenButton Text="Save Changes" Click="SaveButtonClick" Style="margin-top: 1rem;" />
</RadzenStack>
@@ -32,12 +35,16 @@
@code {
private string TokenString { get; set; }
private string PrefixString { get; set; }
private string ServerIdsString { get; set; }
[Parameter]
public string TokenString { get; set; }
[Parameter]
public string PrefixString { get; set; }
[Parameter]
public string ServerIdsString { get; set; }
[Parameter]
public required Action SaveSettings { get; set; }
public required Action<string, string, string> SaveSettings { get; set; }
private async void SaveButtonClick()
{
@@ -49,18 +56,104 @@
OkButtonText = "Restart Now",
CancelButtonText = "Don't Save"
});
if (!response.HasValue)
{
NotificationService.Notify(new NotificationMessage()
{
Severity = NotificationSeverity.Error,
Summary = "Error",
Detail = "An error occurred while trying to save settings."
});
return;
}
if (!response.Value)
{
NotificationService.Notify(new NotificationMessage()
{
Severity = NotificationSeverity.Info,
Summary = "Info",
Detail = "Settings were not saved."
});
return;
}
if (string.IsNullOrWhiteSpace(TokenString) || string.IsNullOrWhiteSpace(PrefixString) || string.IsNullOrWhiteSpace(ServerIdsString))
{
NotificationService.Notify(new NotificationMessage()
{
Severity = NotificationSeverity.Error,
Summary = "Error",
Detail = "Please fill in all fields."
});
return;
}
if(!ValidateToken())
{
NotificationService.Notify(new NotificationMessage()
{
Severity = NotificationSeverity.Error,
Summary = "Error",
Detail = "Invalid token."
});
return;
}
if(!ValidatePrefix())
{
NotificationService.Notify(new NotificationMessage()
{
Severity = NotificationSeverity.Error,
Summary = "Error",
Detail = "Invalid prefix."
});
return;
}
if(!ValidateServerIds())
{
NotificationService.Notify(new NotificationMessage()
{
Severity = NotificationSeverity.Error,
Summary = "Error",
Detail = "Invalid server IDs."
});
return;
}
SaveSettings.Invoke();
SaveSettings.Invoke(TokenString, PrefixString, ServerIdsString);
}
private bool ValidateToken()
{
if(TokenString.Length < 59)
{
return false;
}
return true;
}
private bool ValidatePrefix()
{
if(PrefixString.Length < 1)
{
return false;
}
return true;
}
private bool ValidateServerIds()
{
if(ServerIdsString.Length < 1)
{
return false;
}
return true;
}
}

View File

@@ -1,5 +1,84 @@
@page "/settings"
@using DiscordBotWebUI.Components.CustomTags
@using DiscordBotWebUI.ServerCommunication
@inject NotificationService NotificationService
<PageTitle>Settings</PageTitle>
<SettingsComponent/>
@if (SettingsLoaded)
{
<SettingsComponent SaveSettings="SaveSettings"
TokenString="@Token"
PrefixString="@Prefix"
ServerIdsString="@ServerIds"/>
}
else
{
<p>Loading...</p>
}
@code {
private string Token { get; set; }
private string Prefix { get; set; }
private string ServerIds { get; set; }
private bool SettingsLoaded { get; set; }
[Inject]
private ApiHandler ApiHandler { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(!firstRender)
{
return;
}
var response = await ApiHandler.GetAsync("/api/settings/get");
if (!response.Success)
{
return;
}
Dictionary<string, object>? settings = await JsonManager.ConvertFromJson<Dictionary<string, object>>(response.Message);
if (settings == null)
{
return;
}
Token = settings["token"].ToString();
Prefix = settings["prefix"].ToString();
var serverIds = await JsonManager.ConvertFromJson<List<ulong>>(settings["serverIds"].ToString());
ServerIds = string.Join(";", serverIds);
SettingsLoaded = true;
StateHasChanged();
}
private async void SaveSettings(string token, string prefix, string serverIds)
{
var dict = new Dictionary<string, object>
{
{"token", token},
{"prefix", prefix},
{"ServerID", serverIds.Split(';').Select(ulong.Parse).ToList()}
};
string json = await JsonManager.ConvertToJsonString(dict);
var response = await ApiHandler.PostAsync("/api/settings/update", json);
if (!response.Success)
{
NotificationService.Notify(new NotificationMessage()
{
Severity = NotificationSeverity.Error,
Summary = "Error",
Detail = response.Message
});
}
}
}