Skip to content

Commit

Permalink
Code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jevgenijsp committed Nov 6, 2024
1 parent c008afc commit 2fddfa6
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeaderCell;
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Models;
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Models;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Base;
Expand All @@ -14,27 +13,4 @@ public void ApplySort(string sortColumn, SortDirection? sortDirection)
SortColumn = sortColumn;
SortDirection = sortDirection;
}

public SortableHeaderCellViewModel CreateHeaderCellViewModel(string displayName, string key)
{
return new SortableHeaderCellViewModel
{
DisplayName = displayName,
Key = key,
IsActive = IsActiveSortColumn(key),
SortDirection = GetSortDirection(key)
};
}

private SortDirection? GetSortDirection(string key)
{
if (IsActiveSortColumn(key))
{
return SortDirection ?? Models.SortDirection.Ascending;
}

return null;
}

private bool IsActiveSortColumn(string key) => SortColumn == key;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
style="white-space: nowrap">
@Model.DisplayName

@if (Model.IsActive && Model.SortDirection != null)
@if (Model.IsActive() && Model.GetSortDirection() != null)
{
<span data-feather="@(Model.SortDirection == SortDirection.Ascending ? "arrow-up" : "arrow-down")">
<span data-feather="@(Model.GetSortDirection() == SortDirection.Ascending ? "arrow-up" : "arrow-down")">
</span>
}
</a>
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ public SortableHeaderCellViewComponent(IHttpContextAccessor contextAccessor)
_contextAccessor = contextAccessor;
}

public IViewComponentResult Invoke(SortableHeaderCellViewModel sortableHeaderCellViewModel)
public IViewComponentResult Invoke(string key, string displayName)
{
var context = _contextAccessor.HttpContext;

sortableHeaderCellViewModel.QueryString = context?.Request.QueryString.ToString();

return View(sortableHeaderCellViewModel);
return View(new SortableHeaderCellViewModel
{
QueryString = context?.Request.QueryString.ToString(),
Key = key,
DisplayName = displayName
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Web;
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Base;
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Models;

namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeaderCell;
Expand All @@ -8,17 +9,15 @@ public class SortableHeaderCellViewModel
{
public string DisplayName { get; set; }
public string Key { get; set; }
public SortDirection? SortDirection { get; set; }
public bool IsActive { get; set; }
public string QueryString { get; set; }

public SortDirection? GetNextSortDirection()
{
return SortDirection switch
return GetSortDirection() switch
{
null => Models.SortDirection.Ascending,
Models.SortDirection.Ascending => Models.SortDirection.Descending,
Models.SortDirection.Descending => null,
null => SortDirection.Ascending,
SortDirection.Ascending => SortDirection.Descending,
SortDirection.Descending => null,
_ => throw new ArgumentOutOfRangeException()
};
}
Expand All @@ -35,11 +34,30 @@ public string GetSortColumn()

public string GetSortUrl()
{
var qs = HttpUtility.ParseQueryString(QueryString);
var qs = HttpUtility
.ParseQueryString(QueryString);

qs["sortColumn"] = GetSortColumn();
qs["sortDirection"] = GetNextSortDirection().ToString();
qs[nameof(AbstractSortablePageModel.SortColumn)] = GetSortColumn();
qs[nameof(AbstractSortablePageModel.SortDirection)] = GetNextSortDirection().ToString();

return $"?{qs}";
}

public bool IsActive()
{
var sortColumn = HttpUtility
.ParseQueryString(QueryString)
.Get(nameof(AbstractSortablePageModel.SortColumn));

return !string.IsNullOrEmpty(sortColumn) && sortColumn == Key;
}

public SortDirection? GetSortDirection()
{
var sortDirection = HttpUtility
.ParseQueryString(QueryString)
.Get(nameof(AbstractSortablePageModel.SortDirection));

return Enum.TryParse(sortDirection, out SortDirection sort) ? sort : null;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@page "{handler?}"
@using Geta.NotFoundHandler.Core.Redirects
@using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeaderCell
@using Geta.NotFoundHandler.Core.Providers.RegexRedirects
@using Microsoft.AspNetCore.Mvc.TagHelpers
@model Geta.NotFoundHandler.Admin.Pages.Geta.NotFoundHandler.Admin.DeletedModel

Expand All @@ -11,10 +12,7 @@
<thead>
<tr>
<th>
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("URL", nameof(CustomRedirect.OldUrl))
)
<vc:sortable-header-cell key="@nameof(RegexRedirect.OldUrlRegex)" display-name="Old URL Regex"/>
</th>
<th class="col-1"></th>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DeletedModel(IRedirectsService redirectsService)
[BindProperty(SupportsGet = true)]
public Paging Paging { get; set; }

public void OnGet(string sortColumn, SortDirection sortDirection)
public void OnGet(string sortColumn, SortDirection? sortDirection)
{
ApplySort(sortColumn, sortDirection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
<thead>
<tr>
<th>
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("URL", nameof(CustomRedirect.OldUrl))
)
<vc:sortable-header-cell key="@nameof(CustomRedirect.OldUrl)" display-name="URL"/>
</th>
<th class="col-1"></th>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public IgnoredModel(IRedirectsService redirectsService)
[BindProperty(SupportsGet = true)]
public Paging Paging { get; set; }

public void OnGet(string sortColumn, SortDirection sortDirection)
public void OnGet(string sortColumn, SortDirection? sortDirection)
{
ApplySort(sortColumn, sortDirection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,16 @@
<thead>
<tr>
<th>
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("New URL", nameof(CustomRedirect.NewUrl))
)
<vc:sortable-header-cell key="@nameof(CustomRedirect.NewUrl)" display-name="New URL"/>
</th>
<th>
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("Old URL", nameof(CustomRedirect.OldUrl))
)
<vc:sortable-header-cell key="@nameof(CustomRedirect.OldUrl)" display-name="Old URL"/>
</th>
<th class="col-1 text-center">
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("Wildcard", nameof(CustomRedirect.WildCardSkipAppend))
)
<vc:sortable-header-cell key="@nameof(CustomRedirect.WildCardSkipAppend)" display-name="Wildcard"/>
</th>
<th class="col-1">
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("Redirect Type", nameof(CustomRedirect.RedirectType))
)
<vc:sortable-header-cell key="@nameof(CustomRedirect.RedirectType)" display-name="Redirect Type"/>
</th>
<th class="col-1"></th>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,13 @@
<thead>
<tr>
<th class="col-1">
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("Order Number", nameof(RegexRedirect.OrderNumber))
)
<vc:sortable-header-cell key="@nameof(RegexRedirect.OrderNumber)" display-name="OrderNumber"/>
</th>
<th>
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("Old URL Regex", nameof(RegexRedirect.OldUrlRegex))
)
<vc:sortable-header-cell key="@nameof(RegexRedirect.OldUrlRegex)" display-name="Old URL Regex"/>
</th>
<th>
@await Component.InvokeAsync(
"SortableHeaderCell",
Model.CreateHeaderCellViewModel("New URL Format", nameof(RegexRedirect.NewUrlFormat))
)
<vc:sortable-header-cell key="@nameof(RegexRedirect.NewUrlFormat)" display-name="New URL Format"/>
</th>
<th class="col-1"></th>
<th class="col-1"></th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public RegexModel(
[BindProperty]
public RegexRedirectModel RegexRedirect { get; set; }

public void OnGet(string sortColumn, SortDirection sortDirection)
public void OnGet(string sortColumn, SortDirection? sortDirection)
{
ApplySort(sortColumn, sortDirection);

Expand Down

0 comments on commit 2fddfa6

Please sign in to comment.