Tree.purs 3.51 KB
Newer Older
1
module Gargantext.Components.Nodes.Corpus.Chart.Tree where
2

3
import Data.Generic.Rep (class Generic)
4
import Data.Maybe (Maybe(..))
5
import Data.Newtype (class Newtype)
6
import Data.Tuple.Nested ((/\))
7 8
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Font (mkTooltip, templateFormatter)
9
import Gargantext.Components.Charts.Options.Series (TreeNode, Trees(..), mkTree)
10 11
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsWithCacheLoadView)
import Gargantext.Components.Nodes.Corpus.Chart.Types (MetricsProps, Path, Props, ReloadPath)
12
import Gargantext.Config.REST (AffRESTError)
13
import Gargantext.Hooks.Loader (HashedResponse(..))
14
import Gargantext.Prelude
15
import Gargantext.Routes (SessionRoute(..))
16
import Gargantext.Sessions (Session, get)
17
import Gargantext.Types (ChartType(..))
18
import Gargantext.Utils.CacheAPI as GUC
19
import Gargantext.Utils.Reactix as R2
20
import Gargantext.Utils.Toestand as T2
21 22 23 24
import Reactix as R
import Reactix.DOM.HTML as H
import Simple.JSON as JSON
import Toestand as T
25

26

27
here :: R2.Here
28
here = R2.here "Gargantext.Components.Nodes.Corpus.Chart.Tree"
29

30 31
newtype Metrics = Metrics {
    "data" :: Array TreeNode
32
  }
33 34 35 36
derive instance Generic Metrics _
derive instance Newtype Metrics _
derive newtype instance JSON.ReadForeign Metrics
derive newtype instance JSON.WriteForeign Metrics
37

38
type Loaded  = Array TreeNode
39

40 41 42 43 44 45
type LoadedProps =
  ( metrics :: Array TreeNode
  | MetricsProps )

scatterOptions :: Record LoadedProps -> Options
scatterOptions { onClick, onInit, metrics: nodes } = Options
46 47 48
  { mainTitle : "Tree"
  , subTitle  : "Tree Sub Title"
  , xAxis     : xAxis' []
49
  , yAxis     : yAxis' { position : "", show: false, min:0}
50
  , series    : [ mkTree TreeMap nodes]
51 52
  , addZoom   : false
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
53 54
  , onClick
  , onInit
55 56
-- TODO improve the formatter:
-- https://ecomfe.github.io/echarts-examples/public/editor.html?c=treemap-obama
57

58
  }
59

60
getMetricsHash :: Session -> ReloadPath -> AffRESTError String
61
getMetricsHash session (_ /\ { corpusId, listId, tabType }) = do
62
  get session $ ChartHash { chartType: ChartTree, listId: mListId, tabType } (Just corpusId)
63 64
  where
    mListId = if listId == 0 then Nothing else (Just listId)
65

66
chartUrl :: Record Path -> SessionRoute
67 68 69
chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartTree, limit, listId: mListId, tabType} (Just corpusId)
  where
    mListId = if listId == 0 then Nothing else (Just listId)
70 71 72 73 74

handleResponse :: HashedResponse Metrics -> Loaded
handleResponse (HashedResponse { value: Metrics ms }) = ms."data"

mkRequest :: Session -> ReloadPath -> GUC.Request
75
mkRequest session (_ /\ path) = GUC.makeGetRequest session $ chartUrl path
76

77
tree :: Record Props -> R.Element
78
tree props = R.createElement treeCpt props []
79
treeCpt :: R.Component Props
80
treeCpt = here.component "tree" cpt
81
  where
82
    cpt { path, session, onClick, onInit } _ = do
83
      reload <- T.useBox T2.newReload
84

85
      pure $ metricsWithCacheLoadView
86
        { getMetricsHash
87 88 89 90 91 92
        , handleResponse
        , loaded
        , mkRequest: mkRequest session
        , path
        , reload
        , session
93 94
        , onClick
        , onInit
95
        }
96

97 98 99 100
loaded :: R2.Leaf LoadedProps
loaded = R2.leaf loadedCpt
loadedCpt :: R.Component LoadedProps
loadedCpt = here.component "loaded" cpt where
101
  cpt p _ = do
102 103
    pure $ H.div {} [
      {-  U.reloadButton reload
104
  , U.chartUpdateButton { chartType: ChartTree, path, reload, session }
105 106
  , -} chart $ scatterOptions p
       ]
107