Tabs.purs 6.15 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 38 39 40 41 42 43 44
    activeTab    :: T.Box Int
  , cacheState   :: T.Box CacheState
  , corpusData   :: CorpusData
  , corpusId     :: Int
  , reloadForest :: T2.ReloadS
  , reloadRoot   :: T2.ReloadS
  , session      :: Session
  , 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
tabsCpt = here.component "tabs" cpt where
54 55 56
  cpt props@{ activeTab } _ = do
    pure $ Tab.tabs { activeTab
                    , 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
  cpt props@{ cacheState
            , corpusData: { defaultListId }
            , corpusId
            , reloadForest
            , reloadRoot
            , mode
            , session
            , tasks } _ = do
      chartsReload <- T.useBox T2.newReload
81

82 83
      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
84
      let path' = {
85 86 87 88
          corpusId: nodeId
        , limit: params.limit
        , listId: fromMaybe defaultListId $ A.head listIds
        , tabType: tabType
89 90
        }
      let chartParams = {
James Laver's avatar
James Laver committed
91 92 93 94
          corpusId: path'.corpusId
        , limit: Just path'.limit
        , listId: path'.listId
        , tabType: path'.tabType
95
        }
96 97

      pure $ R.fragment
98
        ( charts chartParams tabNgramType
99
        <> [ NT.mainNgramsTable { afterSync: afterSync chartsReload
100
                                , cacheState
101
                                , defaultListId
James Laver's avatar
James Laver committed
102
                                , path
103 104
                                , reloadForest
                                , reloadRoot
105
                                , session
106
                                , tabNgramType
107
                                , tabType
108
                                , tasks
109
                                , withAutoUpdate: false
110
                                } []
111
           ]
112
        )
113
      where
114
        afterSync chartsReload _ = do
115 116
          case mNgramsType of
            Just ngramsType -> do
117 118 119 120
              -- 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
121
              liftEffect $ T2.reload chartsReload
122 123
            Nothing         -> pure unit

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

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

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

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