Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
purescript-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grégoire Locqueville
purescript-gargantext
Commits
9f399030
Commit
9f399030
authored
Sep 27, 2019
by
James Laver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor G.Hooks.Loader - incl new useAff
parent
f9df8e9f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
14 deletions
+26
-14
Loader.purs
src/Gargantext/Hooks/Loader.purs
+26
-14
No files found.
src/Gargantext/Hooks/Loader.purs
View file @
9f399030
module Gargantext.Hooks.Loader where
module Gargantext.Hooks.Loader where
import Data.Maybe (Maybe(..), isNothing, maybe)
import Data.Functor ((<$>))
import Control.Monad ((=<<))
import Data.Maybe (Maybe(..), isNothing, maybe, maybe')
import Data.Tuple (fst)
import Data.Tuple (fst)
import Data.Tuple.Nested ((/\))
import Data.Tuple.Nested ((/\))
import Gargantext.Prelude
import Gargantext.Prelude
...
@@ -10,36 +12,46 @@ import Reactix as R
...
@@ -10,36 +12,46 @@ import Reactix as R
import Gargantext.Utils.Reactix as R2
import Gargantext.Utils.Reactix as R2
import Gargantext.Components.LoadingSpinner (loadingSpinner)
import Gargantext.Components.LoadingSpinner (loadingSpinner)
useLoader :: forall path state. path -> (path -> Aff state) -> (state -> R.Element) -> R.Hooks R.Element
useAff :: forall st. Aff st -> R.Hooks (Maybe st)
useLoader path loader render = do
useAff loader = do
state <- R.useState' Nothing
(loaded /\ setLoaded) <- R.useState' Nothing
loader' <- R.useMemo2 path loader (\_ -> loader path)
R.useEffect1 loader $ do
useLoaderEffect state loader'
if isNothing loaded then
pure $ maybe (loadingSpinner {}) render (fst state)
R2.affEffect "G.H.Loader.useAff" $
loader >>= (liftEffect <<< setLoaded <<< const <<< Just)
else pure R.nothing
pure loaded
useLoader :: forall path st. path -> (path -> Aff st) -> (st -> R.Element) -> R.Hooks R.Element
useLoader path loader render
= maybe' (\_ -> loadingSpinner {}) render
<$> (useAff =<< R.useMemo2 path loader (\_ -> loader path))
useLoader2 :: forall path st
ate. R.State path -> (path -> Aff state) -> (state
-> R.Element) -> R.Hooks R.Element
useLoader2 :: forall path st
. R.State path -> (path -> Aff st) -> (st
-> R.Element) -> R.Hooks R.Element
useLoader2 path loader render = do
useLoader2 path loader render = do
state <- R.useState' Nothing
state <- R.useState' Nothing
useLoaderEffect2 path state loader
useLoaderEffect2 path state loader
pure $ maybe (loadingSpinner {}) render (fst state)
pure $ maybe (loadingSpinner {}) render (fst state)
useLoaderEffect :: forall state.
R.State (Maybe state) -> Aff state
-> R.Hooks Unit
useLoaderEffect :: forall state.
Aff state -> R.State (Maybe state)
-> R.Hooks Unit
useLoaderEffect
(state /\ setState) loader
= do
useLoaderEffect
loader (state /\ setState)
= do
R.useEffect2 state loader $ do
R.useEffect2 state loader $ do
if isNothing state then
if isNothing state then
R2.affEffect "useLoader" $ loader >>= (liftEffect <<< setState <<< const <<< Just)
R2.affEffect "
G.H.Loader.
useLoader" $ loader >>= (liftEffect <<< setState <<< const <<< Just)
else pure R.nothing
else pure R.nothing
useLoaderEffect' :: forall state. Aff state -> R.Hooks (R.State (Maybe state))
useLoaderEffect' :: forall state. Aff state -> R.Hooks (R.State (Maybe state))
useLoaderEffect' aff = do
useLoaderEffect' aff = do
state <- R.useState' Nothing
state <- R.useState' Nothing
useLoaderEffect
state aff
useLoaderEffect
aff state
pure state
pure state
useLoaderEffect2 :: forall st path. R.State path -> R.State (Maybe st) -> (path -> Aff st) -> R.Hooks Unit
useLoaderEffect2 :: forall st path. R.State path -> R.State (Maybe st) -> (path -> Aff st) -> R.Hooks Unit
useLoaderEffect2 path state loader = useRepointer path loader >>= useLoaderEffect state
useLoaderEffect2 path state loader = do
aff <- useRepointer path loader
useLoaderEffect aff state
useRepointer :: forall path st. R.State path -> (path -> Aff st) -> R.Hooks (Aff st)
useRepointer :: forall path st. R.State path -> (path -> Aff st) -> R.Hooks (Aff st)
useRepointer path@(path' /\ _) loader = R.useMemo
1
path' (\_ -> loader path')
useRepointer path@(path' /\ _) loader = R.useMemo
2 loader
path' (\_ -> loader path')
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment