[Tree] Remove the tree state from the layout state, use a loader for the initial tree

parent 4450820a
...@@ -19,7 +19,8 @@ import Effect.Aff (Aff) ...@@ -19,7 +19,8 @@ import Effect.Aff (Aff)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Console (log) import Effect.Console (log)
import Prelude (identity) import Prelude (identity)
import React (ReactElement) import React (ReactClass, ReactElement)
import React as React
import React.DOM (a, button, div, h5, i, input, li, span, text, ul) import React.DOM (a, button, div, h5, i, input, li, span, text, ul)
import React.DOM.Props (_id, _type, className, href, title, onClick, onInput, placeholder, style, defaultValue, _data) import React.DOM.Props (_id, _type, className, href, title, onClick, onInput, placeholder, style, defaultValue, _data)
import React.DOM.Props as DOM import React.DOM.Props as DOM
...@@ -27,12 +28,15 @@ import Thermite (PerformAction, Render, Spec, createClass, defaultPerformAction, ...@@ -27,12 +28,15 @@ import Thermite (PerformAction, Render, Spec, createClass, defaultPerformAction,
import Gargantext.Config (toUrl, End(..), NodeType(..), defaultRoot) import Gargantext.Config (toUrl, End(..), NodeType(..), defaultRoot)
import Gargantext.Config.REST (get, put, post, delete, deleteWithBody) import Gargantext.Config.REST (get, put, post, delete, deleteWithBody)
import Gargantext.Components.Loader as Loader
type Name = String type Name = String
type Open = Boolean type Open = Boolean
type URL = String type URL = String
type ID = Int type ID = Int
type Props = { root :: ID }
data NTree a = NTree a (Array (NTree a)) data NTree a = NTree a (Array (NTree a))
type FTree = NTree LNode type FTree = NTree LNode
...@@ -41,7 +45,6 @@ data Action = ShowPopOver ID ...@@ -41,7 +45,6 @@ data Action = ShowPopOver ID
| ToggleFolder ID | ToggleFolder ID
| RenameNode String ID | RenameNode String ID
| Submit ID String | Submit ID String
--| Initialize
| DeleteNode ID | DeleteNode ID
| Create ID | Create ID
| SetNodeValue String ID | SetNodeValue String ID
...@@ -203,12 +206,12 @@ nodeOptionsRename d activated id = case activated of ...@@ -203,12 +206,12 @@ nodeOptionsRename d activated id = case activated of
] ]
false -> [] false -> []
type LoadedTreeViewProps = Loader.InnerProps Int FTree ()
loadedTreeview :: Spec State LoadedTreeViewProps Action
treeview :: Spec State {} Action loadedTreeview = simpleSpec performAction render
treeview = simpleSpec performAction render
where where
render :: Render State {} Action render :: Render State LoadedTreeViewProps Action
render dispatch _ {state} _ = render dispatch _ {state} _ =
[ div [className "tree"] [ div [className "tree"]
[ toHtml dispatch state [ toHtml dispatch state
...@@ -216,7 +219,23 @@ treeview = simpleSpec performAction render ...@@ -216,7 +219,23 @@ treeview = simpleSpec performAction render
] ]
] ]
treeViewClass :: ReactClass (Loader.InnerProps Int FTree (children :: React.Children))
treeViewClass = createClass "TreeView" loadedTreeview (\{loaded: t} -> {state: t})
treeLoaderClass :: Loader.LoaderClass Int FTree
treeLoaderClass = Loader.createLoaderClass "TreeLoader" loadNode
treeLoader :: Loader.Props' Int FTree -> ReactElement
treeLoader props = React.createElement treeLoaderClass props []
treeview :: Spec {} Props Void
treeview = simpleSpec defaultPerformAction render
where
render :: Render {} Props Void
render _ {root} _ _ =
[ treeLoader { path: root
, component: treeViewClass
} ]
renameTreeView :: (Action -> Effect Unit) -> FTree -> Int -> ReactElement renameTreeView :: (Action -> Effect Unit) -> FTree -> Int -> ReactElement
renameTreeView d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, showRenameBox }) ary) nid = renameTreeView d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, showRenameBox }) ary) nid =
......
...@@ -20,12 +20,10 @@ dispatchAction :: forall ignored m. ...@@ -20,12 +20,10 @@ dispatchAction :: forall ignored m.
(Action -> m Unit) -> ignored -> Routes -> m Unit (Action -> m Unit) -> ignored -> Routes -> m Unit
dispatchAction dispatcher _ Home = do dispatchAction dispatcher _ Home = do
dispatcher Initialize
dispatcher $ SetRoute Home dispatcher $ SetRoute Home
-- dispatcher $ LandingA TODO -- dispatcher $ LandingA TODO
dispatchAction dispatcher _ Login = do dispatchAction dispatcher _ Login = do
dispatcher Initialize
dispatcher $ SetRoute Login dispatcher $ SetRoute Login
-- dispatcher $ LoginA TODO -- dispatcher $ LoginA TODO
......
...@@ -25,10 +25,8 @@ import Gargantext.Router (Routes) ...@@ -25,10 +25,8 @@ import Gargantext.Router (Routes)
------------------------------------------------------------------------ ------------------------------------------------------------------------
data Action data Action
= Initialize = LoginA LN.Action
| LoginA LN.Action
| SetRoute Routes | SetRoute Routes
| TreeViewA Tree.Action
| SearchA S.Action | SearchA S.Action
| Search String | Search String
| AddCorpusA AC.Action | AddCorpusA AC.Action
...@@ -68,20 +66,11 @@ performAction Go _ _ = void do ...@@ -68,20 +66,11 @@ performAction Go _ _ = void do
--modifyState id --modifyState id
--------------------------------------------------------- ---------------------------------------------------------
performAction Initialize _ state = do
_ <- logs "loading Initial nodes"
case state.initialized of
false -> do
d <- lift $ Tree.loadNode defaultRoot
modifyState_ $ _ { initialized = true, ntreeState = {state: d} }
_ -> pure unit
performAction (LoginA _) _ _ = pure unit performAction (LoginA _) _ _ = pure unit
performAction (AddCorpusA _) _ _ = pure unit performAction (AddCorpusA _) _ _ = pure unit
performAction (SearchA _) _ _ = pure unit performAction (SearchA _) _ _ = pure unit
performAction (UserPageA _) _ _ = pure unit performAction (UserPageA _) _ _ = pure unit
performAction (DocumentViewA _) _ _ = pure unit performAction (DocumentViewA _) _ _ = pure unit
performAction (TreeViewA _) _ _ = pure unit
performAction (GraphExplorerA _) _ _ = pure unit performAction (GraphExplorerA _) _ _ = pure unit
performAction (AnnuaireAction _) _ _ = pure unit performAction (AnnuaireAction _) _ _ = pure unit
...@@ -123,12 +112,6 @@ _documentViewAction = prism DocumentViewA \action -> ...@@ -123,12 +112,6 @@ _documentViewAction = prism DocumentViewA \action ->
DocumentViewA caction -> Right caction DocumentViewA caction -> Right caction
_-> Left action _-> Left action
_treeAction :: Prism' Action Tree.Action
_treeAction = prism TreeViewA \action ->
case action of
TreeViewA caction -> Right caction
_-> Left action
_graphExplorerAction :: Prism' Action GE.Action _graphExplorerAction :: Prism' Action GE.Action
_graphExplorerAction = prism GraphExplorerA \action -> _graphExplorerAction = prism GraphExplorerA \action ->
case action of case action of
......
...@@ -11,6 +11,7 @@ import Thermite (Render, Spec, _render, defaultPerformAction, defaultRender, foc ...@@ -11,6 +11,7 @@ import Thermite (Render, Spec, _render, defaultPerformAction, defaultRender, foc
import Unsafe.Coerce (unsafeCoerce) import Unsafe.Coerce (unsafeCoerce)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Config (defaultRoot)
import Gargantext.Components.Data.Lang (Lang(..)) import Gargantext.Components.Data.Lang (Lang(..))
import Gargantext.Components.Login as LN import Gargantext.Components.Login as LN
import Gargantext.Components.Tree as Tree import Gargantext.Components.Tree as Tree
...@@ -23,10 +24,10 @@ import Gargantext.Pages.Corpus.Dashboard as Dsh ...@@ -23,10 +24,10 @@ import Gargantext.Pages.Corpus.Dashboard as Dsh
import Gargantext.Pages.Corpus.Graph as GE import Gargantext.Pages.Corpus.Graph as GE
import Gargantext.Pages.Corpus.Tabs.Ngrams.NgramsTable as NG import Gargantext.Pages.Corpus.Tabs.Ngrams.NgramsTable as NG
import Gargantext.Pages.Home as L import Gargantext.Pages.Home as L
import Gargantext.Pages.Layout.Actions (Action(..), _addCorpusAction, _documentViewAction, _graphExplorerAction, _loginAction, _searchAction, _treeAction, _userPageAction, performAction) import Gargantext.Pages.Layout.Actions (Action(..), _addCorpusAction, _documentViewAction, _graphExplorerAction, _loginAction, _searchAction, _userPageAction, performAction)
import Gargantext.Pages.Layout.Specs.AddCorpus as AC import Gargantext.Pages.Layout.Specs.AddCorpus as AC
import Gargantext.Pages.Layout.Specs.Search as S import Gargantext.Pages.Layout.Specs.Search as S
import Gargantext.Pages.Layout.States (AppState, _addCorpusState, _documentViewState, _graphExplorerState, _loginState, _searchState, _treeState, _userPageState) import Gargantext.Pages.Layout.States (AppState, _addCorpusState, _documentViewState, _graphExplorerState, _loginState, _searchState, _userPageState)
import Gargantext.Router (Routes(..)) import Gargantext.Router (Routes(..))
layoutSpec :: Spec AppState {} Action layoutSpec :: Spec AppState {} Action
...@@ -85,7 +86,7 @@ layout0 layout = ...@@ -85,7 +86,7 @@ layout0 layout =
cont $ fold cont $ fold
[ withState \st -> [ withState \st ->
if ((\(LN.State s) -> s.loginC) st.loginState == true) if ((\(LN.State s) -> s.loginC) st.loginState == true)
then ls as then ls $ cmapProps (const {root: defaultRoot}) as
else outerLayout1 else outerLayout1
, rs bs , rs bs
] ]
...@@ -95,7 +96,7 @@ layout0 layout = ...@@ -95,7 +96,7 @@ layout0 layout =
rs = over _render \render d p s c -> [ div [ className "col-md-10"] (render d p s c) ] rs = over _render \render d p s c -> [ div [ className "col-md-10"] (render d p s c) ]
cont = over _render \render d p s c -> [ div [className "row" ] (render d p s c) ] cont = over _render \render d p s c -> [ div [className "row" ] (render d p s c) ]
as = focus _treeState _treeAction Tree.treeview as = noState Tree.treeview
bs = innerLayout $ layout bs = innerLayout $ layout
...@@ -108,7 +109,7 @@ layout0 layout = ...@@ -108,7 +109,7 @@ layout0 layout =
] ]
] ]
-- TODO avoid code duplication with layout0
layout1 :: Spec AppState {} Action layout1 :: Spec AppState {} Action
-> Spec AppState {} Action -> Spec AppState {} Action
layout1 layout = layout1 layout =
...@@ -124,7 +125,7 @@ layout1 layout = ...@@ -124,7 +125,7 @@ layout1 layout =
cont $ fold cont $ fold
[ withState \st -> [ withState \st ->
if ((\(LN.State s) -> s.loginC) st.loginState == true) if ((\(LN.State s) -> s.loginC) st.loginState == true)
then ls as then ls $ cmapProps (const {root: defaultRoot}) as
else outerLayout1 else outerLayout1
, rs bs , rs bs
] ]
...@@ -137,7 +138,7 @@ layout1 layout = ...@@ -137,7 +138,7 @@ layout1 layout =
rs = over _render \render d p s c -> [ div [if (s.showTree) then className "col-md-10" else className "col-md-12"] (render d p s c) ] rs = over _render \render d p s c -> [ div [if (s.showTree) then className "col-md-10" else className "col-md-12"] (render d p s c) ]
cont = over _render \render d p s c -> [ div [className "row" ] (render d p s c) ] cont = over _render \render d p s c -> [ div [className "row" ] (render d p s c) ]
as = focus _treeState _treeAction Tree.treeview as = noState Tree.treeview
bs = innerLayout $ layout bs = innerLayout $ layout
......
...@@ -21,12 +21,10 @@ type AppState = ...@@ -21,12 +21,10 @@ type AppState =
, searchState :: S.State , searchState :: S.State
, userPageState :: C.State , userPageState :: C.State
, documentState :: D.State , documentState :: D.State
, ntreeState :: Tree.State
, search :: String , search :: String
, showLogin :: Boolean , showLogin :: Boolean
, showCorpus :: Boolean , showCorpus :: Boolean
, graphExplorerState :: GE.State , graphExplorerState :: GE.State
, initialized :: Boolean
, showTree :: Boolean , showTree :: Boolean
} }
...@@ -38,12 +36,10 @@ initAppState = ...@@ -38,12 +36,10 @@ initAppState =
, searchState : S.initialState , searchState : S.initialState
, userPageState : C.initialState , userPageState : C.initialState
, documentState : D.initialState {} , documentState : D.initialState {}
, ntreeState : {state: Tree.exampleTree}
, search : "" , search : ""
, showLogin : false , showLogin : false
, showCorpus : false , showCorpus : false
, graphExplorerState : GE.initialState , graphExplorerState : GE.initialState
, initialized : false
, showTree : false , showTree : false
} }
...@@ -63,8 +59,5 @@ _userPageState = lens (\s -> s.userPageState) (\s ss -> s{userPageState = ss}) ...@@ -63,8 +59,5 @@ _userPageState = lens (\s -> s.userPageState) (\s ss -> s{userPageState = ss})
_documentViewState :: Lens' AppState D.State _documentViewState :: Lens' AppState D.State
_documentViewState = lens (\s -> s.documentState) (\s ss -> s{documentState = ss}) _documentViewState = lens (\s -> s.documentState) (\s ss -> s{documentState = ss})
_treeState :: Lens' AppState Tree.State
_treeState = lens (\s -> s.ntreeState) (\s ss -> s {ntreeState = ss})
_graphExplorerState :: Lens' AppState GE.State _graphExplorerState :: Lens' AppState GE.State
_graphExplorerState = lens (\s -> s.graphExplorerState) (\s ss -> s{graphExplorerState = ss}) _graphExplorerState = lens (\s -> s.graphExplorerState) (\s ss -> s{graphExplorerState = ss})
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