Skip to content

Commit

Permalink
HACK: Added componentClassWithMount which allows using the componen…
Browse files Browse the repository at this point in the history
…tDidMount event.

See
#39
  • Loading branch information
ajnsit committed Jan 29, 2020
1 parent b7aaaf4 commit c8b710c
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/Concur/React.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Concur.Core.Discharge (discharge, dischargePartialEffect)
import Concur.Core.Types (Widget)
import Data.Either (Either(..))
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Console (log)
import React as R

Expand All @@ -19,22 +20,25 @@ type ComponentState
mkComponentState :: HTML -> ComponentState
mkComponentState v = { view: v }

componentClass :: forall a. Widget HTML a -> R.ReactClass {}
componentClass winit = R.component "Concur" component
componentClassWithMount :: forall a. Effect Unit -> Widget HTML a -> R.ReactClass {}
componentClassWithMount onMount winit = R.component "Concur" component
where
component this = do
Tuple winit' v <- dischargePartialEffect winit
pure { state: mkComponentState v
, render: render <$> R.getState this
, componentDidMount: handler this (Right winit')
}
handler this (Right r) = do
v <- discharge (handler this) r
void $ R.writeState this (mkComponentState v)
handler _ (Left err) = do
log ("FAILED! " <> show err)
pure unit
render st = R.toElement st.view
component this = do
Tuple winit' v <- dischargePartialEffect winit
pure { state: mkComponentState v
, render: render <$> R.getState this
, componentDidMount: onMount *> handler this (Right winit')
}
handler this (Right r) = do
v <- discharge (handler this) r
void $ R.writeState this (mkComponentState v)
handler _ (Left err) = do
log ("FAILED! " <> show err)
pure unit
render st = R.toElement st.view

componentClass :: forall a. Widget HTML a -> R.ReactClass {}
componentClass = componentClassWithMount mempty

renderComponent :: forall a. Widget HTML a -> R.ReactElement
renderComponent init = R.createLeafElement (componentClass init) {}

0 comments on commit c8b710c

Please sign in to comment.