~jack/casaaaaa

b0dddd9d658eb74c998e3e38db4d1e7dea61eaed — Jack Kelly 2 years ago e2a9596
Trigger search on form submit, as well as on edit timeout
1 files changed, 17 insertions(+), 8 deletions(-)

M Main.hs
M Main.hs => Main.hs +17 -8
@@ 20,6 20,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}



@@ 79,9 80,9 @@ body =
        el "h1" $ text "CASAAAAA"
        el "h2" $ text "CASA Abbreviations And Acronyms Assistant"

    (searchTermD, searchFuncD) <-
      divClass "container" . form $
        liftA2 (,) searchWidget searchFuncWidget
    rec (submitE, (searchTermD, searchFuncD)) <-
          divClass "container" . form $
            liftA2 (,) (searchWidget submitE) searchFuncWidget

    networkView $ do
      searchFuncM <- searchFuncD


@@ 114,12 115,13 @@ body =

    pure ()

form :: forall t m a. DomBuilder t m => m a -> m a
form children =
form :: forall t m a. DomBuilder t m => m a -> m (Event t (), a)
form children = do
  let config =
        (def :: ElementConfig EventResult t (DomBuilderSpace m))
          & elementConfig_eventSpec %~ addEventSpecFlags (Proxy :: Proxy (DomBuilderSpace m)) Submit (const preventDefault)
   in snd <$> element "form" config children
  (domElement, a) <- element "form" config children
  pure (domEvent Submit domElement, a)

searchWidget ::
  ( DomBuilder t m,


@@ 129,8 131,9 @@ searchWidget ::
    PerformEvent t m,
    TriggerEvent t m
  ) =>
  Event t () ->
  m (Dynamic t (Maybe Text))
searchWidget = do
searchWidget searchNowE = do
  let textInputConfig =
        def & initialAttributes
          .~ Map.fromList


@@ 138,7 141,13 @@ searchWidget = do
              ("placeholder", "Type to search...")
            ]
  searchBox <- inputElement textInputConfig
  searchE <- filter (not . T.null) <$> debounce 0.2 (updated (value searchBox))
  searchTimeoutE <- debounce 0.2 (updated (value searchBox))
  let searchE =
        filter (not . T.null) $
          leftmost
            [ searchTimeoutE,
              current (value searchBox) <@ searchNowE
            ]
  holdDyn Nothing (Just <$> searchE) >>= improvingMaybe

type SearchFunc = String -> String -> String -> Bool -> [Fuzzy Acronym String]