{-# 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}
^{kindWidget}
^{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