Frame.purs 2.3 KB
Newer Older
arturo's avatar
arturo committed
1 2 3
module Gargantext.Components.Nodes.Frame
  ( node
  ) where
4

Karen Konou's avatar
Karen Konou committed
5 6
import Gargantext.Prelude

arturo's avatar
arturo committed
7 8 9 10 11 12
import Data.Maybe (Maybe(..), isJust)
import Data.Tuple.Nested ((/\))
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Frame.Layout (layout)
import Gargantext.Components.Frame.Types (Hyperdata)
import Gargantext.Components.Node (NodePoly)
13
import Gargantext.Config.REST (AffRESTError, logRESTError)
arturo's avatar
arturo committed
14 15
import Gargantext.Hooks.Loader (useLoaderEffect)
import Gargantext.Hooks.Session (useSession)
16
import Gargantext.Routes (SessionRoute(NodeAPI))
arturo's avatar
arturo committed
17
import Gargantext.Sessions (Session, get)
18
import Gargantext.Types (NodeType(..))
19
import Gargantext.Utils.Reactix as R2
20
import Gargantext.Utils.Toestand as T2
21
import Reactix as R
22 23

type Props =
24 25
  ( nodeId   :: Int
  , nodeType :: NodeType
26 27
  )

arturo's avatar
arturo committed
28 29
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Frame"
30

arturo's avatar
arturo committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
node :: R2.Leaf ( key :: String | Props )
node = R2.leaf nodeCpt

nodeCpt :: R.Component ( key :: String | Props )
nodeCpt = here.component "node" cpt where
  cpt { nodeId
      , nodeType
      } _ = do
    -- | States
    -- |
    session <- useSession

    state'  /\ state  <- R2.useBox' Nothing
    reload' /\ reload <- R2.useBox' T2.newReload

    -- | Hooks
    -- |
    useLoaderEffect
      { errorHandler: logRESTError here "[frameLayout]"
      , loader: loadframeWithReload
      , path:
          { nodeId
          , reload: reload'
          , session
          }
      , state
      }

    -- | Render
    -- |
    pure $

      B.cloak
      { isDisplayed: isJust state'
      , idlingPhaseDuration: Just 150
      , cloakSlot:
          B.preloader
          {}

      , defaultSlot:
          R2.fromMaybe state' \frame ->
            layout
            { frame
            , nodeId
            , reload
            , nodeType
            }
      }



-----------------------------------------------------------

type LoadProps =
  ( nodeId  :: Int
  , session :: Session
87 88
  )

arturo's avatar
arturo committed
89 90 91
type ReloadProps =
  ( reload :: T2.Reload
  | LoadProps
92 93
  )

94
-- Just to make reloading effective
95
loadframeWithReload :: Record ReloadProps -> AffRESTError (NodePoly Hyperdata)
arturo's avatar
arturo committed
96 97 98 99
loadframeWithReload { nodeId, session } = loadframe { nodeId, session }

loadframe :: Record LoadProps -> AffRESTError (NodePoly Hyperdata)
loadframe { nodeId, session } = get session $ NodeAPI Node (Just nodeId) ""