Merge pull request #3885 from zyhfish/task/fix-infinite-loop-of-module-message

avoid the infinite loop issue of calling AddModuleMessage method.
This commit is contained in:
Shaun Walker 2024-02-24 09:56:03 -05:00 committed by GitHub
commit 98de661868
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 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; } = true;
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

@ -11,19 +11,13 @@
@if (ModuleType != null)
{
@((MarkupString)$"<!-- Render Mode: {ModuleState.RenderMode} -->")
@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
@ -48,6 +42,8 @@
private string _messagePosition;
private bool _progressIndicator = false;
private string _error;
private ModuleMessage moduleMessageTop;
private ModuleMessage moduleMessageBottom;
[Parameter]
public SiteState SiteState { get; set; }
@ -112,19 +108,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()
@ -132,6 +129,22 @@
_messageContent = "";
}
private void Refresh()
{
var messageTopVisible = !string.IsNullOrEmpty(_messageContent) && _messagePosition == "top";
var messageBottomVisible = !string.IsNullOrEmpty(_messageContent) && _messagePosition == "bottom";
if (moduleMessageTop != null)
{
moduleMessageTop.RefreshMessage(_messageContent, _messageType, messageTopVisible);
}
if (moduleMessageBottom != null)
{
moduleMessageBottom.RefreshMessage(_messageContent, _messageType, messageBottomVisible);
}
}
protected override async Task OnErrorAsync(Exception exception)
{
// retrieve friendly localized error