Commit 871130cb authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[toestand] more state refactoring

parent 68298c5b
module Gargantext.Components.Charts.Options.Series where
import Prelude (class Show, bind, map, pure, show, ($), (+), (<<<), (<>))
import Prelude (class Eq, class Show, bind, map, pure, show, ($), (+), (<<<), (<>), eq)
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (~>), (:=))
import Data.Argonaut.Core (jsonEmptyObject)
import Data.Array (foldl)
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..), maybe)
import Record.Unsafe (unsafeSet)
import Unsafe.Coerce (unsafeCoerce)
......@@ -186,11 +187,14 @@ toJsTree maybeSurname (TreeNode x) =
name = maybe "" (\x' -> x' <> ">") maybeSurname <> x.name
data TreeNode = TreeNode {
name :: String
children :: Array TreeNode
, name :: String
, value :: Int
, children :: Array TreeNode
}
derive instance genericTreeNode :: Generic TreeNode _
instance eqTreeNode :: Eq TreeNode where
eq (TreeNode n1) (TreeNode n2) = eq n1 n2
instance decodeTreeNode :: DecodeJson TreeNode where
decodeJson json = do
obj <- decodeJson json
......
module Gargantext.Components.DocsTable.Types where
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, jsonEmptyObject, (.:), (:=), (~>))
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (Maybe(..))
......@@ -24,6 +26,9 @@ newtype DocumentsView
, title :: String
, url :: String
}
derive instance genericDocumentsView :: Generic DocumentsView _
instance eqDocumentsView :: Eq DocumentsView where
eq = genericEq
{-
derive instance genericDocumentsView :: Generic DocumentsView _
......
......@@ -4,10 +4,11 @@
module Gargantext.Components.FacetsTable where
import Prelude
( class Show, Unit, bind, const, discard, identity, mempty, not
( class Eq, class Show, Unit, bind, const, discard, identity, mempty, not
, otherwise, pure, unit, void, ($), (*>), (<$>), (<<<), (<>), (==), (>) )
import Data.Argonaut (class EncodeJson, jsonEmptyObject, (:=), (~>))
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Generic.Rep.Show (genericShow)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Sequence (Seq)
......@@ -68,7 +69,8 @@ newtype Pair =
}
derive instance genericPair :: Generic Pair _
instance eqPair :: Eq Pair where
eq = genericEq
instance showPair :: Show Pair where
show = genericShow
......@@ -90,7 +92,8 @@ newtype DocumentsView =
}
derive instance genericDocumentsView :: Generic DocumentsView _
instance eqDocumentsView :: Eq DocumentsView where
eq = genericEq
instance showDocumentsView :: Show DocumentsView where
show = genericShow
......
......@@ -48,11 +48,13 @@ type MetricsWithCacheLoadViewProps res ret = (
| MetricsProps
)
metricsWithCacheLoadView :: forall res ret. DecodeJson res =>
metricsWithCacheLoadView :: forall res ret.
Eq ret => DecodeJson res =>
Record (MetricsWithCacheLoadViewProps res ret) -> R.Element
metricsWithCacheLoadView p = R.createElement metricsWithCacheLoadViewCpt p []
metricsWithCacheLoadViewCpt :: forall res ret. DecodeJson res =>
metricsWithCacheLoadViewCpt :: forall res ret.
Eq ret => DecodeJson res =>
R.Component (MetricsWithCacheLoadViewProps res ret)
metricsWithCacheLoadViewCpt = here.component "metricsWithCacheLoadView" cpt
where
......
......@@ -2,6 +2,8 @@ module Gargantext.Components.Nodes.Corpus.Chart.Histo where
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (~>), (:=))
import Data.Argonaut.Core (jsonEmptyObject)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Maybe (Maybe(..))
import Data.Tuple.Nested ((/\))
import Effect.Aff (Aff)
......@@ -9,6 +11,8 @@ import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
import Gargantext.Prelude (class Eq, bind, map, pure, ($), (==))
import Gargantext.Components.Charts.Options.Color (grey)
import Gargantext.Components.Charts.Options.Data (dataSerie)
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
......@@ -17,7 +21,6 @@ import Gargantext.Components.Charts.Options.Series (seriesBarD1)
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsWithCacheLoadView)
import Gargantext.Components.Nodes.Corpus.Chart.Types (MetricsProps, Path, Props, ReloadPath)
import Gargantext.Hooks.Loader (HashedResponse(..))
import Gargantext.Prelude (bind, map, pure, ($), (==))
import Gargantext.Routes (SessionRoute(..))
import Gargantext.Sessions (Session, get)
import Gargantext.Types (ChartType(..))
......@@ -31,7 +34,9 @@ here = R2.here "Gargantext.Components.Nodes.Corpus.Chart.Histo"
newtype ChartMetrics = ChartMetrics {
"data" :: HistoMetrics
}
derive instance genericChartMetrics :: Generic ChartMetrics _
instance eqChartMetrics :: Eq ChartMetrics where
eq = genericEq
instance decodeChartMetrics :: DecodeJson ChartMetrics where
decodeJson json = do
obj <- decodeJson json
......@@ -39,7 +44,9 @@ instance decodeChartMetrics :: DecodeJson ChartMetrics where
pure $ ChartMetrics { "data": d }
newtype HistoMetrics = HistoMetrics { dates :: Array String, count :: Array Number }
derive instance genericHistoMetrics :: Generic HistoMetrics _
instance eqHistoMetrics :: Eq HistoMetrics where
eq = genericEq
instance decodeHistoMetrics :: DecodeJson HistoMetrics where
decodeJson json = do
obj <- decodeJson json
......
module Gargantext.Components.Nodes.Corpus.Chart.Metrics where
import Gargantext.Prelude (bind, negate, pure, ($), (<$>), (<>))
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (~>), (:=))
import Data.Argonaut.Core (jsonEmptyObject)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Map as Map
import Data.Map (Map)
import Data.Maybe (Maybe(..))
......@@ -14,6 +14,8 @@ import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
import Gargantext.Prelude (class Eq, bind, negate, pure, ($), (<$>), (<>))
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, yAxis')
import Gargantext.Components.Charts.Options.Type (xAxis)
import Gargantext.Components.Charts.Options.Series (Series, seriesScatterD2)
......@@ -40,7 +42,9 @@ newtype Metric = Metric
, y :: Number
, cat :: TermList
}
derive instance genericMetric :: Generic Metric _
instance eqMetric :: Eq Metric where
eq = genericEq
instance decodeMetric :: DecodeJson Metric where
decodeJson json = do
obj <- decodeJson json
......@@ -49,7 +53,6 @@ instance decodeMetric :: DecodeJson Metric where
y <- obj .: "y"
cat <- obj .: "cat"
pure $ Metric { label, x, y, cat }
instance encodeMetric :: EncodeJson Metric where
encodeJson (Metric { label, x, y, cat }) =
"label" := encodeJson label
......
module Gargantext.Components.Nodes.Corpus.Chart.Pie where
import Gargantext.Prelude (bind, map, pure, ($), (==), (>))
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (~>), (:=))
import Data.Argonaut.Core (jsonEmptyObject)
import Data.Array (zip, filter)
import Data.Array as A
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Maybe (Maybe(..))
import Data.String (take, joinWith, Pattern(..), split, length)
import Data.Tuple (Tuple(..))
......@@ -14,6 +15,8 @@ import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
import Gargantext.Prelude (class Eq, bind, map, pure, ($), (==), (>))
import Gargantext.Components.Charts.Options.Color (blue)
import Gargantext.Components.Charts.Options.Data (dataSerie)
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
......@@ -47,7 +50,9 @@ newtype HistoMetrics = HistoMetrics
{ dates :: Array String
, count :: Array Number
}
derive instance genericHistoMetrics :: Generic HistoMetrics _
instance eqHistoMetrics :: Eq HistoMetrics where
eq = genericEq
instance decodeHistoMetrics :: DecodeJson HistoMetrics where
decodeJson json = do
obj <- decodeJson json
......
......@@ -81,6 +81,7 @@ treeCpt = here.component "tree" cpt
where
cpt {path, session} _ = do
reload <- T.useBox T2.newReload
pure $ metricsWithCacheLoadView {
getMetricsHash
, handleResponse
......
......@@ -35,10 +35,8 @@ newtype Field a =
type FTField = Field FieldType
derive instance genericFTField :: Generic (Field FieldType) _
instance eqFTField :: Eq (Field FieldType) where
eq = genericEq
instance showFTField :: Show (Field FieldType) where
show = genericShow
......
......@@ -8,6 +8,7 @@ import Effect.Aff (Aff, launchAff_, throwError)
import Effect.Class (liftEffect)
import Effect.Exception (error)
import Reactix as R
import Toestand as T
import Gargantext.Components.LoadingSpinner (loadingSpinner)
import Gargantext.Prelude
......@@ -77,35 +78,38 @@ type LoaderWithCacheAPIProps path res ret = (
useLoaderWithCacheAPI :: forall path res ret.
Eq path => DecodeJson res =>
Eq ret => Eq path => DecodeJson res =>
Record (LoaderWithCacheAPIProps path res ret)
-> R.Hooks R.Element
useLoaderWithCacheAPI { cacheEndpoint, handleResponse, mkRequest, path, renderer } = do
state <- R.useState' Nothing
state <- T.useBox Nothing
state' <- T.useLive T.unequal state
useCachedAPILoaderEffect { cacheEndpoint
, handleResponse
, mkRequest
, path
, state }
pure $ maybe (loadingSpinner {}) renderer (fst state)
pure $ maybe (loadingSpinner {}) renderer state'
type LoaderWithCacheAPIEffectProps path res ret = (
cacheEndpoint :: path -> Aff Hash
, handleResponse :: HashedResponse res -> ret
, mkRequest :: path -> GUC.Request
, path :: path
, state :: R.State (Maybe ret)
, state :: T.Box (Maybe ret)
)
useCachedAPILoaderEffect :: forall path res ret.
Eq path => DecodeJson res =>
Eq ret => Eq path => DecodeJson res =>
Record (LoaderWithCacheAPIEffectProps path res ret)
-> R.Hooks Unit
useCachedAPILoaderEffect { cacheEndpoint
, handleResponse
, mkRequest
, path
, state: state@(state' /\ setState) } = do
, state } = do
state' <- T.useLive T.unequal state
oPath <- R.useRef path
R.useEffect' $ do
......@@ -131,4 +135,4 @@ useCachedAPILoaderEffect { cacheEndpoint
else
throwError $ error $ "Fetched clean cache but hashes don't match: " <> h <> " != " <> cacheReal
liftEffect $ do
setState $ const $ Just $ handleResponse val
T.write_ (Just $ handleResponse val) state
......@@ -32,7 +32,6 @@ flipHanded l r LeftHanded = R.fragment [r, l]
flipHanded l r RightHanded = R.fragment [l, r]
derive instance ed :: Generic Handed _
instance eqHanded :: Eq Handed where
eq = genericEq
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment