[NGRAMS-TABLE] Basic support for arbitrary patches and versions from the server

parent 1ec62fb5
...@@ -357,6 +357,7 @@ applyNgramsTablePatch p (NgramsTable m) = ...@@ -357,6 +357,7 @@ applyNgramsTablePatch p (NgramsTable m) =
type State = type State =
{ ngramsTablePatch :: NgramsTablePatch { ngramsTablePatch :: NgramsTablePatch
, ngramsVersion :: Version
, ngramsParent :: Maybe NgramsTerm -- Nothing means we are not currently grouping terms , ngramsParent :: Maybe NgramsTerm -- Nothing means we are not currently grouping terms
, ngramsChildren :: Map NgramsTerm Boolean , ngramsChildren :: Map NgramsTerm Boolean
-- ^ Used only when grouping. -- ^ Used only when grouping.
...@@ -370,9 +371,11 @@ type State = ...@@ -370,9 +371,11 @@ type State =
_ngramsChildren = prop (SProxy :: SProxy "ngramsChildren") _ngramsChildren = prop (SProxy :: SProxy "ngramsChildren")
initialState :: forall props. props -> State initialState :: forall props. { loaded :: VersionedNgramsTable | props }
initialState _ = -> State
initialState {loaded: Versioned {version}} =
{ ngramsTablePatch: mempty { ngramsTablePatch: mempty
, ngramsVersion: version
, ngramsParent: Nothing , ngramsParent: Nothing
, ngramsChildren: mempty , ngramsChildren: mempty
, searchQuery: "" , searchQuery: ""
...@@ -484,14 +487,14 @@ tableContainer {searchQuery, dispatch, ngramsParent, ngramsChildren, ngramsTable ...@@ -484,14 +487,14 @@ tableContainer {searchQuery, dispatch, ngramsParent, ngramsChildren, ngramsTable
putTable :: {nodeId :: Int, tabType :: TabType} -> Versioned NgramsTablePatch -> Aff (Versioned NgramsTablePatch) putTable :: {nodeId :: Int, tabType :: TabType} -> Versioned NgramsTablePatch -> Aff (Versioned NgramsTablePatch)
putTable {nodeId, tabType} = put (toUrl Back (PutNgrams tabType Nothing) $ Just nodeId) putTable {nodeId, tabType} = put (toUrl Back (PutNgrams tabType Nothing) $ Just nodeId)
commitPatch :: {nodeId :: Int, tabType :: TabType} -> NgramsTablePatch -> StateCoTransformer State Unit commitPatch :: {nodeId :: Int, tabType :: TabType} -> Versioned NgramsTablePatch -> StateCoTransformer State Unit
commitPatch props pt = do commitPatch props pt@(Versioned {data: tablePatch}) = do
Versioned {version, data: new_patch} <- lift $ putTable props $ Versioned {version: 1, data: pt} Versioned {version: newVersion, data: newPatch} <- lift $ putTable props pt
when (version /= 1) $ modifyState_ $ \s ->
throwError $ error "commitPatch: expected version 1 only" s { ngramsVersion = newVersion
when (not $ isEmptyPatchMap new_patch) $ , ngramsTablePatch = newPatch <> tablePatch <> s.ngramsTablePatch
throwError $ error "commitPatch: expected empty patch only" }
modifyState_ $ \s -> s { ngramsTablePatch = pt <> s.ngramsTablePatch } -- TODO: check that pt.version == s.ngramsTablePatch.version
toggleMap :: forall a. a -> Maybe a -> Maybe a toggleMap :: forall a. a -> Maybe a -> Maybe a
toggleMap _ (Just _) = Nothing toggleMap _ (Just _) = Nothing
...@@ -511,7 +514,8 @@ ngramsTableSpec = simpleSpec performAction render ...@@ -511,7 +514,8 @@ ngramsTableSpec = simpleSpec performAction render
modifyState_ $ setParentResetChildren p modifyState_ $ setParentResetChildren p
performAction (ToggleChild b c) _ _ = performAction (ToggleChild b c) _ _ =
modifyState_ $ _ngramsChildren <<< at c %~ toggleMap b modifyState_ $ _ngramsChildren <<< at c %~ toggleMap b
performAction (SetTermListItem n pl) {path: {nodeId, tabType}} _ = commitPatch {nodeId, tabType} pt performAction (SetTermListItem n pl) {path: {nodeId, tabType}} {ngramsVersion} =
commitPatch {nodeId, tabType} (Versioned {version: ngramsVersion, data: pt})
where where
pe = NgramsPatch { patch_list: pl, patch_children: mempty } pe = NgramsPatch { patch_list: pl, patch_children: mempty }
pt = PatchMap $ Map.singleton n pe pt = PatchMap $ Map.singleton n pe
...@@ -521,9 +525,10 @@ ngramsTableSpec = simpleSpec performAction render ...@@ -521,9 +525,10 @@ ngramsTableSpec = simpleSpec performAction render
performAction AddTermChildren {path: {nodeId, tabType}} performAction AddTermChildren {path: {nodeId, tabType}}
{ ngramsParent: Just parent { ngramsParent: Just parent
, ngramsChildren , ngramsChildren
, ngramsVersion
} = do } = do
modifyState_ $ setParentResetChildren Nothing modifyState_ $ setParentResetChildren Nothing
commitPatch {nodeId, tabType} pt commitPatch {nodeId, tabType} (Versioned {version: ngramsVersion, data: pt})
where where
pc = patchSetFromMap ngramsChildren pc = patchSetFromMap ngramsChildren
pe = NgramsPatch { patch_list: mempty, patch_children: pc } pe = NgramsPatch { patch_list: mempty, patch_children: pc }
...@@ -533,12 +538,10 @@ ngramsTableSpec = simpleSpec performAction render ...@@ -533,12 +538,10 @@ ngramsTableSpec = simpleSpec performAction render
render :: Render State Props' Action render :: Render State Props' Action
render dispatch { path: {nodeId, tabType} render dispatch { path: {nodeId, tabType}
, loaded: Versioned { version, data: initTable } , loaded: Versioned { data: initTable }
, dispatch: loaderDispatch } , dispatch: loaderDispatch }
{ ngramsTablePatch, ngramsParent, ngramsChildren, searchQuery } { ngramsTablePatch, ngramsParent, ngramsChildren, searchQuery }
_reactChildren _reactChildren =
| version /= 1 = [ p [] [text "Invalid version"] ]
| otherwise =
[ T.tableElt [ T.tableElt
{ rows { rows
, setParams: \params -> loaderDispatch (Loader.SetPath {nodeId, params, tabType}) , setParams: \params -> loaderDispatch (Loader.SetPath {nodeId, params, tabType})
......
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