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 ()