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

[toestand] more state refactoring

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