Utils.purs 2.15 KB
Newer Older
1
module Gargantext.Components.Nodes.Corpus.Chart.Utils where
2

3
import Data.Maybe (Maybe(..))
4
import Effect (Effect)
5 6
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
7 8 9 10
import Reactix as R
import Reactix.DOM.HTML as H

import Gargantext.Prelude
11
import Gargantext.Components.Nodes.Corpus.Chart.API (recomputeChart)
12 13 14
import Gargantext.Components.Nodes.Corpus.Chart.Types (Path)
import Gargantext.Sessions (Session)
import Gargantext.Types as T
15
import Gargantext.Utils.Reactix as R2
16
import Gargantext.Utils.Toestand as T2
17

18
here :: R2.Here
19
here = R2.here "Gargantext.Components.Nodes.Corpus.Chart.Utils"
20

21
reloadButtonWrap :: T2.ReloadS -> R.Element -> R.Element
22 23 24 25 26
reloadButtonWrap setReload el = H.div {} [
    reloadButton setReload
  , el
  ]

27
reloadButton :: T2.ReloadS -> R.Element
28 29
reloadButton reloadS = H.a { className, on: { click }, title: "Reload" } [] where
  className = "reload-btn fa fa-refresh"
30
  click _ = T2.reload reloadS
31 32


33 34 35
mNgramsTypeFromTabType :: T.TabType -> Maybe T.CTabNgramType
mNgramsTypeFromTabType (T.TabCorpus (T.TabNgramType ngramType))   = Just ngramType
mNgramsTypeFromTabType (T.TabDocument (T.TabNgramType ngramType)) = Just ngramType
36
mNgramsTypeFromTabType _                                          = Nothing
37 38


39 40
type ChartUpdateButtonProps =
  ( chartType :: T.ChartType
41 42 43
  , path      :: Record Path
  , reload    :: T2.ReloadS
  , session   :: Session
44 45 46 47 48 49
  )

chartUpdateButton :: Record ChartUpdateButtonProps -> R.Element
chartUpdateButton p = R.createElement chartUpdateButtonCpt p []

chartUpdateButtonCpt :: R.Component ChartUpdateButtonProps
50 51 52 53 54 55 56 57 58 59 60 61
chartUpdateButtonCpt = here.component "chartUpdateButton" cpt where
  cpt {  path: { corpusId, listId, tabType }
      , reload, chartType, session } _ = do
    pure $ H.a { className, on: { click }, title: "Update chart data" } []
    where
      className = "chart-update-button fa fa-database"
      click :: forall a. a -> Effect Unit
      click _ = do
        launchAff_ $ do
          case mNgramsTypeFromTabType tabType of
            Just ngramsType -> do
              _ <- recomputeChart session chartType ngramsType corpusId listId
62
              liftEffect $ T2.reload reload
63
            Nothing -> pure unit