[Loader2] working

parent bb8227db
module Gargantext.Components.Loader2 where module Gargantext.Components.Loader2 where
--import Control.Monad.Cont.Trans (lift)
import Data.Maybe (Maybe(..), isNothing) import Data.Maybe (Maybe(..), isNothing)
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Gargantext.Prelude import Gargantext.Prelude
import Effect (Effect) import Effect.Aff (Aff, launchAff, launchAff_, killFiber)
--import Effect.Aff (Aff) import Effect.Class (liftEffect)
import Effect.Exception (error)
import Reactix as R import Reactix as R
type State path loaded = { currentPath :: path, loaded :: Maybe loaded } type State path loaded = { currentPath :: path, loaded :: Maybe loaded }
...@@ -15,22 +15,26 @@ useLoader ...@@ -15,22 +15,26 @@ useLoader
. Eq path . Eq path
=> Show path => Show path
=> path => path
-> (path -> Effect loaded) -> (path -> Aff loaded)
-> (path -> loaded -> Array R.Element) -> (path -> loaded -> R.Element)
-> R.Hooks (Array R.Element) -> R.Hooks R.Element
useLoader newPath loader render = do useLoader newPath loader render = do
{currentPath, loaded} /\ setState <- R.useState' { currentPath: newPath, loaded: Nothing } {currentPath, loaded} /\ setState <- R.useState' { currentPath: newPath, loaded: Nothing }
-- What about cleanup handlers? R.useEffect $ \_ ->
R.useEffect' $ \_ -> if (isNothing loaded || newPath /= currentPath) then do
when (isNothing loaded || newPath /= currentPath) do
logs $ "useLoader " <> show {newPath, currentPath, loadedIsNothing: isNothing loaded} logs $ "useLoader " <> show {newPath, currentPath, loadedIsNothing: isNothing loaded}
freshlyLoaded <- loader newPath
setState { currentPath: newPath, loaded: Just freshlyLoaded } fiber <- launchAff do
freshlyLoaded <- loader newPath
liftEffect $ setState { currentPath: newPath, loaded: Just freshlyLoaded }
pure $ \_ -> launchAff_ $ killFiber (error "useLoader") fiber
else do
pure $ \_ -> pure unit
pure case loaded of pure case loaded of
Nothing -> Nothing ->
-- TODO load spinner -- TODO load spinner
[] R.fragment []
Just loadedData -> Just loadedData ->
render currentPath loadedData render currentPath loadedData
...@@ -229,10 +229,12 @@ treeview :: Spec {} Props Void ...@@ -229,10 +229,12 @@ treeview :: Spec {} Props Void
treeview = simpleSpec defaultPerformAction render treeview = simpleSpec defaultPerformAction render
where where
render :: Render {} Props Void render :: Render {} Props Void
render _ {root} _ _ = render _ props _ _ = [R2.scuff $ R.createElement cpt props []]
R.hooksComponent "TreeView" \props children ->
cpt =
R.hooksComponent "TreeView" \{root} _children ->
useLoader root loadNode \currentPath loaded -> useLoader root loadNode \currentPath loaded ->
React.createElement treeViewClass {tree: loaded} R2.buff $ React.createElement treeViewClass {tree: loaded} []
--nodePopupView :: forall s. (Action -> Effect Unit) -> FTree -> RAction s -> R.Element --nodePopupView :: forall s. (Action -> Effect Unit) -> FTree -> RAction s -> R.Element
...@@ -588,8 +590,9 @@ fldr :: Boolean -> String ...@@ -588,8 +590,9 @@ fldr :: Boolean -> String
fldr open = if open then "fas fa-folder-open" else "fas fa-folder" fldr open = if open then "fas fa-folder-open" else "fas fa-folder"
loadNode :: ID -> Effect FTree loadNode :: ID -> Aff FTree
loadNode a = lift ((get <<< toUrl Back Tree <<< Just) a) -- loadNode a = lift ((get <<< toUrl Back Tree <<< Just) a)
loadNode = get <<< toUrl Back Tree <<< Just
----- TREE CRUD Operations ----- TREE CRUD Operations
......
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