Lists.purs 5.06 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.Config.REST (logRESTError)
16
import Gargantext.Hooks.Loader (useLoader)
17
import Gargantext.Sessions (WithSession, WithSessionContext, Session, sessionId, getCacheState, setCacheState)
18
import Gargantext.Types as GT
19
import Gargantext.Utils.Reactix as R2
20 21 22 23
import Reactix as R
import Reactix.DOM.HTML as H
import Record as Record
import Toestand as T
24

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

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

35
type Props = WithSession CommonPropsNoSession
36 37 38

type CommonPropsSessionContext = WithSessionContext CommonPropsNoSession

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

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

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

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

64 65 66 67 68 69 70 71
    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

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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
90
                              , boxes
91 92 93 94 95 96 97
                              , cacheState
                              , corpusData
                              , corpusId
                              , key: "listsLayoutWithKey-tabs-" <> (show cacheState')
                              , session
                              }
                            ] }
98
    where
99
      errorHandler = logRESTError here "[listsLayoutWithKey]"
100 101 102
      afterCacheStateChange cacheState = do
        launchAff_ $ clearCache unit
        sessionUpdate $ setCacheState session nodeId cacheState
103

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

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

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

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

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

      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 } []
      ]

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

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