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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
{-# LANGUAGE ApplicativeDo #-}
module Handler.Common
( inventoryListing
, itemForm
, referenceListing
, referenceForm
, kinds
, InventoryState(..)
, ReferenceState(..)
, FormState(..)
, HasFormState(..)
) where
import Import
import Data.Unique
import qualified Data.Text as Text (pack)
import Control.Lens
import Handler.Common.Types
dayFormat :: Day -> String
dayFormat = formatTime defaultTimeLocale "%e. %b %y"
itemForm :: Maybe Item -- ^ Update existing item or insert new?
-> Html -> MForm Handler (FormResult Item, Widget)
itemForm proto identView = do
today <- utctDay <$> liftIO getCurrentTime
(kindRes, kindWidget) <- kindField $ itemKind <$> proto
(boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown"
(expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never"
(openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never"
let itemRes = do
itemKind <- kindRes
itemBought <- boughtRes
itemExpires <- expiresRes
itemOpened <- openedRes
return Item{ itemNormKind = normalizeKind itemKind, ..}
return . (itemRes, ) $
[whamlet|
$newline never
#{identView}
<div .td>^{kindWidget}
<div .td>^{boughtWidget}
<div .td>^{expiresWidget}
<div .td>^{openedWidget}
|]
where
dayForm :: Maybe (Maybe Day) -> String -> MForm Handler (FormResult (Maybe Day), Widget)
dayForm proto label = do
today <- utctDay <$> liftIO getCurrentTime
checkboxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique
(fmap (fromMaybe False) -> isNothingRes, isNothingView) <-
mopt checkBoxField ("" { fsId = Just $ Text.pack checkboxId }) . Just . Just . fromMaybe True $ fmap isNothing proto
(dayRes, dayView) <-
mreq dayField "" . Just . fromMaybe today $ join proto
let res = (bool Just (const Nothing) <$> isNothingRes) <*> dayRes
return . (res, ) $ do
[whamlet|
$newline never
<div .table>
<div .tr>
<label for=#{checkboxId} .checkbox .td>
^{fvInput isNothingView}
<span>
#{label}
<div .tr>
<div .td .dayInput>^{fvInput dayView}
|]
inventoryListing :: InventoryState -> Widget
inventoryListing InventoryState{ invFormState = formState, ..} = $(widgetFile "inventoryListing")
referenceForm :: Maybe Reference -- ^ Update existing item or insert new?
-> Html -> MForm Handler (FormResult Reference, Widget)
referenceForm proto identView = do
(kindRes, kindWidget) <- kindField $ referenceKind <$> proto
let referenceRes = do
referenceKind <- kindRes
return Reference{ referenceNormKind = normalizeKind referenceKind, .. }
return . (referenceRes, ) $
[whamlet|
$newline never
#{identView}
<div .td>^{kindWidget}
|]
referenceListing :: ReferenceState -> Widget
referenceListing ReferenceState{ refFormState = formState, ..} = $(widgetFile "referenceListing")
kindField :: Maybe Text -> MForm Handler (FormResult Text, Widget)
kindField proto = do
optionId <- ("options" <>) . tshow . hashUnique <$> liftIO newUnique
let
attrs = [ ("list", optionId)
, ("autocomplete", "off")
]
(kindRes, kindView) <- mreq textField ("" { fsAttrs = attrs }) proto
options <- lift kinds
return . (kindRes, ) $
[whamlet|
$newline never
^{fvInput kindView}
<datalist ##{optionId}>
$forall opt <- options
<option value=#{opt}>
|]
kinds :: Handler [Text]
kinds = do
stock <- runDB $ selectList [] []
reference <- runDB $ selectList [] []
return $ concat
[ [ itemKind | Entity _ Item{..} <- stock ]
, [ referenceKind | Entity _ Reference{..} <- reference ]
]
|