Document.purs 2.27 KB
Newer Older
arturo's avatar
arturo committed
1 2 3 4 5 6
module Gargantext.Components.Nodes.Corpus.Document
  ( node
  ) where

import Gargantext.Prelude

7 8
import DOM.Simple (document, querySelector)
import Data.Maybe (Maybe(..), isJust, maybe)
arturo's avatar
arturo committed
9 10 11 12 13 14 15 16 17
import Data.Tuple.Nested ((/\))
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Document.API (loadData)
import Gargantext.Components.Document.Layout (layout)
import Gargantext.Components.Document.Types (LoadedData, DocPath)
import Gargantext.Config.REST (logRESTError)
import Gargantext.Hooks.Loader (useLoaderEffect)
import Gargantext.Hooks.Session (useSession)
import Gargantext.Types (CTabNgramType(..), ListId, NodeID, TabSubType(..), TabType(..))
18
import Gargantext.Utils.Reactix as R2
19
import Reactix as R
20

arturo's avatar
arturo committed
21 22
type Props =
  ( listId    :: ListId
23 24
  , mCorpusId :: Maybe NodeID
  , nodeId    :: NodeID
25
  )
26

arturo's avatar
arturo committed
27 28
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Corpus.Document"
29

arturo's avatar
arturo committed
30 31 32
node :: R2.Leaf ( key :: String | Props )
node = R2.leaf nodeCpt
nodeCpt :: R.Component ( key :: String | Props )
33 34 35 36 37
nodeCpt = R2.hereComponent here "node" hCpt where
  hCpt hp { listId
          , mCorpusId
          , nodeId
          } _ = do
arturo's avatar
arturo committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    -- | States
    -- |
    session <- useSession

    state' /\ state <- R2.useBox' (Nothing :: Maybe LoadedData)

    -- | Computed
    -- |
    let
      tabType :: TabType
      tabType = TabDocument (TabNgramType CTabTerms)

      path :: DocPath
      path = { listIds: [listId], mCorpusId, nodeId, session, tabType }

    -- | Hooks
    -- |
    useLoaderEffect
56
      { errorHandler: logRESTError hp
arturo's avatar
arturo committed
57 58 59 60 61
      , loader: loadData
      , path
      , state
      }

62 63 64 65 66 67 68 69 70 71
    -- @XXX: reset "main-page__main-route" wrapper margin
    --       see Gargantext.Components.Router) (@TODO?)
    R.useLayoutEffect1 [] do
      let mEl = querySelector document ".main-page__main-route"
      -- Mount
      mEl >>= maybe R.nothing (flip R2.addClass ["p-0"])
      -- Unmount
      pure $
        mEl >>= maybe R.nothing (flip R2.removeClass ["p-0"])

arturo's avatar
arturo committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    -- | Render
    -- |
    pure $

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

      , defaultSlot:
          R2.fromMaybe state' \loaded ->
            layout
            { loaded
            , path
88
            , session
arturo's avatar
arturo committed
89 90
            }
      }