summaryrefslogtreecommitdiff
path: root/Handler/Employment.hs
blob: a69b67a67bf9bb1cb4ea445a789479911c484a0f (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
43
{-# LANGUAGE TupleSections, OverloadedStrings #-}
module Handler.Employment where

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

getEmploymentR :: EmploymentId -> Handler Html
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 Html
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))
    <*> aopt doubleField "Custom anniversary max (0 = infinite)" (Just (employmentPtoMaxRollover tc))

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