|
| 1 | +@page "/current-user" |
| 2 | +@layout AuthenticatedLayout |
| 3 | +@inject CrudSnackbarService SnackbarService |
| 4 | +@inject UserClient UserClient |
| 5 | + |
| 6 | +<LoadingComponent LongTask="Loading" @ref="loadingRef"> |
| 7 | + <MudContainer MaxWidth="MaxWidth.Medium" Class="mt-6"> |
| 8 | + <MudPaper Class="pa-4"> |
| 9 | + <MudForm @ref="form" @bind-IsValid="@success"> |
| 10 | + <MudText Typo="Typo.h5" Class="mb-4">Personal Information</MudText> |
| 11 | + @if (user == null) |
| 12 | + { |
| 13 | + <MudText Typo="Typo.h6">User not found!</MudText> |
| 14 | + } |
| 15 | + else |
| 16 | + { |
| 17 | + <MudTextField |
| 18 | + @bind-Value="user.Name" |
| 19 | + Label="Full Name" |
| 20 | + Required="true" |
| 21 | + RequiredError="Name is required!" |
| 22 | + Class="mt-3"/> |
| 23 | + |
| 24 | + <MudTextField |
| 25 | + @bind-Value="user.NeptunCode" |
| 26 | + Label="Neptun Code" |
| 27 | + Required="true" |
| 28 | + RequiredError="Neptun code is required!" |
| 29 | + Validation="@(new Func<string, IEnumerable<string>>(ValidateNeptunCode))" |
| 30 | + Class="mt-3"/> |
| 31 | + |
| 32 | + <MudTextField |
| 33 | + @bind-Value="user.GithubId" |
| 34 | + Label="GitHub ID" |
| 35 | + Required="true" |
| 36 | + RequiredError="GitHub ID is required!" |
| 37 | + Class="mt-3"/> |
| 38 | + |
| 39 | + <div class="d-flex align-center justify-space-between mt-6"> |
| 40 | + <MudButton |
| 41 | + Variant="Variant.Filled" |
| 42 | + Color="Color.Primary" |
| 43 | + DisableElevation="true" |
| 44 | + OnClick="SubmitForm"> |
| 45 | + Save Changes |
| 46 | + </MudButton> |
| 47 | + </div> |
| 48 | + } |
| 49 | + </MudForm> |
| 50 | + </MudPaper> |
| 51 | + </MudContainer> |
| 52 | +</LoadingComponent> |
| 53 | + |
| 54 | +@code { |
| 55 | + private MudForm form; |
| 56 | + private bool success; |
| 57 | + private User? user; |
| 58 | + private LoadingComponent loadingRef; |
| 59 | + |
| 60 | + private IEnumerable<string> ValidateNeptunCode(string neptunCode) |
| 61 | + { |
| 62 | + if (string.IsNullOrEmpty(neptunCode)) |
| 63 | + yield return "Neptun code is required!"; |
| 64 | + if (neptunCode?.Length != 6) |
| 65 | + yield return "Neptun code must be exactly 6 characters!"; |
| 66 | + if (!neptunCode?.All(c => char.IsLetterOrDigit(c)) ?? false) |
| 67 | + yield return "Neptun code can only contain letters and numbers!"; |
| 68 | + } |
| 69 | + |
| 70 | + private async Task SubmitForm() |
| 71 | + { |
| 72 | + if (user == null) |
| 73 | + { |
| 74 | + SnackbarService.ShowError("User not found!"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + await form.Validate(); |
| 79 | + |
| 80 | + if (form.IsValid) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + // Here you would typically save the user data |
| 85 | + await UserClient.UpdateAsync(user.Id, user); |
| 86 | + |
| 87 | + SnackbarService.ShowAddSuccess(); |
| 88 | + } |
| 89 | + catch (Exception ex) |
| 90 | + { |
| 91 | + SnackbarService.ShowAddError(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + private async Task Loading() |
| 98 | + { |
| 99 | + user = await UserClient.GetCurrentUserAsync(); |
| 100 | + if (user == null) |
| 101 | + { |
| 102 | + SnackbarService.ShowError("User not found!"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments