Debian Codegraph @

Hledger/Web/Handler/EditR.hs

raw
1 {-# LANGUAGE LambdaCase #-}
2 {-# LANGUAGE NamedFieldPuns #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TemplateHaskell #-}
7
8 module Hledger.Web.Handler.EditR
9 ( getEditR
10 , postEditR
11 ) where
12
13 import Control.Monad.Except (runExceptT)
14 import Hledger.Web.Import
15 import Hledger.Web.Widget.Common
16 (fromFormSuccess, helplink, journalFile404, writeJournalTextIfValidAndChanged)
17
18 editForm :: FilePath -> Text -> Form Text
19 editForm f txt =
20 identifyForm "edit" $ \extra -> do
21 (tRes, tView) <- mreq textareaField fs (Just (Textarea txt))
22 pure (unTextarea <$> tRes, $(widgetFile "edit-form"))
23 where
24 fs = FieldSettings "text" mzero mzero mzero [("class", "form-control"), ("rows", "25")]
25
26 getEditR :: FilePath -> Handler ()
27 getEditR f = do
28 checkServerSideUiEnabled
29 postEditR f
30
31 postEditR :: FilePath -> Handler ()
32 postEditR f = do
33 checkServerSideUiEnabled
34 VD {j} <- getViewData
35 require EditPermission
36
37 (f', txt) <- journalFile404 f j
38 ((res, view), enctype) <- runFormPost (editForm f' txt)
39 newtxt <- fromFormSuccess (showForm view enctype) res
40 runExceptT (writeJournalTextIfValidAndChanged f newtxt) >>= \case
41 Left e -> do
42 setMessage $ "Failed to load journal: " <> toHtml e
43 showForm view enctype
44 Right () -> do
45 setMessage $ "Saved journal " <> toHtml f <> "\n"
46 redirect JournalR
47 where
48 showForm view enctype =
49 sendResponse <=< defaultLayout $ do
50 setTitle "Edit journal"
51 [whamlet|<form method=post enctype=#{enctype}>^{view}|]