Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeps search state on post, allow order by column #80

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public IActionResult OnPostDeleteAllIgnoredSuggestions()
Message = $"All {count} ignored suggestions permanently removed";
CardType = CardType.Success;

return RedirectToPage(new {
return RedirectToPage(new
{
Message,
CardType
});
Expand Down Expand Up @@ -168,7 +169,7 @@ public IActionResult OnPostImportDeletedRedirects()

public IActionResult OnPostExportRedirects()
{
var redirects = _redirectsService.GetSaved().ToList();
var redirects = _redirectsService.GetRedirects(new Data.QueryParams() { QueryState = RedirectState.Saved }).Redirects.ToList();
var document = _redirectsXmlParser.Export(redirects);

var memoryStream = new MemoryStream();
Expand Down Expand Up @@ -208,5 +209,6 @@ private CustomRedirectCollection ReadDeletedRedirectsFromImportFile()
public class DeleteSuggestionsModel
{
public int MaxErrors { get; set; } = 5;

public int MinimumDays { get; set; } = 30;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
<nav aria-label="Table pagination">
<ul class="pagination">
<li class="page-item @(Model.HasPreviousPage ? string.Empty : "disabled")">
<a class="page-link" href="@Model.PageUrl(Model.PageNumber-1)" aria-label="Previous">
<button type="button" class="page-link" name="page" value="@(Model.PageNumber-1)" aria-label="Previous page">
<span aria-hidden="true">&laquo;</span>
</a>
</button>
</li>

@for (int i = 1; i <= Model.PageCount; i++)
{
<li class="page-item"><a class="page-link" href="@Model.PageUrl(i)">@i</a></li>
<li class="page-item @(Model.PageNumber != i ? string.Empty : "disabled")">
<button type="button" class="page-link" name="page" value="@i" aria-label="Goto page @i">
@i
</button>
</li>
}
<li class="page-item @(Model.HasNextPage ? string.Empty : "disabled")">
<a class="page-link" href="@Model.PageUrl(Model.PageNumber+1)" aria-label="Next">
<button type="button" class="page-link" name="page" value="@(Model.PageNumber+1)" aria-label="Next page">
<span aria-hidden="true">&raquo;</span>
</a>
</button>
</li>
</ul>
</nav>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using X.PagedList;

namespace Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Components.Pager
{
Expand All @@ -13,15 +13,16 @@ public PagerViewComponent(IHttpContextAccessor contextAccessor)
_contextAccessor = contextAccessor;
}

public IViewComponentResult Invoke(IPagedList items)
public IViewComponentResult Invoke(int page, int pageSize, int totalCount)
{
var context = _contextAccessor.HttpContext;
var pageCount = pageSize is int ps && ps > 0 ? (int)Math.Ceiling((decimal)totalCount / ps) : 1;
return View(new PagerViewModel
{
HasPreviousPage = items.HasPreviousPage,
HasNextPage = items.HasNextPage,
PageNumber = items.PageNumber,
PageCount = items.PageCount,
HasPreviousPage = page > 1,
HasNextPage = pageCount > page,
PageNumber = page,
PageCount = pageCount,
QueryString = context.Request.QueryString.ToString()
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using System.Web;

namespace Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Components.Pager
{
public class PagerViewModel
{
public bool HasPreviousPage { get; set; }

public bool HasNextPage { get; set; }

public int PageNumber { get; set; }

public int PageCount { get; set; }
public string QueryString { get; set; }

public string PageUrl(int page)
{
var qs = HttpUtility.ParseQueryString(QueryString);
qs["page"] = page.ToString();
return $"?{qs}";
}
public string QueryString { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@using System.Data.SqlClient;
@model Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeader.SortableHeaderViewModel

@{
var sortBy = Model.InternalName;
var isCurrent = Model.Params.SortBy == Model.InternalName;
var nextSortDirection = isCurrent && Model.Params.SortDirection == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
}

<th class="sortable-header@(isCurrent ? " current" : "")@(Model.AdditionalClass != null ? $" {Model.AdditionalClass}" : "")"
data-sort-by="@sortBy" data-sort-direction="@nextSortDirection">
<button type="button" title="Sort @Model.DisplayName @nextSortDirection.ToString().ToLower()">
@Model.DisplayName

@if (isCurrent && nextSortDirection == SortOrder.Ascending)
{
<span data-feather="arrow-up"></span>
}
else
{
<span data-feather="arrow-down"></span>
}
</button>
</th>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Geta.NotFoundHandler.Data;
using Microsoft.AspNetCore.Mvc;

namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeader
{
public class SortableHeaderViewComponent : ViewComponent
{
public IViewComponentResult Invoke(string displayName, string internalName, QueryParams @params, string additionalClass = null)
{
return View(new SortableHeaderViewModel(displayName, internalName, @params, additionalClass));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Geta.NotFoundHandler.Data;

namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeader
{
public class SortableHeaderViewModel
{
public SortableHeaderViewModel(string displayName, string internalName, QueryParams @params, string additionalClass = null)
{
DisplayName = displayName;
InternalName = internalName;
Params = @params;
AdditionalClass = additionalClass;
}

public string DisplayName { get; init; }

public string InternalName { get; init; }

public QueryParams Params { get; init; }

public string AdditionalClass { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
@page "{handler?}"
@model Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.DeletedModel
@model DeletedModel

@await Component.InvokeAsync("Card", new { message = Model.Message })
@await Component.InvokeAsync("Card", new { message = Model.OperationMessage, cardType = CardType.Success })

<form method="post">
<div class="table-responsive mt-3">
<table class="table table-hover table-sm" aria-label="Deleted redirects">
<thead>
<tr>
<th>URL</th>
<th class="col-1"></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" class="form-control" asp-for="DeletedRedirect.OldUrl">
<span asp-validation-for="DeletedRedirect.OldUrl" class="text-danger"></span>
</td>
<form id="tableQueryState" method="get">
@Html.HiddenForQueryParams()
</form>

<div class="table-responsive mt-3">
<table class="table table-hover table-sm" aria-label="Deleted redirects">
<thead>
<tr>
@await Component.InvokeAsync("SortableHeader", new { DisplayName = "URL", InternalName = nameof(CustomRedirect.OldUrl), Model.Params })
<th class="col-1"></th>
</tr>
</thead>
<form method="post">
@Html.HiddenForQueryParams()
<tbody>
<tr>
<td>
<input type="text" class="form-control" asp-for="DeletedRedirect.OldUrl">
<span asp-validation-for="DeletedRedirect.OldUrl" class="text-danger"></span>
</td>
<td>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary" asp-page-handler="create">
<span data-feather="plus"></span> add
</button>
</div>
</td>
</tr>
@foreach (var item in Model.Results.Redirects)
{
<tr class="align-middle">
<td>@item.OldUrl</td>
<td>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary"
asp-page-handler="create">
<span data-feather="plus"></span> add
<button type="submit" class="btn btn-danger" asp-page-handler="delete" asp-route-oldurl="@item.OldUrl">
<span data-feather="trash-2"></span> delete
</button>
</div>
</td>
</tr>
@foreach (var item in Model.Items)
{
<tr class="align-middle">
<td>@item.OldUrl</td>
<td>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-danger"
asp-page-handler="delete" asp-route-oldurl="@item.OldUrl">
<span data-feather="trash-2"></span> delete
</button>
</div>
</td>
</tr>
}
</tbody>
</table>
@await Component.InvokeAsync(typeof(Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Components.Pager.PagerViewComponent), new { Model.Items })
</div>
</form>
}
</tbody>
</form>
</table>
@await Component.InvokeAsync(typeof(Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Components.Pager.PagerViewComponent), new { Model.Params.Page, Model.Params.PageSize, Model.Results.TotalCount })
</div>
Original file line number Diff line number Diff line change
@@ -1,63 +1,39 @@
using System.Linq;
using Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.Models;
using Geta.NotFoundHandler.Core.Redirects;
using Geta.NotFoundHandler.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using X.PagedList;

namespace Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin;

[Authorize(Constants.PolicyName)]
public class DeletedModel : PageModel
public class DeletedModel : BaseCustomRedirectPageModel
{
private readonly IRedirectsService _redirectsService;

public DeletedModel(IRedirectsService redirectsService)
public DeletedModel(IRedirectsService redirectsService) : base(redirectsService, RedirectState.Deleted,
"There are currently {0} URLs that return a Deleted response.This tells crawlers to remove these URLs from their index. ")
{
_redirectsService = redirectsService;
}

public string Message { get; set; }

public IPagedList<CustomRedirect> Items { get; set; } = Enumerable.Empty<CustomRedirect>().ToPagedList();

[BindProperty]
public DeletedRedirectModel DeletedRedirect { get; set; }

[BindProperty(SupportsGet = true)]
public Paging Paging { get; set; }

public void OnGet()
{
Load();
}

public IActionResult OnPostCreate()
{
if (!ModelState.IsValid)
if (ModelState.IsValid)
{
Load();
return Page();
RedirectsService.AddDeletedRedirect(DeletedRedirect.OldUrl);
OperationMessage = $"Added delete redirect for {DeletedRedirect.OldUrl}";
DeletedRedirect = new DeletedRedirectModel();
return LoadPage(true);
}

_redirectsService.AddDeletedRedirect(DeletedRedirect.OldUrl);

return RedirectToPage();
return LoadPage();
}

public IActionResult OnPostDelete(string oldUrl)
{
_redirectsService.DeleteByOldUrl(oldUrl);
return RedirectToPage();
}

private void Load()
{
var items = _redirectsService.GetDeleted().ToPagedList(Paging.PageNumber, Paging.PageSize);
Message =
$"There are currently {items.TotalItemCount} URLs that return a Deleted response. This tells crawlers to remove these URLs from their index.";
Items = items;
RedirectsService.DeleteByOldUrl(oldUrl);
OperationMessage = $"Removed {oldUrl}";
return LoadPage(true);
}
}
Loading
Loading