diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/MainLayout.razor b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/MainLayout.razor index 45496a7984..20b96bef45 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/MainLayout.razor +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/MainLayout.razor @@ -22,4 +22,5 @@ @Body + diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiNotificationService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiNotificationService.cs index a9816dc325..753dad4dcd 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiNotificationService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/IUiNotificationService.cs @@ -1,12 +1,13 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; namespace Volo.Abp.AspNetCore.Components.WebAssembly { public interface IUiNotificationService { - Task Info(string message); - Task Success(string message); - Task Warn(string message); - Task Error(string message); + Task Info(string message, string title = null, Action options = null); + Task Success(string message, string title = null, Action options = null); + Task Warn(string message, string title = null, Action options = null); + Task Error(string message, string title = null, Action options = null); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiNotificationService.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiNotificationService.cs index 85ee10287c..950cca3047 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiNotificationService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/NullUiNotificationService.cs @@ -1,25 +1,26 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Components.WebAssembly { public class NullUiNotificationService : IUiNotificationService, ITransientDependency { - public Task Info(string message) + public Task Info(string message, string title = null, Action options = null) { return Task.CompletedTask; } - public Task Success(string message) + public Task Success(string message, string title = null, Action options = null) { return Task.CompletedTask; } - public Task Warn(string message) + public Task Warn(string message, string title = null, Action options = null) { return Task.CompletedTask; } - public Task Error(string message) + public Task Error(string message, string title = null, Action options = null) { return Task.CompletedTask; } diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationEventArgs.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationEventArgs.cs new file mode 100644 index 0000000000..402bfb329b --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationEventArgs.cs @@ -0,0 +1,23 @@ +using System; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly +{ + public class UiNotificationEventArgs : EventArgs + { + public UiNotificationEventArgs(UiNotificationType notificationType, string message, string title, UiNotificationOptions options) + { + NotificationType = notificationType; + Message = message; + Title = title; + Options = options; + } + + public UiNotificationType NotificationType { get; set; } + + public string Message { get; } + + public string Title { get; } + + public UiNotificationOptions Options { get; } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationOptions.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationOptions.cs new file mode 100644 index 0000000000..2d51716cd9 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationOptions.cs @@ -0,0 +1,18 @@ +namespace Volo.Abp.AspNetCore.Components.WebAssembly +{ + /// + /// Options to override notification appearance. + /// + public class UiNotificationOptions + { + /// + /// Custom text for the Ok button. + /// + public string OkButtonText { get; set; } + + /// + /// Custom icon for the Ok button. + /// + public object OkButtonIcon { get; set; } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationType.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationType.cs new file mode 100644 index 0000000000..78f3a86bb5 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/UiNotificationType.cs @@ -0,0 +1,10 @@ +namespace Volo.Abp.AspNetCore.Components.WebAssembly +{ + public enum UiNotificationType + { + Info, + Success, + Warning, + Error, + } +} diff --git a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiNotificationService.cs b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiNotificationService.cs index 549a1f4d07..d1e6ed6ce2 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiNotificationService.cs +++ b/framework/src/Volo.Abp.BlazoriseUI/BlazoriseUiNotificationService.cs @@ -1,4 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; +using Localization.Resources.AbpUi; +using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.AspNetCore.Components.WebAssembly; @@ -7,34 +10,79 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.BlazoriseUI { [Dependency(ReplaceServices = true)] - public class BlazoriseUiNotificationService : IUiNotificationService, ITransientDependency + public class BlazoriseUiNotificationService : IUiNotificationService, IScopedDependency { + /// + /// An event raised after the notification is received. + /// + public event EventHandler NotificationReceived; + + private readonly IStringLocalizer localizer; + public ILogger Logger { get; set; } - public BlazoriseUiNotificationService() + public BlazoriseUiNotificationService( + IStringLocalizer localizer) { + this.localizer = localizer; + Logger = NullLogger.Instance; } - public Task Info(string message) + public Task Info(string message, string title = null, Action options = null) { + var uiNotificationOptions = CreateDefaultOptions(); + options?.Invoke(uiNotificationOptions); + Logger.LogInformation(message); + + NotificationReceived?.Invoke(this, new UiNotificationEventArgs(UiNotificationType.Info, message, title, uiNotificationOptions)); + return Task.CompletedTask; } - public Task Success(string message) + public Task Success(string message, string title = null, Action options = null) { + var uiNotificationOptions = CreateDefaultOptions(); + options?.Invoke(uiNotificationOptions); + + Logger.LogInformation(message); + + NotificationReceived?.Invoke(this, new UiNotificationEventArgs(UiNotificationType.Success, message, title, uiNotificationOptions)); + return Task.CompletedTask; } - public Task Warn(string message) + public Task Warn(string message, string title = null, Action options = null) { + var uiNotificationOptions = CreateDefaultOptions(); + options?.Invoke(uiNotificationOptions); + + Logger.LogWarning(message); + + NotificationReceived?.Invoke(this, new UiNotificationEventArgs(UiNotificationType.Warning, message, title, uiNotificationOptions)); + return Task.CompletedTask; } - public Task Error(string message) + public Task Error(string message, string title = null, Action options = null) { + var uiNotificationOptions = CreateDefaultOptions(); + options?.Invoke(uiNotificationOptions); + + Logger.LogError(message); + + NotificationReceived?.Invoke(this, new UiNotificationEventArgs(UiNotificationType.Error, message, title, uiNotificationOptions)); + return Task.CompletedTask; } + + protected virtual UiNotificationOptions CreateDefaultOptions() + { + return new UiNotificationOptions + { + OkButtonText = localizer["Ok"], + }; + } } } diff --git a/framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor b/framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor new file mode 100644 index 0000000000..349eee2c40 --- /dev/null +++ b/framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor.cs b/framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor.cs new file mode 100644 index 0000000000..691b6a7a1a --- /dev/null +++ b/framework/src/Volo.Abp.BlazoriseUI/Components/UiNotificationAlert.razor.cs @@ -0,0 +1,71 @@ +using System; +using System.Threading.Tasks; +using Blazorise.Snackbar; +using Microsoft.AspNetCore.Components; +using Volo.Abp.AspNetCore.Components.WebAssembly; + +namespace Volo.Abp.BlazoriseUI.Components +{ + public partial class UiNotificationAlert : ComponentBase, IDisposable + { + protected SnackbarStack SnackbarStack { get; set; } + + [Parameter] public UiNotificationType NotificationType { get; set; } + + [Parameter] public string Message { get; set; } + + [Parameter] public string Title { get; set; } + + [Parameter] public UiNotificationOptions Options { get; set; } + + [Parameter] public EventCallback Okayed { get; set; } + + [Parameter] public EventCallback Closed { get; set; } + + [Inject] protected BlazoriseUiNotificationService UiNotificationService { get; set; } + + protected virtual SnackbarColor GetSnackbarColor(UiNotificationType notificationType) + { + return notificationType switch + { + UiNotificationType.Info => SnackbarColor.Info, + UiNotificationType.Success => SnackbarColor.Success, + UiNotificationType.Warning => SnackbarColor.Warning, + UiNotificationType.Error => SnackbarColor.Danger, + _ => SnackbarColor.None, + }; + } + + protected override void OnInitialized() + { + base.OnInitialized(); + + UiNotificationService.NotificationReceived += OnNotificationReceived; + } + + protected virtual void OnNotificationReceived(object sender, UiNotificationEventArgs e) + { + NotificationType = e.NotificationType; + Message = e.Message; + Title = e.Title; + Options = e.Options; + + SnackbarStack.Push(Message, GetSnackbarColor(e.NotificationType)); + } + + public virtual void Dispose() + { + if (UiNotificationService != null) + { + UiNotificationService.NotificationReceived -= OnNotificationReceived; + } + } + + protected virtual Task OnSnackbarClosed(SnackbarClosedEventArgs eventArgs) + { + return eventArgs.CloseReason == SnackbarCloseReason.UserClosed + ? Okayed.InvokeAsync() + : Closed.InvokeAsync(); + } + } +} diff --git a/framework/src/Volo.Abp.BlazoriseUI/Volo.Abp.BlazoriseUI.csproj b/framework/src/Volo.Abp.BlazoriseUI/Volo.Abp.BlazoriseUI.csproj index 1134821b78..110387927d 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/Volo.Abp.BlazoriseUI.csproj +++ b/framework/src/Volo.Abp.BlazoriseUI/Volo.Abp.BlazoriseUI.csproj @@ -14,6 +14,7 @@ + diff --git a/framework/src/Volo.Abp.BlazoriseUI/_Imports.razor b/framework/src/Volo.Abp.BlazoriseUI/_Imports.razor index 37be71828c..0aae8ed947 100644 --- a/framework/src/Volo.Abp.BlazoriseUI/_Imports.razor +++ b/framework/src/Volo.Abp.BlazoriseUI/_Imports.razor @@ -1,2 +1,3 @@ @using Microsoft.AspNetCore.Components.Web -@using Blazorise \ No newline at end of file +@using Blazorise +@using Blazorise.Snackbar \ No newline at end of file diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html index 9f11ef4bd8..b8037cca39 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/wwwroot/index.html @@ -12,6 +12,7 @@ +