summaryrefslogtreecommitdiff
path: root/Handler/UpdateItem.hs
blob: 353572b557fc6e3d374447aac6750f5e94ed0235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Handler.UpdateItem where

import Import

import Handler.Common

getUpdateItemR, postUpdateItemR :: ItemId -> Handler TypedContent
getUpdateItemR = postUpdateItemR
postUpdateItemR fsUpdateItem = do
  Just entity <- fmap (Entity fsUpdateItem) <$> runDB (get fsUpdateItem)
  
  ((updateResult, fsUpdateForm), fsUpdateEncoding) <- runFormPost . itemForm . Just $ entityVal entity

  mapM_ (addMessage "formError" . toHtml) =<< case updateResult of 
    FormSuccess Item{..} -> [] <$ runDB (update fsUpdateItem [ ItemKind =. itemKind
                                                             , ItemNormKind =. normalizeKind itemKind
                                                             , ItemBought =. itemBought
                                                             , ItemExpires =. itemExpires
                                                             , ItemOpened =. itemOpened
                                                             ])
    FormFailure errors -> return errors
    _ -> return []

  selectRep $ do
    provideRep $ case updateResult of
      FormSuccess _ -> redirect $ InventoryListingR :#: fsUpdateItem :: Handler Html
      _ -> do
        (sortOn entityVal -> stock) <- runDB $ selectList [] []
        defaultLayout $ inventoryListing InventoryState
          { formState = Just UpdateForm{..}
          , ..
          }
    provideJson ()