Sync.purs 4.34 KB
Newer Older
1 2
module Gargantext.Components.Forest.Tree.Node.Tools.Sync where

arturo's avatar
arturo committed
3 4
import Gargantext.Prelude

5
import Data.Maybe (Maybe(..))
6
import Effect.Aff (Aff, launchAff_)
7
import Effect.Class (liftEffect)
arturo's avatar
arturo committed
8
import Gargantext.Components.Bootstrap as B
arturo's avatar
arturo committed
9
import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), Elevation(..), Variant(..))
10
import Gargantext.Components.GraphExplorer.API as GraphAPI
11
import Gargantext.Sessions (Session)
12
import Gargantext.Types as GT
arturo's avatar
arturo committed
13
import Gargantext.Utils ((?))
14
import Gargantext.Utils.Reactix as R2
15 16 17
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
18

19
here :: R2.Here
20
here = R2.here "Gargantext.Components.Forest.Tree.Node.Tools.Sync"
21 22 23 24


-- | Sync Node (Graph)
type NodeActionsGraphProps =
25
  ( id             :: GT.ID
26 27
  , graphVersions  :: Record GraphAPI.GraphVersions
  , session        :: Session
James Laver's avatar
James Laver committed
28
  , refresh :: Unit -> Aff Unit
29 30
  )

31 32
nodeActionsGraph :: R2.Component NodeActionsGraphProps
nodeActionsGraph = R.createElement nodeActionsGraphCpt
33
nodeActionsGraphCpt :: R.Component NodeActionsGraphProps
arturo's avatar
arturo committed
34 35 36 37 38
nodeActionsGraphCpt = here.component "nodeActionsGraph" cpt where
  cpt { id, graphVersions, session, refresh } _ =
    let sameVersions = (graphVersions.gv_graph == Just graphVersions.gv_repo)
    in pure $

arturo's avatar
arturo committed
39
      R2.when (not sameVersions) $
arturo's avatar
arturo committed
40 41 42

        graphUpdateButton { id, session, refresh }

43 44

type GraphUpdateButtonProps =
45
  ( id :: GT.ID
46
  , session :: Session
James Laver's avatar
James Laver committed
47
  , refresh :: Unit -> Aff Unit
48 49 50 51 52
  )

graphUpdateButton :: Record GraphUpdateButtonProps -> R.Element
graphUpdateButton p = R.createElement graphUpdateButtonCpt p []
graphUpdateButtonCpt :: R.Component GraphUpdateButtonProps
53
graphUpdateButtonCpt = here.component "graphUpdateButton" cpt
54
  where
James Laver's avatar
James Laver committed
55
    cpt { id, session, refresh } _ = do
56 57
      enabled <- T.useBox true
      enabled' <- T.useLive T.unequal enabled
58

arturo's avatar
arturo committed
59 60 61 62 63
      pure $

        B.iconButton
        { className: "mainleaf__update-icon"
        , variant: Secondary
arturo's avatar
arturo committed
64
        , elevation: Level1
arturo's avatar
arturo committed
65 66 67 68 69 70 71 72 73 74 75 76
        , status: enabled' ? Enabled $ Disabled
        , callback: const $ onClick enabled' enabled
        , name: "refresh"
        }

        -- H.div { className: "update-button "
        --            <> if enabled'
        --                  then "enabled"
        --                  else "disabled text-muted"
        --            } [ H.span { className: "fa fa-refresh"
        --              , on: { click: onClick enabled' enabled } } []
        --              ]
77
      where
78 79
        onClick false _ = pure unit
        onClick true enabled = do
80
          launchAff_ $ do
81
            liftEffect $ T.write_ false enabled
82
            _g <- GraphAPI.updateGraphVersions { graphId: id, session }
83
            liftEffect $ T.write_ true enabled
James Laver's avatar
James Laver committed
84
            refresh unit
85 86
          pure unit

87

88 89 90 91
-- | Sync Node (List)
type NodeActionsNodeListProps =
  (
    listId :: GT.ListId
92
  , nodeId :: GT.ID
93 94
  , nodeType :: GT.TabSubType GT.CTabNgramType
  , session :: Session
James Laver's avatar
James Laver committed
95
  , refresh :: Unit -> Aff Unit
96 97 98 99 100
  )

nodeActionsNodeList :: Record NodeActionsNodeListProps -> R.Element
nodeActionsNodeList p = R.createElement nodeActionsNodeListCpt p []
nodeActionsNodeListCpt :: R.Component NodeActionsNodeListProps
arturo's avatar
arturo committed
101 102 103
nodeActionsNodeListCpt = here.component "nodeActionsNodeList" cpt where
  cpt props _ = pure $ nodeListUpdateButton props

104 105 106

type NodeListUpdateButtonProps =
  ( listId :: GT.ListId
107
  , nodeId :: GT.ID
108 109
  , nodeType :: GT.TabSubType GT.CTabNgramType
  , session :: Session
James Laver's avatar
James Laver committed
110
  , refresh :: Unit -> Aff Unit
111 112 113 114 115
  )

nodeListUpdateButton :: Record NodeListUpdateButtonProps -> R.Element
nodeListUpdateButton p = R.createElement nodeListUpdateButtonCpt p []
nodeListUpdateButtonCpt :: R.Component NodeListUpdateButtonProps
116
nodeListUpdateButtonCpt = here.component "nodeListUpdateButton" cpt
117
  where
118
    cpt _ _ = do
119
      -- enabled <- T.useBox true
120

121
      pure $ H.div {} [] {- { className: "update-button "
122 123 124 125 126 127 128 129 130 131 132
                     <> if (fst enabled) then "enabled" else "disabled text-muted"
                   } [ H.span { className: "fa fa-refresh"
                     , on: { click: onClick enabled } } []
                     ]
      where
        onClick (false /\ _) _ = pure unit
        onClick (true /\ setEnabled) _ = do
          launchAff_ $ do
            liftEffect $ setEnabled $ const false
            _ <- NTAPI.updateNodeList { listId, nodeId, nodeType, session }
            liftEffect $ setEnabled $ const true
James Laver's avatar
James Laver committed
133
            refresh unit
134
          pure unit
135
      -}