avoid the infinite loop issue of calling AddModuleMessage method.

This commit is contained in:
Ben 2024-02-24 20:45:22 +08:00
parent f5e13c25a7
commit 724e78d4d5
2 changed files with 40 additions and 17 deletions

View File

@ -2,10 +2,10 @@
@inherits ModuleControlBase
@inject NavigationManager NavigationManager
@if (!string.IsNullOrEmpty(_message))
@if (Visible)
{
<div class="@_classname alert-dismissible fade show mb-3" role="alert">
@((MarkupString)_message)
@((MarkupString)Message)
@if (PageState != null)
{
@if (Type == MessageType.Error && UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
@ -21,7 +21,6 @@
}
@code {
private string _message = string.Empty;
private string _classname = string.Empty;
private string _formname = "ModuleMessageForm";
@ -31,6 +30,20 @@
[Parameter]
public MessageType Type { get; set; }
[Parameter]
public bool Visible{ get; set; }
public void RefreshMessage(string message, MessageType type, bool visible)
{
Message = message;
Type = type;
Visible = visible;
UpdateClassName();
StateHasChanged();
}
protected override void OnInitialized()
{
if (ModuleState != null)
@ -41,8 +54,12 @@
protected override void OnParametersSet()
{
_message = Message;
if (!string.IsNullOrEmpty(_message))
UpdateClassName();
}
private void UpdateClassName()
{
if (!string.IsNullOrEmpty(Message))
{
_classname = GetMessageType(Type);
}
@ -72,6 +89,6 @@
private void DismissModal()
{
_message = "";
Message = "";
}
}

View File

@ -10,19 +10,13 @@
{
@if (ModuleType != null)
{
@if (!string.IsNullOrEmpty(_messageContent) && _messagePosition == "top")
{
<ModuleMessage Message="@_messageContent" Type="@_messageType" />
}
<ModuleMessage @ref="moduleMessageTop" Message="@_messageContent" Type="@_messageType" Visible="@(!string.IsNullOrEmpty(_messageContent) && _messagePosition == "top")" />
@DynamicComponent
@if (_progressIndicator)
{
<div class="app-progress-indicator"></div>
}
@if (!string.IsNullOrEmpty(_messageContent) && _messagePosition == "bottom")
{
<ModuleMessage Message="@_messageContent" Type="@_messageType" />
}
<ModuleMessage @ref="moduleMessageBottom" Message="@_messageContent" Type="@_messageType" Visible="@(!string.IsNullOrEmpty(_messageContent) && _messagePosition == "bottom")" />
}
}
else
@ -47,6 +41,8 @@
private string _messagePosition;
private bool _progressIndicator = false;
private string _error;
private ModuleMessage moduleMessageTop;
private ModuleMessage moduleMessageBottom;
[Parameter]
public SiteState SiteState { get; set; }
@ -111,19 +107,20 @@
_messageType = type;
_messagePosition = position;
_progressIndicator = false;
StateHasChanged();
Refresh();
}
public void ShowProgressIndicator()
{
_progressIndicator = true;
StateHasChanged();
Refresh();
}
public void HideProgressIndicator()
{
_progressIndicator = false;
StateHasChanged();
Refresh();
}
private void DismissMessage()
@ -131,6 +128,15 @@
_messageContent = "";
}
private void Refresh()
{
var messageTopVisible = !string.IsNullOrEmpty(_messageContent) && _messagePosition == "top";
var messageBottomVisible = !string.IsNullOrEmpty(_messageContent) && _messagePosition == "bottom";
moduleMessageTop.RefreshMessage(_messageContent, _messageType, messageTopVisible);
moduleMessageBottom.RefreshMessage(_messageContent, _messageType, messageBottomVisible);
}
protected override async Task OnErrorAsync(Exception exception)
{
// retrieve friendly localized error