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

3 4
import Gargantext.Prelude

5
import Data.Generic.Rep (class Generic)
6
import Data.Maybe (Maybe(..))
7
import Data.Newtype (class Newtype)
8
import Data.Tuple.Nested ((/\))
9 10
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Font (mkTooltip, templateFormatter)
11
import Gargantext.Components.Charts.Options.Series (TreeNode, Trees(..), mkTree)
12 13
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsWithCacheLoadView)
import Gargantext.Components.Nodes.Corpus.Chart.Types (MetricsProps, Path, Props, ReloadPath)
14
import Gargantext.Config.REST (AffRESTError)
15
import Gargantext.Hooks.Loader (HashedResponse(..))
16
import Gargantext.Routes (SessionRoute(..))
17
import Gargantext.Sessions (Session, get)
18
import Gargantext.Types (ChartType(..))
19
import Gargantext.Utils.CacheAPI as GUC
20
import Gargantext.Utils.Reactix as R2
21
import Gargantext.Utils.Toestand as T2
22 23 24 25
import Reactix as R
import Reactix.DOM.HTML as H
import Simple.JSON as JSON
import Toestand as T
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
scatterOptions :: Record MetricsProps -> Array TreeNode -> Options
scatterOptions { onClick, onInit } nodes = Options
42 43 44
  { mainTitle : "Tree"
  , subTitle  : "Tree Sub Title"
  , xAxis     : xAxis' []
45
  , yAxis     : yAxis' { position : "", show: false, min:0}
46
  , series    : [ mkTree TreeMap nodes]
47 48
  , addZoom   : false
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
49 50
  , onClick
  , onInit
51 52
-- TODO improve the formatter:
-- https://ecomfe.github.io/echarts-examples/public/editor.html?c=treemap-obama
53

54
  }
55

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

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

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

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

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

81 82
      pure $ metricsWithCacheLoadView
        { boxes
83
        , getMetricsHash
84 85 86 87 88 89
        , handleResponse
        , loaded
        , mkRequest: mkRequest session
        , path
        , reload
        , session
90 91
        , onClick
        , onInit
92
        }
93

94
loaded :: Record MetricsProps -> Loaded -> R.Element
95
loaded p loaded' =
96
  H.div {} [
97
  {-  U.reloadButton reload
98
  , U.chartUpdateButton { chartType: ChartTree, path, reload, session }
99
  , -} chart (scatterOptions p loaded')
100
  ]