Share.purs 3.16 KB
Newer Older
1 2
module Gargantext.Components.Forest.Tree.Node.Action.Share where

3 4 5
import Data.Argonaut as Argonaut
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
6 7 8
import Data.Maybe (Maybe(..))
import Data.Tuple.Nested ((/\))
import Effect.Aff (Aff)
9 10 11 12
import Prelude (($))
import Reactix as R
import Reactix.DOM.HTML as H

13 14
import Gargantext.Components.Forest.Tree.Node.Action (Action)
import Gargantext.Components.Forest.Tree.Node.Action as Action
15
import Gargantext.Components.Forest.Tree.Node.Tools as Tools
16
import Gargantext.Components.Forest.Tree.Node.Tools.SubTree (subTreeView, SubTreeParamsIn)
17 18 19 20
import Gargantext.Prelude (class Eq, class Show, bind, pure)
import Gargantext.Routes as GR
import Gargantext.Sessions (Session, post)
import Gargantext.Types (ID)
21
import Gargantext.Types as GT
22
import Gargantext.Utils.Argonaut (genericSumDecodeJson, genericSumEncodeJson)
23

24
thisModule :: String
25
thisModule = "Gargantext.Components.Forest.Tree.Node.Action.Share"
26

27
------------------------------------------------------------------------
28 29
shareReq :: Session -> ID -> ShareNodeParams -> Aff ID
shareReq session nodeId =
30
  post session $ GR.NodeAPI GT.Node (Just nodeId) "share"
31 32

shareAction :: String -> Action
33
shareAction username = Action.ShareTeam username
34 35 36

------------------------------------------------------------------------
textInputBox :: Record Tools.TextInputBoxProps -> R.Element
37
textInputBox p = Tools.textInputBox p []
38

39
------------------------------------------------------------------------
40 41
data ShareNodeParams = ShareTeamParams   { username :: String }
               | SharePublicParams { node_id  :: Int    }
42

43
derive instance eqShareNodeParams :: Eq ShareNodeParams
44

45
derive instance genericShareNodeParams :: Generic ShareNodeParams _
46

47
instance showShareNodeParams :: Show ShareNodeParams where
48 49
  show = genericShow

50
instance decodeJsonShareNodeParams :: Argonaut.DecodeJson ShareNodeParams where
51 52
  decodeJson = genericSumDecodeJson

53
instance encodeJsonShareNodeParams :: Argonaut.EncodeJson ShareNodeParams where
54
  encodeJson = genericSumEncodeJson
55 56 57 58 59


------------------------------------------------------------------------
shareNode :: Record SubTreeParamsIn -> R.Element
shareNode p = R.createElement shareNodeCpt p []
60

61 62 63
shareNodeCpt :: R.Component SubTreeParamsIn
shareNodeCpt = R.hooksComponentWithModule thisModule "shareNode" cpt
  where
64
    cpt p@{dispatch, subTreeParams, id, nodeType, session, handed} _ = do
65 66 67 68
      action@(valAction /\ setAction) :: R.State Action <- R.useState' (Action.SharePublic {params: Nothing})

      let button = case valAction of
              Action.SharePublic {params} -> case params of
69
                Just val -> Tools.submitButton (Action.SharePublic {params: Just val}) dispatch
70 71 72
                Nothing -> H.div {} []
              _   -> H.div {} []

73
      pure $ Tools.panel [ subTreeView { action
74 75 76 77 78 79 80
                                       , dispatch
                                       , id
                                       , nodeType
                                       , session
                                       , subTreeParams
                                       , handed
                                       }
81 82
              ] button