From d84b462a711ce95593ff05a7581e722562c3835a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 14 Mar 2017 01:06:28 +0100 Subject: Implement old bar.hs --- Handler/Common.hs | 78 +++++++++++++++++++++++++++++++++++++++++++++ Handler/Common/Types.hs | 23 +++++++++++++ Handler/DeleteItem.hs | 10 ++++++ Handler/InventoryListing.hs | 26 +++++++++++++++ Handler/Item.hs | 31 ++++++++++++++++++ Handler/OpenItem.hs | 12 +++++++ Handler/UpdateItem.hs | 33 +++++++++++++++++++ 7 files changed, 213 insertions(+) create mode 100644 Handler/Common.hs create mode 100644 Handler/Common/Types.hs create mode 100644 Handler/DeleteItem.hs create mode 100644 Handler/InventoryListing.hs create mode 100644 Handler/Item.hs create mode 100644 Handler/OpenItem.hs create mode 100644 Handler/UpdateItem.hs (limited to 'Handler') 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 @@ +{-# LANGUAGE ApplicativeDo #-} + +module Handler.Common + ( inventoryListing + , itemForm + , InventoryState(..) + , FormState(..) + ) 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, kindView) <- mreq textField "" $ 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} +
^{fvInput kindView} +
^{boughtWidget} +
^{expiresWidget} +
^{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 +
+
+