aboutsummaryrefslogtreecommitdiff
path: root/client/src/View/Income/Reducer.hs
diff options
context:
space:
mode:
authorJoris2019-10-27 20:26:29 +0100
committerJoris2019-10-27 20:26:29 +0100
commitb97ad942495352c3fc1e0c820cfba82a9693ac7a (patch)
treef554831888929e2eff5e1fe478f92758637d37cf /client/src/View/Income/Reducer.hs
parent8ef4d96644bce59bbb736af6359e644743e5610a (diff)
downloadbudget-b97ad942495352c3fc1e0c820cfba82a9693ac7a.tar.gz
budget-b97ad942495352c3fc1e0c820cfba82a9693ac7a.tar.bz2
budget-b97ad942495352c3fc1e0c820cfba82a9693ac7a.zip
WIP Set up server side paging for incomes
Diffstat (limited to 'client/src/View/Income/Reducer.hs')
-rw-r--r--client/src/View/Income/Reducer.hs66
1 files changed, 66 insertions, 0 deletions
diff --git a/client/src/View/Income/Reducer.hs b/client/src/View/Income/Reducer.hs
new file mode 100644
index 0000000..5b346cb
--- /dev/null
+++ b/client/src/View/Income/Reducer.hs
@@ -0,0 +1,66 @@
+module View.Income.Reducer
+ ( perPage
+ , reducer
+ , In(..)
+ ) where
+
+import Data.Text (Text)
+import qualified Data.Text as T
+import Reflex.Dom (Dynamic, Event, MonadWidget)
+import qualified Reflex.Dom as R
+
+import Common.Model (IncomesAndCount)
+
+import Loadable (Loadable (..))
+import qualified Loadable as Loadable
+import qualified Util.Ajax as AjaxUtil
+
+perPage :: Int
+perPage = 7
+
+data In t a b c = In
+ { _in_newPage :: Event t Int
+ , _in_currentPage :: Dynamic t Int
+ , _in_addIncome :: Event t a
+ , _in_editIncome :: Event t b
+ , _in_deleteIncome :: Event t c
+ }
+
+data Action
+ = LoadPage Int
+ | GetResult (Either Text IncomesAndCount)
+
+reducer :: forall t m a b c. MonadWidget t m => In t a b c -> m (Dynamic t (Loadable IncomesAndCount))
+reducer input = do
+
+ postBuild <- R.getPostBuild
+
+ let loadPage =
+ R.leftmost
+ [ 1 <$ postBuild
+ , _in_newPage 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)
+ ]
+
+ 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
+ (R.leftmost
+ [ LoadPage <$> loadPage
+ , GetResult <$> getResult
+ ])
+
+ where
+ pageUrl p =
+ "api/v2/incomes?page="
+ <> (T.pack . show $ p)
+ <> "&perPage="
+ <> (T.pack . show $ perPage)