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

3 4
import Gargantext.Prelude

5
import Data.Eq.Generic (genericEq)
6
import Data.Generic.Rep (class Generic)
7
import Data.Maybe (Maybe(..))
8
import Data.Newtype (class Newtype)
9
import Gargantext.Config.REST (AffRESTError, logRESTError)
10
import Gargantext.Ends (toUrl)
11 12
import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Routes (SessionRoute(..))
13
import Gargantext.Sessions (Session, get)
James Laver's avatar
James Laver committed
14
import Gargantext.Types (NodeType(..), NodeID)
15
import Gargantext.Utils.Reactix as R2
16 17 18
import Reactix as R
import Reactix.DOM.HTML as H
import Simple.JSON as JSON
19

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
fileLayout = R2.leafComponent fileLayoutCpt
52
fileLayoutCpt :: R.Component FileLayoutProps
James Laver's avatar
James Laver committed
53
fileLayoutCpt = here.component "fileLayout" cpt where
54
  cpt { nodeId, session } _ = do
55 56 57 58
    useLoader { errorHandler
              , loader: loadFile session
              , path: nodeId
              , render: onLoad }
59
      where
60
        errorHandler = logRESTError here "[fileLayout]"
61
        onLoad loaded = fileLayoutLoaded { loaded, nodeId, session }
James Laver's avatar
James Laver committed
62

63
loadFile :: Session -> NodeID -> AffRESTError File
James Laver's avatar
James Laver committed
64 65 66 67
loadFile session nodeId = get session $ NodeAPI Node (Just nodeId) ""

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

fileLayoutLoaded :: Record FileLayoutLoadedProps -> R.Element
fileLayoutLoaded props = R.createElement fileLayoutLoadedCpt props []
fileLayoutLoadedCpt :: R.Component FileLayoutLoadedProps
James Laver's avatar
James Laver committed
74 75 76 77 78 79 80 81 82 83
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")