SessionLoader.purs 1.15 KB
Newer Older
James Laver's avatar
James Laver committed
1 2 3
-- | A component that loads the session specified in the route and provides it to its child.
-- |
-- | If the session cannot be loaded, displays the homepage.
4 5
module Gargantext.Components.SessionLoader
where
James Laver's avatar
James Laver committed
6 7 8 9

import Data.Maybe (Maybe(..))
import Reactix as R
import Toestand as T
10 11 12

import Gargantext.Prelude

James Laver's avatar
James Laver committed
13 14 15 16 17 18 19 20
import Gargantext.Sessions as Sessions
import Gargantext.Sessions (Session, Sessions)
import Gargantext.Types (SessionId)
import Gargantext.Utils.Reactix as R2

here :: R2.Here
here = R2.here "Gargantext.Components.SessionWrapper"

James Laver's avatar
James Laver committed
21
type Props =
22
  (
23 24
    context   :: R.Context Session
  , fallback  :: R.Element
25
  , sessionId :: SessionId
26
  , sessions  :: T.Box Sessions
27
  )
James Laver's avatar
James Laver committed
28

James Laver's avatar
James Laver committed
29
sessionWrapper :: R2.Component Props
James Laver's avatar
James Laver committed
30 31
sessionWrapper = R.createElement sessionWrapperCpt

James Laver's avatar
James Laver committed
32
sessionWrapperCpt :: R.Component Props
James Laver's avatar
James Laver committed
33
sessionWrapperCpt = here.component "sessionWrapper" cpt where
34
  cpt { fallback, context, sessionId, sessions } content = do
35 36 37
    sessions' <- T.useLive T.unequal sessions
    pure $ cp sessions'
    where
James Laver's avatar
James Laver committed
38
      cp sessions' = c $ Sessions.lookup sessionId sessions' where
39
        c (Just session) = (R.provideContext context session content)
James Laver's avatar
James Laver committed
40
        c Nothing = fallback