Update advanced/part8

This commit is contained in:
Richard Feldman
2018-08-13 22:20:59 -04:00
parent 8574576934
commit 29125fbc8f
4 changed files with 349 additions and 257 deletions

View File

@@ -1,8 +1,11 @@
module PaginatedList exposing (PaginatedList, fromList, map, mapPage, page, total, values, view)
module PaginatedList exposing (PaginatedList, fromList, fromRequestBuilder, map, page, total, values, view)
import Html exposing (Html, a, li, text, ul)
import Html.Attributes exposing (class, classList, href)
import Html.Events exposing (onClick)
import Http
import HttpBuilder exposing (RequestBuilder)
import Task exposing (Task)
@@ -54,29 +57,18 @@ map transform (PaginatedList info) =
PaginatedList { info | values = List.map transform info.values }
mapPage : (Int -> Int) -> PaginatedList a -> PaginatedList a
mapPage transform (PaginatedList info) =
PaginatedList { info | page = transform info.page }
-- VIEW
view : (Int -> msg) -> PaginatedList a -> Int -> Html msg
view toMsg list resultsPerPage =
view : (Int -> msg) -> PaginatedList a -> Html msg
view toMsg (PaginatedList info) =
let
totalPages =
ceiling (toFloat (total list) / toFloat resultsPerPage)
activePage =
page list
viewPageLink currentPage =
pageLink toMsg currentPage (currentPage == activePage)
pageLink toMsg currentPage (currentPage == info.page)
in
if totalPages > 1 then
List.range 1 totalPages
if info.total > 1 then
List.range 1 info.total
|> List.map viewPageLink
|> ul [ class "pagination" ]
@@ -96,3 +88,31 @@ pageLink toMsg targetPage isActive =
]
[ text (String.fromInt targetPage) ]
]
-- HTTP
{-| I considered accepting a record here so I don't mess up the argument order.
-}
fromRequestBuilder :
Int
-> Int
-> RequestBuilder (PaginatedList a)
-> Task Http.Error (PaginatedList a)
fromRequestBuilder resultsPerPage pageNumber builder =
let
offset =
(pageNumber - 1) * resultsPerPage
params =
[ ( "limit", String.fromInt resultsPerPage )
, ( "offset", String.fromInt offset )
]
in
builder
|> HttpBuilder.withQueryParams params
|> HttpBuilder.toRequest
|> Http.toTask
|> Task.map (\(PaginatedList info) -> PaginatedList { info | page = pageNumber })