Commit 8ef4d966 authored by Joris's avatar Joris

Refactor income form component concerning payload creation

parent 62f990c9
......@@ -5,7 +5,8 @@ module View.Income.Form
) where
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (ToJSON)
import Data.Aeson (Value)
import qualified Data.Aeson as Aeson
import qualified Data.Maybe as Maybe
import Data.Text (Text)
import qualified Data.Text as T
......@@ -16,7 +17,8 @@ import qualified Data.Validation as V
import Reflex.Dom (Dynamic, Event, MonadWidget)
import qualified Reflex.Dom as R
import Common.Model (EditIncomeForm (..), Income (..))
import Common.Model (CreateIncomeForm (..),
EditIncomeForm (..), Income (..))
import qualified Common.Msg as Msg
import qualified Common.Util.Time as TimeUtil
import qualified Common.Validation.Income as IncomeValidation
......@@ -25,16 +27,16 @@ import qualified Component.Modal as Modal
import qualified Component.ModalForm as ModalForm
import qualified Util.Ajax as Ajax
data In t a = In
{ _in_operation :: Operation a
data In t = In
{ _in_operation :: Operation
}
data Operation a
= New (Text -> Text -> a)
| Clone (Text -> Text -> a) Income
| Edit (Text -> Text -> a) Income
data Operation
= New
| Clone Income
| Edit Income
view :: forall t m a. (MonadWidget t m, ToJSON a) => In t a -> Modal.Content t m Income
view :: forall t m a. MonadWidget t m => In t -> Modal.Content t m Income
view input cancel = do
rec
......@@ -57,7 +59,7 @@ view input cancel = do
form
:: Event t String
-> Event t ()
-> m (Dynamic t (Validation Text a))
-> m (Dynamic t (Validation Text Value))
form reset confirm = do
amount <- Input._out_raw <$> (Input.view
(Input.defaultIn
......@@ -92,30 +94,26 @@ view input cancel = do
amount =
case op of
New _ -> ""
Clone _ income -> T.pack . show . _income_amount $ income
Edit _ income -> T.pack . show . _income_amount $ income
New -> ""
Clone income -> T.pack . show . _income_amount $ income
Edit income -> T.pack . show . _income_amount $ income
date currentDay =
case op of
New _ -> currentDay
Clone _ _ -> currentDay
Edit _ income -> _income_date income
Edit income -> _income_date income
_ -> currentDay
ajax =
case op of
New _ -> Ajax.post
Clone _ _ -> Ajax.post
Edit _ _ -> Ajax.put
Edit _ -> Ajax.put
_ -> Ajax.post
headerLabel =
case op of
New _ -> Msg.get Msg.Income_AddLong
Clone _ _ -> Msg.get Msg.Income_AddLong
Edit _ _ -> Msg.get Msg.Income_Edit
Edit _ -> Msg.get Msg.Income_Edit
_ -> Msg.get Msg.Income_AddLong
mkPayload =
case op of
New f -> f
Clone f _ -> f
Edit f _ -> f
Edit income -> \a b -> Aeson.toJSON $ EditIncomeForm (_income_id income) a b
_ -> \a b -> Aeson.toJSON $ CreateIncomeForm a b
......@@ -11,8 +11,7 @@ import qualified Data.Time.Clock as Clock
import Reflex.Dom (Dynamic, Event, MonadWidget)
import qualified Reflex.Dom as R
import Common.Model (CreateIncomeForm (..), Currency,
Income (..), User (..))
import Common.Model (Currency, Income (..), User (..))
import qualified Common.Model as CM
import qualified Common.Msg as Msg
import qualified Common.View.Format as Format
......@@ -73,7 +72,7 @@ view input =
addIncome <- Modal.view $ Modal.In
{ Modal._in_show = addIncome
, Modal._in_content = Form.view $ Form.In { Form._in_operation = Form.New CreateIncomeForm }
, Modal._in_content = Form.view $ Form.In { Form._in_operation = Form.New }
}
return $ Out
......
......@@ -11,9 +11,8 @@ import qualified Data.Text as T
import Reflex.Dom (Dynamic, Event, MonadWidget)
import qualified Reflex.Dom as R
import Common.Model (CreateIncomeForm (..), Currency,
EditIncomeForm (..), Income (..),
User (..), UserId)
import Common.Model (Currency, Income (..), User (..),
UserId)
import qualified Common.Model as CM
import qualified Common.Msg as Msg
import qualified Common.View.Format as Format
......@@ -50,11 +49,11 @@ view input = do
, Table._in_resetPage = _in_resetPage input
, Table._in_cloneModal = \income ->
Form.view $ Form.In
{ Form._in_operation = Form.Clone CreateIncomeForm income
{ Form._in_operation = Form.Clone income
}
, Table._in_editModal = \income ->
Form.view $ Form.In
{ Form._in_operation = Form.Edit (EditIncomeForm $ _income_id income) income
{ Form._in_operation = Form.Edit income
}
, Table._in_deleteModal = \income ->
ConfirmDialog.view $ ConfirmDialog.In
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment