Link.purs 2.77 KB
Newer Older
1
module Gargantext.Components.Forest.Tree.Node.Action.Link where
2

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

13 14
import Gargantext.Components.Forest.Tree.Node.Action (Action(..))
import Gargantext.Components.Forest.Tree.Node.Tools (submitButton, panel)
15
import Gargantext.Components.Forest.Tree.Node.Tools.SubTree (subTreeView, SubTreeParamsIn)
16
import Gargantext.Prelude
17
import Gargantext.Routes (SessionRoute(..))
18
import Gargantext.Sessions (Session, post)
19
import Gargantext.Types  as GT
20
import Gargantext.Utils.Argonaut (genericSumDecodeJson, genericSumEncodeJson)
21 22
import Gargantext.Utils.Reactix as R2

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

26
data LinkNodeReq = LinkNodeReq { nodeType :: GT.NodeType, id :: GT.ID }
27 28 29 30 31 32 33 34 35 36 37 38 39 40

derive instance eqLinkNodeReq :: Eq LinkNodeReq
derive instance genericLinkNodeReq :: Generic LinkNodeReq _
instance showLinkNodeReq :: Show LinkNodeReq where
  show = genericShow
instance decodeJsonLinkNodeReq :: Argonaut.DecodeJson LinkNodeReq where
  decodeJson = genericSumDecodeJson
instance encodeJsonLinkNodeReq :: Argonaut.EncodeJson LinkNodeReq where
  encodeJson = genericSumEncodeJson


linkNodeReq :: Session -> Maybe GT.NodeType -> GT.ID -> GT.ID -> Aff GT.AsyncTaskWithType
linkNodeReq session nt fromId toId = do
  task <- post session (NodeAPI GT.Node (Just fromId) "update")
41
                       (LinkNodeReq { nodeType: linkNodeType nt, id: toId })
42 43 44 45 46 47 48
  pure $ GT.AsyncTaskWithType {task, typ: GT.UpdateNode }

linkNodeType :: Maybe GT.NodeType -> GT.NodeType
linkNodeType (Just GT.Corpus)   = GT.Annuaire
linkNodeType (Just GT.Annuaire) = GT.Corpus
linkNodeType  _   = GT.Error

49

50 51
linkNode :: R2.Component SubTreeParamsIn
linkNode = R.createElement linkNodeCpt
52

53
linkNodeCpt :: R.Component SubTreeParamsIn
54
linkNodeCpt = here.component "linkNode" cpt
55
  where
56
    cpt p@{dispatch, subTreeParams, id, nodeType, session, handed} _ = do
57 58
      action <- T.useBox (LinkNode { nodeType: Nothing, params: Nothing})
      action' <- T.useLive T.unequal action
59

60 61
      let button = case action' of
              LinkNode { params } -> case params of
62
                Just val -> submitButton (LinkNode {nodeType: Just nodeType, params: Just val}) dispatch
63 64
                Nothing -> H.div {} []
              _                   -> H.div {} []
65

66 67 68
      pure $ panel [
          subTreeView { action
                      , dispatch
69
                      , handed
70 71 72 73
                      , id
                      , nodeType
                      , session
                      , subTreeParams
74
                      } []
75
              ] button