Common.purs 2.23 KB
Newer Older
1 2
module Gargantext.Components.Nodes.Corpus.Chart.Common where

3
import Data.Argonaut (class DecodeJson, class EncodeJson)
4 5
import Data.Tuple (fst, Tuple(..))
import Data.Tuple.Nested ((/\))
6 7 8 9 10
import Effect.Aff (Aff)
import Reactix as R

import Gargantext.Prelude

11
import Gargantext.Components.Nodes.Corpus.Chart.Types
12
import Gargantext.Hooks.Loader (HashedResponse, useLoader, useLoaderWithCache)
13 14 15
import Gargantext.Sessions (Session)

type MetricsLoadViewProps a = (
16 17
    getMetrics :: Session -> Tuple Reload (Record Path) -> Aff a
  , loaded :: Session -> Record Path -> R.State Reload -> a -> R.Element
18 19 20 21 22 23 24 25 26
  | MetricsProps
  )

metricsLoadView :: forall a. Record (MetricsLoadViewProps a) -> R.Element
metricsLoadView p = R.createElement metricsLoadViewCpt p []

metricsLoadViewCpt :: forall a. R.Component (MetricsLoadViewProps a)
metricsLoadViewCpt = R.hooksComponent "G.C.N.C.C.metricsLoadView" cpt
  where
27
    cpt { getMetrics, loaded, path, reload, session } _ = do
28
      useLoader (fst reload /\ path) (getMetrics session) $ \l ->
29
        loaded session path reload l
30 31

type MetricsWithCacheLoadViewProps a = (
32 33 34 35 36
    keyFunc :: Tuple Reload (Record Path) -> String
  , getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse a)
  , getMetricsMD5 :: Session -> Tuple Reload (Record Path) -> Aff String
  , loaded :: Session -> Record Path -> R.State Reload -> a -> R.Element
  | MetricsProps
37 38 39
  )

metricsWithCacheLoadView :: forall a. DecodeJson a => EncodeJson a =>
40
                            Record (MetricsWithCacheLoadViewProps a) -> R.Element
41 42
metricsWithCacheLoadView p = R.createElement metricsWithCacheLoadViewCpt p []

43
metricsWithCacheLoadViewCpt :: forall a. DecodeJson a => EncodeJson a => R.Component (MetricsWithCacheLoadViewProps a)
44 45
metricsWithCacheLoadViewCpt = R.hooksComponent "G.C.N.C.C.metricsWithCacheLoadView" cpt
  where
46 47
    cpt { getMetrics, getMetricsMD5, keyFunc, loaded, path, reload, session } _ = do
      useLoaderWithCache (fst reload /\ path) (metricsKeyFunc keyFunc) (getMetricsMD5 session) (getMetrics session) $ \l ->
48
        loaded session path reload l
49 50
    metricsKeyFunc keyFunc st@(_ /\ { corpusId, listId, tabType }) =
     "metrics-" <> (show tabType) <> "-" <> (show corpusId) <> "-" <> (show listId) <> "--" <> (keyFunc st)