Skip to content

Instantly share code, notes, and snippets.

@xkikeg
Last active December 18, 2015 11:49
Show Gist options
  • Save xkikeg/5778184 to your computer and use it in GitHub Desktop.
Save xkikeg/5778184 to your computer and use it in GitHub Desktop.
Yesod Book :: Sessions :: Ultimate Destination
{-# LANGUAGE OverloadedStrings, TypeFamilies, TemplateHaskell,
QuasiQuotes, MultiParamTypeClasses #-}
import Yesod
data UltDest = UltDest
mkYesod "UltDest" [parseRoutes|
/ RootR GET
/setname SetNameR GET POST
/sayhello SayHelloR GET
|]
instance Yesod UltDest
instance RenderMessage UltDest FormMessage where
renderMessage _ _ = defaultFormMessage
getRootR = defaultLayout [whamlet|
<p>
<a href=@{SetNameR}>Set your name
<p>
<a href=@{SayHelloR}>Say hello
|]
-- Display the set name form
getSetNameR = defaultLayout [whamlet|
<form method=post>
My name is #
<input type=text name=name>
. #
<input type=submit value="Set name">
|]
-- Retreive the submitted name from the user
postSetNameR :: Handler ()
postSetNameR = do
-- Get the submitted name and set it in the session
name <- runInputPost $ ireq textField "name"
setSession "name" name
-- After we get a name, redirect to the ultimate destination.
-- If no destination is set, default to the homepage
redirectUltDest RootR
getSayHelloR = do
-- Lookup the name value set in the session
mname <- lookupSession "name"
case mname of
Nothing -> do
-- No name in the session, set the current page as
-- the ultimate destination and redirect to the
-- SetName page
setUltDestCurrent
setMessage "Please tell me your name"
redirect SetNameR
Just name -> defaultLayout [whamlet|
<p>Welcome #{name}
|]
main :: IO ()
main = warp 3000 UltDest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment