Skip to content

Instantly share code, notes, and snippets.

@cschneid
Created March 24, 2015 16:12
Show Gist options
  • Save cschneid/544dc7b166edfa81cb11 to your computer and use it in GitHub Desktop.
Save cschneid/544dc7b166edfa81cb11 to your computer and use it in GitHub Desktop.
module Login where
import qualified Data.Text as T
type LoginMessage = T.Text
data LoginState =
-- This user is logged in
LoggedIn User
-- The user is not logged in. Show a form and let them do stuff
-- The message allows for failed logins to display something
| LoggedOut (Maybe LoginMessage)
-- We have submitted this user, and are waiting for a response
-- from the server
| LogginInProcess
deriving (Show)
data LoginAction =
LogOutA
| SubmitFormA User
| ResponseFailureA LoginMessage
| ResponseSuccessA User
deriving (Show)
loginTransition :: LoginState -> LoginAction -> LoginState
loginTransition LoggedIn LoginLogoutA = LoggedOut Nothing
loginTransition LoggedOut (SubmitFormA u) = LoginInProgress u
loginTransition LoginInProgress (ResponseFailureA m) = LoggedOut m
loginTransition LoginInProgress (ResponseSuccessA u) = LoggedIn u
loginTransition state action =
error $ "Invalid transition. From " ++ show state ++ " with " ++ show action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment