Skip to content

Commit

Permalink
Actually initialize the ref
Browse files Browse the repository at this point in the history
  • Loading branch information
thomashoneyman committed Nov 30, 2018
1 parent 54657f3 commit c3bbc1d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions example/real-world/Component.purs
Expand Up @@ -193,9 +193,9 @@ component =
case st'.optionsEnabled of
true -> do
let spec' = O.OptionsForm $ _ { enable = F.InputField true } $ unwrap optionsFormInputs
void $ H.query' CP.cp2 unit $ F.initialize_ spec'
void $ H.query' CP.cp2 unit $ F.loadForm_ spec'
_ -> do
void $ H.query' CP.cp2 unit $ F.initialize_ defaultInputs
void $ H.query' CP.cp2 unit $ F.loadForm_ defaultInputs
pure a

MetricDropdown m a -> a <$ case m of
Expand Down
2 changes: 1 addition & 1 deletion src/Formless.purs
Expand Up @@ -28,4 +28,4 @@ import Formless.Transform.Row (class MakeInputFieldsFromRow, class MakeSProxies,
import Formless.Types.Component (Component, DSL, Debouncer, HTML, HTML', Input, Input', InternalState(..), Message(..), Message', PublicState, Query(..), Query', State, StateRow, StateStore, ValidStatus(..))
import Formless.Types.Form (ErrorType, FormField(..), FormFieldRow, FormProxy(..), InputField(..), InputFunction(..), InputType, OutputField(..), OutputType, U(..))
import Formless.Validation (EmptyValidators(..), Validation(..), hoistFn, hoistFnE, hoistFnE_, hoistFnME, hoistFnME_, hoistFn_, noValidation, runValidation)
import Formless.Query (andThen, andThen_, asyncModifyValidate, asyncModifyValidate_, asyncSetValidate, asyncSetValidate_, getState, initialize, initialize_, modify, modifyAll, modifyAll_, modifyValidate, modifyValidateAll, modifyValidateAll_, modifyValidate_, modify_, raise, raise_, reset, resetAll, resetAll_, reset_, send, send', set, setAll, setAll_, setValidate, setValidateAll, setValidateAll_, setValidate_, set_, submit, submitReply, submit_, validate, validateAll, validateAll_, validate_)
import Formless.Query (andThen, andThen_, asyncModifyValidate, asyncModifyValidate_, asyncSetValidate, asyncSetValidate_, getState, loadForm, loadForm_, modify, modifyAll, modifyAll_, modifyValidate, modifyValidateAll, modifyValidateAll_, modifyValidate_, modify_, raise, raise_, reset, resetAll, resetAll_, reset_, send, send', set, setAll, setAll_, setValidate, setValidateAll, setValidateAll_, setValidate_, set_, submit, submitReply, submit_, validate, validateAll, validateAll_, validate_)
15 changes: 11 additions & 4 deletions src/Formless/Component.purs
Expand Up @@ -54,11 +54,13 @@ component
=> Newtype (form Variant U) (Variant us)
=> Component pq cq cs form m
component =
H.parentComponent
H.lifecycleParentComponent
{ initialState
, render: extract
, eval
, receiver: HE.input Receive
, initializer: Just $ H.action Initialize
, finalizer: Nothing
}
where

Expand All @@ -80,6 +82,12 @@ component =

eval :: Query pq cq cs form m ~> DSL pq cq cs form m
eval = case _ of
Initialize a -> do
ref <- H.liftEffect $ Ref.new Nothing
modifyState_ \st -> st
{ internal = over InternalState (_ { debounceRef = Just ref }) st.internal }
pure a

Modify variant a -> do
modifyState_ \st -> st
{ form = Internal.unsafeModifyInputVariant identity variant st.form }
Expand Down Expand Up @@ -218,9 +226,8 @@ component =
H.raise (Emit query)
pure a

Initialize formInputs a -> do
LoadForm formInputs a -> do
st <- getState
ref <- H.liftEffect $ Ref.new Nothing
new <- modifyState _
{ validity = Incomplete
, dirty = false
Expand All @@ -233,11 +240,11 @@ component =
(_
{ allTouched = false
, initialInputs = formInputs
, debounceRef = Just ref
}
)
st.internal
}

H.raise $ Changed $ getPublicState new
pure a

Expand Down
4 changes: 2 additions & 2 deletions src/Formless/Internal/Debounce.purs
Expand Up @@ -5,7 +5,6 @@ import Prelude
import Data.Maybe (Maybe(..))
import Data.Newtype (unwrap)
import Data.Traversable (traverse, traverse_)
import Debug.Trace (spy)
import Effect.Aff (Milliseconds, delay, error, forkAff, killFiber)
import Effect.Aff.AVar as AVar
import Effect.Aff.Class (class MonadAff)
Expand Down Expand Up @@ -49,9 +48,10 @@ debounceForm ms pre post last = do
modifyState_ _ { form = form }
last

H.liftEffect $ traverse_ (Ref.write $ Just { var, fiber }) ref
form <- pre
modifyState_ _ { form = form }
H.liftEffect $ traverse_ (Ref.write $ Just { var, fiber }) ref
pure unit

Just db -> do
let var = db.var
Expand Down
8 changes: 4 additions & 4 deletions src/Formless/Query.purs
Expand Up @@ -71,20 +71,20 @@ getState = H.request GetState
-- | Replace all form inputs with a new set of inputs, and re-initialize
-- | the form to a new state. Useful to set a new "initial state" for a form,
-- | especially when filling a form with data loaded asynchronously.
initialize
loadForm
:: pq cq cs form m a
. form Record InputField
-> a
-> Query pq cq cs form m a
initialize = Initialize
loadForm = LoadForm

-- | `initialize` as an action, so you don't need to specify a `Unit`
-- | result. Use to skip a use of `Halogen.action`.
initialize_
loadForm_
:: pq cq cs form m
. form Record InputField
-> Query pq cq cs form m Unit
initialize_ = flip Initialize unit
loadForm_ = flip LoadForm unit

-- | Perform two Formless actions in sequence. Can be chained arbitrarily.
-- | Useful when a field needs to modify itself on change and also trigger
Expand Down
3 changes: 2 additions & 1 deletion src/Formless/Types/Component.purs
Expand Up @@ -32,9 +32,10 @@ data Query pq cq cs form m a
| SubmitReply (Maybe (form Record OutputField) -> a)
| GetState (PublicState form -> a)
| Send cs (cq a)
| LoadForm (form Record InputField) a
| SyncFormData a
| Raise (pq Unit) a
| Initialize (form Record InputField) a
| Initialize a
| Receive (Input pq cq cs form m) a
| AndThen (Query pq cq cs form m Unit) (Query pq cq cs form m Unit) a

Expand Down

0 comments on commit c3bbc1d

Please sign in to comment.