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

3
import Prelude (bind, pure, ($), (==))
4 5
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (~>), (:=))
import Data.Argonaut.Core (jsonEmptyObject)
6
import Data.Maybe (Maybe(..))
7
import Data.Tuple.Nested ((/\))
8
import Effect.Aff (Aff)
9 10
import Reactix as R
import Reactix.DOM.HTML as H
11
import Toestand as T
12

13 14 15
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)
16 17
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsWithCacheLoadView)
import Gargantext.Components.Nodes.Corpus.Chart.Types (MetricsProps, Path, Props, ReloadPath)
18
import Gargantext.Hooks.Loader (HashedResponse(..))
19
import Gargantext.Routes (SessionRoute(..))
20
import Gargantext.Sessions (Session, get)
21
import Gargantext.Types (ChartType(..))
22
import Gargantext.Utils.CacheAPI as GUC
23
import Gargantext.Utils.Reactix as R2
24
import Gargantext.Utils.Toestand as T2
25

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

29 30
newtype Metrics = Metrics {
    "data" :: Array TreeNode
31 32 33 34 35
  }

instance decodeMetrics :: DecodeJson Metrics where
  decodeJson json = do
    obj <- decodeJson json
36
    d   <- obj .: "data"
37
    pure $ Metrics { "data": d }
38 39 40 41 42
instance encodeMetrics :: EncodeJson Metrics where
  encodeJson (Metrics { "data": d }) =
       "data" := encodeJson d
    ~> jsonEmptyObject

43
type Loaded  = Array TreeNode
44

45 46 47 48 49
scatterOptions :: Array TreeNode -> Options
scatterOptions nodes = Options
  { mainTitle : "Tree"
  , subTitle  : "Tree Sub Title"
  , xAxis     : xAxis' []
50
  , yAxis     : yAxis' { position : "", show: false, min:0}
51
  , series    : [ mkTree TreeMap nodes]
52 53
  , addZoom   : false
  , tooltip   : mkTooltip { formatter: templateFormatter "{b0}" }
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 String
60
getMetricsHash session (_ /\ { corpusId, limit, 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 74 75

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

mkRequest :: Session -> ReloadPath -> GUC.Request
mkRequest session (_ /\ path@{ corpusId, limit, listId, tabType }) = GUC.makeGetRequest session $ chartUrl path

76
tree :: Record Props -> R.Element
77
tree props = R.createElement treeCpt props []
78

79
treeCpt :: R.Component Props
80
treeCpt = here.component "tree" cpt
81
  where
82
    cpt {path, session} _ = do
83
      reload <- T.useBox T2.newReload
84

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

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