{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}

module Hledger.Web.Handler.EditR
  ( getEditR
  , postEditR
  ) where

import Hledger.Web.Import
import Hledger.Web.Widget.Common
       (fromFormSuccess, helplink, journalFile404, writeJournalTextIfValidAndChanged)

editForm :: FilePath -> Text -> Markup -> MForm Handler (FormResult Text, Widget)
editForm :: FilePath
-> Text -> Markup -> MForm Handler (FormResult Text, Widget)
editForm FilePath
f Text
txt =
  Text
-> (Markup
    -> MForm
         Handler (FormResult Text, WidgetFor (HandlerSite Handler) ()))
-> Markup
-> MForm
     Handler (FormResult Text, WidgetFor (HandlerSite Handler) ())
forall (m :: * -> *) a.
Monad m =>
Text
-> (Markup -> MForm m (FormResult a, WidgetFor (HandlerSite m) ()))
-> Markup
-> MForm m (FormResult a, WidgetFor (HandlerSite m) ())
identifyForm Text
"edit" ((Markup
  -> MForm
       Handler (FormResult Text, WidgetFor (HandlerSite Handler) ()))
 -> Markup
 -> MForm
      Handler (FormResult Text, WidgetFor (HandlerSite Handler) ()))
-> (Markup
    -> MForm
         Handler (FormResult Text, WidgetFor (HandlerSite Handler) ()))
-> Markup
-> MForm
     Handler (FormResult Text, WidgetFor (HandlerSite Handler) ())
forall a b. (a -> b) -> a -> b
$ \Markup
extra -> do
    (FormResult Textarea
tRes, FieldView App
tView) <- Field Handler Textarea
-> FieldSettings App
-> Maybe Textarea
-> MForm Handler (FormResult Textarea, FieldView App)
forall site (m :: * -> *) a.
(RenderMessage site FormMessage, HandlerSite m ~ site,
 MonadHandler m) =>
Field m a
-> FieldSettings site
-> Maybe a
-> MForm m (FormResult a, FieldView site)
mreq Field Handler Textarea
forall (m :: * -> *).
(Monad m, RenderMessage (HandlerSite m) FormMessage) =>
Field m Textarea
textareaField FieldSettings App
forall master. FieldSettings master
fs (Textarea -> Maybe Textarea
forall a. a -> Maybe a
Just (Text -> Textarea
Textarea Text
txt))
    (FormResult Text, Widget)
-> RWST
     (Maybe (Map Text [Text], Map Text [FileInfo]), App, [Text])
     Enctype
     Ints
     Handler
     (FormResult Text, Widget)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Textarea -> Text
unTextarea (Textarea -> Text) -> FormResult Textarea -> FormResult Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FormResult Textarea
tRes, $(widgetFile "edit-form"))
  where
    fs :: FieldSettings master
fs = SomeMessage master
-> Maybe (SomeMessage master)
-> Maybe Text
-> Maybe Text
-> [(Text, Text)]
-> FieldSettings master
forall master.
SomeMessage master
-> Maybe (SomeMessage master)
-> Maybe Text
-> Maybe Text
-> [(Text, Text)]
-> FieldSettings master
FieldSettings SomeMessage master
"text" Maybe (SomeMessage master)
forall (m :: * -> *) a. MonadPlus m => m a
mzero Maybe Text
forall (m :: * -> *) a. MonadPlus m => m a
mzero Maybe Text
forall (m :: * -> *) a. MonadPlus m => m a
mzero [(Text
"class", Text
"form-control"), (Text
"rows", Text
"25")]

getEditR :: FilePath -> Handler ()
getEditR :: FilePath -> Handler ()
getEditR FilePath
f = do
  Handler ()
checkServerSideUiEnabled
  FilePath -> Handler ()
postEditR FilePath
f

postEditR :: FilePath -> Handler ()
postEditR :: FilePath -> Handler ()
postEditR FilePath
f = do
  Handler ()
checkServerSideUiEnabled
  VD {[Capability]
caps :: ViewData -> [Capability]
caps :: [Capability]
caps, Journal
j :: ViewData -> Journal
j :: Journal
j} <- Handler ViewData
getViewData
  Bool -> Handler () -> Handler ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Capability
CapManage Capability -> [Capability] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Capability]
caps) (Text -> Handler ()
forall (m :: * -> *) a. MonadHandler m => Text -> m a
permissionDenied Text
"Missing the 'manage' capability")

  (FilePath
f', Text
txt) <- FilePath -> Journal -> HandlerFor App (FilePath, Text)
forall m. FilePath -> Journal -> HandlerFor m (FilePath, Text)
journalFile404 FilePath
f Journal
j
  ((FormResult Text
res, Widget
view), Enctype
enctype) <- (Markup -> MForm Handler (FormResult Text, Widget))
-> Handler ((FormResult Text, Widget), Enctype)
forall (m :: * -> *) a xml.
(RenderMessage (HandlerSite m) FormMessage, MonadResource m,
 MonadHandler m) =>
(Markup -> MForm m (FormResult a, xml))
-> m ((FormResult a, xml), Enctype)
runFormPost (FilePath
-> Text -> Markup -> MForm Handler (FormResult Text, Widget)
editForm FilePath
f' Text
txt)
  Text
newtxt <- HandlerFor App Text -> FormResult Text -> HandlerFor App Text
forall (m :: * -> *) a. Applicative m => m a -> FormResult a -> m a
fromFormSuccess (Widget -> Enctype -> HandlerFor App Text
forall site a a c.
(Yesod site, ToMarkup a, ToWidget site a) =>
a -> a -> HandlerFor site c
showForm Widget
view Enctype
enctype) FormResult Text
res
  FilePath -> Text -> HandlerFor App (Either FilePath ())
forall (m :: * -> *).
MonadHandler m =>
FilePath -> Text -> m (Either FilePath ())
writeJournalTextIfValidAndChanged FilePath
f Text
newtxt HandlerFor App (Either FilePath ())
-> (Either FilePath () -> Handler ()) -> Handler ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Left FilePath
e -> do
      Markup -> Handler ()
forall (m :: * -> *). MonadHandler m => Markup -> m ()
setMessage (Markup -> Handler ()) -> Markup -> Handler ()
forall a b. (a -> b) -> a -> b
$ Markup
"Failed to load journal: " Markup -> Markup -> Markup
forall a. Semigroup a => a -> a -> a
<> FilePath -> Markup
forall a. ToMarkup a => a -> Markup
toHtml FilePath
e
      Widget -> Enctype -> Handler ()
forall site a a c.
(Yesod site, ToMarkup a, ToWidget site a) =>
a -> a -> HandlerFor site c
showForm Widget
view Enctype
enctype
    Right () -> do
      Markup -> Handler ()
forall (m :: * -> *). MonadHandler m => Markup -> m ()
setMessage (Markup -> Handler ()) -> Markup -> Handler ()
forall a b. (a -> b) -> a -> b
$ Markup
"Saved journal " Markup -> Markup -> Markup
forall a. Semigroup a => a -> a -> a
<> FilePath -> Markup
forall a. ToMarkup a => a -> Markup
toHtml FilePath
f Markup -> Markup -> Markup
forall a. Semigroup a => a -> a -> a
<> Markup
"\n"
      Route App -> Handler ()
forall (m :: * -> *) url a.
(MonadHandler m, RedirectUrl (HandlerSite m) url) =>
url -> m a
redirect Route App
JournalR
  where
    showForm :: a -> a -> HandlerFor site c
showForm a
view a
enctype =
      Markup -> HandlerFor site c
forall (m :: * -> *) c a.
(MonadHandler m, ToTypedContent c) =>
c -> m a
sendResponse (Markup -> HandlerFor site c)
-> (WidgetFor site () -> HandlerFor site Markup)
-> WidgetFor site ()
-> HandlerFor site c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< WidgetFor site () -> HandlerFor site Markup
forall site.
Yesod site =>
WidgetFor site () -> HandlerFor site Markup
defaultLayout (WidgetFor site () -> HandlerFor site c)
-> WidgetFor site () -> HandlerFor site c
forall a b. (a -> b) -> a -> b
$ do
        Markup -> WidgetFor site ()
forall (m :: * -> *). MonadWidget m => Markup -> m ()
setTitle Markup
"Edit journal"
        [whamlet|<form method=post enctype=#{enctype}>^{view}|]