Commit eb395150 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[ngrams table] fix pagination in ngrams table

However, still I see too many pages reported wrt the results
returned (i.e. couple last pages are empty).
parent afb7c223
......@@ -21,28 +21,26 @@ import Data.Sequence (Seq, length) as Seq
import Data.Set (Set)
import Data.Set as Set
import Data.Symbol (SProxy(..))
import Data.Tuple (Tuple(..), fst, snd)
import Data.Tuple (Tuple(..), fst)
import Data.Tuple.Nested ((/\))
import DOM.Simple.Console (log, log2)
import DOM.Simple.Console (log2)
import Effect (Effect)
import Effect.Aff (Aff, launchAff_)
import Effect.Class (liftEffect)
import Reactix as R
import Effect.Aff (Aff)
import Reactix (Component, Element, Ref, State, createElement, fragment, hooksComponentWithModule, unsafeEventValue, useEffect', useState') as R
import Reactix.DOM.HTML as H
import Record as Record
import Unsafe.Coerce (unsafeCoerce)
import Gargantext.Prelude
import Gargantext.Prelude (class Show, Unit, bind, const, discard, identity, map, mempty, not, otherwise, pure, read, show, unit, (#), ($), (&&), (/=), (<$>), (<<<), (<>), (=<<), (==), (||))
import Gargantext.AsyncTasks as GAT
import Gargantext.Components.AutoUpdate (autoUpdateElt)
import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Components.Table.Types as T
import Gargantext.Components.Table.Types (ColumnName(..), Rows, TableContainerProps) as T
import Gargantext.Components.NgramsTable.Components as NTC
import Gargantext.Components.NgramsTable.Core
import Gargantext.Components.NgramsTable.Core (Action(..), CoreAction(..), CoreState, Dispatch, NgramsElement(..), NgramsPatch(..), NgramsTable, NgramsTerm, PageParams, PatchMap(..), Version, Versioned(..), VersionedNgramsTable, VersionedWithCountNgramsTable, _NgramsElement, _NgramsRepoElement, _NgramsTable, _children, _list, _ngrams, _ngrams_repo_elements, _ngrams_scores, _occurrences, _root, addNewNgramA, applyNgramsPatches, applyPatchSet, chartsAfterSync, commitPatch, convOrderBy, coreDispatch, filterTermSize, fromNgramsPatches, ngramsRepoElementToNgramsElement, ngramsTermText, normNgram, patchSetFromMap, replace, rootsOf, singletonNgramsTablePatch, syncResetButtons, toVersioned)
import Gargantext.Components.NgramsTable.Loader (useLoaderWithCacheAPI)
import Gargantext.Components.Nodes.Lists.Types as NT
import Gargantext.Components.Table as T
import Gargantext.Components.Table (filterRows, table) as T
import Gargantext.Routes (SessionRoute(..)) as R
import Gargantext.Sessions (Session, get)
import Gargantext.Types (CTabNgramType, OrderBy(..), SearchQuery, TabType, TermList(..), TermSize, termLists, termSizes)
......@@ -55,35 +53,12 @@ import Gargantext.Utils.Seq (mapMaybe) as Seq
thisModule :: String
thisModule = "Gargantext.Components.NgramsTable"
type State' =
CoreState
( ngramsParent :: Maybe NgramsTerm -- Nothing means we are not currently grouping terms
, ngramsChildren :: Map NgramsTerm Boolean
-- ^ Used only when grouping.
-- This updates the children of `ngramsParent`,
-- ngrams set to `true` are to be added, and `false` to
-- be removed.
, ngramsSelection :: Set NgramsTerm
-- ^ The set of selected checkboxes of the first column.
)
_ngramsChildren :: forall row. Lens' { ngramsChildren :: Map NgramsTerm Boolean | row } (Map NgramsTerm Boolean)
_ngramsChildren = prop (SProxy :: SProxy "ngramsChildren")
_ngramsSelection :: forall row. Lens' { ngramsSelection :: Set NgramsTerm | row } (Set NgramsTerm)
_ngramsSelection = prop (SProxy :: SProxy "ngramsSelection")
initialState' :: VersionedNgramsTable -> State'
initialState' (Versioned {version}) =
{ ngramsChildren: mempty
, ngramsLocalPatch: mempty
, ngramsParent: Nothing
, ngramsSelection: mempty
, ngramsStagePatch: mempty
, ngramsValidPatch: mempty
, ngramsVersion: version
}
type State =
CoreState (
ngramsChildren :: Map NgramsTerm Boolean
......@@ -290,7 +265,8 @@ type CommonProps = (
)
type Props = (
mTotalRows :: Maybe Int
cacheState :: NT.CacheState
, mTotalRows :: Maybe Int
, path :: R.State PageParams
, state :: R.State State
, versioned :: VersionedNgramsTable
......@@ -303,21 +279,22 @@ loadedNgramsTable = R.createElement loadedNgramsTableCpt
loadedNgramsTableCpt :: R.Component Props
loadedNgramsTableCpt = R.hooksComponentWithModule thisModule "loadedNgramsTable" cpt
cpt { afterSync
, appReload
, asyncTasksRef
, mTotalRows
, path: path@(path'@{ listIds, nodeId, params, searchQuery, scoreType, termListFilter, termSizeFilter } /\ setPath)
, sidePanelTriggers
, state: (state@{ ngramsChildren
, ngramsLocalPatch
, ngramsParent
, ngramsSelection
, ngramsVersion } /\ setState)
, tabNgramType
, treeReloadRef
, versioned: Versioned { data: initTable }
, withAutoUpdate } _ = do
cpt props@{ afterSync
, appReload
, asyncTasksRef
, cacheState
, mTotalRows
, path: path@(path'@{ listIds, nodeId, params, searchQuery, scoreType, termListFilter, termSizeFilter } /\ setPath)
, sidePanelTriggers
, state: (state@{ ngramsChildren
, ngramsLocalPatch
, ngramsParent
, ngramsSelection
, ngramsVersion } /\ setState)
, tabNgramType
, treeReloadRef
, versioned: Versioned { data: initTable }
, withAutoUpdate } _ = do
pure $ R.fragment $
autoUpdate <> [
......@@ -327,19 +304,19 @@ loadedNgramsTable = R.createElement loadedNgramsTableCpt
]
, search ]
<>
[ T.table { syncResetButton: [ syncResetButton ]
, colNames
[ T.table { colNames
, container: tableContainer { dispatch: performAction
, ngramsChildren
, ngramsParent
, ngramsSelection
, ngramsTable
, path
, tabNgramType
, syncResetButton: [ syncResetButton ]
, tabNgramType
}
, params: params /\ setParams -- TODO-LENS
, rows: filteredConvertedRows
, syncResetButton: [ syncResetButton ]
, totalRecords
, wrapColElts: wrapColElts { allNgramsSelected
, dispatch: performAction
......@@ -349,13 +326,16 @@ loadedNgramsTable = R.createElement loadedNgramsTableCpt
, syncResetButton ]
where
afterSync = chartsAfterSync path' asyncTasksRef nodeId treeReloadRef
afterSync' _ = do
chartsAfterSync path' asyncTasksRef nodeId treeReloadRef unit
afterSync unit
performAction = mkDispatch { filteredRows, path: path', state: state /\ setState }
syncResetButton = syncResetButtons { afterSync
syncResetButton = syncResetButtons { afterSync: afterSync'
, ngramsLocalPatch
, performAction: performAction <<< CoreAction }
autoUpdate :: Array R.Element
autoUpdate = if withAutoUpdate then
[ R2.buff
......@@ -363,49 +343,17 @@ loadedNgramsTable = R.createElement loadedNgramsTableCpt
{ duration: 5000
, effect: performAction
$ CoreAction
$ Synchronize { afterSync }
$ Synchronize { afterSync: afterSync' }
}
]
else []
setParentResetChildren :: Maybe NgramsTerm -> State -> State
setParentResetChildren p = _ { ngramsParent = p, ngramsChildren = mempty }
performAction :: Action -> Effect Unit
performAction (SetParentResetChildren p) =
setState $ setParentResetChildren p
performAction (ToggleChild b c) =
setState $ \s@{ ngramsChildren: nc } -> s { ngramsChildren = newNC nc }
where
newNC nc = Map.alter (maybe (Just b) (const Nothing)) c nc
performAction (ToggleSelect c) =
setState $ \s@{ ngramsSelection: ns } -> s { ngramsSelection = toggleSet c ns }
performAction ToggleSelectAll =
setState toggler
where
toggler s =
if allNgramsSelected then
s { ngramsSelection = Set.empty :: Set NgramsTerm }
else
s { ngramsSelection = selectNgramsOnFirstPage filteredRows }
performAction AddTermChildren =
case ngramsParent of
Nothing ->
-- impossible but harmless
pure unit
Just parent -> do
let pc = patchSetFromMap ngramsChildren
pe = NgramsPatch { patch_list: mempty, patch_children: pc }
pt = singletonNgramsTablePatch parent pe
setState $ setParentResetChildren Nothing
commitPatch (Versioned {version: ngramsVersion, data: pt}) (state /\ setState)
performAction (CoreAction a) = coreDispatch path' (state /\ setState) a
totalRecords = fromMaybe (Seq.length rows) mTotalRows
filteredConvertedRows :: T.Rows
filteredConvertedRows = convertRow <$> filteredRows
filteredRows :: PreConversionRows
filteredRows = T.filterRows { params } rows
-- no need to filter offset if cache is off
filteredRows = if cacheState == NT.CacheOn then T.filterRows { params } rows else rows
ng_scores :: Map NgramsTerm (Additive Int)
ng_scores = ngramsTable ^. _NgramsTable <<< _ngrams_scores
rows :: PreConversionRows
......@@ -471,6 +419,56 @@ loadedNgramsTable = R.createElement loadedNgramsTableCpt
setSearchQuery :: String -> Effect Unit
setSearchQuery x = setPath $ _ { searchQuery = x }
type MkDispatchProps = (
filteredRows :: PreConversionRows
, path :: PageParams
, state :: R.State State
)
mkDispatch :: Record MkDispatchProps -> (Action -> Effect Unit)
mkDispatch { filteredRows
, path
, state: (state@{ ngramsChildren
, ngramsLocalPatch
, ngramsParent
, ngramsSelection
, ngramsVersion } /\ setState) } = performAction
where
allNgramsSelected = allNgramsSelectedOnFirstPage ngramsSelection filteredRows
setParentResetChildren :: Maybe NgramsTerm -> State -> State
setParentResetChildren p = _ { ngramsParent = p, ngramsChildren = mempty }
performAction :: Action -> Effect Unit
performAction (SetParentResetChildren p) =
setState $ setParentResetChildren p
performAction (ToggleChild b c) =
setState $ \s@{ ngramsChildren: nc } -> s { ngramsChildren = newNC nc }
where
newNC nc = Map.alter (maybe (Just b) (const Nothing)) c nc
performAction (ToggleSelect c) =
setState $ \s@{ ngramsSelection: ns } -> s { ngramsSelection = toggleSet c ns }
performAction ToggleSelectAll =
setState toggler
where
toggler s =
if allNgramsSelected then
s { ngramsSelection = Set.empty :: Set NgramsTerm }
else
s { ngramsSelection = selectNgramsOnFirstPage filteredRows }
performAction AddTermChildren =
case ngramsParent of
Nothing ->
-- impossible but harmless
pure unit
Just parent -> do
let pc = patchSetFromMap ngramsChildren
pe = NgramsPatch { patch_list: mempty, patch_children: pc }
pt = singletonNgramsTablePatch parent pe
setState $ setParentResetChildren Nothing
commitPatch (Versioned {version: ngramsVersion, data: pt}) (state /\ setState)
performAction (CoreAction a) = coreDispatch path (state /\ setState) a
displayRow :: State -> SearchQuery -> NgramsTable -> Maybe NgramsTerm -> Maybe TermList -> Maybe TermSize -> NgramsElement -> Boolean
displayRow state@{ ngramsChildren
......@@ -549,6 +547,7 @@ mainNgramsTable = R.createElement mainNgramsTableCpt
let render versioned = mainNgramsTablePaint { afterSync
, appReload
, asyncTasksRef
, cacheState: fst cacheState
, path: fst pathS
, sidePanelTriggers
, tabNgramType
......@@ -567,6 +566,7 @@ mainNgramsTable = R.createElement mainNgramsTableCpt
let render versionedWithCount = mainNgramsTablePaintNoCache { afterSync
, appReload
, asyncTasksRef
, cacheState: fst cacheState
, pathS
, sidePanelTriggers
, tabNgramType
......@@ -621,7 +621,8 @@ mainNgramsTable = R.createElement mainNgramsTableCpt
handleResponse v = v
type MainNgramsTablePaintProps = (
path :: PageParams
cacheState :: NT.CacheState
, path :: PageParams
, versioned :: VersionedNgramsTable
| CommonProps
)
......@@ -635,6 +636,7 @@ mainNgramsTablePaint = R.createElement mainNgramsTablePaintCpt
cpt props@{ afterSync
, appReload
, asyncTasksRef
, cacheState
, path
, sidePanelTriggers
, tabNgramType
......@@ -646,6 +648,7 @@ mainNgramsTablePaint = R.createElement mainNgramsTablePaintCpt
pure $ loadedNgramsTable { afterSync
, appReload
, asyncTasksRef
, cacheState
, mTotalRows: Nothing
, path: pathS
, sidePanelTriggers
......@@ -656,8 +659,9 @@ mainNgramsTablePaint = R.createElement mainNgramsTablePaintCpt
, withAutoUpdate
} []
type MainNgramsTablePaintNoCacheProps =
( pathS :: R.State PageParams
type MainNgramsTablePaintNoCacheProps = (
cacheState :: NT.CacheState
, pathS :: R.State PageParams
, versionedWithCount :: VersionedWithCountNgramsTable
| CommonProps
)
......@@ -671,6 +675,7 @@ mainNgramsTablePaintNoCache = R.createElement mainNgramsTablePaintNoCacheCpt
cpt props@{ afterSync
, appReload
, asyncTasksRef
, cacheState
, pathS
, sidePanelTriggers
, tabNgramType
......@@ -685,6 +690,7 @@ mainNgramsTablePaintNoCache = R.createElement mainNgramsTablePaintNoCacheCpt
afterSync
, appReload
, asyncTasksRef
, cacheState
, mTotalRows: Just count
, path: pathS
, sidePanelTriggers
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment