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

Refactor G.Hooks.Loader - incl new useAff

parent f9df8e9f
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.Nested ((/\))
import Gargantext.Prelude
......@@ -10,36 +12,46 @@ import Reactix as R
import Gargantext.Utils.Reactix as R2
import Gargantext.Components.LoadingSpinner (loadingSpinner)
useLoader :: forall path state. path -> (path -> Aff state) -> (state -> R.Element) -> R.Hooks R.Element
useLoader path loader render = do
state <- R.useState' Nothing
loader' <- R.useMemo2 path loader (\_ -> loader path)
useLoaderEffect state loader'
pure $ maybe (loadingSpinner {}) render (fst state)
useAff :: forall st. Aff st -> R.Hooks (Maybe st)
useAff loader = do
(loaded /\ setLoaded) <- R.useState' Nothing
R.useEffect1 loader $ do
if isNothing loaded then
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
state <- R.useState' Nothing
useLoaderEffect2 path state loader
pure $ maybe (loadingSpinner {}) render (fst state)
useLoaderEffect :: forall state. R.State (Maybe state) -> Aff state -> R.Hooks Unit
useLoaderEffect (state /\ setState) loader = do
useLoaderEffect :: forall state. Aff state -> R.State (Maybe state) -> R.Hooks Unit
useLoaderEffect loader (state /\ setState) = do
R.useEffect2 state loader $ do
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
useLoaderEffect' :: forall state. Aff state -> R.Hooks (R.State (Maybe state))
useLoaderEffect' aff = do
state <- R.useState' Nothing
useLoaderEffect state aff
useLoaderEffect aff state
pure state
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 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