Contact.purs 3.35 KB
Newer Older
1 2 3 4
module Gargantext.Components.Forest.Tree.Node.Action.Contact where

import Data.Maybe (Maybe(..))
import Data.Tuple.Nested ((/\))
Alexandre Delanoë's avatar
Alexandre Delanoë committed
5
import Effect.Aff (Aff, launchAff)
6
import Effect.Uncurried (mkEffectFn1)
7 8 9 10
import Prelude (($))
import Reactix as R
import Reactix.DOM.HTML as H

11
import Gargantext.Components.Forest.Tree.Node.Action (Action)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
12 13 14
import Gargantext.Components.Forest.Tree.Node.Action.Contact.Types (AddContactParams(..))
-- import Gargantext.Components.Forest.Tree.Node.Tools.SubTree (subTreeView, SubTreeParamsIn)
import Gargantext.Prelude (Unit, bind, const, discard, pure, (<<<), (<>))
15 16 17 18
import Gargantext.Routes as GR
import Gargantext.Sessions (Session, post)
import Gargantext.Types (ID)
import Gargantext.Types as GT
Alexandre Delanoë's avatar
Alexandre Delanoë committed
19
import Gargantext.Utils.Reactix as R2
20 21

thisModule = "Gargantext.Components.Forest.Tree.Node.Action.Contact"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

------------------------------------------------------------------------
contactReq :: Session -> ID -> AddContactParams -> Aff ID
contactReq session nodeId =
  post session $ GR.NodeAPI GT.Annuaire (Just nodeId) "contact"

------------------------------------------------------------------------
type TextInputBoxProps =
  ( id        :: ID
  , dispatch  :: Action -> Aff Unit
  , params    :: Record AddContactProps
  , isOpen    :: R.State Boolean
  , boxName   :: String
  , boxAction :: AddContactParams -> Action
  )

type AddContactProps = ( firstname :: String, lastname :: String)

textInputBox :: Record TextInputBoxProps -> R.Element
textInputBox p@{ boxName, boxAction, dispatch, isOpen: (true /\ setIsOpen), params } = R.createElement el p []
  where
    {firstname, lastname} = params
44
    el = R.hooksComponentWithModule thisModule (boxName <> "Box") cpt
Alexandre Delanoë's avatar
Alexandre Delanoë committed
45 46
    cpt {id, params:params'} _ = do
      let {firstname, lastname} = params'
47 48 49 50 51 52 53 54 55 56 57
      stateFirstname <- R.useState' firstname
      stateLastname  <- R.useState'  lastname
      pure $ H.div {className: "from-group row-no-padding"}
        [ textInput stateFirstname firstname
        , textInput stateLastname  lastname
        , submitBtn stateFirstname stateLastname
        , cancelBtn
        ]
      where
        textInput (_ /\ set) default =
          H.div {className: "col-md-8"}
58
          [ H.input { className: "form-control"
59
                    , defaultValue: default
60 61
                    , on: { input: set
                                   <<< const
62
                                   <<< R.unsafeEventValue }
63 64
                    , placeholder: (boxName <> " Node")
                    , type: "text"
65 66 67 68 69
                    }
          ]
        submitBtn (val1 /\ _) (val2 /\ _) =
          H.a {className: "btn glyphitem glyphicon glyphicon-ok col-md-2 pull-left"
              , type: "button"
70
              , on: { click: \_ -> do
71 72
                    setIsOpen $ const false
                    launchAff $ dispatch ( boxAction (AddContactParams {firstname:val1, lastname:val2} ))
73
                    }
74 75 76 77
              , title: "Submit"
              } []
        cancelBtn =
          H.a {className: "btn text-danger glyphitem glyphicon glyphicon-remove col-md-2 pull-left"
78
              , on: { click: \_ -> setIsOpen $ const false }
79
              , title: "Cancel"
80
              , type: "button"
81 82 83
              } []
textInputBox p@{ boxName, isOpen: (false /\ _) } = R.createElement el p []
  where
84
    el = R.hooksComponentWithModule thisModule (boxName <> "Box") cpt
85 86 87 88
    cpt {} _ = pure $ H.div {} []