Skip to content

Commit

Permalink
Do not filter out parameters with the same name in pagination links
Browse files Browse the repository at this point in the history
  • Loading branch information
eahlberg committed May 11, 2022
1 parent 7e8c2ba commit 085ec10
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 49 deletions.
10 changes: 6 additions & 4 deletions src/Yesod/Paginator/Widgets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ getUpdateGetParams
getUpdateGetParams pageParamName = do
params <- handlerToWidget $ reqGetParams <$> getRequest
pure
$ \number ->
nubOn fst
$ [(unPageParamName pageParamName, tshow number)]
<> params
$ \number -> formatParams number params
where
formatParams :: Show a => a -> [(Text, Text)] -> [(Text, Text)]
formatParams number params =
let name = unPageParamName pageParamName
in [(name, tshow number)] <> filter ((/=) name . fst) params

renderGetParams :: [(Text, Text)] -> Text
renderGetParams [] = ""
Expand Down
111 changes: 66 additions & 45 deletions test/Yesod/Paginator/WidgetsSpec.hs
Original file line number Diff line number Diff line change
@@ -1,56 +1,77 @@
{-# LANGUAGE OverloadedStrings #-}
module Yesod.Paginator.WidgetsSpec
( spec
)
where
) where

import SpecHelper

spec :: Spec
spec = withApp $ do
describe "simple" $ it "works" $ do
get $ SimpleR 10 3 3

statusIs 200
bodyContains $ concat
[ "<ul class=\"pagination\">"
, "<li class=\"prev disabled\"><a>«</a></li>"
, "<li class=\"active disabled\"><a>1</a></li>"
, "<li class=\"next\"><a href=\"?p=2\">2</a></li>"
, "<li class=\"next\"><a href=\"?p=3\">3</a></li>"
, "<li class=\"next\"><a href=\"?p=2\">»</a></li>"
, "</ul>"
]

request $ do
addGetParam "p" "3"
setUrl $ SimpleR 10 3 3

statusIs 200
bodyContains $ concat
[ "<ul class=\"pagination\">"
, "<li class=\"prev\"><a href=\"?p=2\">«</a></li>"
, "<li class=\"prev\"><a href=\"?p=2\">2</a></li>"
, "<li class=\"active disabled\"><a>3</a></li>"
, "<li class=\"next\"><a href=\"?p=4\">4</a></li>"
, "<li class=\"next\"><a href=\"?p=4\">»</a></li>"
, "</ul>"
]

request $ do
addGetParam "p" "4"
setUrl $ SimpleR 10 3 3

statusIs 200
bodyContains $ concat
[ "<ul class=\"pagination\">"
, "<li class=\"prev\"><a href=\"?p=3\">«</a></li>"
, "<li class=\"prev\"><a href=\"?p=2\">2</a></li>"
, "<li class=\"prev\"><a href=\"?p=3\">3</a></li>"
, "<li class=\"active disabled\"><a>4</a></li>"
, "<li class=\"next disabled\"><a>»</a></li>"
, "</ul>"
]
describe "simple" $ do
it "works" $ do
get $ SimpleR 10 3 3

statusIs 200
bodyContains $ concat
[ "<ul class=\"pagination\">"
, "<li class=\"prev disabled\"><a>«</a></li>"
, "<li class=\"active disabled\"><a>1</a></li>"
, "<li class=\"next\"><a href=\"?p=2\">2</a></li>"
, "<li class=\"next\"><a href=\"?p=3\">3</a></li>"
, "<li class=\"next\"><a href=\"?p=2\">»</a></li>"
, "</ul>"
]

request $ do
addGetParam "p" "3"
setUrl $ SimpleR 10 3 3

statusIs 200
bodyContains $ concat
[ "<ul class=\"pagination\">"
, "<li class=\"prev\"><a href=\"?p=2\">«</a></li>"
, "<li class=\"prev\"><a href=\"?p=2\">2</a></li>"
, "<li class=\"active disabled\"><a>3</a></li>"
, "<li class=\"next\"><a href=\"?p=4\">4</a></li>"
, "<li class=\"next\"><a href=\"?p=4\">»</a></li>"
, "</ul>"
]

request $ do
addGetParam "p" "4"
setUrl $ SimpleR 10 3 3

statusIs 200
bodyContains $ concat
[ "<ul class=\"pagination\">"
, "<li class=\"prev\"><a href=\"?p=3\">«</a></li>"
, "<li class=\"prev\"><a href=\"?p=2\">2</a></li>"
, "<li class=\"prev\"><a href=\"?p=3\">3</a></li>"
, "<li class=\"active disabled\"><a>4</a></li>"
, "<li class=\"next disabled\"><a>»</a></li>"
, "</ul>"
]

it "includes all parameters with the same name" $ do
get $ SimpleR 10 3 3

request $ do
addGetParam "p" "3"
addGetParam "ids[]" "1"
addGetParam "ids[]" "2"
setUrl $ SimpleR 10 3 3

statusIs 200

bodyContains $ concat
[ "<ul class=\"pagination\">"
, "<li class=\"prev\"><a href=\"?p=2&amp;ids%5B%5D=2&amp;ids%5B%5D=1\">«</a></li>"
, "<li class=\"prev\"><a href=\"?p=2&amp;ids%5B%5D=2&amp;ids%5B%5D=1\">2</a></li>"
, "<li class=\"active disabled\"><a>3</a></li>"
, "<li class=\"next\"><a href=\"?p=4&amp;ids%5B%5D=2&amp;ids%5B%5D=1\">4</a></li>"
, "<li class=\"next\"><a href=\"?p=4&amp;ids%5B%5D=2&amp;ids%5B%5D=1\">»</a></li>"
, "</ul>"
]

describe "ellipsed" $ it "works" $ do
get $ EllipsedR 10 3 3
Expand Down

0 comments on commit 085ec10

Please sign in to comment.