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

import Data.Argonaut (class DecodeJson, decodeJson, (.:))
4 5
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
6
import Data.Maybe (Maybe(..))
7
import Effect.Aff (Aff)
8 9 10 11
import Reactix as R
import Reactix.DOM.HTML as H

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

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

James Laver's avatar
James Laver committed
22 23 24
newtype HyperdataFile =
  HyperdataFile
  { mime :: String
25 26 27
  , name :: String
  , path :: String
  }
28 29 30
derive instance genericHyperdataFile :: Generic HyperdataFile _
instance eqHyperdataFile :: Eq HyperdataFile where
  eq = genericEq
31 32
instance decodeHyperdataFile :: DecodeJson HyperdataFile where
  decodeJson json = do
James Laver's avatar
James Laver committed
33
    obj  <- decodeJson json
34 35 36
    mime <- obj .: "mime"
    name <- obj .: "name"
    path <- obj .: "path"
James Laver's avatar
James Laver committed
37
    pure $ HyperdataFile { mime, name, path }
38

James Laver's avatar
James Laver committed
39 40 41 42
newtype File =
  File
  { id        :: Int
  , date      :: String
43
  , hyperdata :: HyperdataFile
James Laver's avatar
James Laver committed
44
  , name      :: String
45
  }
46 47 48
derive instance genericFile :: Generic File _
instance eqFile :: Eq File where
  eq = genericEq
49 50
instance decodeFile :: DecodeJson File where
  decodeJson json = do
James Laver's avatar
James Laver committed
51 52 53 54 55 56
    obj       <- decodeJson json
    id        <- obj .: "id"
    date      <- obj .: "date"
    name      <- obj .: "name"
    hyperdata <- (obj .: "hyperdata") >>= decodeJson
    pure $ File { id, date, hyperdata, name }
57

James Laver's avatar
James Laver committed
58
type FileLayoutProps = ( nodeId :: NodeID, session :: R.Context Session )
59

James Laver's avatar
James Laver committed
60
fileLayout :: R2.Leaf FileLayoutProps
61 62 63
fileLayout props = R.createElement fileLayoutCpt props []

fileLayoutCpt :: R.Component FileLayoutProps
James Laver's avatar
James Laver committed
64 65 66 67 68 69 70 71 72 73 74 75 76
fileLayoutCpt = here.component "fileLayout" cpt where
  cpt { nodeId, session } _ = R.useContext session >>= cp where
    cp s = useLoader nodeId (loadFile s) onLoad where
      onLoad loaded = fileLayoutLoaded { loaded, nodeId, session: s } where
        key = show (sessionId s) <> "-" <> show nodeId

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

type FileLayoutLoadedProps =
  ( loaded  :: File
  , nodeId  :: Int
  , session :: Session
77 78 79 80 81 82
  )

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

fileLayoutLoadedCpt :: R.Component FileLayoutLoadedProps
James Laver's avatar
James Laver committed
83 84 85 86 87 88 89 90 91 92
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")