Loader.purs 1.11 KB
Newer Older
1 2
module Gargantext.Components.Loader where

3
import Prelude
4
import Data.Maybe (Maybe(..), maybe')
5
import Data.Tuple.Nested ((/\))
6
import Effect.Aff (Aff)
7 8
import Effect.Class (liftEffect)
import Reactix as R
9

10
import Gargantext.Components.LoadingSpinner (loadingSpinner)
11 12 13
import Gargantext.Utils.Reactix as R2

thisModule = "Gargantext.Components.Loader"
14 15 16 17 18 19

type Props path loaded =
  ( path  :: path
  , load  :: path -> Aff loaded
  , paint :: loaded -> R.Element )

20 21 22 23
loader :: forall path loaded. path
                            -> (path -> Aff loaded)
                            -> (loaded -> R.Element)
                            -> R.Element
24 25 26 27
loader path load paint =
  R.createElement loaderCpt {path,load,paint} []

loaderCpt :: forall path loaded. R.Component (Props path loaded)
28
loaderCpt = R.hooksComponentWithModule thisModule "loader" cpt where
29 30 31 32 33 34
  cpt {path, load, paint} _ = do
    (loaded /\ setLoaded) <- R.useState' Nothing
    R.useEffect3 path load paint $ do
      R2.affEffect "G.H.Loader.useAff" $
        load path >>= (liftEffect <<< setLoaded <<< const <<< Just)
    pure $ maybe' (\_ -> loadingSpinner {}) paint loaded