Improve the loader component

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