Lists.purs 5.03 KB
Newer Older
1
module Gargantext.Components.Nodes.Lists where
2

3 4
import Gargantext.Prelude

5
import Effect (Effect)
6
import Effect.Aff (launchAff_)
7
import Gargantext.Components.App.Data (Boxes)
8
import Gargantext.Components.NgramsTable.Loader (clearCache)
9
import Gargantext.Components.Node (NodePoly(..))
10
import Gargantext.Components.Nodes.Corpus (loadCorpusWithChild)
11
import Gargantext.Components.Nodes.Corpus.Types (getCorpusInfo, CorpusInfo(..), Hyperdata(..))
12
import Gargantext.Components.Nodes.Lists.Tabs as Tabs
13
import Gargantext.Components.Nodes.Lists.Types (CacheState(..))
14
import Gargantext.Components.Table as Table
15
import Gargantext.Hooks.Loader (useLoader)
16
import Gargantext.Sessions (WithSession, WithSessionContext, Session, sessionId, getCacheState, setCacheState)
17
import Gargantext.Types as GT
18
import Gargantext.Utils.Reactix as R2
19 20 21 22
import Reactix as R
import Reactix.DOM.HTML as H
import Record as Record
import Toestand as T
23

24 25
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Lists"
26 27
--------------------------------------------------------

28
type CommonPropsNoSession =
29 30 31
  ( boxes         :: Boxes
  , nodeId        :: Int
  , sessionUpdate :: Session -> Effect Unit
32
  )
33

34
type Props = WithSession CommonPropsNoSession
35 36 37

type CommonPropsSessionContext = WithSessionContext CommonPropsNoSession

38
type WithTreeProps = ( handed :: GT.Handed | Props )
39

40 41
listsLayout :: R2.Component Props
listsLayout = R.createElement listsLayoutCpt
42
listsLayoutCpt :: R.Component Props
43
listsLayoutCpt = here.component "listsLayout" cpt where
44
  cpt props@{ nodeId, session } _ = do
45
    let sid = sessionId session
46
    pure $ listsLayoutWithKey (Record.merge props { key: show sid <> "-" <> show nodeId }) []
47

48 49 50
type KeyProps =
  ( key :: String
  | Props )
51

52 53
listsLayoutWithKey :: R2.Component KeyProps
listsLayoutWithKey = R.createElement listsLayoutWithKeyCpt
54
listsLayoutWithKeyCpt :: R.Component KeyProps
55
listsLayoutWithKeyCpt = here.component "listsLayoutWithKey" cpt where
56
  cpt { boxes: boxes@{ reloadMainPage }
57
      , nodeId
58
      , session
59
      , sessionUpdate } _ = do
60
    activeTab <- T.useBox 0
61
    _reloadMainPage' <- T.useLive T.unequal reloadMainPage
62

63 64 65 66 67 68 69 70
    let path = { nodeId, session }

    cacheState <- T.useBox $ getCacheState CacheOn session nodeId
    cacheState' <- T.useLive T.unequal cacheState

    R.useEffectOnce' $ do
      T.listen (\{ new } -> afterCacheStateChange new) cacheState

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    useLoader { errorHandler
              , path
              , loader: loadCorpusWithChild
              , render: \corpusData@{ corpusId, corpusNode: NodePoly poly } ->
                          let { date, hyperdata : Hyperdata h, name } = poly
                              CorpusInfo { authors, desc, query } = getCorpusInfo h.fields
                          in
                            R.fragment [
                              Table.tableHeaderLayout {
                                cacheState
                              , date
                              , desc
                              , key: "listsLayoutWithKey-header-" <> (show cacheState')
                              , query
                              , title: "Corpus " <> name
                              , user: authors } []
                            , Tabs.tabs {
                                activeTab
89
                              , boxes
90 91 92 93 94 95 96
                              , cacheState
                              , corpusData
                              , corpusId
                              , key: "listsLayoutWithKey-tabs-" <> (show cacheState')
                              , session
                              }
                            ] }
97
    where
98
      errorHandler err = here.log2 "[listsLayoutWithKey] RESTError" err
99 100 101
      afterCacheStateChange cacheState = do
        launchAff_ $ clearCache unit
        sessionUpdate $ setCacheState session nodeId cacheState
102

103
type SidePanelProps =
104 105
  ( session        :: Session
  , sidePanelState :: T.Box GT.SidePanelState
106 107 108 109
  )

sidePanel :: R2.Component SidePanelProps
sidePanel = R.createElement sidePanelCpt
110
sidePanelCpt :: R.Component SidePanelProps
111
sidePanelCpt = here.component "sidePanel" cpt
112
  where
113 114
    cpt { session
        , sidePanelState } _ = do
115

116
      sidePanelState' <- T.useLive T.unequal sidePanelState
117

118 119 120
      let mainStyle = case sidePanelState' of
            GT.Opened -> { display: "block" }
            _         -> { display: "none" }
121

122
      let closeSidePanel _ = T.write_ GT.Closed sidePanelState
123 124 125 126 127 128 129 130 131 132 133

      pure $ H.div { style: mainStyle } [
        H.div { className: "header" } [
          H.span { className: "btn btn-danger"
                 , on: { click: closeSidePanel } } [
            H.span { className: "fa fa-times" } []
          ]
        ]
      , sidePanelDocView { session } []
      ]

134
type SidePanelDocView = ( session :: Session )
135 136 137

sidePanelDocView :: R2.Component SidePanelDocView
sidePanelDocView = R.createElement sidePanelDocViewCpt
138
sidePanelDocViewCpt :: R.Component SidePanelDocView
139
sidePanelDocViewCpt = here.component "sidePanelDocView" cpt where
140
  cpt { } _ = do
141 142
    -- pure $ H.h4 {} [ H.text txt ]
    pure $ H.div {} [ H.text "Hello ngrams" ]