2024-03-09 17:59:21 +01:00
|
|
|
|
using TaikoWebUI.Pages.Dialogs;
|
2023-11-12 18:56:57 +01:00
|
|
|
|
|
|
|
|
|
namespace TaikoWebUI.Pages;
|
|
|
|
|
|
|
|
|
|
public partial class AccessCode
|
|
|
|
|
{
|
|
|
|
|
[Parameter]
|
|
|
|
|
public int Baid { get; set; }
|
2024-03-09 00:42:56 +01:00
|
|
|
|
|
2023-11-12 18:56:57 +01:00
|
|
|
|
private string inputAccessCode = "";
|
|
|
|
|
private MudForm bindAccessCodeForm = default!;
|
|
|
|
|
|
|
|
|
|
private User? User { get; set; } = new();
|
2024-03-09 00:42:56 +01:00
|
|
|
|
|
2023-11-12 18:56:57 +01:00
|
|
|
|
private DashboardResponse? response;
|
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();
|
2023-11-12 18:56:57 +01:00
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
await base.OnInitializedAsync();
|
|
|
|
|
await InitializeUser();
|
2024-03-09 17:59:21 +01:00
|
|
|
|
|
|
|
|
|
userSetting = await Client.GetFromJsonAsync<UserSetting>($"api/UserSettings/{Baid}");
|
|
|
|
|
|
|
|
|
|
if (LoginService.IsLoggedIn && !LoginService.IsAdmin)
|
|
|
|
|
{
|
|
|
|
|
breadcrumbs.Add(new BreadcrumbItem("Dashboard", href: "/"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
breadcrumbs.Add(new BreadcrumbItem("Users", href: "/Users"));
|
|
|
|
|
};
|
|
|
|
|
breadcrumbs.Add(new BreadcrumbItem($"{userSetting?.MyDonName}", href: null, disabled: true));
|
|
|
|
|
breadcrumbs.Add(new BreadcrumbItem("Access Codes", href: $"/Users/{Baid}/AccessCode", disabled: false));
|
2023-11-12 18:56:57 +01:00
|
|
|
|
}
|
2024-03-09 00:42:56 +01:00
|
|
|
|
|
2023-11-12 18:56:57 +01:00
|
|
|
|
private async Task InitializeUser()
|
|
|
|
|
{
|
|
|
|
|
response = await Client.GetFromJsonAsync<DashboardResponse>("api/Dashboard");
|
|
|
|
|
LoginService.ResetLoggedInUser(response);
|
|
|
|
|
if (LoginService.IsAdmin || !LoginService.LoginRequired)
|
|
|
|
|
{
|
|
|
|
|
if (response is not null)
|
|
|
|
|
{
|
|
|
|
|
User = response.Users.FirstOrDefault(u => u.Baid == Baid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (LoginService.IsLoggedIn)
|
|
|
|
|
{
|
|
|
|
|
User = LoginService.GetLoggedInUser();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-09 00:42:56 +01:00
|
|
|
|
|
2023-11-12 18:56:57 +01:00
|
|
|
|
private async Task DeleteAccessCode(string accessCode)
|
|
|
|
|
{
|
|
|
|
|
var parameters = new DialogParameters<AccessCodeDeleteConfirmDialog>
|
|
|
|
|
{
|
|
|
|
|
{ x => x.User, User },
|
|
|
|
|
{ x => x.AccessCode, accessCode }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var dialog = DialogService.Show<AccessCodeDeleteConfirmDialog>("Delete Access Code", parameters);
|
|
|
|
|
var result = await dialog.Result;
|
|
|
|
|
|
|
|
|
|
if (result.Canceled) return;
|
2024-03-09 00:42:56 +01:00
|
|
|
|
|
2023-11-12 18:56:57 +01:00
|
|
|
|
await InitializeUser();
|
|
|
|
|
NavigationManager.NavigateTo(NavigationManager.Uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task OnBind()
|
|
|
|
|
{
|
|
|
|
|
if (response != null)
|
|
|
|
|
{
|
2023-12-19 16:27:17 +01:00
|
|
|
|
var result = await LoginService.BindAccessCode(inputAccessCode.ToUpper().Trim(), response.Users.First(u => u.Baid == Baid), Client);
|
2023-11-12 18:56:57 +01:00
|
|
|
|
switch (result)
|
|
|
|
|
{
|
|
|
|
|
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;
|
2023-12-17 00:48:42 +01:00
|
|
|
|
case 4:
|
|
|
|
|
await DialogService.ShowMessageBox(
|
|
|
|
|
"Error",
|
|
|
|
|
(MarkupString)
|
|
|
|
|
"Access code cannot be empty.<br />Please enter a valid access code.",
|
|
|
|
|
"Ok");
|
|
|
|
|
break;
|
2023-12-19 16:27:17 +01:00
|
|
|
|
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;
|
2023-11-12 18:56:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|