Search.purs 2.28 KB
Newer Older
Alexandre Delanoë's avatar
Alexandre Delanoë committed
1 2
module Gargantext.Components.Forest.Tree.Node.Action.Search where

3 4
import Gargantext.Prelude

Alexandre Delanoë's avatar
Alexandre Delanoë committed
5 6 7
import Data.Maybe (Maybe)
import Effect (Effect)
import Effect.Aff (Aff, launchAff)
8
import Gargantext.Components.App.Data (Boxes)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
9 10 11 12 13 14
import Gargantext.Components.Forest.Tree.Node.Action (Action(..))
import Gargantext.Components.Forest.Tree.Node.Action.Add (NodePopup)
import Gargantext.Components.Forest.Tree.Node.Action.Search.SearchBar (searchBar)
import Gargantext.Components.Forest.Tree.Node.Action.Search.SearchField (defaultSearch)
import Gargantext.Components.Lang (allLangs)
import Gargantext.Sessions (Session)
15
import Gargantext.Types (ID)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
16
import Gargantext.Types as GT
17
import Gargantext.Utils.Reactix as R2
18 19 20
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
Alexandre Delanoë's avatar
Alexandre Delanoë committed
21

22 23 24 25 26
here :: R2.Here
here = R2.here "Gargantext.Components.Forest.Tree.Node.Action.Search"


type Props =
27 28
  ( boxes     :: Boxes
  , dispatch  :: Action -> Aff Unit
29 30 31
  , id        :: Maybe ID
  , nodePopup :: Maybe NodePopup
  , session   :: Session )
Alexandre Delanoë's avatar
Alexandre Delanoë committed
32 33

-- | Action : Search
34 35 36 37 38
actionSearch :: R2.Component Props
actionSearch = R.createElement actionSearchCpt
actionSearchCpt :: R.Component Props
actionSearchCpt = here.component "actionSearch" cpt
  where
39
    cpt { boxes: { errors }, dispatch, id, nodePopup, session } _ = do
40 41 42 43
      search <- T.useBox $ defaultSearch { node_id = id }
      pure $ R.fragment [ H.p { className: "action-search" }
                              [ H.text $ "Search and create a private "
                                      <> "corpus with the search query as corpus name." ]
44 45
                        , searchBar { errors
                                    , langs: allLangs
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
                                    , onSearch: searchOn dispatch nodePopup
                                    , search
                                    , session
                                    } []
                        ]
        where
          searchOn :: (Action -> Aff Unit)
                  -> Maybe NodePopup
                  -> GT.AsyncTaskWithType
                  -> Effect Unit
          searchOn dispatch' p task = do
            _ <- launchAff $ dispatch' (DoSearch task)
            -- close popup
            _ <- launchAff $ dispatch' ClosePopover
            -- TODO
            --snd p $ const Nothing
            pure unit