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

3
import Data.Either (Either)
4
import Data.Generic.Rep (class Generic)
5
import Data.Maybe (Maybe(..))
6
import Data.Show.Generic (genericShow)
7
import Effect.Aff (Aff)
8 9
import Reactix as R
import Reactix.DOM.HTML as H
10 11
import Simple.JSON as JSON
import Simple.JSON.Generics as JSONG
12
import Toestand as T
13

14 15
import Gargantext.Prelude

16 17
import Gargantext.Components.Forest.Tree.Node.Action.Types (Action)
import Gargantext.Components.Forest.Tree.Node.Action.Types as Action
18
import Gargantext.Components.Forest.Tree.Node.Tools as Tools
19
import Gargantext.Components.Forest.Tree.Node.Tools.SubTree (subTreeView, SubTreeParamsIn)
20
import Gargantext.Config.REST (RESTError)
21 22 23
import Gargantext.Routes as GR
import Gargantext.Sessions (Session, post)
import Gargantext.Types (ID)
24
import Gargantext.Types as GT
25
import Gargantext.Utils.SimpleJSON as GUSJ
26
import Gargantext.Utils.Reactix as R2
27

28 29
here :: R2.Here
here = R2.here "Gargantext.Components.Forest.Tree.Node.Action.Share"
30

31
------------------------------------------------------------------------
32
shareReq :: Session -> ID -> ShareNodeParams -> Aff (Either RESTError ID)
33
shareReq session nodeId =
34
  post session $ GR.NodeAPI GT.Node (Just nodeId) "share"
35 36

shareAction :: String -> Action
37
shareAction username = Action.ShareTeam username
38 39


40
------------------------------------------------------------------------
41 42
data ShareNodeParams = ShareTeamParams   { username :: String }
               | SharePublicParams { node_id  :: Int    }
43 44
derive instance Eq ShareNodeParams
derive instance Generic ShareNodeParams _
45 46 47 48 49 50
instance JSON.ReadForeign ShareNodeParams where readImpl = GUSJ.taggedSumRep
instance JSON.WriteForeign ShareNodeParams where
  writeImpl (ShareTeamParams { username }) = JSON.writeImpl { "type": "ShareTeamParams"
                                                            , username }
  writeImpl (SharePublicParams { node_id }) = JSON.writeImpl { "type": "SharePublicParams"
                                                             , node_id }
51
instance Show ShareNodeParams where show = genericShow
52 53

------------------------------------------------------------------------
54 55 56
type ShareNode =
  ( id :: ID
  , dispatch :: Action -> Aff Unit )
57

58 59 60
shareNode :: R2.Component ShareNode
shareNode = R.createElement shareNodeCpt
shareNodeCpt :: R.Component ShareNode
61
shareNodeCpt = here.component "shareNode" cpt
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  where
    cpt { dispatch, id } _ = do
      isOpen <- T.useBox true
      pure $ Tools.panel
                [ Tools.textInputBox { boxAction: shareAction
                                     , boxName: "Share"
                                     , dispatch
                                     , id
                                     , isOpen
                                     , text: "username" } []
                ] (H.div {} [])
------------------------------------------------------------------------
publishNode :: R2.Component SubTreeParamsIn
publishNode = R.createElement publishNodeCpt
publishNodeCpt :: R.Component SubTreeParamsIn
publishNodeCpt = here.component "publishNode" cpt
78
  where
79
    cpt { boxes, dispatch, id, nodeType, session, subTreeParams } _ = do
80 81
      action <- T.useBox (Action.SharePublic { params: Nothing })
      action' <- T.useLive T.unequal action
82

83 84
      let button = case action' of
              Action.SharePublic { params } -> case params of
85
                Just val -> Tools.submitButton (Action.SharePublic {params: Just val}) dispatch
86 87 88
                Nothing -> H.div {} []
              _   -> H.div {} []

89 90
      pure $ Tools.panel
        [ subTreeView { action
91
                      , boxes
92 93 94 95 96 97 98
                      , dispatch
                      , id
                      , nodeType
                      , session
                      , subTreeParams
                      } []
        ] button