Commit 4f0eb144 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[toestand] fixes to forest storing state

parent ddc424fc
...@@ -31,5 +31,8 @@ appCpt = here.component "app" cpt where ...@@ -31,5 +31,8 @@ appCpt = here.component "app" cpt where
tasksReductor <- GAT.useTasks boxes.reloadRoot boxes.reloadForest tasksReductor <- GAT.useTasks boxes.reloadRoot boxes.reloadForest
R.useEffectOnce' $ do R.useEffectOnce' $ do
T.write (Just tasksReductor) tasks T.write (Just tasksReductor) tasks
R.useEffectOnce' $ do
R2.loadLocalStorageState R2.openNodesKey boxes.forestOpen
T.listen (R2.listenLocalStorageState R2.openNodesKey) boxes.forestOpen
useHashRouter Router.router boxes.route -- Install router to window useHashRouter Router.router boxes.route -- Install router to window
pure $ router { boxes, tasks } -- Render router component pure $ router { boxes, tasks } -- Render router component
...@@ -5,7 +5,7 @@ module Gargantext.Components.Forest ...@@ -5,7 +5,7 @@ module Gargantext.Components.Forest
, forestLayoutMain , forestLayoutMain
, forestLayoutRaw , forestLayoutRaw
, Common , Common
, LayoutProps , Props
) where ) where
import Data.Array as A import Data.Array as A
...@@ -41,18 +41,15 @@ type Common = ...@@ -41,18 +41,15 @@ type Common =
, tasks :: T.Box (Maybe GAT.Reductor) , tasks :: T.Box (Maybe GAT.Reductor)
) )
type LayoutProps = type Props =
( backend :: T.Box (Maybe Backend) ( backend :: T.Box (Maybe Backend)
, forestOpen :: T.Box OpenNodes
, reloadForest :: T.Box T2.Reload , reloadForest :: T.Box T2.Reload
, sessions :: T.Box Sessions , sessions :: T.Box Sessions
, showLogin :: T.Box Boolean , showLogin :: T.Box Boolean
| Common | Common
) )
type Props = (
forestOpen :: T.Box OpenNodes
| LayoutProps )
type TreeExtra = ( type TreeExtra = (
forestOpen :: T.Box OpenNodes forestOpen :: T.Box OpenNodes
, session :: Session , session :: Session
...@@ -129,10 +126,10 @@ plus handed showLogin backend = H.div { className: "row" } ...@@ -129,10 +126,10 @@ plus handed showLogin backend = H.div { className: "row" }
"btn btn-primary col-5 " <> switchHanded "ml-1 mr-auto" "mr-1 ml-auto" handed "btn btn-primary col-5 " <> switchHanded "ml-1 mr-auto" "mr-1 ml-auto" handed
forestLayout :: R2.Component LayoutProps forestLayout :: R2.Component Props
forestLayout = R.createElement forestLayoutCpt forestLayout = R.createElement forestLayoutCpt
forestLayoutCpt :: R.Component LayoutProps forestLayoutCpt :: R.Component Props
forestLayoutCpt = here.component "forestLayout" cpt where forestLayoutCpt = here.component "forestLayout" cpt where
cpt props@{ handed } children = cpt props@{ handed } children =
pure $ R.fragment pure $ R.fragment
...@@ -141,10 +138,10 @@ forestLayoutCpt = here.component "forestLayout" cpt where ...@@ -141,10 +138,10 @@ forestLayoutCpt = here.component "forestLayout" cpt where
-- Renders its first child component in the top bar and the rest in -- Renders its first child component in the top bar and the rest in
-- the main view. -- the main view.
forestLayoutWithTopBar :: R2.Component LayoutProps forestLayoutWithTopBar :: R2.Component Props
forestLayoutWithTopBar = R.createElement forestLayoutWithTopBarCpt forestLayoutWithTopBar = R.createElement forestLayoutWithTopBarCpt
forestLayoutWithTopBarCpt :: R.Component LayoutProps forestLayoutWithTopBarCpt :: R.Component Props
forestLayoutWithTopBarCpt = here.component "forestLayoutWithTopBar" cpt where forestLayoutWithTopBarCpt = here.component "forestLayoutWithTopBar" cpt where
cpt props@{ handed } children = do cpt props@{ handed } children = do
let { head: topBarChild, tail: mainChildren } = let { head: topBarChild, tail: mainChildren } =
...@@ -153,19 +150,20 @@ forestLayoutWithTopBarCpt = here.component "forestLayoutWithTopBar" cpt where ...@@ -153,19 +150,20 @@ forestLayoutWithTopBarCpt = here.component "forestLayoutWithTopBar" cpt where
[ topBar { handed } [ topBarChild ] [ topBar { handed } [ topBarChild ]
, forestLayoutMain props mainChildren ] , forestLayoutMain props mainChildren ]
forestLayoutMain :: R2.Component LayoutProps forestLayoutMain :: R2.Component Props
forestLayoutMain = R.createElement forestLayoutMainCpt forestLayoutMain = R.createElement forestLayoutMainCpt
forestLayoutMainCpt :: R.Component LayoutProps forestLayoutMainCpt :: R.Component Props
forestLayoutMainCpt = here.component "forestLayoutMain" cpt where forestLayoutMainCpt = here.component "forestLayoutMain" cpt where
cpt props children = pure $ forestLayoutRaw props [ mainPage {} children ] cpt props children = pure $ forestLayoutRaw props [ mainPage {} children ]
forestLayoutRaw :: R2.Component LayoutProps forestLayoutRaw :: R2.Component Props
forestLayoutRaw = R.createElement forestLayoutRawCpt forestLayoutRaw = R.createElement forestLayoutRawCpt
forestLayoutRawCpt :: R.Component LayoutProps forestLayoutRawCpt :: R.Component Props
forestLayoutRawCpt = here.component "forestLayoutRaw" cpt where forestLayoutRawCpt = here.component "forestLayoutRaw" cpt where
cpt p@{ backend cpt p@{ backend
, forestOpen
, frontends , frontends
, reloadForest , reloadForest
, reloadRoot , reloadRoot
...@@ -174,15 +172,14 @@ forestLayoutRawCpt = here.component "forestLayoutRaw" cpt where ...@@ -174,15 +172,14 @@ forestLayoutRawCpt = here.component "forestLayoutRaw" cpt where
, showLogin , showLogin
, tasks } children = do , tasks } children = do
handed' <- T.useLive T.unequal p.handed handed' <- T.useLive T.unequal p.handed
forestOpen <- T.useBox $ Set.empty
pure $ R2.row $ reverseHanded pure $ R2.row $ reverseHanded
([ H.div { className: "col-md-2 forest-layout-raw-tree" } ([ H.div { className: "col-md-2 forest-layout-raw-tree" }
[ forest' p.handed forestOpen ] [ forest' p.handed ]
] <> children ] <> children
) handed' ) handed'
where where
forest' handed forestOpen = forest' handed =
forest { backend forest { backend
, frontends , frontends
, forestOpen , forestOpen
......
...@@ -34,7 +34,7 @@ here :: R2.Here ...@@ -34,7 +34,7 @@ here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Lists" here = R2.here "Gargantext.Components.Nodes.Lists"
type ListsWithForest = type ListsWithForest =
( forestProps :: Record Forest.LayoutProps ( forestProps :: Record Forest.Props
, listsProps :: Record CommonProps , listsProps :: Record CommonProps
) )
......
...@@ -41,7 +41,7 @@ here = R2.here "Gargantext.Components.Nodes.Texts" ...@@ -41,7 +41,7 @@ here = R2.here "Gargantext.Components.Nodes.Texts"
-------------------------------------------------------- --------------------------------------------------------
type TextsWithForest = ( type TextsWithForest = (
forestProps :: Record Forest.LayoutProps forestProps :: Record Forest.Props
, textsProps :: Record CommonProps , textsProps :: Record CommonProps
) )
......
...@@ -92,8 +92,16 @@ forested = R.createElement forestedCpt ...@@ -92,8 +92,16 @@ forested = R.createElement forestedCpt
forestedCpt :: R.Component Props forestedCpt :: R.Component Props
forestedCpt = here.component "forested" cpt forestedCpt = here.component "forested" cpt
where where
cpt { boxes: { backend, handed, reloadForest, reloadRoot, route, sessions, showLogin }, tasks } children = do cpt { boxes: { backend
, forestOpen
, handed
, reloadForest
, reloadRoot
, route
, sessions
, showLogin }, tasks } children = do
pure $ forestLayout { backend pure $ forestLayout { backend
, forestOpen
, frontends: defaultFrontends , frontends: defaultFrontends
, handed , handed
, reloadForest , reloadForest
...@@ -189,6 +197,7 @@ lists = R.createElement listsCpt ...@@ -189,6 +197,7 @@ lists = R.createElement listsCpt
listsCpt :: R.Component SessionNodeProps listsCpt :: R.Component SessionNodeProps
listsCpt = here.component "lists" cpt where listsCpt = here.component "lists" cpt where
cpt props@{ boxes: { backend cpt props@{ boxes: { backend
, forestOpen
, handed , handed
, reloadForest , reloadForest
, reloadRoot , reloadRoot
...@@ -204,6 +213,7 @@ listsCpt = here.component "lists" cpt where ...@@ -204,6 +213,7 @@ listsCpt = here.component "lists" cpt where
pure $ authed sessionProps $ pure $ authed sessionProps $
Lists.listsWithForest Lists.listsWithForest
{ forestProps: { backend { forestProps: { backend
, forestOpen
, frontends , frontends
, handed , handed
, reloadForest , reloadForest
...@@ -291,6 +301,7 @@ textsCpt :: R.Component SessionNodeProps ...@@ -291,6 +301,7 @@ textsCpt :: R.Component SessionNodeProps
textsCpt = here.component "texts" cpt textsCpt = here.component "texts" cpt
where where
cpt props@{ boxes: { backend cpt props@{ boxes: { backend
, forestOpen
, handed , handed
, reloadForest , reloadForest
, reloadRoot , reloadRoot
...@@ -306,6 +317,7 @@ textsCpt = here.component "texts" cpt ...@@ -306,6 +317,7 @@ textsCpt = here.component "texts" cpt
pure $ authed sessionProps $ pure $ authed sessionProps $
Texts.textsWithForest Texts.textsWithForest
{ forestProps: { backend { forestProps: { backend
, forestOpen
, frontends , frontends
, handed , handed
, route , route
......
...@@ -35,6 +35,7 @@ import Reactix.DOM.HTML as H ...@@ -35,6 +35,7 @@ import Reactix.DOM.HTML as H
import Reactix.React (react) import Reactix.React (react)
import Reactix.SyntheticEvent as RE import Reactix.SyntheticEvent as RE
import Reactix.Utils (currySecond, hook, tuple) import Reactix.Utils (currySecond, hook, tuple)
import Toestand as T
import Unsafe.Coerce (unsafeCoerce) import Unsafe.Coerce (unsafeCoerce)
import Web.File.Blob (Blob) import Web.File.Blob (Blob)
import Web.File.File as WF import Web.File.File as WF
...@@ -327,25 +328,22 @@ openNodesKey = "garg-open-nodes" ...@@ -327,25 +328,22 @@ openNodesKey = "garg-open-nodes"
type LocalStorageKey = String type LocalStorageKey = String
useLocalStorageState :: forall s. Argonaut.DecodeJson s => Argonaut.EncodeJson s => LocalStorageKey -> s -> R.Hooks (R.State s) loadLocalStorageState :: forall s. Argonaut.DecodeJson s => LocalStorageKey -> T.Box s -> Effect Unit
useLocalStorageState key s = do loadLocalStorageState key cell = do
-- we need to synchronously get the initial state from local storage storage <- getls
Tuple state setState' <- R.useState \_ -> unsafePerformEffect do item :: Maybe String <- getItem key storage
item :: Maybe String <- getItem key =<< getls
let json = hush <<< Argonaut.jsonParser =<< item let json = hush <<< Argonaut.jsonParser =<< item
let parsed = hush <<< Argonaut.decodeJson =<< json let parsed = hush <<< Argonaut.decodeJson =<< json
pure $ fromMaybe s parsed case parsed of
Nothing -> pure unit
Just p -> void $ T.write p cell
let listenLocalStorageState :: forall s. Argonaut.EncodeJson s => LocalStorageKey -> T.Change s -> Effect Unit
setState update = do listenLocalStorageState key { old, new } = do
let new = update state
setState' (\_ -> new)
let json = Json.stringify $ Argonaut.encodeJson new let json = Json.stringify $ Argonaut.encodeJson new
storage <- getls storage <- getls
setItem key json storage setItem key json storage
pure (Tuple state setState)
getMessageDataStr :: DE.MessageEvent -> String getMessageDataStr :: DE.MessageEvent -> String
getMessageDataStr = getMessageData getMessageDataStr = getMessageData
......
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