Move.purs 2.3 KB
Newer Older
1 2 3 4
module Gargantext.Components.Forest.Tree.Node.Action.Move
  ( moveNodeReq
  , moveNode
  ) where
5

6 7
import Gargantext.Prelude

8
import Data.Maybe (Maybe(..))
9
import Gargantext.Components.Forest.Tree.Node.Action.Types (Action(..))
10
import Gargantext.Components.Forest.Tree.Node.Tools (submitButton, panel)
11
import Gargantext.Components.Forest.Tree.Node.Tools.SubTree (SubTreeParamsIn, subTreeView)
12
import Gargantext.Config.REST (AffRESTError)
13 14
import Gargantext.Routes (SessionRoute(..))
import Gargantext.Sessions (Session, put_)
15
import Gargantext.Types as GT
16
import Gargantext.Utils.Reactix as R2
17 18 19
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
20

21 22
here :: R2.Here
here = R2.here "Gargantext.Components.Forest.Tree.Node.Action.Move"
23

24
moveNodeReq :: Session -> GT.ID -> GT.ID -> AffRESTError (Array GT.ID)
25
moveNodeReq session fromId toId =
26 27
  put_ session $ NodeAPI GT.Node (Just fromId) ("move/" <> show toId)

28 29
moveNode :: R2.Component SubTreeParamsIn
moveNode = R.createElement moveNodeCpt
30
moveNodeCpt :: R.Component SubTreeParamsIn
31
moveNodeCpt = here.component "moveNode" cpt
32
  where
33
    cpt { boxes, dispatch, id, nodeType, session, subTreeParams } _ = do
34
      action :: T.Box Action <- T.useBox (MoveNode {params: Nothing})
35

36 37
      pure $

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        moveNode' { action
            , boxes
            , dispatch
            , id
            , nodeType
            , session
            , subTreeParams
            } []

type Props =
  ( action :: T.Box Action
  | SubTreeParamsIn
  )

-- @XXX re-render issue -> clone component
moveNode' :: R2.Component Props
moveNode' = R.createElement moveNodeCpt'
moveNodeCpt' :: R.Component Props
arturo's avatar
arturo committed
56
moveNodeCpt' = here.component "__clone__" cpt where
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  cpt { boxes, dispatch, id, nodeType, session, subTreeParams, action } _ = do

    action' <- T.useLive T.unequal action

    let button = case action' of
            MoveNode { params } -> case params of
              Just val -> submitButton (MoveNode {params: Just val}) dispatch
              Nothing -> H.div {} []
            _                   -> H.div {} []

    pure $

      panel
      [ subTreeView { action
                      , boxes
                      , dispatch
                      , id
                      , nodeType
                      , session
                      , subTreeParams
                      } []
      ] button