Frame.purs 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
module Gargantext.Components.Nodes.Frame where

import Data.Maybe (Maybe(..))
import Data.Tuple (fst)
import Effect.Aff (Aff)
import Reactix as R
import Reactix.DOM.HTML as H
import Data.Argonaut as Argonaut
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
--import Gargantext.Utils.Argonaut (genericSumDecodeJson, genericSumEncodeJson, genericEnumDecodeJson, genericEnumEncodeJson)
import Data.Argonaut (decodeJson, (.:))

14 15 16 17 18
import Gargantext.Components.Node (NodePoly(..))
import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Prelude
import Gargantext.Routes (SessionRoute(NodeAPI))
import Gargantext.Sessions (Session, get, sessionId)
19
import Gargantext.Types (NodeType(..), ReloadS)
20
import Gargantext.Utils.Argonaut (genericSumEncodeJson)
21 22 23
import Gargantext.Utils.Reactix as R2

thisModule = "Gargantext.Components.Nodes.Frame"
24

25
data Hyperdata =
26
  Hyperdata { base     :: String
27 28 29 30 31 32 33 34 35 36 37 38 39 40
            , frame_id :: String
            }

derive instance eqHyperdata :: Eq Hyperdata

derive instance genericHyperdata :: Generic Hyperdata _

instance showHyperdata :: Show Hyperdata where
  show = genericShow

instance decodeJsonHyperdata :: Argonaut.DecodeJson Hyperdata where
-- TODO
--  decodeJson = genericSumDecodeJson
  decodeJson json = do
41 42
    obj      <- decodeJson json
    base     <- obj .: "base"
43 44 45 46 47 48 49 50 51
    frame_id <- obj .: "frame_id"
    pure $ Hyperdata {base, frame_id}


instance encodeJsonHyperdata :: Argonaut.EncodeJson Hyperdata where
  encodeJson = genericSumEncodeJson


type Props =
52 53 54
  ( nodeId   :: Int
  , session  :: Session
  , nodeType :: NodeType
55 56 57 58 59 60 61
  )

type KeyProps =
  ( key :: String
  | Props
  )

62
frameLayout :: Record Props -> R.Element
63 64
frameLayout props = R.createElement frameLayoutCpt props []

65
frameLayoutCpt :: R.Component Props
66
frameLayoutCpt = R.hooksComponentWithModule thisModule "frameLayout" cpt
67
  where
68
    cpt {nodeId, session, nodeType} _ = do
69 70
      let sid = sessionId session

71
      pure $ frameLayoutWithKey { key: show sid <> "-" <> show nodeId, nodeId, session, nodeType}
72 73 74 75 76

frameLayoutWithKey :: Record KeyProps -> R.Element
frameLayoutWithKey props = R.createElement frameLayoutWithKeyCpt props []

frameLayoutWithKeyCpt :: R.Component KeyProps
77
frameLayoutWithKeyCpt = R.hooksComponentWithModule thisModule "frameLayoutWithKey" cpt
78
  where
79
    cpt { nodeId, session, nodeType} _ = do
80 81 82
      reload <- R.useState' 0

      useLoader {nodeId, reload: fst reload, session} loadframeWithReload $
83
        \frame -> frameLayoutView {frame, nodeId, reload, session, nodeType}
84 85 86

type ViewProps =
  ( frame  :: NodePoly Hyperdata
87
  , reload  :: ReloadS
88 89 90 91 92 93 94
  | Props
  )


type Base = String
type FrameId = String

95
hframeUrl :: NodeType -> Base -> FrameId -> String
96
hframeUrl NodeFrameNotebook _    frame_id = frame_id  -- Temp fix : frame_id is currently the whole url created
97
hframeUrl _             base frame_id = base <> "/" <> frame_id <> "?both"
98 99 100 101 102

frameLayoutView :: Record ViewProps -> R.Element
frameLayoutView props = R.createElement frameLayoutViewCpt props []

frameLayoutViewCpt :: R.Component ViewProps
103
frameLayoutViewCpt = R.hooksComponentWithModule thisModule "frameLayoutView" cpt
104
  where
105
    cpt {frame: (NodePoly {hyperdata: Hyperdata {base, frame_id}}), nodeId, reload, session, nodeType} _ = do
106
      pure $ H.div { className : "frame" }
107
                   [ H.iframe { src: hframeUrl nodeType base frame_id
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                              , width: "100%"
                              , height: "100%"
                              } []
                   ]


type LoadProps = 
  ( nodeId  :: Int
  , session :: Session
  )

loadframe' :: Record LoadProps -> Aff (NodePoly Hyperdata)
loadframe' {nodeId, session} = get session $ NodeAPI Node (Just nodeId) ""

-- Just to make reloading effective
loadframeWithReload :: {reload :: Int  | LoadProps} -> Aff (NodePoly Hyperdata)
loadframeWithReload {nodeId, session} = loadframe' {nodeId, session}