From 7bc954b779a9bc4e1c5e60f2648101c62ed22e72 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 14 Mar 2017 18:33:42 +0100 Subject: Reference & list --- Application.hs | 5 +++ Foundation.hs | 11 ++++++ Handler/Common.hs | 64 ++++++++++++++++++++++++++++++++--- Handler/Common/Types.hs | 23 +++++++++++-- Handler/DeleteRefItem.hs | 10 ++++++ Handler/InventoryListing.hs | 2 +- Handler/Item.hs | 10 +++--- Handler/Kinds.hs | 8 +++++ Handler/List.hs | 71 +++++++++++++++++++++++++++++++++++++++ Handler/ReferenceItem.hs | 25 ++++++++++++++ Handler/ReferenceListing.hs | 27 +++++++++++++++ Handler/UpdateItem.hs | 20 +++++------ Settings.hs | 7 ++++ bar.cabal | 6 ++++ bar.nix | 12 +++---- config/routes | 6 ++++ config/settings.yml | 2 ++ shell.nix | 2 +- stack.yaml | 2 +- templates/default-layout.cassius | 44 +++++++++++++++--------- templates/inventoryListing.hamlet | 4 +-- templates/referenceListing.hamlet | 17 ++++++++++ 22 files changed, 327 insertions(+), 51 deletions(-) create mode 100644 Handler/DeleteRefItem.hs create mode 100644 Handler/Kinds.hs create mode 100644 Handler/List.hs create mode 100644 Handler/ReferenceItem.hs create mode 100644 Handler/ReferenceListing.hs create mode 100644 templates/referenceListing.hamlet diff --git a/Application.hs b/Application.hs index 048a316..85ceb2f 100644 --- a/Application.hs +++ b/Application.hs @@ -37,6 +37,11 @@ import Handler.UpdateItem import Handler.OpenItem import Handler.DeleteItem import Handler.Item +import Handler.ReferenceListing +import Handler.ReferenceItem +import Handler.DeleteRefItem +import Handler.Kinds +import Handler.List -- This line actually creates our YesodDispatch instance. It is the second half -- of the call to mkYesodData which occurs in Foundation.hs. Please see the diff --git a/Foundation.hs b/Foundation.hs index d192c08..d7425d5 100644 --- a/Foundation.hs +++ b/Foundation.hs @@ -78,6 +78,13 @@ instance Yesod App where -- Define the menu items of the header. let menuItems = [ MenuItem "Inventory" InventoryListingR + , MenuItem "Reference" ReferenceListingR + , MenuItem "List" ListR + ] + currentMenu = listToMaybe + [ menuItemLabel + | MenuItem{..} <- menuItems + , Just menuItemRoute == mCurrentRoute ] -- We break up the default layout into two components: @@ -87,6 +94,10 @@ instance Yesod App where -- you to use normal widget features in default-layout. pc <- widgetToPageContent $ do + setTitle . toHtml . maybe "Bar Inventory" ("Bar Inventory – " <>) $ do + cM <- currentMenu + guard $ cM /= "Inventory" + return cM addScript $ StaticR jquery_js addScript $ StaticR webshim_polyfiller_js $(widgetFile "default-layout") diff --git a/Handler/Common.hs b/Handler/Common.hs index 38fb1ce..2416d15 100644 --- a/Handler/Common.hs +++ b/Handler/Common.hs @@ -3,8 +3,13 @@ module Handler.Common ( inventoryListing , itemForm + , referenceListing + , referenceForm + , kinds , InventoryState(..) + , ReferenceState(..) , FormState(..) + , HasFormState(..) ) where import Import @@ -25,7 +30,7 @@ itemForm :: Maybe Item -- ^ Update existing item or insert new? itemForm proto identView = do today <- utctDay <$> liftIO getCurrentTime - (kindRes, kindView) <- mreq textField "" $ itemKind <$> proto + (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" @@ -41,7 +46,7 @@ itemForm proto identView = do [whamlet| $newline never #{identView} -
^{fvInput kindView} +
^{kindWidget}
^{boughtWidget}
^{expiresWidget}
^{openedWidget} @@ -73,6 +78,55 @@ itemForm proto identView = do |] inventoryListing :: InventoryState -> Widget -inventoryListing InventoryState{..} = do - setTitle "Bar Inventory" - $(widgetFile "inventoryListing") +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} +
^{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} + + $forall opt <- options +