Show membership status on profile page

This commit is contained in:
Saku Laesvuori 2023-09-28 11:00:26 +03:00
parent fb69b58f92
commit 7f64d8f926
Signed by: slaesvuo
GPG Key ID: 257D284A2A1D3A32
1 changed files with 17 additions and 7 deletions

View File

@ -19,6 +19,7 @@ import Datarekisteri.Frontend.Types
import Datarekisteri.Frontend.FormFields import Datarekisteri.Frontend.FormFields
import Data.Morpheus.Client import Data.Morpheus.Client
import Datarekisteri.Core.Types import Datarekisteri.Core.Types
import Data.Maybe (fromJust)
import Yesod hiding (emailField) import Yesod hiding (emailField)
import Yesod.Auth import Yesod.Auth
@ -33,6 +34,7 @@ query ProfilePage($id: UserID) {
homeplace homeplace
birthdate birthdate
phoneNumber phoneNumber
isMember
} }
permissions permissions
} }
@ -92,14 +94,18 @@ profileForm userID user extraHtml = do
|] |]
return (profileUpdateRes, widget) return (profileUpdateRes, widget)
profile :: UserID -> (Widget, Enctype) -> (Widget, Enctype) -> Widget profile :: ProfilePageUser -> (Widget, Enctype) -> (Widget, Enctype) -> Widget
profile user (profileWidget, profileEnctype) (passwordWidget, passwordEnctype) = do profile user (profileWidget, profileEnctype) (passwordWidget, passwordEnctype) = do
setTitle "Muokkaa profiilia" setTitle "Muokkaa profiilia"
passwordRoute <- handlerToWidget $ maybeAuthorized (UpdatePasswordR user) True let userID = let ProfilePageUser {..} = user in id
passwordRoute <- handlerToWidget $ maybeAuthorized (UpdatePasswordR userID) True
[whamlet| [whamlet|
<h1> <h1>
Omat tiedot $if isMember user
<form action="@{ProfileR user}" method="post" enctype="#{profileEnctype}"> Jäsentiedot
$else
Jäsenhakemuksen tiedot
<form action="@{ProfileR userID}" method="post" enctype="#{profileEnctype}">
^{profileWidget} ^{profileWidget}
<input type="submit" value="Päivitä tiedot"> <input type="submit" value="Päivitä tiedot">
$maybe route <- passwordRoute $maybe route <- passwordRoute
@ -114,7 +120,7 @@ getProfile userID = do
passwordForm <- liftHandler $ generateFormPost passwordForm passwordForm <- liftHandler $ generateFormPost passwordForm
profileForm <- liftHandler $ generateFormPost $ profileForm <- liftHandler $ generateFormPost $
profileForm ((\x -> let ProfilePageUser {..} = x in id) <$> user) user profileForm ((\x -> let ProfilePageUser {..} = x in id) <$> user) user
defaultLayout $ profile ((\x -> let ProfilePageUser {..} = x in id) $ fromJust user) defaultLayout $ profile (fromJust user)
profileForm passwordForm profileForm passwordForm
where fromJust = fromMaybe $ error "Tried to access the profile of an inexistent user" where fromJust = fromMaybe $ error "Tried to access the profile of an inexistent user"
@ -131,7 +137,9 @@ postProfileR userID = do
FormSuccess update -> apiRequest @UpdateProfile True update >> redirect (ProfileR userID) FormSuccess update -> apiRequest @UpdateProfile True update >> redirect (ProfileR userID)
_ -> do _ -> do
passwordForm <- liftHandler $ generateFormPost passwordForm passwordForm <- liftHandler $ generateFormPost passwordForm
defaultLayout $ profile userID (widget, enctype) passwordForm ProfilePage {..} <- apiRequest True (ProfilePageArgs {id = Just userID})
defaultLayout $ profile (fromJust user) (widget, enctype) passwordForm
--- XXX fromJust explodes if the user no longer exists
postUpdatePasswordR :: (YesodAuth DataIdClient, AuthId DataIdClient ~ Text) => UserID -> Handler Html postUpdatePasswordR :: (YesodAuth DataIdClient, AuthId DataIdClient ~ Text) => UserID -> Handler Html
postUpdatePasswordR userID = do postUpdatePasswordR userID = do
@ -141,4 +149,6 @@ postUpdatePasswordR userID = do
apiRequest @UpdatePassword True (UpdatePasswordArgs {password = new, user = Just userID}) >> redirect (ProfileR userID) apiRequest @UpdatePassword True (UpdatePasswordArgs {password = new, user = Just userID}) >> redirect (ProfileR userID)
_ -> do _ -> do
profileForm <- liftHandler $ generateFormPost $ profileForm (Just userID) Nothing profileForm <- liftHandler $ generateFormPost $ profileForm (Just userID) Nothing
defaultLayout $ profile userID profileForm (widget, enctype) ProfilePage {..} <- apiRequest True (ProfilePageArgs {id = Just userID})
defaultLayout $ profile (fromJust user) profileForm (widget, enctype)
--- XXX fromJust explodes if the user no longer exists