From 652e82fa62c7cc19590af62a147f423c89626652 Mon Sep 17 00:00:00 2001 From: Filip Gralinski Date: Wed, 30 Sep 2015 20:15:33 +0200 Subject: [PATCH] add "your account" option --- Application.hs | 1 + Foundation.hs | 1 + Handler/YourAccount.hs | 42 +++++++++++++++++++++++++++++++++ config/models | 1 + config/routes | 2 ++ gonito.cabal | 1 + messages/en.msg | 3 +++ templates/default-layout.hamlet | 1 + templates/your-account.hamlet | 7 ++++++ 9 files changed, 59 insertions(+) create mode 100644 Handler/YourAccount.hs create mode 100644 templates/your-account.hamlet diff --git a/Application.hs b/Application.hs index f56cac2..8621aba 100644 --- a/Application.hs +++ b/Application.hs @@ -40,6 +40,7 @@ import Handler.CreateChallenge import Handler.ListChallenges import Handler.ShowChallenge import Handler.Shared +import Handler.YourAccount -- This line actually creates our YesodDispatch instance. It is the second half -- of the call to mkYesodData which occurs in Foundation.hs. Please see the diff --git a/Foundation.hs b/Foundation.hs index 73df144..518bbfe 100644 --- a/Foundation.hs +++ b/Foundation.hs @@ -171,6 +171,7 @@ instance YesodAuth App where insert User { userIdent = credsIdent creds , userPassword = Nothing + , userName = Nothing } -- You can add other plugins like BrowserID, email or OAuth here diff --git a/Handler/YourAccount.hs b/Handler/YourAccount.hs new file mode 100644 index 0000000..45e19f3 --- /dev/null +++ b/Handler/YourAccount.hs @@ -0,0 +1,42 @@ +module Handler.YourAccount where + +import Import +import Yesod.Form.Bootstrap3 (BootstrapFormLayout (..), renderBootstrap3, + withSmallInput) + +import Handler.Shared +import Handler.Extract + +getYourAccountR :: Handler Html +getYourAccountR = do + userId <- requireAuthId + user <- runDB $ get404 userId + (formWidget, formEnctype) <- generateFormPost (yourAccountForm $ userName user) + let submission = Nothing :: Maybe (Import.FileInfo, Text) + handlerName = "getYourAccountR" :: Text + defaultLayout $ do + aDomId <- newIdent + setTitle "Your account" + $(widgetFile "your-account") + +postYourAccountR :: Handler Html +postYourAccountR = do + ((result, formWidget), formEnctype) <- runFormPost (yourAccountForm Nothing) + let handlerName = "postYourAccountR" :: Text + accountData = case result of + FormSuccess res -> Just res + _ -> Nothing + Just (name, aboutMe) = accountData + userId <- requireAuthId + runDB $ update userId [UserName =. name] + user <- runDB $ get404 userId + defaultLayout $ do + aDomId <- newIdent + setTitle "Your account" + $(widgetFile "your-account") + + +yourAccountForm :: Maybe Text -> Form (Maybe Text, Maybe Text) +yourAccountForm maybeName = renderBootstrap3 BootstrapBasicForm $ (,) + <$> aopt textField (fieldSettingsLabel MsgAccountName) (Just maybeName) + <*> aopt textField (fieldSettingsLabel MsgAboutMe) Nothing diff --git a/config/models b/config/models index e9dd15c..2017d2f 100644 --- a/config/models +++ b/config/models @@ -2,6 +2,7 @@ User ident Text password Text Maybe UniqueUser ident + name Text Maybe deriving Typeable Email email Text diff --git a/config/routes b/config/routes index 8ad0eba..12153f6 100644 --- a/config/routes +++ b/config/routes @@ -16,3 +16,5 @@ /challenge-submission/#Text ChallengeSubmissionR GET POST /challenge-my-submissions/#Text ChallengeMySubmissionsR GET /challenge-all-submissions/#Text ChallengeAllSubmissionsR GET + +/account YourAccountR GET POST diff --git a/gonito.cabal b/gonito.cabal index 668ff9d..4eec42f 100644 --- a/gonito.cabal +++ b/gonito.cabal @@ -30,6 +30,7 @@ library Handler.Shared Handler.ShowChallenge Handler.Extract + Handler.YourAccount if flag(dev) || flag(library-only) cpp-options: -DDEVELOPMENT diff --git a/messages/en.msg b/messages/en.msg index a9a5312..2c9351b 100644 --- a/messages/en.msg +++ b/messages/en.msg @@ -13,3 +13,6 @@ Submit: Submit SubmissionUrl: Submission repo URL SubmissionBranch: Submission repo branch SubmissionDescription: Submission description +YourAccount: your account +AboutMe: about me +AccountName: name diff --git a/templates/default-layout.hamlet b/templates/default-layout.hamlet index 62140c8..5e9f5bf 100644 --- a/templates/default-layout.hamlet +++ b/templates/default-layout.hamlet @@ -3,6 +3,7 @@ _{MsgLoggedAs} #{userIdent $ entityVal user} \ | _{MsgCreateChallenge} \ | _{MsgListChallenges} + \ | _{MsgYourAccount} \ | _{MsgLogOut} $nothing _{MsgLogIn} diff --git a/templates/your-account.hamlet b/templates/your-account.hamlet new file mode 100644 index 0000000..cfe9c03 --- /dev/null +++ b/templates/your-account.hamlet @@ -0,0 +1,7 @@ +

Your account + +

+

+ ^{formWidget} +