Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not filter out parameters with the same name in pagination links #41

Merged
merged 8 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

None

## [v1.1.2.2](https://github.com/pbrisbin/yesod-paginator/compare/v1.1.2.1...v1.1.2.2)
eahlberg marked this conversation as resolved.
Show resolved Hide resolved

- Fix (issue with filtering out query
parameters)[https://github.com/pbrisbin/yesod-paginator/issues/40]
eahlberg marked this conversation as resolved.
Show resolved Hide resolved

## [v1.1.2.1](https://github.com/pbrisbin/yesod-paginator/compare/v1.1.2.0...v1.1.2.1)

- Support GHCs 9.0 and 9.2
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yesod-paginator
version: 1.1.2.1
version: 1.1.2.2
synopsis: A pagination approach for yesod
description: Paginate a list showing a per-item widget and links to other pages
category: Web, Yesod
Expand Down
13 changes: 9 additions & 4 deletions src/Yesod/Paginator/Widgets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module Yesod.Paginator.Widgets
, simpleWith
, ellipsed
, ellipsedWith

-- * Exported for testing
, filterParams
eahlberg marked this conversation as resolved.
Show resolved Hide resolved
) where

import Yesod.Paginator.Prelude
Expand Down Expand Up @@ -178,12 +181,14 @@ getUpdateGetParams
getUpdateGetParams pageParamName = do
params <- handlerToWidget $ reqGetParams <$> getRequest
pure
$ \number ->
nubOn fst
$ [(unPageParamName pageParamName, tshow number)]
<> params
$ \number -> filterParams pageParamName number params

renderGetParams :: [(Text, Text)] -> Text
renderGetParams [] = ""
renderGetParams ps = "?" <> T.intercalate "&" (map renderGetParam ps)
where renderGetParam (k, v) = encodeText k <> "=" <> encodeText v

filterParams :: Show a => PageParamName -> a -> [(Text, Text)] -> [(Text, Text)]
filterParams pageParamName number params =
let name = unPageParamName pageParamName
in [(name, tshow number)] <> filter ((/=) name . fst) params
1 change: 1 addition & 0 deletions test/SpecHelper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Test.Hspec as X
import Yesod.Core
import Yesod.Paginator as X
import Yesod.Paginator.Prelude as X
import Yesod.Paginator.Widgets as X
import Yesod.Test as X

data App = App
Expand Down
21 changes: 19 additions & 2 deletions test/Yesod/Paginator/WidgetsSpec.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Yesod.Paginator.WidgetsSpec
( spec
)
where
) where

import SpecHelper

Expand Down Expand Up @@ -189,3 +188,21 @@ spec = withApp $ do
, "<li class=\"next disabled\"><a>»</a></li>"
, "</ul>"
]

describe "filterParams" $ it "works" $ do
eahlberg marked this conversation as resolved.
Show resolved Hide resolved
let pageParamName = PageParamName "p"
pageNumber :: Int
pageNumber = 3
params :: [(Text, Text)]
params =
[ ("p", "2")
, ("p", "3")
, ("foo", "bar")
, ("ids[]", "1")
, ("ids[]", "2")
]

assertEq
"filters page params not equal to the page number but keeps query params with the same name"
(filterParams pageParamName pageNumber params)
[("p", "3"), ("foo", "bar"), ("ids[]", "1"), ("ids[]", "2")]
eahlberg marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions yesod-paginator.cabal
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
-- This file has been generated from package.yaml by hpack version 0.34.5.
eahlberg marked this conversation as resolved.
Show resolved Hide resolved
--
-- see: https://github.com/sol/hpack

name: yesod-paginator
version: 1.1.2.1
version: 1.1.2.2
synopsis: A pagination approach for yesod
description: Paginate a list showing a per-item widget and links to other pages
category: Web, Yesod
Expand Down