Skip to content

Commit

Permalink
added time zone to profile manage page
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfoneil committed Apr 21, 2024
1 parent 657c8d7 commit f1965eb
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
@using System.ComponentModel.DataAnnotations
@using Microsoft.AspNetCore.Identity
@using ApiAuthDemo.Data
@using Microsoft.EntityFrameworkCore;

@inject UserManager<ApplicationUser> UserManager
@inject SignInManager<ApplicationUser> SignInManager
@inject IdentityUserAccessor UserAccessor
@inject IdentityRedirectManager RedirectManager
@inject ApplicationDbContext DbContext

<PageTitle>Profile</PageTitle>

Expand All @@ -28,6 +30,16 @@
<label for="phone-number" class="form-label">Phone number</label>
<ValidationMessage For="() => Input.PhoneNumber" class="text-danger" />
</div>
<div class="form-floating mb-3">
<InputSelect @bind-Value="Input.TimeZoneId" class="form-control">
<option value="">(not set)</option>
@foreach (var item in allTimeZones)
{
<option value="@item.Id">@item.StandardName</option>
}
</InputSelect>
<label for="time-zone" class="form-label">Time zone</label>
</div>
<button type="submit" class="w-100 btn btn-lg btn-primary">Save</button>
</EditForm>
</div>
Expand All @@ -37,6 +49,8 @@
private ApplicationUser user = default!;
private string? username;
private string? phoneNumber;
private string? timeZoneId;
private IEnumerable<TimeZoneInfo> allTimeZones = [];

[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;
Expand All @@ -46,11 +60,15 @@

protected override async Task OnInitializedAsync()
{
allTimeZones = TimeZoneInfo.GetSystemTimeZones().OrderBy(val => val.StandardName);

user = await UserAccessor.GetRequiredUserAsync(HttpContext);
username = await UserManager.GetUserNameAsync(user);
phoneNumber = await UserManager.GetPhoneNumberAsync(user);
timeZoneId = user.TimeZoneId;

Input.PhoneNumber ??= phoneNumber;
Input.TimeZoneId ??= timeZoneId;
}

private async Task OnValidSubmitAsync()
Expand All @@ -64,6 +82,13 @@
}
}

if (Input.TimeZoneId != timeZoneId)
{
await DbContext.Users
.Where(row => row.UserName == user.UserName)
.ExecuteUpdateAsync(setter => setter.SetProperty(row => row.TimeZoneId, Input.TimeZoneId));
}

await SignInManager.RefreshSignInAsync(user);
RedirectManager.RedirectToCurrentPageWithStatus("Your profile has been updated", HttpContext);
}
Expand All @@ -73,5 +98,8 @@
[Phone]
[Display(Name = "Phone number")]
public string? PhoneNumber { get; set; }

[Display(Name = "Time Zone")]
public string? TimeZoneId { get; set; }
}
}

0 comments on commit f1965eb

Please sign in to comment.