File.purs 2.54 KB
Newer Older
1 2
module Gargantext.Components.Nodes.File where

3
import Data.Generic.Rep (class Generic)
4
import Data.Eq.Generic (genericEq)
5
import Data.Maybe (Maybe(..))
6
import Data.Newtype (class Newtype)
7
import Effect.Aff (Aff)
8 9
import Reactix as R
import Reactix.DOM.HTML as H
10
import Simple.JSON as JSON
11 12

import Gargantext.Prelude
13
import Gargantext.Ends (toUrl)
14 15
import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Routes (SessionRoute(..))
James Laver's avatar
James Laver committed
16 17
import Gargantext.Sessions (Session, get, sessionId)
import Gargantext.Types (NodeType(..), NodeID)
18 19
import Gargantext.Utils.Reactix as R2

James Laver's avatar
James Laver committed
20 21
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.File"
22

James Laver's avatar
James Laver committed
23 24 25
newtype HyperdataFile =
  HyperdataFile
  { mime :: String
26 27 28
  , name :: String
  , path :: String
  }
29 30 31 32
derive instance Generic HyperdataFile _
derive instance Newtype HyperdataFile _
derive newtype instance JSON.ReadForeign HyperdataFile
instance Eq HyperdataFile where
33
  eq = genericEq
34

James Laver's avatar
James Laver committed
35 36 37 38
newtype File =
  File
  { id        :: Int
  , date      :: String
39
  , hyperdata :: HyperdataFile
James Laver's avatar
James Laver committed
40
  , name      :: String
41
  }
42 43 44 45
derive instance Generic File _
derive instance Newtype File _
derive newtype instance JSON.ReadForeign File
instance Eq File where
46
  eq = genericEq
47

48
type FileLayoutProps = ( nodeId :: NodeID, session :: Session )
49

James Laver's avatar
James Laver committed
50
fileLayout :: R2.Leaf FileLayoutProps
51 52 53
fileLayout props = R.createElement fileLayoutCpt props []

fileLayoutCpt :: R.Component FileLayoutProps
James Laver's avatar
James Laver committed
54
fileLayoutCpt = here.component "fileLayout" cpt where
55 56 57 58 59
  cpt { nodeId, session } _ = do
    useLoader nodeId (loadFile session) onLoad
      where
        onLoad loaded = fileLayoutLoaded { loaded, nodeId, session }
        key = show (sessionId session) <> "-" <> show nodeId
James Laver's avatar
James Laver committed
60 61 62 63 64 65

loadFile :: Session -> NodeID -> Aff File
loadFile session nodeId = get session $ NodeAPI Node (Just nodeId) ""

type FileLayoutLoadedProps =
  ( loaded  :: File
66
  | FileLayoutProps
67 68 69 70 71 72
  )

fileLayoutLoaded :: Record FileLayoutLoadedProps -> R.Element
fileLayoutLoaded props = R.createElement fileLayoutLoadedCpt props []

fileLayoutLoadedCpt :: R.Component FileLayoutLoadedProps
James Laver's avatar
James Laver committed
73 74 75 76 77 78 79 80 81 82
fileLayoutLoadedCpt = here.component "fileLayoutLoaded" cpt where
  cpt { loaded: File { hyperdata: HyperdataFile hyperdata }, nodeId, session } _ = do
    R.useEffect' $ here.log hyperdata
    pure $
      H.div { className: "col-md-12" }
      [ H.div { className: "row" } [ H.h2 {} [ H.text hyperdata.name ] ]
      , H.div { className: "row" }
        [ H.div { className: "btn btn-primary" }
          [ H.a { href, target: "_blank" } [ H.text "Download" ]]]] where
      href = toUrl session ("node/" <> show nodeId <> "/file/download")