Last active
August 29, 2015 14:17
-
-
Save cschneid/06ddfa9a40dfd2f44001 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module MealPlan where | |
| import Network.Http.StatusHelpers | |
| import qualified Grocery.Types as GT | |
| import Grocery.ListHelpers | |
| import Types | |
| import qualified Login.StateMachine as Login | |
| import Login.Action | |
| import JavaScript.JQuery | |
| import Data.Default | |
| import Control.Concurrent | |
| import Prelude hiding (div) | |
| import Blaze.Core | |
| import Blaze.ReactJS.Base | |
| import qualified Blaze.ReactJS.Run as ReactJS | |
| import qualified Text.Blaze.Event as E | |
| import qualified Text.Blaze.Event.Keycode as Keycode | |
| import qualified Text.Blaze.Html5 as H | |
| import qualified Text.Blaze.Html5.Attributes as A | |
| import qualified Data.Text as T | |
| import Data.Time | |
| import Control.Monad.State | |
| import Control.Monad.Writer | |
| import Control.Lens | |
| import Data.Typeable | |
| data UIState = LoginScreen | |
| | RegisterScreen | |
| | MealPlanScreen | |
| deriving (Eq, Show, Typeable) | |
| data MealPlanState = MealPlanState { | |
| _uiState :: UIState | |
| , _user :: GT.User | |
| , _loginState :: Login.LoginState | |
| } deriving (Eq, Show, Typeable) | |
| makeLenses ''MealPlanState | |
| consoleLog :: T.Text -> ActionChannel -> IO () | |
| consoleLog t _ = print t | |
| ------------------------------------------------------------------------------ | |
| -- Rendering | |
| ------------------------------------------------------------------------------ | |
| renderState :: MealPlanState -> WindowState MealPlanAction | |
| renderState state = WindowState | |
| { _wsBody = renderBody state | |
| , _wsPath = "" | |
| } | |
| renderBody :: MealPlanState -> H.Html MealPlanAction | |
| renderBody st = do | |
| H.div $ H.toHtml $ show st | |
| case st ^. uiState of | |
| LoginScreen -> renderLoginForm st | |
| RegisterScreen -> renderRegisterForm st | |
| MealPlanScreen -> return () | |
| renderRegisterForm :: MealPlanState -> H.Html MealPlanAction | |
| renderRegisterForm st = do | |
| H.h1 "Register!" | |
| renderLoginForm :: MealPlanState -> H.Html MealPlanAction | |
| renderLoginForm st = do | |
| let email = st ^. user . GT.userEmail | |
| let pass = case st ^. user . GT.userPassword of | |
| GT.HashedPassword (Just x) -> x | |
| GT.HashedPassword Nothing -> "No Hashes Password" | |
| GT.UnhashedPassword x -> x | |
| H.input H.! A.id "userEmail" | |
| H.! A.placeholder "Email:" | |
| H.! A.value (H.toValue email) | |
| H.! E.onValueChange UpdateUsernameA | |
| H.input H.! A.id "userPassword" | |
| H.! A.type_ "password" | |
| H.! A.placeholder "Password:" | |
| H.! A.value (H.toValue pass) | |
| H.! E.onValueChange UpdateUserPasswordA | |
| H.button H.! E.onClick' LoginUserA $ "Login!" | |
| H.button H.! E.onClick' RegisterNewUserA $ "Register!" | |
| ------------------------------------------------------------------------------ | |
| -- Handling | |
| ------------------------------------------------------------------------------ | |
| applyMealPlanAction :: MealPlanAction -> ApplyActionM MealPlanState MealPlanRequest () | |
| applyMealPlanAction action = | |
| case action of | |
| UpdateUsernameA e -> user . GT.userEmail .= e | |
| UpdateUserPasswordA p -> user . GT.userPassword .= GT.UnhashedPassword p | |
| LoginUserA -> do | |
| u <- use user | |
| tell [loginUser u] | |
| LoginResultA laction -> loginState %= (Login.transition laction) | |
| RegisterNewUserA -> uiState .= RegisterScreen | |
| NullActionA -> return () | |
| ------------------------------------------------------------------------------ | |
| -- App | |
| ------------------------------------------------------------------------------ | |
| app :: App MealPlanState MealPlanAction MealPlanRequest | |
| app = App | |
| { appInitialState = initialState | |
| , appInitialRequest = [] | |
| , appApplyAction = runApplyActionM . applyMealPlanAction | |
| } | |
| initialState :: MealPlanState | |
| initialState = MealPlanState LoginScreen GT.emptyUser (Login.LoggedOut Nothing) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment