Skip to content

Commit

Permalink
Use standard renderPagination in list page for Jobs Dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
oclbdk committed Dec 28, 2021
1 parent 31f0793 commit 0e76447
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 38 deletions.
14 changes: 9 additions & 5 deletions IHP/Job/Dashboard.hs
Expand Up @@ -38,6 +38,7 @@ import IHP.ControllerPrelude
import Unsafe.Coerce
import IHP.Job.Queue ()
import IHP.RouterPrelude hiding (get, tshow, error, map, putStrLn, elem)
import IHP.Pagination.Types
import qualified Database.PostgreSQL.Simple as PG
import qualified Database.PostgreSQL.Simple.Types as PG
import qualified Database.PostgreSQL.Simple.FromField as PG
Expand Down Expand Up @@ -176,11 +177,14 @@ instance JobsDashboard '[] where

listJob = error "listJob: Requested job type not in JobsDashboard Type"
listJob' _ = do
let page = fromMaybe 1 $ param "page"
table = param "tableName"
numberOfPages <- numberOfPagesForTable table 25
jobs <- queryBaseJobsFromTablePaginated table (page - 1) 25
render $ HtmlView $ renderBaseJobTablePaginated table jobs page numberOfPages
let table = param "tableName"
options = defaultPaginationOptions
page = paramOrDefault 1 "page"
pageSize = paramOrDefault (maxItems options) "maxItems"
totalItems <- totalRecordsForTable table
jobs <- queryBaseJobsFromTablePaginated table (page - 1) pageSize
let pagination = Pagination { currentPage = page, totalItems, pageSize, window = windowSize options }
render $ HtmlView $ renderBaseJobTablePaginated table jobs pagination
render $ EmptyView

viewJob = error "viewJob: Requested job type not in JobsDashboard Type"
Expand Down
10 changes: 7 additions & 3 deletions IHP/Job/Dashboard/Utils.hs
Expand Up @@ -7,10 +7,14 @@ import qualified Database.PostgreSQL.Simple.Types as PG

numberOfPagesForTable :: (?modelContext::ModelContext) => Text -> Int -> IO Int
numberOfPagesForTable table pageSize = do
(PG.Only totalRecords : _) <- sqlQuery
(PG.Query $ cs $ "SELECT COUNT(*) FROM " <> table)
()
totalRecords <- totalRecordsForTable table
pure $ case totalRecords `quotRem` pageSize of
(pages, 0) -> pages
(pages, _) -> pages + 1

totalRecordsForTable :: (?modelContext::ModelContext) => Text -> IO Int
totalRecordsForTable table = do
(PG.Only totalRecords : _) <- sqlQuery
(PG.Query $ cs $ "SELECT COUNT(*) FROM " <> table)
()
pure totalRecords
35 changes: 5 additions & 30 deletions IHP/Job/Dashboard/View.hs
Expand Up @@ -12,6 +12,8 @@ import IHP.ViewPrelude (JobStatus(..), ControllerContext, Html, View, hsx, html,
import qualified Data.List as List
import IHP.Job.Dashboard.Types
import IHP.Job.Dashboard.Utils
import IHP.Pagination.Types
import IHP.Pagination.ViewFunctions
import qualified IHP.Log as Log

-- | Provides a type-erased view. This allows us to specify a view as a return type without needed
Expand Down Expand Up @@ -87,8 +89,8 @@ renderBaseJobTable table rows =
|]
where renderHeader field = [hsx|<th>{field}</th>|]

renderBaseJobTablePaginated :: Text -> [BaseJob] -> Int -> Int -> Html
renderBaseJobTablePaginated table jobs page totalPages =
renderBaseJobTablePaginated :: Text -> [BaseJob] -> Pagination -> Html
renderBaseJobTablePaginated table jobs pagination =
let
headers :: [Text] = ["ID", "Updated At", "Status", "", ""]
lastJobIndex = (List.length jobs) - 1
Expand All @@ -111,37 +113,10 @@ renderBaseJobTablePaginated table jobs page totalPages =
</tbody>
</table>
</div>
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-end">
{renderPrev}
{when (totalPages /= 1) renderDest}
{renderNext}
</ul>
</nav>
{renderPagination pagination}
|]
where
renderHeader field = [hsx|<th>{field}</th>|]
renderDest = [hsx|<li class="page-item active"><a class="page-link" href={ListJobAction table page}>{page}</a></li>|]
renderPrev
| page == 1 = [hsx||]
| otherwise = [hsx|
<li class="page-item">
<a class="page-link" href={ListJobAction table (page - 1)} aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
<span class="sr-only">Previous</span>
</a>
</li>
|]
renderNext
| page == totalPages || totalPages == 0 = [hsx||]
| otherwise = [hsx|
<li class="page-item">
<a class="page-link" href={ListJobAction table (page + 1)} aria-label="Next">
<span aria-hidden="true">&raquo;</span>
<span class="sr-only">Next</span>
</a>
</li>
|]

renderBaseJobTableRow :: BaseJob -> Html
renderBaseJobTableRow job = [hsx|
Expand Down

0 comments on commit 0e76447

Please sign in to comment.