Commit 9f399030 authored by James Laver's avatar James Laver

Refactor G.Hooks.Loader - incl new useAff

parent f9df8e9f
module Gargantext.Hooks.Loader where module Gargantext.Hooks.Loader where
import Data.Maybe (Maybe(..), isNothing, maybe) import Data.Functor ((<$>))
import Control.Monad ((=<<))
import Data.Maybe (Maybe(..), isNothing, maybe, maybe')
import Data.Tuple (fst) import Data.Tuple (fst)
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Gargantext.Prelude import Gargantext.Prelude
...@@ -10,36 +12,46 @@ import Reactix as R ...@@ -10,36 +12,46 @@ import Reactix as R
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
import Gargantext.Components.LoadingSpinner (loadingSpinner) import Gargantext.Components.LoadingSpinner (loadingSpinner)
useLoader :: forall path state. path -> (path -> Aff state) -> (state -> R.Element) -> R.Hooks R.Element useAff :: forall st. Aff st -> R.Hooks (Maybe st)
useLoader path loader render = do useAff loader = do
state <- R.useState' Nothing (loaded /\ setLoaded) <- R.useState' Nothing
loader' <- R.useMemo2 path loader (\_ -> loader path) R.useEffect1 loader $ do
useLoaderEffect state loader' if isNothing loaded then
pure $ maybe (loadingSpinner {}) render (fst state) R2.affEffect "G.H.Loader.useAff" $
loader >>= (liftEffect <<< setLoaded <<< const <<< Just)
else pure R.nothing
pure loaded
useLoader :: forall path st. path -> (path -> Aff st) -> (st -> R.Element) -> R.Hooks R.Element
useLoader path loader render
= maybe' (\_ -> loadingSpinner {}) render
<$> (useAff =<< R.useMemo2 path loader (\_ -> loader path))
useLoader2 :: forall path state. R.State path -> (path -> Aff state) -> (state -> R.Element) -> R.Hooks R.Element useLoader2 :: forall path st. R.State path -> (path -> Aff st) -> (st -> R.Element) -> R.Hooks R.Element
useLoader2 path loader render = do useLoader2 path loader render = do
state <- R.useState' Nothing state <- R.useState' Nothing
useLoaderEffect2 path state loader useLoaderEffect2 path state loader
pure $ maybe (loadingSpinner {}) render (fst state) pure $ maybe (loadingSpinner {}) render (fst state)
useLoaderEffect :: forall state. R.State (Maybe state) -> Aff state -> R.Hooks Unit useLoaderEffect :: forall state. Aff state -> R.State (Maybe state) -> R.Hooks Unit
useLoaderEffect (state /\ setState) loader = do useLoaderEffect loader (state /\ setState) = do
R.useEffect2 state loader $ do R.useEffect2 state loader $ do
if isNothing state then if isNothing state then
R2.affEffect "useLoader" $ loader >>= (liftEffect <<< setState <<< const <<< Just) R2.affEffect "G.H.Loader.useLoader" $ loader >>= (liftEffect <<< setState <<< const <<< Just)
else pure R.nothing else pure R.nothing
useLoaderEffect' :: forall state. Aff state -> R.Hooks (R.State (Maybe state)) useLoaderEffect' :: forall state. Aff state -> R.Hooks (R.State (Maybe state))
useLoaderEffect' aff = do useLoaderEffect' aff = do
state <- R.useState' Nothing state <- R.useState' Nothing
useLoaderEffect state aff useLoaderEffect aff state
pure state pure state
useLoaderEffect2 :: forall st path. R.State path -> R.State (Maybe st) -> (path -> Aff st) -> R.Hooks Unit useLoaderEffect2 :: forall st path. R.State path -> R.State (Maybe st) -> (path -> Aff st) -> R.Hooks Unit
useLoaderEffect2 path state loader = useRepointer path loader >>= useLoaderEffect state useLoaderEffect2 path state loader = do
aff <- useRepointer path loader
useLoaderEffect aff state
useRepointer :: forall path st. R.State path -> (path -> Aff st) -> R.Hooks (Aff st) useRepointer :: forall path st. R.State path -> (path -> Aff st) -> R.Hooks (Aff st)
useRepointer path@(path' /\ _) loader = R.useMemo1 path' (\_ -> loader path') useRepointer path@(path' /\ _) loader = R.useMemo2 loader path' (\_ -> loader path')
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