aboutsummaryrefslogtreecommitdiff
path: root/client/src/View/Income/Income.hs
blob: c48f325ad36705202a3b34e21405bfbbcd75bfcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module View.Income.Income
  ( init
  , view
  , In(..)
  ) where

import           Data.Aeson          (FromJSON)
import           Prelude             hiding (init)
import           Reflex.Dom          (Dynamic, Event, MonadWidget)
import qualified Reflex.Dom          as R

import           Common.Model        (Currency, Income (..),
                                      IncomesAndCount (..), UserId)

import qualified Component.Pages     as Pages
import           Loadable            (Loadable (..))
import qualified Loadable
import qualified Util.Ajax           as AjaxUtil
import qualified Util.Reflex         as ReflexUtil
-- import qualified View.Income.Header as Header
import           View.Income.Init    (Init (..))
import qualified View.Income.Reducer as Reducer
import qualified View.Income.Table   as Table

data In t = In
  { _in_currentUser :: UserId
  , _in_currency    :: Currency
  , _in_init        :: Dynamic t (Loadable Init)
  }

init :: forall t m. MonadWidget t m => m (Dynamic t (Loadable Init))
init = do
  users <- AjaxUtil.getNow "api/users"
  incomes <- AjaxUtil.getNow "api/incomes"
  payments <- AjaxUtil.getNow "api/payments"
  return $ do
    us <- users
    is <- incomes
    ps <- payments
    return $ Init <$> us <*> is <*> ps

view :: forall t m. MonadWidget t m => In t -> m ()
view input = do
  -- rec
    -- incomes <- Reducer.reducer
    --   { Reducer._in_newPage      = ReflexUtil.flatten (Table._out_newPage <$> table)
    --   , Reducer._in_currentPage  = ReflexUtil.flatten (Table._out_currentPage <$> table)
    --   , Reducer._in_addIncome    = ReflexUtil.flatten (Table._out_add <$> table)
    --   , Reducer._in_editIncome   = ReflexUtil.flatten (Table._out_edit <$> table)
    --   , Reducer._in_deleteIncome = ReflexUtil.flatten (Table._out_delete <$> table)
    --   }

  rec
    incomes <- Reducer.reducer $ Reducer.In
      { Reducer._in_newPage      = Pages._out_newPage pages
      , Reducer._in_currentPage  = Pages._out_currentPage pages
      , Reducer._in_addIncome    = Table._out_add table
      , Reducer._in_editIncome   = Table._out_edit table
      , Reducer._in_deleteIncome = Table._out_delete table
      }

    table <- Table.view $ Table.In
      { Table._in_currentUser = _in_currentUser input
      , Table._in_currency = _in_currency input
      , Table._in_incomes = R.ffor incomes $ \case
          Loaded (IncomesAndCount xs _) -> xs
          _         -> []
      }

    pages <- Pages.view $ Pages.In
      { Pages._in_total = R.ffor incomes $ \case
          Loaded (IncomesAndCount _ n) -> n
          _         -> 0
      , Pages._in_perPage = Reducer.perPage
      }

  -- -- table :: Event t (Maybe (Table.Out t))
  -- table <- R.dyn . R.ffor incomes . Loadable.view $ \incomes ->
  --   Table.view $ Table.In
  --     { Table._in_currentUser = _in_currentUser input
  --     , Table._in_currency = _in_currency input
  --     , Table._in_incomes = incomes
  --     }

  return ()