diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-15 14:47:31 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2017-03-15 14:47:31 +0100 |
commit | fe5cd6ad6c61eb13ca99acd1b69cd09b84051404 (patch) | |
tree | 4afc8cb5ae4171047d6af17082fb74d49c726abe /Handler/Common.hs | |
parent | 668961c90368b55a3409ae93b96e288f8ebe33a4 (diff) | |
download | bar-fe5cd6ad6c61eb13ca99acd1b69cd09b84051404.tar bar-fe5cd6ad6c61eb13ca99acd1b69cd09b84051404.tar.gz bar-fe5cd6ad6c61eb13ca99acd1b69cd09b84051404.tar.bz2 bar-fe5cd6ad6c61eb13ca99acd1b69cd09b84051404.tar.xz bar-fe5cd6ad6c61eb13ca99acd1b69cd09b84051404.zip |
Support types
Diffstat (limited to 'Handler/Common.hs')
-rw-r--r-- | Handler/Common.hs | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/Handler/Common.hs b/Handler/Common.hs index 2416d15..1cf63de 100644 --- a/Handler/Common.hs +++ b/Handler/Common.hs | |||
@@ -18,6 +18,9 @@ import Data.Unique | |||
18 | 18 | ||
19 | import qualified Data.Text as Text (pack) | 19 | import qualified Data.Text as Text (pack) |
20 | 20 | ||
21 | import Data.Set (Set) | ||
22 | import qualified Data.Set as Set | ||
23 | |||
21 | import Control.Lens | 24 | import Control.Lens |
22 | 25 | ||
23 | import Handler.Common.Types | 26 | import Handler.Common.Types |
@@ -26,11 +29,14 @@ dayFormat :: Day -> String | |||
26 | dayFormat = formatTime defaultTimeLocale "%e. %b %y" | 29 | dayFormat = formatTime defaultTimeLocale "%e. %b %y" |
27 | 30 | ||
28 | itemForm :: Maybe Item -- ^ Update existing item or insert new? | 31 | itemForm :: Maybe Item -- ^ Update existing item or insert new? |
29 | -> Html -> MForm Handler (FormResult Item, Widget) | 32 | -> Html -> MForm Handler (FormResult (WithType Item), Widget) |
30 | itemForm proto identView = do | 33 | itemForm proto identView = do |
31 | today <- utctDay <$> liftIO getCurrentTime | 34 | today <- utctDay <$> liftIO getCurrentTime |
35 | |||
36 | t <- lift . runDB $ maybe (return Nothing) (fmap (Just . kindType) . getType) proto | ||
32 | 37 | ||
33 | (kindRes, kindWidget) <- kindField $ itemKind <$> proto | 38 | (kindRes, kindWidget) <- kindField $ itemKind <$> proto |
39 | (typeRes, typeWidget) <- typeField $ t | ||
34 | (boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown" | 40 | (boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown" |
35 | (expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never" | 41 | (expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never" |
36 | (openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never" | 42 | (openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never" |
@@ -40,13 +46,15 @@ itemForm proto identView = do | |||
40 | itemBought <- boughtRes | 46 | itemBought <- boughtRes |
41 | itemExpires <- expiresRes | 47 | itemExpires <- expiresRes |
42 | itemOpened <- openedRes | 48 | itemOpened <- openedRes |
43 | return Item{ itemNormKind = normalizeKind itemKind, ..} | 49 | t <- typeRes |
50 | return $ Item{ itemNormKind = normalizeKind itemKind, ..} `WithType` t | ||
44 | 51 | ||
45 | return . (itemRes, ) $ | 52 | return . (itemRes, ) $ |
46 | [whamlet| | 53 | [whamlet| |
47 | $newline never | 54 | $newline never |
48 | #{identView} | 55 | #{identView} |
49 | <div .td>^{kindWidget} | 56 | <div .td>^{kindWidget} |
57 | <div .td>^{typeWidget} | ||
50 | <div .td>^{boughtWidget} | 58 | <div .td>^{boughtWidget} |
51 | <div .td>^{expiresWidget} | 59 | <div .td>^{expiresWidget} |
52 | <div .td>^{openedWidget} | 60 | <div .td>^{openedWidget} |
@@ -81,19 +89,24 @@ inventoryListing :: InventoryState -> Widget | |||
81 | inventoryListing InventoryState{ invFormState = formState, ..} = $(widgetFile "inventoryListing") | 89 | inventoryListing InventoryState{ invFormState = formState, ..} = $(widgetFile "inventoryListing") |
82 | 90 | ||
83 | referenceForm :: Maybe Reference -- ^ Update existing item or insert new? | 91 | referenceForm :: Maybe Reference -- ^ Update existing item or insert new? |
84 | -> Html -> MForm Handler (FormResult Reference, Widget) | 92 | -> Html -> MForm Handler (FormResult (WithType Reference), Widget) |
85 | referenceForm proto identView = do | 93 | referenceForm proto identView = do |
94 | t <- lift . runDB $ maybe (return Nothing) (fmap (Just . kindType) . getType) proto | ||
95 | |||
86 | (kindRes, kindWidget) <- kindField $ referenceKind <$> proto | 96 | (kindRes, kindWidget) <- kindField $ referenceKind <$> proto |
97 | (typeRes, typeWidget) <- typeField $ t | ||
87 | 98 | ||
88 | let referenceRes = do | 99 | let referenceRes = do |
89 | referenceKind <- kindRes | 100 | referenceKind <- kindRes |
90 | return Reference{ referenceNormKind = normalizeKind referenceKind, .. } | 101 | t <- typeRes |
102 | return $ Reference{ referenceNormKind = normalizeKind referenceKind, .. } `WithType` t | ||
91 | 103 | ||
92 | return . (referenceRes, ) $ | 104 | return . (referenceRes, ) $ |
93 | [whamlet| | 105 | [whamlet| |
94 | $newline never | 106 | $newline never |
95 | #{identView} | 107 | #{identView} |
96 | <div .td>^{kindWidget} | 108 | <div .td>^{kindWidget} |
109 | <div .td>^{typeWidget} | ||
97 | |] | 110 | |] |
98 | 111 | ||
99 | referenceListing :: ReferenceState -> Widget | 112 | referenceListing :: ReferenceState -> Widget |
@@ -121,6 +134,28 @@ kindField proto = do | |||
121 | <option value=#{opt}> | 134 | <option value=#{opt}> |
122 | |] | 135 | |] |
123 | 136 | ||
137 | typeField :: Maybe Text -> MForm Handler (FormResult Text, Widget) | ||
138 | typeField proto = do | ||
139 | optionId <- ("options" <>) . tshow . hashUnique <$> liftIO newUnique | ||
140 | |||
141 | let | ||
142 | attrs = [ ("list", optionId) | ||
143 | , ("autocomplete", "off") | ||
144 | ] | ||
145 | |||
146 | (typeRes, typeView) <- mreq textField ("" { fsAttrs = attrs }) proto | ||
147 | |||
148 | (Set.fromList . map (kindType . entityVal) -> options) <- lift . runDB $ selectList [] [] | ||
149 | |||
150 | return . (typeRes, ) $ | ||
151 | [whamlet| | ||
152 | $newline never | ||
153 | ^{fvInput typeView} | ||
154 | <datalist ##{optionId}> | ||
155 | $forall opt <- Set.toList options | ||
156 | <option value=#{opt}> | ||
157 | |] | ||
158 | |||
124 | kinds :: Handler [Text] | 159 | kinds :: Handler [Text] |
125 | kinds = do | 160 | kinds = do |
126 | stock <- runDB $ selectList [] [] | 161 | stock <- runDB $ selectList [] [] |