0

I know there is a Paginator package for Yesod but I prefer a simpler UI so I was creating a simple pagination logic for my app. However, I couldn't figure out a way to convert the parameter value to Integer.

import Data.Text (unpack, singleton)
import Data.Maybe 

one = singleton '1' -- convert char to Text, required by fromMaybe

getTestPanelR :: Handler Html
getTestPanelR = do
    ptext <- lookupGetParam "p" -- guessing returns Maybe Text
    p <- fromMaybe one ptext -- ??? does  not work
    -- pn <- ??? Once p is extracted successfully, how to convert to an integer?

    s <- runDB $ selectList [] [Asc PersonName, LimitTo 10 , OffsetBy $ (pn - 1) * 10]
    (widget, enctype) <- generateFormPost $ entryForm Nothing
    defaultLayout $ do
        $(widgetFile "person")

When I run the above Code I get the following error message:

No instance for (MonadHandler Maybe)
  arising from a use of `lookupGetParam'
Possible fix: add an instance declaration for (MonadHandler Maybe)
In the second argument of `($)', namely `lookupGetParam "p"'
In a stmt of a 'do' block:
  p <- fromMaybe one $ lookupGetParam "p"
In the expression:
...

When I write out 'ptext' using #{show ptext} it shows Just "1". Having gotten the GET parameter, how do I convert it to an integer so I can do pagination? (need to add 1 for 'next' and subtract 1 for 'prev')

FWIW, when I try this using GHCi, it works fine:

Prelude Data.Maybe Data.Text> let one = singleton '1' 
Prelude Data.Maybe Data.Text> let x = Just $ singleton '5'
Prelude Data.Maybe Data.Text> let y = fromMaybe one x
Prelude Data.Maybe Data.Text> y
"5"
Prelude Data.Maybe Data.Text> read $ Data.Text.unpack y ::Int -- This is probably unsafe because I cannot trust 'y' in my web app
5

Update:

I tired @Ankur's suggestion pageNumber <- (lookupGetParam "p" >>= return . (read :: String -> Int) . fromMaybe "1") and I get the following error:

Couldn't match expected type `String' with actual type `Text'
Expected type: Maybe Text -> String
  Actual type: Maybe Text -> Text
In the return type of a call of `fromMaybe'
In the second argument of `(.)', namely `fromMaybe "1"'
Build failure, pausing...

If change the "1" to one (Data.Text.singleton '1'), I still get the exact same error message.

Thanks!

Ecognium
  • 2,046
  • 1
  • 19
  • 35

1 Answers1

1

lookupGetParam returns ParamValue which is type ParamValue = String. So basically it is String rather than Text.

Try this:

pageNumber <- (lookupGetParam "p" >>= return . (read :: String -> Int) . fromMaybe "1") 

UPDATE:

Actually the latest version of lookupGetParam is Text based so adding the OverloadedStrings language extension should get the job done:

Put this {-# LANGUAGE OverloadedStrings #-} at the start of the code file and use:

pageNumber <- (lookupGetParam "p" >>= return . (read :: String -> Int) . unpack . fromMaybe "1") 
Ankur
  • 33,367
  • 2
  • 46
  • 72
  • Thanks @Ankur. Please see my update above -- I still get an error. Actually I got this error message before when I accidentally used "1" in fromMaybe. That's when I went down the Text path but my lack of Haskell knowledge made it hard for me to debug. Any thoughts on what could be going wrong with your suggestion as well? – Ecognium Jul 18 '13 at 15:25