1
0
mirror of synced 2024-12-12 14:51:08 +01:00
TaikoLocalServer/TaikoWebUI/Pages/AccessCode.razor.cs

125 lines
4.1 KiB
C#
Raw Normal View History

2024-03-09 17:59:21 +01:00
using TaikoWebUI.Pages.Dialogs;
namespace TaikoWebUI.Pages;
public partial class AccessCode
{
[Parameter]
public int Baid { get; set; }
2024-03-09 00:42:56 +01:00
private string inputAccessCode = "";
private MudForm bindAccessCodeForm = default!;
2024-05-17 00:32:46 +02:00
private User? User { get; set; }
2024-03-09 17:59:21 +01:00
private UserSetting? userSetting;
2024-03-09 00:42:56 +01:00
2024-03-09 17:59:21 +01:00
private readonly List<BreadcrumbItem> breadcrumbs = new();
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
if (AuthService.LoginRequired && !AuthService.IsLoggedIn)
{
await AuthService.LoginWithAuthToken();
}
await InitializeUser();
2024-03-09 17:59:21 +01:00
userSetting = await Client.GetFromJsonAsync<UserSetting>($"api/UserSettings/{Baid}");
2024-05-17 00:32:46 +02:00
if (AuthService.IsLoggedIn && !AuthService.IsAdmin)
2024-03-09 17:59:21 +01:00
{
breadcrumbs.Add(new BreadcrumbItem(Localizer["Dashboard"], href: "/"));
2024-03-09 17:59:21 +01:00
}
else
{
breadcrumbs.Add(new BreadcrumbItem(Localizer["Users"], href: "/Users"));
2024-05-17 00:32:46 +02:00
}
2024-03-09 17:59:21 +01:00
breadcrumbs.Add(new BreadcrumbItem($"{userSetting?.MyDonName}", href: null, disabled: true));
breadcrumbs.Add(new BreadcrumbItem(Localizer["Access Codes"], href: $"/Users/{Baid}/AccessCode", disabled: false));
}
2024-03-09 00:42:56 +01:00
private async Task InitializeUser()
{
2024-05-17 00:32:46 +02:00
if (!AuthService.LoginRequired)
{
2024-05-17 00:32:46 +02:00
var users = await Client.GetFromJsonAsync<List<User>>("api/Users");
if (users != null) User = users.FirstOrDefault(u => u.Baid == Baid);
}
2024-05-17 00:32:46 +02:00
else if (AuthService.IsLoggedIn)
{
2024-05-17 00:32:46 +02:00
User = await Client.GetFromJsonAsync<User>($"api/Users/{Baid}");
}
}
2024-03-09 00:42:56 +01:00
private async Task DeleteAccessCode(string accessCode)
{
var parameters = new DialogParameters<AccessCodeDeleteConfirmDialog>
{
{ x => x.User, User },
{ x => x.AccessCode, accessCode }
};
2024-05-17 00:32:46 +02:00
var dialog = await DialogService.ShowAsync<AccessCodeDeleteConfirmDialog>("Delete Access Code", parameters);
var result = await dialog.Result;
if (result.Canceled) return;
2024-03-09 00:42:56 +01:00
await InitializeUser();
NavigationManager.NavigateTo(NavigationManager.Uri);
}
private async Task OnBind()
{
2024-05-17 00:32:46 +02:00
if (User == null) return;
var result = await AuthService.BindAccessCode(inputAccessCode.ToUpper().Trim(), User);
switch (result)
{
2024-05-17 00:32:46 +02:00
case 0:
await DialogService.ShowMessageBox(
"Error",
(MarkupString)
"Not logged in.<br />Please log in first and try again.",
"Ok");
break;
case 1:
await DialogService.ShowMessageBox(
"Success",
"New access code bound successfully.",
"Ok");
await InitializeUser();
NavigationManager.NavigateTo(NavigationManager.Uri);
break;
case 2:
await DialogService.ShowMessageBox(
"Error",
(MarkupString)
"Bound access code upper limit reached.<br />Please delete one access code first.",
"Ok");
break;
case 3:
await DialogService.ShowMessageBox(
"Error",
(MarkupString)
"Access code already bound.<br />Please delete it from the bound user first.",
"Ok");
break;
case 4:
await DialogService.ShowMessageBox(
"Error",
(MarkupString)
"Access code cannot be empty.<br />Please enter a valid access code.",
"Ok");
break;
case 5:
await DialogService.ShowMessageBox(
"Error",
(MarkupString)
"You can't do that!<br />You need to be an admin to edit someone else's access codes.",
"Ok");
break;
}
}
}