{-# LANGUAGE ApplicativeDo #-} module Handler.Common ( inventoryListing , itemForm , referenceListing , referenceForm , kinds , InventoryState(..) , ReferenceState(..) , FormState(..) , HasFormState(..) , stockSort, referenceSort ) where import Import import Data.Unique import qualified Data.Text as Text import Data.Set (Set) import qualified Data.Set as Set 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 (WithType Item), Widget) itemForm proto identView = do today <- utctDay <$> liftIO getCurrentTime t <- lift . runDB $ maybe (return Nothing) (fmap (Just . kindType) . getType) proto (kindRes, kindWidget) <- kindField $ itemKind <$> proto (typeRes, typeWidget) <- typeField $ t (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 t <- typeRes return $ Item{ itemNormKind = normalizeKind itemKind, ..} `WithType` t return . (itemRes, ) $ [whamlet| $newline never #{identView}