Skip to content

Commit

Permalink
Amended to show pages numbers where we know we have all possible sear…
Browse files Browse the repository at this point in the history
…ch results returned
  • Loading branch information
hortha committed Sep 15, 2023
1 parent 77f0513 commit 4514bc8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<govuk-button class="moj-search__button" type="submit">Search</govuk-button>
</div>
<govuk-select asp-for="SortBy">
<govuk-select-label>Sort by</govuk-select-label>
<govuk-select-item value="@ContactSearchSortByOption.LastNameAscending">@ContactSearchSortByOption.LastNameAscending.GetDisplayName()</govuk-select-item>
<govuk-select-item value="@ContactSearchSortByOption.LastNameDescending">@ContactSearchSortByOption.LastNameDescending.GetDisplayName()</govuk-select-item>
<govuk-select-item value="@ContactSearchSortByOption.FirstNameAscending">@ContactSearchSortByOption.FirstNameAscending.GetDisplayName()</govuk-select-item>
Expand All @@ -38,21 +37,6 @@
{
<div class="govuk-grid-row" data-testid="search-results">
<div class="govuk-grid-column-full">
@if (Model.TotalKnownPages > 1)
{
<govuk-pagination>
@if (Model.PreviousPage.HasValue)
{
<govuk-pagination-previous asp-page="Index" asp-route-search="@Model.Search" asp-route-pagenumber="@Model.PreviousPage" asp-route-sortby="@Model.SortBy" />
}

@if (Model.NextPage.HasValue)
{
<govuk-pagination-next asp-page="Index" asp-route-search="@Model.Search" asp-route-pagenumber="@Model.NextPage" asp-route-sortby="@Model.SortBy" />
}
</govuk-pagination>
}

<table class="govuk-table">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
Expand Down Expand Up @@ -92,6 +76,21 @@
<govuk-pagination-previous asp-page="Index" asp-route-search="@Model.Search" asp-route-pagenumber="@Model.PreviousPage" asp-route-sortby="@Model.SortBy" />
}

@if (Model.DisplayPageNumbers)
{
@for (int i = 0; i < Model.PaginationPages!.Length; i++)
{
var item = Model.PaginationPages[i];

if (i > 0 && Model.PaginationPages[i - 1] != item - 1)
{
<govuk-pagination-ellipsis />
}

<govuk-pagination-item asp-page="Index" asp-route-search="@Model.Search" asp-route-pagenumber="@item" is-current="@(item == Model.PageNumber)">@item</govuk-pagination-item>
}
}

@if (Model.NextPage.HasValue)
{
<govuk-pagination-next asp-page="Index" asp-route-search="@Model.Search" asp-route-pagenumber="@Model.NextPage" asp-route-sortby="@Model.SortBy" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,24 @@ public IndexModel(
public string? Search { get; set; }

[BindProperty(SupportsGet = true)]
[Display(Name = "Sort by")]
public ContactSearchSortByOption SortBy { get; set; }

[BindProperty(SupportsGet = true)]
public int? PageNumber { get; set; }

public PersonInfo[]? SearchResults { get; set; }
public int[]? PaginationPages { get; set; }

public int TotalKnownPages { get; set; }

public bool DisplayPageNumbers { get; set; }

public int? PreviousPage { get; set; }

public int? NextPage { get; set; }

public PersonInfo[]? SearchResults { get; set; }

public async Task<IActionResult> OnGet()
{
PageNumber ??= 1;
Expand Down Expand Up @@ -100,6 +105,18 @@ private async Task<IActionResult> PerformSearch()
PreviousPage = PageNumber > 1 ? PageNumber - 1 : null;
NextPage = PageNumber < TotalKnownPages ? PageNumber + 1 : null;

if (contacts.Length < MaxSearchResultCount)
{
DisplayPageNumbers = true;
PaginationPages = Enumerable.Range(-2, 5).Select(offset => PageNumber!.Value + offset)
.Append(1)
.Append(TotalKnownPages)
.Where(page => page <= TotalKnownPages && page >= 1)
.Distinct()
.Order()
.ToArray();
}

SearchResults = contacts
.Skip((PageNumber!.Value - 1) * PageSize)
.Take(PageSize)
Expand Down

0 comments on commit 4514bc8

Please sign in to comment.