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