LANCommander/LANCommander.Server/UI/Pages/Settings/UserSaves.razor

66 lines
2.2 KiB
Text

@page "/Settings/UserSaves"
@using LANCommander.SDK.Enums
@inject StorageLocationService StorageLocationService
@inject IMessageService MessageService
@inject ILogger<UserSaves> Logger
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
<PageHeader Title="User Saves" />
<PageContent>
<Form Model="Settings" Layout="@FormLayout.Vertical">
<FormItem Label="Storage Path">
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@SaveStorageLocation.Path" OkText="Select Path" Title="Choose Path" OnSelected="OnPathSelected" />
</FormItem>
<FormItem Label="Max Size (MB)">
<AntDesign.InputNumber @bind-Value="context.UserSaves.MaxSize" Min="1" DefaultValue="25" />
</FormItem>
<FormItem Label="Maximum Number of Saves">
<AntDesign.InputNumber @bind-Value="context.UserSaves.MaxSaves" Min="0" DefaultValue="0" />
</FormItem>
<FormItem>
<Button OnClick="Save" Type="@ButtonType.Primary">Save</Button>
</FormItem>
</Form>
</PageContent>
@code {
Settings Settings = SettingService.GetSettings();
string RootPath = Path.GetPathRoot(Directory.GetCurrentDirectory());
StorageLocation SaveStorageLocation = new();
protected override async Task OnInitializedAsync()
{
SaveStorageLocation = await StorageLocationService.FirstOrDefaultAsync(l => l.Default && l.Type == StorageLocationType.Save);
}
async Task Save()
{
try
{
SettingService.SaveSettings(Settings);
await StorageLocationService.UpdateAsync(SaveStorageLocation);
MessageService.Success("Settings saved!");
}
catch (Exception ex)
{
MessageService.Error("An unknown error occurred.");
Logger.LogError(ex, "An unknown error occurred.");
}
}
void OnPathSelected(string path)
{
var appPath = Directory.GetCurrentDirectory();
if (path != null && path.StartsWith(appPath))
path = path.Substring(appPath.Length).TrimStart(Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
SaveStorageLocation.Path = path;
}
}