Merge pull request #4027 from zyhfish/task/fix-issue-4025

Fix #4025: add user setting to keep the view mode.
This commit is contained in:
Shaun Walker 2024-03-21 15:35:10 -04:00 committed by GitHub
commit 8adbdcc675
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 76 additions and 24 deletions

View File

@ -2,6 +2,7 @@
@inherits ThemeControlBase
@inject NavigationManager NavigationManager
@inject IPageService PageService
@inject ISettingService SettingService
@if (ShowLanguageSwitcher)
{
@ -99,7 +100,7 @@
return false;
}
private async Task ToggleEditMode(bool EditMode)
private async Task ToggleEditMode(bool editMode)
{
Page page = null;
if (PageState.Page.IsPersonalizable && PageState.User != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Registered))
@ -109,13 +110,23 @@
if (_showEditMode)
{
if (EditMode)
PageState.EditMode = !editMode;
if(PageState.User != null) //keep edit mode for authenticated users.
{
PageState.EditMode = false;
}
else
{
PageState.EditMode = true;
if(PageState.EditMode)
{
var userSettings = new Dictionary<string, string>
{
{ UserSetting.LastViewPageId, PageState.Page.PageId.ToString() }
};
await SettingService.UpdateUserSettingsAsync(userSettings, PageState.User.UserId);
}
else
{
await SettingService.DeleteSettingAsync(EntityNames.User, PageState.User.UserId, UserSetting.LastViewPageId);
}
}
// preserve other querystring parameters

View File

@ -1,5 +1,6 @@
@using System.Diagnostics.CodeAnalysis
@using System.Net
@using Microsoft.AspNetCore.Http
@namespace Oqtane.UI
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject SiteState SiteState
@ -11,6 +12,7 @@
@inject IUserService UserService
@inject IUrlMappingService UrlMappingService
@inject ILogService LogService
@inject ISettingService SettingService
@inject IJSRuntime JSRuntime
@implements IHandleAfterRender
@implements IDisposable
@ -27,7 +29,7 @@
private bool _isInternalNavigation = false;
private bool _navigationInterceptionEnabled;
private PageState _pagestate;
private string _error = "";
private string _error = "";
[Parameter]
public string RenderMode { get; set; }
@ -142,21 +144,6 @@
refresh = true;
}
if (PageState != null)
{
editmode = PageState.EditMode;
lastsyncdate = PageState.LastSyncDate;
visitorId = PageState.VisitorId;
}
if (PageState?.Page.Path != route.PagePath)
{
editmode = false; // reset edit mode when navigating to different page
}
if (querystring.ContainsKey("edit") && querystring["edit"] == "true")
{
editmode = true; // querystring can set edit mode
}
// verify user is authenticated for current site
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity.IsAuthenticated && authState.User.Claims.Any(item => item.Type == "sitekey" && item.Value == SiteState.Alias.SiteKey))
@ -170,12 +157,53 @@
user.IsAuthenticated = authState.User.Identity.IsAuthenticated;
}
}
else
else
{
user = PageState.User;
}
}
if (PageState != null)
{
editmode = PageState.EditMode;
lastsyncdate = PageState.LastSyncDate;
visitorId = PageState.VisitorId;
}
bool? pageChanged = null;
if(user != null && PageState?.Page != null)
{
var userSettings = await SettingService.GetUserSettingsAsync(user.UserId);
if(userSettings.ContainsKey(UserSetting.LastViewPageId))
{
int.TryParse(userSettings.GetValueOrDefault(UserSetting.LastViewPageId), out int lastViewPageId);
pageChanged = lastViewPageId != PageState.Page.PageId;
}
}
if (PageState?.Page.Path != route.PagePath || pageChanged.GetValueOrDefault(false))
{
editmode = false; // reset edit mode when navigating to different page
if (user != null)
{
await SettingService.DeleteSettingAsync(EntityNames.User, user.UserId, UserSetting.LastViewPageId);
}
}
else
{
if (querystring.ContainsKey("edit") && querystring["edit"] == "true")
{
editmode = true; // querystring can set edit mode
}
//check edit mode for authenticated users.
if (!editmode && user != null)
{
editmode = pageChanged.HasValue && !pageChanged.Value;
}
}
// process any sync events
var sync = await SyncService.GetSyncEventsAsync(lastsyncdate);
lastsyncdate = sync.SyncDate;

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Oqtane.Models
{
public class UserSetting
{
public const string LastViewPageId = "Page:LastViewed";
}
}