aboutsummaryrefslogtreecommitdiff
path: root/client/src/View/Income/Reducer.hs
diff options
context:
space:
mode:
authorJoris2019-11-24 16:19:53 +0100
committerJoris2019-11-24 16:19:53 +0100
commit54628c70cb33de5e4309c35b9f6b57bbe9f7a07b (patch)
tree57e331cadfdf81b5598d21f76302f5269fd58344 /client/src/View/Income/Reducer.hs
parent3c67fcf1d524811a18f0c4db3ef6eed1270b9a12 (diff)
downloadbudget-54628c70cb33de5e4309c35b9f6b57bbe9f7a07b.tar.gz
budget-54628c70cb33de5e4309c35b9f6b57bbe9f7a07b.tar.bz2
budget-54628c70cb33de5e4309c35b9f6b57bbe9f7a07b.zip
Compute cumulative income with a DB query
Diffstat (limited to 'client/src/View/Income/Reducer.hs')
-rw-r--r--client/src/View/Income/Reducer.hs40
1 files changed, 19 insertions, 21 deletions
diff --git a/client/src/View/Income/Reducer.hs b/client/src/View/Income/Reducer.hs
index 092d9b3..391890f 100644
--- a/client/src/View/Income/Reducer.hs
+++ b/client/src/View/Income/Reducer.hs
@@ -11,53 +11,51 @@ import qualified Reflex.Dom as R
import Common.Model (IncomePage)
-import Loadable (Loadable (..))
-import qualified Loadable as Loadable
+import Loadable (Loadable2 (..))
import qualified Util.Ajax as AjaxUtil
+import qualified Util.Either as EitherUtil
perPage :: Int
perPage = 7
data In t a b c = In
- { _in_newPage :: Event t Int
- , _in_currentPage :: Dynamic t Int
+ { _in_page :: Event t Int
, _in_addIncome :: Event t a
, _in_editIncome :: Event t b
, _in_deleteIncome :: Event t c
}
-data Action
- = LoadPage Int
- | GetResult (Either Text IncomePage)
-
-reducer :: forall t m a b c. MonadWidget t m => In t a b c -> m (Dynamic t (Loadable IncomePage))
+reducer :: forall t m a b c. MonadWidget t m => In t a b c -> m (Loadable2 t IncomePage)
reducer input = do
postBuild <- R.getPostBuild
+ currentPage <- R.holdDyn 1 (_in_page input)
+
let loadPage =
R.leftmost
[ 1 <$ postBuild
- , _in_newPage input
+ , _in_page input
, 1 <$ _in_addIncome input
- , R.tag (R.current $ _in_currentPage input) (_in_editIncome input)
- , R.tag (R.current $ _in_currentPage input) (_in_deleteIncome input)
+ , R.tag (R.current currentPage) (_in_editIncome input)
+ , R.tag (R.current currentPage) (_in_deleteIncome input)
]
getResult <- AjaxUtil.get $ fmap pageUrl loadPage
- R.foldDyn
- (\action _ -> case action of
- LoadPage _ -> Loading
- GetResult (Left err) -> Error err
- GetResult (Right incomes) -> Loaded incomes
- )
- Loading
+ isLoading <- R.holdDyn
+ True
(R.leftmost
- [ LoadPage <$> loadPage
- , GetResult <$> getResult
+ [ True <$ loadPage
+ , False <$ getResult
])
+ incomePage <- R.holdDyn
+ Nothing
+ (fmap EitherUtil.eitherToMaybe getResult)
+
+ return $ Loadable2 isLoading incomePage
+
where
pageUrl p =
"api/incomes?page="