Predefined.purs 3.28 KB
Newer Older
1 2
module Gargantext.Components.Nodes.Corpus.Chart.Predefined where

3
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson)
4 5 6
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Ord (genericCompare)
import Data.Generic.Rep.Show (genericShow)
7
import Data.Maybe (Maybe(..), fromMaybe)
8 9
import Reactix as R

10
import Gargantext.Prelude
11
import Gargantext.Components.Nodes.Corpus.Chart.Histo   (histo)
12
import Gargantext.Components.Nodes.Corpus.Chart.Metrics (metrics)
13 14
import Gargantext.Components.Nodes.Corpus.Chart.Pie     (pie)
import Gargantext.Components.Nodes.Corpus.Chart.Tree    (tree)
15 16 17 18 19 20 21
import Gargantext.Sessions (Session)
import Gargantext.Types (NodeID, Mode(..), TabSubType(..), TabType(..), modeTabType)


data PredefinedChart =
    CDocsHistogram
  | CAuthorsPie
22
  | CSourcesBar
23 24
  | CInstitutesTree
  | CTermsMetrics
25

26
derive instance genericPredefinedChart :: Generic PredefinedChart _
27

28 29
instance showPredefinedChart :: Show PredefinedChart where
  show = genericShow
30

31
derive instance eqPredefinedChart :: Eq PredefinedChart
32

33 34
instance ordPredefinedChart :: Ord PredefinedChart where
  compare = genericCompare
35

36 37 38
instance decodePredefinedChart :: DecodeJson PredefinedChart where
  decodeJson json = do
    obj <- decodeJson json
39 40
    pure $ fromMaybe CDocsHistogram $ read obj

41 42
instance encodePredefinedChart :: EncodeJson PredefinedChart where
  encodeJson c = encodeJson $ show c
43

44 45 46
instance readPredefinedChart :: Read PredefinedChart where
  read "CDocsHistogram"  = Just CDocsHistogram
  read "CAuthorsPie"     = Just CAuthorsPie
47
  read "CSourcesBar"     = Just CSourcesBar
48 49 50
  read "CInstitutesTree" = Just CInstitutesTree
  read "CTermsMetrics"   = Just CTermsMetrics
  read _                 = Nothing
51 52 53


allPredefinedCharts :: Array PredefinedChart
54 55
allPredefinedCharts =
  [ CDocsHistogram
56 57 58
  , CAuthorsPie
  , CTermsMetrics
  , CInstitutesTree
59
  , CSourcesBar
60 61 62 63
  ]


type Params =
64 65
  ( corpusId :: NodeID
  , session  :: Session
66
  -- optinal params
67 68
  , limit    :: Maybe Int
  , listId   :: Maybe Int
69 70 71
  )

render :: PredefinedChart -> Record Params -> R.Element
72
render CDocsHistogram { corpusId, listId, session } = histo { path, session }
73 74
  where
    path = { corpusId
75 76
           , listId: fromMaybe 0 listId
           , limit: Nothing
77 78
           , tabType: TabCorpus TabDocs
           }
79
render CAuthorsPie { corpusId, listId, session } = pie { path, session }
80 81
  where
    path = { corpusId
82 83
           , listId: fromMaybe 0 listId
           , limit: Nothing
84 85 86 87 88 89 90 91 92 93 94 95 96 97
           , tabType: TabCorpus (TabNgramType $ modeTabType Authors)
           }
render CInstitutesTree { corpusId, limit, listId, session } = tree { path, session }
  where
    path = { corpusId
           , limit
           , listId: fromMaybe 0 listId
           , tabType: TabCorpus (TabNgramType $ modeTabType Institutes)
           }
render CTermsMetrics { corpusId, limit, listId, session } = metrics { path, session }
  where
    path = { corpusId
           , limit
           , listId: fromMaybe 0 listId
98
           , tabType: TabCorpus (TabNgramType $ modeTabType Terms)
99
           }
100 101 102 103 104 105 106 107 108 109 110
render CSourcesBar { corpusId, limit, listId, session } = metrics { path, session }
  where
    path = { corpusId
           , limit
           , listId: fromMaybe 0 listId
           , tabType: TabCorpus (TabNgramType $ modeTabType Sources)
           }