Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhu committed Oct 8, 2024
1 parent 92e782b commit b7ad986
Show file tree
Hide file tree
Showing 34 changed files with 76 additions and 84 deletions.
3 changes: 3 additions & 0 deletions src/Application/Common/Security/UserProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ public class UserProfile

public string? TimeZoneId { get; set; }
public string? LanguageCode { get; set; }
public TimeSpan LocalTimeOffset => string.IsNullOrEmpty(TimeZoneId)
? TimeZoneInfo.Local.BaseUtcOffset
: TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId).BaseUtcOffset;
}
4 changes: 3 additions & 1 deletion src/Application/Common/Security/UserProfileStateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public UserProfile UserProfile

private void NotifyStateChanged() => OnChange?.Invoke();

public void UpdateUserProfile(string userName,string? profilePictureDataUrl, string? fullName,string? phoneNumber)
public void UpdateUserProfile(string userName,string? profilePictureDataUrl, string? fullName,string? phoneNumber,string? timeZoneId,string? languageCode)
{
_userProfile.ProfilePictureDataUrl = profilePictureDataUrl;
_userProfile.DisplayName = fullName;
_userProfile.PhoneNumber = phoneNumber;
_userProfile.TimeZoneId = timeZoneId;
_userProfile.LanguageCode = languageCode;
RemoveApplicationUserCache(userName);
NotifyStateChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AuditTrailsWithPaginationQuery : AuditTrailAdvancedFilter, ICacheab
public override string ToString()
{
return
$"Listview:{ListView}-{LocalTimezoneOffset.TotalHours},AuditType:{AuditType},Search:{Keyword},Sort:{SortDirection},OrderBy:{OrderBy},{PageNumber},{PageSize}";
$"Listview:{ListView}-{CurrentUser?.UserId},AuditType:{AuditType},Search:{Keyword},Sort:{SortDirection},OrderBy:{OrderBy},{PageNumber},{PageSize}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public enum AuditTrailListView

public class AuditTrailAdvancedFilter : PaginationFilter
{
public TimeSpan LocalTimezoneOffset { get; set; }
public AuditType? AuditType { get; set; }
public AuditTrailListView ListView { get; set; } = AuditTrailListView.All;
public UserProfile? CurrentUser { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CleanArchitecture.Blazor.Application.Features.AuditTrails.Queries.PaginationQuery;
using CleanArchitecture.Blazor.Application.Features.Products.Specifications;

namespace CleanArchitecture.Blazor.Application.Features.AuditTrails.Specifications;
#nullable disable warnings
Expand All @@ -8,8 +7,8 @@ public class AuditTrailAdvancedSpecification : Specification<AuditTrail>
public AuditTrailAdvancedSpecification(AuditTrailsWithPaginationQuery filter)
{
DateTime today = DateTime.UtcNow;
var todayrange = today.GetDateRange("TODAY", filter.LocalTimezoneOffset);
var last30daysrange = today.GetDateRange("LAST_30_DAYS",filter.LocalTimezoneOffset);
var todayrange = today.GetDateRange("TODAY", filter.CurrentUser.LocalTimeOffset);
var last30daysrange = today.GetDateRange("LAST_30_DAYS",filter.CurrentUser.LocalTimeOffset);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ContactsWithPaginationQuery : ContactAdvancedFilter, ICacheableRequ
{
public override string ToString()
{
return $"Listview:{ListView}-{LocalTimezoneOffset.TotalHours}, Search:{Keyword}, {OrderBy}, {SortDirection}, {PageNumber}, {PageSize}";
return $"Listview:{ListView}-{CurrentUser?.UserId}, Search:{Keyword}, {OrderBy}, {SortDirection}, {PageNumber}, {PageSize}";
}
public string CacheKey => ContactCacheKey.GetPaginationCacheKey($"{this}");
public MemoryCacheEntryOptions? Options => ContactCacheKey.MemoryCacheEntryOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public enum ContactListView
/// </summary>
public class ContactAdvancedFilter: PaginationFilter
{
public TimeSpan LocalTimezoneOffset { get; set; }
public ContactListView ListView { get; set; } = ContactListView.All;
public UserProfile? CurrentUser { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public ContactAdvancedSpecification(ContactAdvancedFilter filter)
{

DateTime today = DateTime.UtcNow;
var todayrange = today.GetDateRange(ContactListView.TODAY.ToString(), filter.LocalTimezoneOffset);
var last30daysrange = today.GetDateRange(ContactListView.LAST_30_DAYS.ToString(),filter.LocalTimezoneOffset);
var todayrange = today.GetDateRange(ContactListView.TODAY.ToString(), filter.CurrentUser.LocalTimeOffset);
var last30daysrange = today.GetDateRange(ContactListView.LAST_30_DAYS.ToString(),filter.CurrentUser.LocalTimeOffset);

Query.Where(q => q.Name != null)
.Where(filter.Keyword,!string.IsNullOrEmpty(filter.Keyword))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DocumentsWithPaginationQuery : AdvancedDocumentsFilter, ICacheableR
public override string ToString()
{
return
$"CurrentUserId:{CurrentUser?.UserId},ListView:{ListView}-{LocalTimezoneOffset.TotalHours},Search:{Keyword},OrderBy:{OrderBy} {SortDirection},{PageNumber},{PageSize}";
$"CurrentUserId:{CurrentUser?.UserId},ListView:{ListView},Search:{Keyword},OrderBy:{OrderBy} {SortDirection},{PageNumber},{PageSize}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public enum DocumentListView

public class AdvancedDocumentsFilter : PaginationFilter
{
public TimeSpan LocalTimezoneOffset { get; set; }
public DocumentListView ListView { get; set; } = DocumentListView.All;
public UserProfile? CurrentUser { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ public class AdvancedDocumentsSpecification : Specification<Document>
{
public AdvancedDocumentsSpecification(AdvancedDocumentsFilter filter)
{

DateTime today = DateTime.UtcNow;
var todayrange = today.GetDateRange("TODAY", filter.LocalTimezoneOffset);
var last30daysrange = today.GetDateRange("LAST_30_DAYS", filter.LocalTimezoneOffset);

var todayrange = today.GetDateRange("TODAY", filter.CurrentUser.LocalTimeOffset);
var last30daysrange = today.GetDateRange("LAST_30_DAYS", filter.CurrentUser.LocalTimeOffset);
Query.Where(p =>
(p.CreatedBy == filter.CurrentUser.UserId && p.IsPublic == false) ||
(p.IsPublic == true && p.TenantId == filter.CurrentUser.TenantId),
Expand Down
5 changes: 5 additions & 0 deletions src/Application/Features/Identity/DTOs/ApplicationUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class ApplicationUserDto
[Description("Status")] public DateTimeOffset? LockoutEnd { get; set; }
[Description("Time Zone")]
public string? TimeZoneId { get; set; }
[Description("Local Time Offset")]
public TimeSpan LocalTimeOffset => string.IsNullOrEmpty(TimeZoneId)
? TimeZoneInfo.Local.BaseUtcOffset
: TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId).BaseUtcOffset;
[Description("Language")]
public string? LanguageCode { get; set; }
public UserProfile ToUserProfile()
Expand Down Expand Up @@ -79,6 +83,7 @@ private class Mapping : Profile
public Mapping()
{
CreateMap<ApplicationUser, ApplicationUserDto>(MemberList.None)
.ForMember(x => x.LocalTimeOffset, s => s.Ignore())
.ForMember(x => x.EmailConfirmed, s => s.MapFrom(y => y.EmailConfirmed))
.ForMember(x => x.AssignedRoles, s => s.MapFrom(y => y.UserRoles.Select(r => r.Role.Name)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class LogsWithPaginationQuery : LoggerAdvancedFilter, ICacheableRequest<P
public override string ToString()
{
return
$"Listview:{ListView}-{LocalTimezoneOffset.TotalHours},{Level},Search:{Keyword},OrderBy:{OrderBy} {SortDirection},{PageNumber},{PageSize}";
$"Listview:{ListView}-{LocalTimeOffset.TotalHours},{Level},Search:{Keyword},OrderBy:{OrderBy} {SortDirection},{PageNumber},{PageSize}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public enum LogListView

public class LoggerAdvancedFilter : PaginationFilter
{
public TimeSpan LocalTimezoneOffset { get; set; }
public TimeSpan LocalTimeOffset { get; set; }
public LogLevel? Level { get; set; }
public LogListView ListView { get; set; } = LogListView.LAST_30_DAYS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class LoggerAdvancedSpecification : Specification<Logger>
public LoggerAdvancedSpecification(LogsWithPaginationQuery filter)
{
DateTime today = DateTime.UtcNow;
var todayrange = today.GetDateRange("TODAY", filter.LocalTimezoneOffset);
var last30daysrange = today.GetDateRange("LAST_30_DAYS", filter.LocalTimezoneOffset);
var todayrange = today.GetDateRange("TODAY", filter.LocalTimeOffset);
var last30daysrange = today.GetDateRange("LAST_30_DAYS", filter.LocalTimeOffset);
// Build query conditions
Query.Where(p => p.TimeStamp >= todayrange.Start && p.TimeStamp < todayrange.End.AddDays(1),
filter.ListView == LogListView.TODAY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ProductsWithPaginationQuery : ProductAdvancedFilter, ICacheableRequ
public override string ToString()
{
return
$"CurrentUser:{CurrentUser?.UserId},ListView:{ListView}-{LocalTimezoneOffset.TotalHours},Search:{Keyword},Name:{Name},Brand:{Brand},Unit:{Unit},MinPrice:{MinPrice},MaxPrice:{MaxPrice},SortDirection:{SortDirection},OrderBy:{OrderBy},{PageNumber},{PageSize}";
$"CurrentUser:{CurrentUser?.UserId},ListView:{ListView},Search:{Keyword},Name:{Name},Brand:{Brand},Unit:{Unit},MinPrice:{MinPrice},MaxPrice:{MaxPrice},SortDirection:{SortDirection},OrderBy:{OrderBy},{PageNumber},{PageSize}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ public class ProductAdvancedFilter : PaginationFilter

public UserProfile?
CurrentUser { get; set; } // <-- This CurrentUser property gets its value from the information of

public TimeSpan LocalTimezoneOffset { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class ProductAdvancedSpecification : Specification<Product>
public ProductAdvancedSpecification(ProductAdvancedFilter filter)
{
DateTime today = DateTime.UtcNow;
var todayrange = today.GetDateRange("TODAY",filter.LocalTimezoneOffset);
var last30daysrange = today.GetDateRange("LAST_30_DAYS", filter.LocalTimezoneOffset);
var todayrange = today.GetDateRange("TODAY",filter.CurrentUser.LocalTimeOffset);
var last30daysrange = today.GetDateRange("LAST_30_DAYS", filter.CurrentUser.LocalTimeOffset);
Query.Where(x => x.Name != null)
.Where(x => x.Name.Contains(filter.Keyword) || x.Description.Contains(filter.Keyword) ||
x.Brand.Contains(filter.Keyword), !string.IsNullOrEmpty(filter.Keyword))
Expand Down
20 changes: 10 additions & 10 deletions src/Infrastructure/Constants/Localization/LocalizationConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,47 @@ public static class LocalizationConstants
new()
{
Code = "en-US",
DisplayName = "English"
DisplayName = "English (United States)"
},
new()
{
Code = "de-DE",
DisplayName = "Deutsch"
DisplayName = "Deutsch (Deutschland)"
},
new()
{
Code = "ru-RU",
DisplayName = "Russian"
DisplayName = "русский (Россия)"
},
new()
{
Code = "fr-FR",
DisplayName = "French"
DisplayName = "français (France)"
},
new()
{
Code = "ja-JP",
DisplayName = "Japanese"
DisplayName = "日本語 (日本)"
},
new()
{
Code = "km-KH",
DisplayName = "Khmer"
DisplayName = "ខ្មែរ (កម្ពុជា)"
},
new()
{
Code = "ca-ES",
DisplayName = "Catalan"
DisplayName = "català (Espanya)"
},
new()
{
Code = "es-ES",
DisplayName = "Spanish"
DisplayName = "español (España)"
},
new()
{
Code = "zh-CN",
DisplayName = "中文"
DisplayName = "中文(简体,中国)"
},
new()
{
Expand All @@ -62,7 +62,7 @@ public static class LocalizationConstants
new()
{
Code = "ko-kr",
DisplayName = "Korean"
DisplayName = "한국어(대한민국)"
}
};
}
Expand Down
21 changes: 11 additions & 10 deletions src/Server.UI/Components/Autocompletes/LanguageAutocomplete.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
using System.Globalization;
using CleanArchitecture.Blazor.Infrastructure.Constants.Localization;

namespace CleanArchitecture.Blazor.Server.UI.Components.Autocompletes;

public class LanguageAutocomplete<T> : MudAutocomplete<string>
{
private List<CultureInfo> Languages { get; set; }= CultureInfo.GetCultures(CultureTypes.SpecificCultures).ToList();

private List<LanguageCode> Languages { get; set; }= LocalizationConstants.SupportedLanguages.ToList();
public LanguageAutocomplete()
{
SearchFunc = SearchFunc_;
Clearable = true;
Dense = true;
ResetValueOnEmptyText = true;
ToStringFunc = x =>
{
var language = Languages.FirstOrDefault(lang => lang.Name.Equals(x));
return language != null ? $"{language.DisplayName} ({language.Name})" : x;
var language = Languages.FirstOrDefault(lang =>lang.Code.Equals(x, StringComparison.OrdinalIgnoreCase));
return language != null ? $"{language.DisplayName}" : x;
};
}

private Task<IEnumerable<string>> SearchFunc_(string value, CancellationToken cancellation = default)
{
// 如果输入为空,返回完整的语言列表;否则进行模糊搜索
return string.IsNullOrEmpty(value)
? Task.FromResult(Languages.Select(lang => lang.Name).AsEnumerable())
? Task.FromResult(Languages.Select(lang => lang.Code).AsEnumerable())
: Task.FromResult(Languages
.Where(lang => Contains(lang, value))
.Select(lang => lang.Name));
.Select(lang => lang.Code));
}

private static bool Contains(CultureInfo language, string value)
private static bool Contains(LanguageCode language, string value)
{
return language.DisplayName.Contains(value, StringComparison.InvariantCultureIgnoreCase) ||
language.Name.Contains(value, StringComparison.InvariantCultureIgnoreCase);
return language.Code.Contains(value, StringComparison.InvariantCultureIgnoreCase) ||
language.DisplayName.Contains(value, StringComparison.InvariantCultureIgnoreCase);
}

}
1 change: 1 addition & 0 deletions src/Server.UI/Components/Shared/Layout/AppLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
}
private async Task UserProfileStateService_OnChange()
{
UserProfile = UserProfileStateService.UserProfile;
await InvokeAsync(StateHasChanged);
}
private void ResetBoundary()
Expand Down
7 changes: 2 additions & 5 deletions src/Server.UI/Components/Shared/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
<MudDialogProvider />
<MudSnackbarProvider />
<MudThemeProvider @ref="@_mudThemeProvider" Theme="@LayoutService.CurrentTheme" IsDarkMode="@LayoutService.IsDarkMode" IsDarkModeChanged="@LayoutService.SetDarkMode" />
<CascadingValue Name="LocalTimezoneOffset" Value="@_localTimezoneOffset">

@Body
</CascadingValue>

</MudRTLProvider>

@code
{
private TimeSpan _localTimezoneOffset;
private MudThemeProvider _mudThemeProvider = null!;
private bool _defaultDarkMode;

public void Dispose()
{
LayoutService.MajorUpdateOccured -= LayoutServiceOnMajorUpdateOccured;
Expand All @@ -33,7 +31,6 @@
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
_localTimezoneOffset = await new LocalTimezoneOffset(JS).GetLocalOffset();
await ApplyUserPreferences();
StateHasChanged();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Server.UI/Components/UtcToLocalTime/UtcToLocal.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ else
<MudText Class="@Class">@UTCDateTime.ToString(Format)</MudText>
}
@code {
[CascadingParameter] private UserProfile? UserProfile { get; set; }
// Parameter to accept the UTC time that needs to be converted
[Parameter]
public DateTime UTCDateTime { get; set; }
Expand All @@ -24,13 +25,12 @@ else
private DateTime? localTime;

// This method is called when the component is initialized
protected override async Task OnInitializedAsync()
protected override Task OnInitializedAsync()
{
// Call JavaScript function to get the browser's time zone offset in minutes
var timezoneOffset = await new LocalTimezoneOffset(JSRuntime).GetLocalOffset();

var timeOffset = UserProfile?.LocalTimeOffset ?? TimeZoneInfo.Local.BaseUtcOffset;
// Convert the UTC time to local time by adding the time zone offset
localTime = UTCDateTime.Add(timezoneOffset);
localTime = UTCDateTime.Add(timeOffset);
return Task.CompletedTask;

}
}
2 changes: 1 addition & 1 deletion src/Server.UI/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static IServiceCollection AddServerUI(this IServiceCollection services, I
c.BaseAddress = new Uri("http://10.33.1.150:8000/ocr/predict-by-file");
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}).AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(30)));
services.AddScoped<LocalTimezoneOffset>();
services.AddScoped<LocalTimeOffset>();
services.AddHttpContextAccessor();
services.AddScoped<HubClient>();
services
Expand Down
4 changes: 1 addition & 3 deletions src/Server.UI/Pages/Contacts/Contacts.razor
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@
private Task<AuthenticationState> AuthState { get; set; } = default!;
[CascadingParameter]
private UserProfile? UserProfile { get; set; }
[CascadingParameter(Name = "LocalTimezoneOffset")]
private TimeSpan _localTimezoneOffset { get; set; }


private ContactsWithPaginationQuery Query { get; set; } = new();
[Inject]
Expand Down Expand Up @@ -250,7 +249,6 @@
Query.SortDirection = state.SortDefinitions.FirstOrDefault()?.Descending ?? true ? SortDirection.Descending.ToString() : SortDirection.Ascending.ToString();
Query.PageNumber = state.Page + 1;
Query.PageSize = state.PageSize;
Query.LocalTimezoneOffset = _localTimezoneOffset;
var result = await Mediator.Send(Query).ConfigureAwait(false);
return new GridData<ContactDto>() { TotalItems = result.TotalItems, Items = result.Items };
}
Expand Down
Loading

0 comments on commit b7ad986

Please sign in to comment.