module Component.Table ( view , In(..) , Out(..) ) where import Data.Text (Text) import Reflex.Dom (Dynamic, Event, MonadWidget) import qualified Reflex.Dom as R import qualified Component.Button as Button import qualified Component.Modal as Modal import qualified Component.Pages as Pages import qualified Util.Reflex as ReflexUtil import qualified View.Icon as Icon data In m t h r a = In { _in_headerLabel :: h -> Text , _in_rows :: Dynamic t [r] , _in_cell :: h -> r -> Text , _in_perPage :: Int , _in_resetPage :: Event t () , _in_cloneModal :: Dynamic t r -> Modal.Content t m a } data Out t a = Out { _out_add :: Event t a } view :: forall t m h r a. (MonadWidget t m, Bounded h, Enum h) => In m t h r a -> m (Out t a) view input = R.divClass "table" $ do rec result <- R.divClass "lines" $ do R.divClass "header" $ do flip mapM_ [minBound..] $ \header -> R.divClass "cell" . R.text $ _in_headerLabel input header R.divClass "cell" $ R.blank let rows = getRange (_in_perPage input) <$> (Pages._out_currentPage pages) <*> (_in_rows input) R.simpleList rows $ \r -> R.divClass "row" $ do flip mapM_ [minBound..] $ \h -> R.divClass "cell" $ R.dynText $ R.ffor r (_in_cell input h) clone <- R.divClass "cell button" $ Button._out_clic <$> (Button.view $ Button.defaultIn Icon.clone) cloned <- Modal.view $ Modal.In { Modal._in_show = clone , Modal._in_content = _in_cloneModal input r } return cloned pages <- Pages.view $ Pages.In { Pages._in_total = length <$> (_in_rows input) , Pages._in_perPage = _in_perPage input , Pages._in_reset = _in_resetPage input } -- return $ -- ( R.switch . R.current . fmap (R.leftmost . map (\(a, _, _) -> a)) $ result -- , R.switch . R.current . fmap (R.leftmost . map (\(_, b, _) -> b)) $ result -- , R.switch . R.current . fmap (R.leftmost . map (\(_, _, c) -> c)) $ result -- ) return $ Out { _out_add = R.switch . R.current . fmap R.leftmost $ result } getRange :: forall a. Int -> Int -> [a] -> [a] getRange perPage currentPage = take perPage . drop ((currentPage - 1) * perPage)