summaryrefslogtreecommitdiff
path: root/Handler/Employment.hs
blob: ce31dd18cdc37e8923efcc4a0b80c52046339751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE TupleSections, OverloadedStrings #-}
module Handler.Employment where

import Import
import qualified Data.Text as T
import Yesod.Form.Jquery (jqueryDayField, def)

getEmploymentR :: EmploymentId -> Handler RepHtml
getEmploymentR eid = do
    emp <- runDB $ get404 eid
    user <- runDB $ get404 (employmentUser emp)
    let username = userIdent user
    (formWidget, formEnctype) <- generateFormPost (employmentForm emp)
    defaultLayout $ do
        aDomId <- newIdent
        (setTitle . toHtml) ("Employment span for " `T.append` username)
        $(widgetFile "employmentpage")

postEmploymentR :: EmploymentId -> Handler RepHtml
postEmploymentR eid = do
    emp <- runDB $ get404 eid
    user <- runDB $ get404 (employmentUser emp)
    let username = userIdent user
    ((result, formWidget), formEnctype) <- runFormPost (employmentForm emp)
    _ <- case result of
            FormSuccess res -> (runDB $ replace eid res) >> return ()
            _ -> return ()

    defaultLayout $ do
        aDomId <- newIdent
        (setTitle . toHtml) ("Employment span for " `T.append` username)
        $(widgetFile "employmentpage")

employmentAForm :: Employment -> AForm Handler Employment
employmentAForm tc = Employment
    <$> pure (employmentUser tc)
    <*> areq (jqueryDayField def) "Start date" (Just (employmentStartDate tc))
    <*> aopt (jqueryDayField def) "End date" (Just (employmentEndDate tc))
    <*> areq doubleField "Commitment" (Just (employmentCommitment tc))

employmentForm :: Employment -> Html -> MForm Handler (FormResult Employment, Widget)
employmentForm tc h = renderDivs (employmentAForm tc) h