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

3
import Data.Maybe (Maybe(..))
4
import Data.Tuple.Nested ((/\))
5
import Effect (Effect)
6 7
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
8 9 10 11 12
import Effect.Uncurried (mkEffectFn1)
import Reactix as R
import Reactix.DOM.HTML as H

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

thisModule = "Gargantext.Components.Nodes.Corpus.Chart.Utils"
20 21 22 23 24 25 26 27

reloadButtonWrap :: R.State Int -> R.Element -> R.Element
reloadButtonWrap setReload el = H.div {} [
    reloadButton setReload
  , el
  ]

reloadButton :: R.State Int -> R.Element
28 29 30
reloadButton (_ /\ setReload) = H.a { className
                                    , on: { click: onClick }
                                    , title: "Reload" } []
31
  where
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    className = "reload-btn fa fa-refresh"
    onClick _ = setReload $ (_ + 1)


type ChartUpdateButtonProps = (
    chartType :: T.ChartType
  , path :: Record Path
  , reload :: R.State Int
  , session :: Session
  )

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

chartUpdateButtonCpt :: R.Component ChartUpdateButtonProps
47
chartUpdateButtonCpt = R2.hooksComponent thisModule "chartUpdateButton" cpt
48
  where
49 50 51
    cpt { chartType
        , path: { corpusId, listId, tabType }
        , reload: (_ /\ setReload), session } _ = do
52 53 54 55 56 57 58

      pure $ H.a { className: "chart-update-button fa fa-database"
                 , on: { click: onClick }
                 , title: "Update chart data" } []
      where
        onClick :: forall a. a -> Effect Unit
        onClick _ = do
59
          launchAff_ $ do
60 61 62 63 64 65 66 67 68 69 70 71
            case mNgramsType of
              Just ngramsType -> do
                _ <- recomputeChart session chartType ngramsType corpusId listId
                liftEffect $ setReload $ (_ + 1)
              Nothing -> pure unit

        mNgramsType = case tabType of
            T.TabCorpus (T.TabNgramType ngramType)   -> Just ngramType
            T.TabCorpus _                            -> Nothing
            T.TabDocument (T.TabNgramType ngramType) -> Just ngramType
            T.TabDocument _                          -> Nothing
            T.TabPairing _                           -> Nothing