summaryrefslogtreecommitdiff
path: root/Handler/Common.hs
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2017-03-14 01:06:28 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2017-03-14 01:06:28 +0100
commitd84b462a711ce95593ff05a7581e722562c3835a (patch)
tree41e5af455fea925b2680b29718b24ba2876e803a /Handler/Common.hs
downloadbar-d84b462a711ce95593ff05a7581e722562c3835a.tar
bar-d84b462a711ce95593ff05a7581e722562c3835a.tar.gz
bar-d84b462a711ce95593ff05a7581e722562c3835a.tar.bz2
bar-d84b462a711ce95593ff05a7581e722562c3835a.tar.xz
bar-d84b462a711ce95593ff05a7581e722562c3835a.zip
Implement old bar.hs
Diffstat (limited to 'Handler/Common.hs')
-rw-r--r--Handler/Common.hs78
1 files changed, 78 insertions, 0 deletions
diff --git a/Handler/Common.hs b/Handler/Common.hs
new file mode 100644
index 0000000..38fb1ce
--- /dev/null
+++ b/Handler/Common.hs
@@ -0,0 +1,78 @@
1{-# LANGUAGE ApplicativeDo #-}
2
3module Handler.Common
4 ( inventoryListing
5 , itemForm
6 , InventoryState(..)
7 , FormState(..)
8 ) where
9
10import Import
11
12import Data.Unique
13
14import qualified Data.Text as Text (pack)
15
16import Control.Lens
17
18import Handler.Common.Types
19
20dayFormat :: Day -> String
21dayFormat = formatTime defaultTimeLocale "%e. %b %y"
22
23itemForm :: Maybe Item -- ^ Update existing item or insert new?
24 -> Html -> MForm Handler (FormResult Item, Widget)
25itemForm proto identView = do
26 today <- utctDay <$> liftIO getCurrentTime
27
28 (kindRes, kindView) <- mreq textField "" $ itemKind <$> proto
29 (boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown"
30 (expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never"
31 (openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never"
32
33 let itemRes = do
34 itemKind <- kindRes
35 itemBought <- boughtRes
36 itemExpires <- expiresRes
37 itemOpened <- openedRes
38 return Item{ itemNormKind = normalizeKind itemKind, ..}
39
40 return . (itemRes, ) $
41 [whamlet|
42 $newline never
43 #{identView}
44 <div .td>^{fvInput kindView}
45 <div .td>^{boughtWidget}
46 <div .td>^{expiresWidget}
47 <div .td>^{openedWidget}
48 |]
49 where
50 dayForm :: Maybe (Maybe Day) -> String -> MForm Handler (FormResult (Maybe Day), Widget)
51 dayForm proto label = do
52 today <- utctDay <$> liftIO getCurrentTime
53
54 checkboxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique
55
56 (fmap (fromMaybe False) -> isNothingRes, isNothingView) <-
57 mopt checkBoxField ("" { fsId = Just $ Text.pack checkboxId }) . Just . Just . fromMaybe True $ fmap isNothing proto
58 (dayRes, dayView) <-
59 mreq dayField "" . Just . fromMaybe today $ join proto
60
61 let res = (bool Just (const Nothing) <$> isNothingRes) <*> dayRes
62 return . (res, ) $ do
63 [whamlet|
64 $newline never
65 <div .table>
66 <div .tr>
67 <label for=#{checkboxId} .checkbox .td>
68 ^{fvInput isNothingView}
69 <span>
70 #{label}
71 <div .tr>
72 <div .td .dayInput>^{fvInput dayView}
73 |]
74
75inventoryListing :: InventoryState -> Widget
76inventoryListing InventoryState{..} = do
77 setTitle "Bar Inventory"
78 $(widgetFile "inventoryListing")