Commit e99966b1 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[chart] remove commented out & unnecessary loader code

parent 1fb5bf07
...@@ -32,7 +32,7 @@ import Gargantext.Prelude ...@@ -32,7 +32,7 @@ import Gargantext.Prelude
import Gargantext.Components.Table as T import Gargantext.Components.Table as T
import Gargantext.Ends (Frontends, url) import Gargantext.Ends (Frontends, url)
import Gargantext.Hooks.Loader (useLoader, useLoaderWithCache, useLoaderWithCacheAPI, HashedResponse(..)) import Gargantext.Hooks.Loader (useLoader, useLoaderWithCacheAPI, HashedResponse(..))
import Gargantext.Utils.List (sortWith) as L import Gargantext.Utils.List (sortWith) as L
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
import Gargantext.Routes as Routes import Gargantext.Routes as Routes
...@@ -403,8 +403,6 @@ pageLayout props = R.createElement pageLayoutCpt props [] ...@@ -403,8 +403,6 @@ pageLayout props = R.createElement pageLayoutCpt props []
pageLayoutCpt :: R.Component PageLayoutProps pageLayoutCpt :: R.Component PageLayoutProps
pageLayoutCpt = R.hooksComponent "G.C.DocsTable.pageLayout" cpt where pageLayoutCpt = R.hooksComponent "G.C.DocsTable.pageLayout" cpt where
cpt props@{frontends, session, nodeId, listId, corpusId, tabType, query, params} _ = cpt props@{frontends, session, nodeId, listId, corpusId, tabType, query, params} _ =
-- useLoader path (loadPage session) paint
-- useLoaderWithCache path keyFunc (getPageHash session) (loadPage session) paint
useLoaderWithCacheAPI { useLoaderWithCacheAPI {
cacheEndpoint: getPageHash session cacheEndpoint: getPageHash session
, handleResponse , handleResponse
...@@ -417,9 +415,6 @@ pageLayoutCpt = R.hooksComponent "G.C.DocsTable.pageLayout" cpt where ...@@ -417,9 +415,6 @@ pageLayoutCpt = R.hooksComponent "G.C.DocsTable.pageLayout" cpt where
paint (Tuple count docs) = page params (newProps count) docs paint (Tuple count docs) = page params (newProps count) docs
newProps count = props { totalRecords = count } newProps count = props { totalRecords = count }
-- keyFunc { corpusId, listId, nodeId, tabType } =
-- "page-" <> (show tabType) <> "-" <> (show corpusId) <> "-" <> (show nodeId) <> "-" <> (show listId)
mkRequest :: PageParams -> GUC.Request mkRequest :: PageParams -> GUC.Request
mkRequest p@{ listId, nodeId, tabType } = mkRequest p@{ listId, nodeId, tabType } =
GUC.makeGetRequest session $ NodeAPI Node (Just nodeId) $ "table" <> "?tabType=" <> (showTabType' tabType) <> "&list=" <> (show listId) GUC.makeGetRequest session $ NodeAPI Node (Just nodeId) $ "table" <> "?tabType=" <> (showTabType' tabType) <> "&list=" <> (show listId)
......
...@@ -9,7 +9,7 @@ import Reactix as R ...@@ -9,7 +9,7 @@ import Reactix as R
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Components.Nodes.Corpus.Chart.Types (Reload, Path, Props, MetricsProps, ReloadPath) import Gargantext.Components.Nodes.Corpus.Chart.Types (Reload, Path, Props, MetricsProps, ReloadPath)
import Gargantext.Hooks.Loader (HashedResponse, useLoader, useLoaderWithCache, useLoaderWithCacheAPI) import Gargantext.Hooks.Loader (HashedResponse, useLoader, useLoaderWithCacheAPI)
import Gargantext.Utils.Crypto (Hash) import Gargantext.Utils.Crypto (Hash)
import Gargantext.Sessions (Session) import Gargantext.Sessions (Session)
import Gargantext.Utils.CacheAPI as GUC import Gargantext.Utils.CacheAPI as GUC
...@@ -50,11 +50,6 @@ metricsWithCacheLoadViewCpt :: forall res ret. DecodeJson res => ...@@ -50,11 +50,6 @@ metricsWithCacheLoadViewCpt :: forall res ret. DecodeJson res =>
metricsWithCacheLoadViewCpt = R.hooksComponent "G.C.N.C.C.metricsWithCacheLoadView" cpt metricsWithCacheLoadViewCpt = R.hooksComponent "G.C.N.C.C.metricsWithCacheLoadView" cpt
where where
cpt { getMetricsHash, handleResponse, loaded, mkRequest, path, reload, session } _ = do cpt { getMetricsHash, handleResponse, loaded, mkRequest, path, reload, session } _ = do
-- useLoaderWithCache (fst reload /\ path) (metricsKeyFunc keyFunc) (getMetricsHash session) (getMetrics session) $ \l ->
-- loaded session path reload l
-- metricsKeyFunc keyFunc st@(_ /\ { corpusId, listId, tabType }) =
-- "metrics-" <> (show tabType) <> "-" <> (show corpusId) <> "-" <> (show listId) <> "--" <> (keyFunc st)
useLoaderWithCacheAPI { cacheEndpoint: (getMetricsHash session) useLoaderWithCacheAPI { cacheEndpoint: (getMetricsHash session)
, handleResponse , handleResponse
, mkRequest , mkRequest
......
...@@ -70,92 +70,9 @@ instance encodeHashedResponse :: EncodeJson a => EncodeJson (HashedResponse a) w ...@@ -70,92 +70,9 @@ instance encodeHashedResponse :: EncodeJson a => EncodeJson (HashedResponse a) w
~> "value" := encodeJson value ~> "value" := encodeJson value
~> jsonEmptyObject ~> jsonEmptyObject
useLoaderWithCache :: forall path st. Eq path => DecodeJson st => EncodeJson st =>
path
-> (path -> String)
-> (path -> Aff String)
-> (path -> Aff (HashedResponse st))
-> (st -> R.Element)
-> R.Hooks R.Element
useLoaderWithCache path keyFunc hashEndpoint loader render = do
state <- R.useState' Nothing
useCachedLoaderEffect { cacheEndpoint: hashEndpoint
, keyFunc
, loadRealData: loadRealData state
, path
, state }
pure $ maybe (loadingSpinner {}) render (fst state)
where
loadRealData :: R.State (Maybe st) -> String -> String -> WSS.Storage -> Aff Unit
loadRealData (_ /\ setState) key keyCache localStorage = do
--R2.affEffect "G.H.Loader.useCachedLoaderEffect" $ do
HashedResponse { hash, value: l } <- loader path
liftEffect $ do
let value = stringify $ encodeJson l
WSS.setItem key value localStorage
WSS.setItem keyCache hash localStorage
setState $ const $ Just l
pure unit
type CachedLoaderEffectProps cacheKey path st = (
cacheEndpoint :: path -> Aff cacheKey
, keyFunc :: path -> String
, loadRealData :: String -> String -> WSS.Storage -> Aff Unit
, path :: path
, state :: R.State (Maybe st)
)
useCachedLoaderEffect :: forall path st. Eq path => DecodeJson st => EncodeJson st =>
Record (CachedLoaderEffectProps String path st)
-> R.Hooks Unit
useCachedLoaderEffect { cacheEndpoint, keyFunc, loadRealData, path, state: state@(state' /\ setState) } = do
oPath <- R.useRef path
R.useEffect' $ do type LoaderWithCacheAPIProps path res ret = (
if (R.readRef oPath == path) && (isJust state') then cacheEndpoint :: path -> Aff Hash
pure unit
else do
R.setRef oPath path
let key = "loader--" <> (keyFunc path)
-- log2 "[useCachedLoader] key" key
let keyCache = key <> "-cache"
localStorage <- R2.getls
mState <- WSS.getItem key localStorage
mCache <- WSS.getItem keyCache localStorage
-- log2 "[useCachedLoader] mState" mState
launchAff_ $ do
case mState of
Nothing -> loadRealData key keyCache localStorage
Just stStr -> do
let parsed = parse stStr >>= decode
case parsed of
Left err -> do
-- liftEffect $ log2 "[useCachedLoader] err" err
loadRealData key keyCache localStorage
Right (st :: st) -> do
cacheReal <- cacheEndpoint path
-- liftEffect $ log2 "[useCachedLoader] cacheReal" cacheReal
case mCache of
Nothing -> do
-- liftEffect $ log2 "[useCachedLoader] no stored cache" Nothing
loadRealData key keyCache localStorage
Just cache -> do
-- liftEffect $ log2 "[useCachedLoader] stored cache" cache
if cache == cacheReal then
-- yay! cache hit!
liftEffect $ setState $ const $ Just st
else
loadRealData key keyCache localStorage
where
parse s = GU.mapLeft (\err -> "Error parsing serialised sessions:" <> show err) (jsonParser s)
decode j = GU.mapLeft (\err -> "Error decoding serialised sessions:" <> show err) (decodeJson j)
type LoaderWithCacheAPIProps path res ret =
( cacheEndpoint :: path -> Aff Hash
, handleResponse :: HashedResponse res -> ret , handleResponse :: HashedResponse res -> ret
, mkRequest :: path -> GUC.Request , mkRequest :: path -> GUC.Request
, path :: path , path :: path
...@@ -163,7 +80,8 @@ type LoaderWithCacheAPIProps path res ret = ...@@ -163,7 +80,8 @@ type LoaderWithCacheAPIProps path res ret =
) )
useLoaderWithCacheAPI :: forall path res ret. Eq path => DecodeJson res => useLoaderWithCacheAPI :: forall path res 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
...@@ -175,8 +93,8 @@ useLoaderWithCacheAPI { cacheEndpoint, handleResponse, mkRequest, path, renderer ...@@ -175,8 +93,8 @@ useLoaderWithCacheAPI { cacheEndpoint, handleResponse, mkRequest, path, renderer
, state } , state }
pure $ maybe (loadingSpinner {}) renderer (fst state) pure $ maybe (loadingSpinner {}) renderer (fst 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
......
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