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

3
import Gargantext.Prelude (bind, pure, unit, ($), (<>))
4
import Data.Array as A
5
import Data.Maybe (Maybe(..), fromMaybe)
6
import Data.Tuple (fst)
7
import Data.Tuple.Nested ((/\))
8
import Effect.Class (liftEffect)
9
import Reactix as R
10
import Reactix.DOM.HTML as H
11
import Record as Record
James Laver's avatar
James Laver committed
12 13
import Record.Extra as RX
import Toestand as T
14 15


16
import Gargantext.AsyncTasks as GAT
17
import Gargantext.Components.NgramsTable as NT
18
import Gargantext.Components.NgramsTable.Core as NTC
19
import Gargantext.Components.Nodes.Corpus.Types (CorpusData)
20 21 22
import Gargantext.Components.Nodes.Corpus.Chart.Metrics (metrics)
import Gargantext.Components.Nodes.Corpus.Chart.Pie  (pie, bar)
import Gargantext.Components.Nodes.Corpus.Chart.Tree (tree)
23
import Gargantext.Components.Nodes.Corpus.Chart.Utils (mNgramsTypeFromTabType)
24
import Gargantext.Components.Nodes.Lists.Types
25
import Gargantext.Components.Search as S
26
import Gargantext.Components.Tab as Tab
27
import Gargantext.Sessions (Session)
28 29
import Gargantext.Types
  ( ChartType(..), CTabNgramType(..), Mode(..), TabSubType(..), TabType(..), modeTabType )
30
import Gargantext.Utils.Reactix as R2
James Laver's avatar
James Laver committed
31
import Gargantext.Utils.Toestand as T2
32

James Laver's avatar
James Laver committed
33 34
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Lists.Tabs"
35

36
type Props = (
37
    cacheState        :: T.Box CacheState
38 39
  , corpusData        :: CorpusData
  , corpusId          :: Int
40 41
  , reloadForest      :: T.Box T2.Reload
  , reloadRoot        :: T.Box T2.Reload
42 43
  , session           :: Session
  , sidePanelTriggers :: Record SidePanelTriggers
44
  , tasks             :: T.Box GAT.Storage
45
  )
46

James Laver's avatar
James Laver committed
47
type PropsWithKey = ( key :: String | Props )
48 49

tabs :: Record PropsWithKey -> R.Element
50 51
tabs props = R.createElement tabsCpt props []

52
tabsCpt :: R.Component PropsWithKey
James Laver's avatar
James Laver committed
53 54 55 56
tabsCpt = here.component "tabs" cpt where
  cpt props _ = do
    (selected /\ setSelected) <- R.useState' 0
    pure $ Tab.tabs { selected, tabs: tabs' } where
57 58 59 60
      tabs' = [ "Terms"      /\ view Terms []
              , "Authors"    /\ view Authors []
              , "Institutes" /\ view Institutes []
              , "Sources"    /\ view Sources []
James Laver's avatar
James Laver committed
61 62
              ]
      common = RX.pick props :: Record Props
63
      view mode = ngramsView $ Record.merge common { mode }
64 65 66

type NgramsViewProps = ( mode :: Mode | Props )

67 68
ngramsView :: R2.Component NgramsViewProps
ngramsView = R.createElement ngramsViewCpt
69 70

ngramsViewCpt :: R.Component NgramsViewProps
James Laver's avatar
James Laver committed
71
ngramsViewCpt = here.component "ngramsView" cpt where
72 73 74 75 76 77 78 79 80 81
  cpt props@{ cacheState
            , corpusData: { defaultListId }
            , corpusId
            , reloadForest
            , reloadRoot
            , mode
            , session
            , sidePanelTriggers
            , tasks } _ = do
      chartsReload <- T.useBox T2.newReload
82

83 84
      path <- T.useBox $ NTC.initialPageParams props.session initialPath.corpusId [initialPath.listId] initialPath.tabType
      { listIds, nodeId, params, tabType } <- T.useLive T.unequal path
James Laver's avatar
James Laver committed
85
      let path' = {
86 87 88 89
          corpusId: nodeId
        , limit: params.limit
        , listId: fromMaybe defaultListId $ A.head listIds
        , tabType: tabType
90 91
        }
      let chartParams = {
James Laver's avatar
James Laver committed
92 93 94 95
          corpusId: path'.corpusId
        , limit: Just path'.limit
        , listId: path'.listId
        , tabType: path'.tabType
96
        }
97 98

      pure $ R.fragment
99
        ( charts chartParams tabNgramType
100
        <> [ NT.mainNgramsTable { afterSync: afterSync chartsReload
101
                                , cacheState
102
                                , defaultListId
James Laver's avatar
James Laver committed
103
                                , path
104 105
                                , reloadForest
                                , reloadRoot
106
                                , session
107
                                , sidePanelTriggers
108
                                , tabNgramType
109
                                , tabType
110
                                , tasks
111
                                , withAutoUpdate: false
112
                                } []
113
           ]
114
        )
115
      where
116
        afterSync chartsReload _ = do
117 118
          case mNgramsType of
            Just ngramsType -> do
119 120 121 122
              -- NOTE: No need to recompute chart, after ngrams are sync this
              -- should be recomputed already
              -- We just refresh it
              -- _ <- recomputeChart session chartType ngramsType corpusId listId
123
              liftEffect $ T2.reload chartsReload
124 125
            Nothing         -> pure unit

126
        tabNgramType = modeTabType mode
127
        tabType      = TabCorpus (TabNgramType tabNgramType)
128
        mNgramsType  = mNgramsTypeFromTabType tabType
129
        listId       = defaultListId
130 131
        initialPath  = { corpusId
                       -- , limit: Just 1000
132 133 134
                       , listId
                       , tabType
                       }
135

136
        charts params CTabTerms = [
137
          H.div {className: "row"}
Alexandre Delanoë's avatar
Alexandre Delanoë committed
138
                [ H.div {className: "col-12 d-flex justify-content-center"}
139 140 141 142 143 144 145
                  [ H.img { src: "images/Gargantextuel-212x300.jpg"
                          , id: "funnyimg"
                        }
                  ]
                ]

          {-
146
              R2.select { className: "form-control"
147 148
                        , defaultValue: show chartType
                        , on: { change: \e -> setChartType
149 150 151
                                             $ const
                                             $ fromMaybe Histo
                                             $ chartTypeFromString
152
                                             $ R.unsafeEventValue e
153 154
                              }
                        } [
155 156 157
                H.option { value: show Histo     } [ H.text $ show Histo     ]
              , H.option { value: show Scatter   } [ H.text $ show Scatter   ]
              , H.option { value: show ChartBar  } [ H.text $ show ChartBar  ]
158
              , H.option { value: show ChartPie  } [ H.text $ show ChartPie  ]
159 160 161
              , H.option { value: show ChartTree } [ H.text $ show ChartTree ]
              ]
            ]
162
          ]
163
        , getChartFunction chartType $ { path: params, session }
164
        -}
165
        ]
166
        charts params _        = [ chart params mode ]
167

168 169 170 171
        chart path Authors    = pie     { path, session }
        chart path Institutes = tree    { path, session }
        chart path Sources    = bar     { path, session }
        chart path Terms      = metrics { path, session }