Loader.purs 1.13 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
import Gargantext.Utils.Reactix as R2

13
thisModule :: String
14
thisModule = "Gargantext.Components.Loader"
15 16 17 18 19 20

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

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

loaderCpt :: forall path loaded. R.Component (Props path loaded)
29
loaderCpt = R.hooksComponentWithModule thisModule "loader" cpt where
30 31 32 33 34 35
  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