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

3
import Data.Either (Either)
4
import Data.Generic.Rep (class Generic)
5
import Data.Maybe (Maybe(..))
6
import Data.Newtype (class Newtype)
7
import Data.Tuple.Nested ((/\))
8
import Effect.Aff (Aff)
9 10
import Reactix as R
import Reactix.DOM.HTML as H
11
import Simple.JSON as JSON
12
import Toestand as T
13

14 15
import Gargantext.Prelude

16 17 18
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Series (TreeNode, Trees(..), mkTree)
import Gargantext.Components.Charts.Options.Font (mkTooltip, templateFormatter)
19 20
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsWithCacheLoadView)
import Gargantext.Components.Nodes.Corpus.Chart.Types (MetricsProps, Path, Props, ReloadPath)
21
import Gargantext.Config.REST (RESTError)
22
import Gargantext.Hooks.Loader (HashedResponse(..))
23
import Gargantext.Routes (SessionRoute(..))
24
import Gargantext.Sessions (Session, get)
25
import Gargantext.Types (ChartType(..))
26
import Gargantext.Utils.CacheAPI as GUC
27
import Gargantext.Utils.Reactix as R2
28
import Gargantext.Utils.Toestand as T2
29

30
here :: R2.Here
31
here = R2.here "Gargantext.Components.Nodes.Corpus.Chart.Tree"
32

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

41
type Loaded  = Array TreeNode
42

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

57
  }
58

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

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

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

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

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

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

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