Improve the loader component

parent 2a9787c1
module Gargantext.Components.Loader where module Gargantext.Components.Loader where
import Control.Monad.Cont.Trans (lift)
import Data.Maybe (Maybe(..)) import Data.Maybe (Maybe(..))
import Data.Either (Either(..))
import Data.Traversable (traverse_)
import React as React import React as React
import React (ReactClass) import React (ReactClass, Children)
import Gargantext.Prelude import Gargantext.Prelude
import Effect.Aff (Aff, launchAff, launchAff_, makeAff, nonCanceler, killFiber) import Effect.Aff (Aff)
import Effect.Exception (error)
import Thermite (Render, PerformAction, simpleSpec, modifyState_, createReactSpec)
type InnerProps a b = type InnerProps a b =
{ path :: a { path :: a
, loaded :: Maybe b , loaded :: Maybe b
, children :: React.Children , children :: Children
} }
type Props a b = { path :: a type PropsRow a b c =
, component :: ReactClass (InnerProps a b) ( path :: a
} , component :: ReactClass (InnerProps a b)
| c
)
type Props a b = Record (PropsRow a b (children :: Children))
type Props' a b = Record (PropsRow a b ())
data Action a = ForceReload | SetPath a
type State a b = { currentPath :: a, loaded :: Maybe b }
createLoaderClass :: forall a b
. Eq a
=> String
-> (a -> Aff b)
-> ReactClass (Record (PropsRow a b (children :: Children)))
createLoaderClass name loader =
React.component name
(\this -> do
s <- spec this
pure { state: s.state
, render: s.render
, componentDidMount: dispatcher this ForceReload
})
where
initialState {path} = {currentPath: path, loaded: Nothing}
performAction :: PerformAction (State a b) (Props' a b) (Action a)
performAction ForceReload _ {currentPath} = do
loaded <- lift $ loader currentPath
modifyState_ $ _ { loaded = Just loaded }
performAction (SetPath newPath) _ {currentPath} =
when (newPath /= currentPath) $ do
loaded <- lift $ loader newPath
modifyState_ $ _ { currentPath = newPath, loaded = Just loaded }
render :: Render (State a b) (Props' a b) (Action a)
render _d {component} {currentPath, loaded} c =
[React.createElement component {path: currentPath, loaded} c]
{spec, dispatcher} = createReactSpec (simpleSpec performAction render) initialState
{-
createLoaderClass :: forall a b createLoaderClass :: forall a b
. String . String
-> (a -> Aff b) -> (a -> Aff b)
...@@ -49,3 +91,4 @@ createLoaderClass name loader = React.component name mk ...@@ -49,3 +91,4 @@ createLoaderClass name loader = React.component name mk
{loaded} <- React.getState this {loaded} <- React.getState this
pure $ React.createElement component {path, loaded} [] pure $ React.createElement component {path, loaded} []
} }
-}
...@@ -57,8 +57,8 @@ type State = ...@@ -57,8 +57,8 @@ type State =
--, tree :: FTree --, tree :: FTree
} }
initialState :: State initialState :: Props -> State
initialState = initialState _ =
{ rows : Nothing { rows : Nothing
, currentPage : 1 , currentPage : 1
, pageSize : PS10 , pageSize : PS10
......
...@@ -71,7 +71,7 @@ layout = simpleSpec defaultPerformAction render ...@@ -71,7 +71,7 @@ layout = simpleSpec defaultPerformAction render
render _ {annuaireId} _ _ = render _ {annuaireId} _ _ =
[ annuaireLoader [ annuaireLoader
{ path: annuaireId { path: annuaireId
, component: createClass "LoadedAnnuaire" loadedAnnuaireSpec {} , component: createClass "LoadedAnnuaire" loadedAnnuaireSpec (const {})
} ] } ]
loadedAnnuaireSpec :: Spec {} Props Void loadedAnnuaireSpec :: Spec {} Props Void
...@@ -178,5 +178,5 @@ getAnnuaireInfo id = get $ toUrl Back Node id ...@@ -178,5 +178,5 @@ getAnnuaireInfo id = get $ toUrl Back Node id
annuaireLoaderClass :: ReactClass (Loader.Props Int AnnuaireInfo) annuaireLoaderClass :: ReactClass (Loader.Props Int AnnuaireInfo)
annuaireLoaderClass = createLoaderClass "AnnuaireLoader" getAnnuaireInfo annuaireLoaderClass = createLoaderClass "AnnuaireLoader" getAnnuaireInfo
annuaireLoader :: Loader.Props Int AnnuaireInfo -> ReactElement annuaireLoader :: Loader.Props' Int AnnuaireInfo -> ReactElement
annuaireLoader = React.createLeafElement annuaireLoaderClass annuaireLoader props = React.createElement annuaireLoaderClass props []
...@@ -28,9 +28,9 @@ type Props = Tabs.Props ...@@ -28,9 +28,9 @@ type Props = Tabs.Props
type State = { tabsView :: Tabs.State type State = { tabsView :: Tabs.State
} }
initialState :: State initialState :: Props -> State
initialState = { tabsView : Tabs.initialState initialState _props =
} { tabsView : Tabs.initialState {} }
------------------------------------------------------------------------ ------------------------------------------------------------------------
_tabsView :: forall a b. Lens' { tabsView :: a | b } a _tabsView :: forall a b. Lens' { tabsView :: a | b } a
...@@ -87,5 +87,5 @@ getCorpus = get <<< toUrl Back Corpus ...@@ -87,5 +87,5 @@ getCorpus = get <<< toUrl Back Corpus
corpusLoaderClass :: ReactClass (Loader.Props Int (NodePoly CorpusInfo)) corpusLoaderClass :: ReactClass (Loader.Props Int (NodePoly CorpusInfo))
corpusLoaderClass = createLoaderClass "CorpusLoader" getCorpus corpusLoaderClass = createLoaderClass "CorpusLoader" getCorpus
corpusLoader :: Loader.Props Int (NodePoly CorpusInfo) -> ReactElement corpusLoader :: Loader.Props' Int (NodePoly CorpusInfo) -> ReactElement
corpusLoader = React.createLeafElement corpusLoaderClass corpusLoader props = React.createElement corpusLoaderClass props []
...@@ -26,8 +26,8 @@ type State = ...@@ -26,8 +26,8 @@ type State =
, inputValue :: String , inputValue :: String
} }
initialState :: State initialState :: {} -> State
initialState = initialState {} =
{ document : Nothing { document : Nothing
, inputValue : "" , inputValue : ""
} }
......
...@@ -173,12 +173,13 @@ type State = ...@@ -173,12 +173,13 @@ type State =
, termTypeFilter :: Maybe TermType -- Nothing means all , termTypeFilter :: Maybe TermType -- Nothing means all
} }
initialState :: State initialState :: forall props. props -> State
initialState = { ngramsTablePatch: mempty initialState _ =
, searchQuery: "" { ngramsTablePatch: mempty
, termListFilter: Nothing , searchQuery: ""
, termTypeFilter: Nothing , termListFilter: Nothing
} , termTypeFilter: Nothing
}
data Action data Action
= SetTermListItem NgramsTerm (Replace TermList) = SetTermListItem NgramsTerm (Replace TermList)
...@@ -322,8 +323,8 @@ getNgramsTable = get <<< toUrl Back (Ngrams TabTerms Nothing) ...@@ -322,8 +323,8 @@ getNgramsTable = get <<< toUrl Back (Ngrams TabTerms Nothing)
ngramsLoaderClass :: ReactClass (Loader.Props Int NgramsTable) ngramsLoaderClass :: ReactClass (Loader.Props Int NgramsTable)
ngramsLoaderClass = Loader.createLoaderClass "NgramsLoader" getNgramsTable ngramsLoaderClass = Loader.createLoaderClass "NgramsLoader" getNgramsTable
ngramsLoader :: Loader.Props Int NgramsTable -> ReactElement ngramsLoader :: Loader.Props' Int NgramsTable -> ReactElement
ngramsLoader = React.createLeafElement ngramsLoaderClass ngramsLoader props = React.createElement ngramsLoaderClass props []
ngramsTableSpec :: Spec {} Props Void ngramsTableSpec :: Spec {} Props Void
ngramsTableSpec = simpleSpec defaultPerformAction render ngramsTableSpec = simpleSpec defaultPerformAction render
......
...@@ -15,8 +15,8 @@ import Gargantext.Components.Tab as Tab ...@@ -15,8 +15,8 @@ import Gargantext.Components.Tab as Tab
import Thermite (Spec, focus, hideState, cmapProps) import Thermite (Spec, focus, hideState, cmapProps)
pureTabs :: Spec {} Props Void -- pureTabs :: Spec {} Props Void
pureTabs = hideState initialState statefulTabs -- pureTabs = hideState initialState statefulTabs
statefulTabs :: Spec State Props Action statefulTabs :: Spec State Props Action
statefulTabs = statefulTabs =
......
...@@ -12,8 +12,8 @@ type State = ...@@ -12,8 +12,8 @@ type State =
, activeTab :: Int , activeTab :: Int
} }
initialState :: State initialState :: {} -> State
initialState = initialState _ =
{ docsView : {} { docsView : {}
, ngramsView : {} -- N.initialState , ngramsView : {} -- N.initialState
, activeTab : 0 , activeTab : 0
......
...@@ -26,7 +26,7 @@ landingData FR = Fr.landingData ...@@ -26,7 +26,7 @@ landingData FR = Fr.landingData
landingData EN = En.landingData landingData EN = En.landingData
layoutLanding :: Lang -> Spec {} {} Void layoutLanding :: Lang -> Spec {} {} Void
layoutLanding = hideState (unwrap initialState) layoutLanding = hideState (const $ unwrap initialState)
<<< focusState (re _Newtype) <<< focusState (re _Newtype)
<<< layoutLanding' <<< landingData <<< layoutLanding' <<< landingData
......
...@@ -36,7 +36,7 @@ initAppState = ...@@ -36,7 +36,7 @@ initAppState =
, addCorpusState : AC.initialState , addCorpusState : AC.initialState
, searchState : S.initialState , searchState : S.initialState
, userPageState : C.initialState , userPageState : C.initialState
, documentState : D.initialState , documentState : D.initialState {}
, ntreeState : Tree.exampleTree , ntreeState : Tree.exampleTree
, search : "" , search : ""
, showLogin : false , showLogin : false
......
...@@ -24,7 +24,7 @@ setUnsafeComponentWillMount = unsafeSet "unsafeComponentWillMount" ...@@ -24,7 +24,7 @@ setUnsafeComponentWillMount = unsafeSet "unsafeComponentWillMount"
main :: Effect Unit main :: Effect Unit
main = do main = do
case T.createReactSpec layoutSpec initAppState of case T.createReactSpec layoutSpec (const initAppState) of
{ spec, dispatcher } -> void $ do { spec, dispatcher } -> void $ do
let setRouting this = void $ do let setRouting this = void $ do
matches routing (routeHandler (dispatchAction (dispatcher this))) matches routing (routeHandler (dispatchAction (dispatcher this)))
......
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