Skip to content

Commit

Permalink
Persisting list state (sort, page, etc) between edits in the admin pa…
Browse files Browse the repository at this point in the history
…nel. Closes #130.
  • Loading branch information
impworks committed Jan 7, 2024
1 parent 1cfd3d9 commit 3dd3d98
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
41 changes: 38 additions & 3 deletions src/Bonsai/Areas/Admin/Controllers/AdminControllerBase.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Bonsai.Areas.Admin.Logic.Auth;
using System;
using Bonsai.Areas.Admin.Logic.Auth;
using Bonsai.Areas.Admin.Utils;
using Bonsai.Areas.Admin.ViewModels.Common;
using Bonsai.Code.Infrastructure;
using Bonsai.Code.Utils;
using Bonsai.Code.Utils.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;

namespace Bonsai.Areas.Admin.Controllers
{
Expand Down Expand Up @@ -44,9 +47,23 @@ public override void OnActionExecuted(ActionExecutedContext context)
/// <summary>
/// Saves the message for the next page.
/// </summary>
protected ActionResult RedirectToSuccess(string msg)
protected ActionResult RedirectToSuccess(string msg = null)
{
ShowMessage(msg);
if(!string.IsNullOrEmpty(msg))
ShowMessage(msg);

if (TempData[ListStateKey] is string listState)
{
try
{
var json = (ListRequestVM) JsonConvert.DeserializeObject(listState, ListStateType);
var url = ListRequestHelper.GetUrl(DefaultActionUrl, json);
return Redirect(url);
}
catch
{
}
}
return Redirect(DefaultActionUrl);
}

Expand All @@ -61,5 +78,23 @@ protected void ShowMessage(string msg, bool success = true)
Message = msg
});
}

/// <summary>
/// Stores the last list state in the temporary storage.
/// </summary>
protected void PersistListState(ListRequestVM request)
{
TempData[ListStateKey] = JsonConvert.SerializeObject(request, Formatting.None);
}

/// <summary>
/// Returns the key for persisting storage.
/// </summary>
private string ListStateKey => GetType().Name + "." + nameof(ListRequestVM);

/// <summary>
/// Default type to use for list state deserialization.
/// </summary>
protected virtual Type ListStateType => typeof(ListRequestVM);
}
}
3 changes: 3 additions & 0 deletions src/Bonsai/Areas/Admin/Controllers/ChangesetsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public ChangesetsController(ChangesetsManagerService changes, AppDbContext db)
private readonly ChangesetsManagerService _changes;
private readonly AppDbContext _db;

protected override Type ListStateType => typeof(ChangesetsListRequestVM);

#region Public methods

/// <summary>
Expand All @@ -31,6 +33,7 @@ public ChangesetsController(ChangesetsManagerService changes, AppDbContext db)
[HttpGet]
public async Task<ActionResult> Index(ChangesetsListRequestVM request)
{
PersistListState(request);
var vm = await _changes.GetChangesetsAsync(request);
return View(vm);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Bonsai/Areas/Admin/Controllers/MediaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ public MediaController(MediaManagerService media, PagesManagerService pages, IBa
private readonly IBackgroundJobService _jobs;
private readonly AppDbContext _db;

protected override Type ListStateType => typeof(MediaListRequestVM);

/// <summary>
/// Displays the list of pages.
/// </summary>
[HttpGet]
public async Task<ActionResult> Index(MediaListRequestVM request)
{
PersistListState(request);
var vm = await _media.GetMediaAsync(request);
return View(vm);
}
Expand Down Expand Up @@ -144,7 +147,7 @@ public async Task<ActionResult> Update(MediaEditorVM vm)
return RedirectToAction("Update", new {id = nextId.Value, saveAction = vm.SaveAction});
}

return Redirect(DefaultActionUrl);
return RedirectToSuccess();
}
catch (ValidationException ex)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Bonsai/Areas/Admin/Controllers/PagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public PagesController(PagesManagerService pages, ISearchEngine search, AppDbCon
private readonly AppDbContext _db;
private readonly IBackgroundJobService _jobs;

protected override Type ListStateType => typeof(PagesListRequestVM);

/// <summary>
/// Readable captions of the fields.
/// </summary>
Expand All @@ -61,6 +63,7 @@ public PagesController(PagesManagerService pages, ISearchEngine search, AppDbCon
[HttpGet]
public async Task<ActionResult> Index(PagesListRequestVM request)
{
PersistListState(request);
var vm = await _pages.GetPagesAsync(request);
return View(vm);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Bonsai/Areas/Admin/Controllers/RelationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ public RelationsController(RelationsManagerService rels, PagesManagerService pag
private readonly AppDbContext _db;
private readonly IBackgroundJobService _jobs;

protected override Type ListStateType => typeof(RelationsListRequestVM);

/// <summary>
/// Displays the list of relations.
/// </summary>
[HttpGet]
[Route("")]
public async Task<ActionResult> Index(RelationsListRequestVM request)
{
PersistListState(request);
ViewBag.Data = await GetDataAsync(request);
var rels = await _rels.GetRelationsAsync(request);
return View(rels);
Expand Down
7 changes: 5 additions & 2 deletions src/Bonsai/Areas/Admin/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ public UsersController(UsersManagerService users, PagesManagerService pages, Bon
private readonly ISearchEngine _search;
private readonly AppDbContext _db;

protected override Type ListStateType => typeof(UsersListRequestVM);

/// <summary>
/// Displays the list of users.
/// </summary>
[HttpGet]
[Route("")]
public async Task<ActionResult> Index([FromQuery] UsersListRequestVM vm)
public async Task<ActionResult> Index([FromQuery] UsersListRequestVM request)
{
var users = await _users.GetUsersAsync(vm);
PersistListState(request);
var users = await _users.GetUsersAsync(request);
ViewBag.AllowPasswordAuth = _config.GetStaticConfig().Auth.AllowPasswordAuth;
return View(users);
}
Expand Down

0 comments on commit 3dd3d98

Please sign in to comment.