File.purs 2.88 KB
Newer Older
1 2 3 4 5
module Gargantext.Components.Nodes.File where

import Data.Argonaut (class DecodeJson, decodeJson, (.:))
import Data.Maybe (Maybe(..))
import DOM.Simple.Console (log2)
6
import Effect.Aff (Aff)
7 8 9 10
import Reactix as R
import Reactix.DOM.HTML as H

import Gargantext.Prelude
11
import Gargantext.Ends (toUrl)
12 13 14 15
import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Routes (SessionRoute(..))
import Gargantext.Sessions (Session, get)
import Gargantext.Types as T
16 17 18
import Gargantext.Utils.Reactix as R2

thisModule = "Gargantext.Components.Nodes.File"
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72


newtype HyperdataFile = HyperdataFile {
    mime :: String
  , name :: String
  , path :: String
  }

instance decodeHyperdataFile :: DecodeJson HyperdataFile where
  decodeJson json = do
    obj <- decodeJson json
    mime <- obj .: "mime"
    name <- obj .: "name"
    path <- obj .: "path"
    pure $ HyperdataFile {
        mime
      , name
      , path
      }


newtype File = File {
    id :: Int
  , date :: String
  , hyperdata :: HyperdataFile
  , name :: String
  }

instance decodeFile :: DecodeJson File where
  decodeJson json = do
    obj <- decodeJson json
    id <- obj .: "id"
    date <- obj .: "date"
    hyperdata' <- obj .: "hyperdata"
    hyperdata <- decodeJson hyperdata'
    name <- obj .: "name"

    pure $ File {
        id
      , date
      , hyperdata
      , name
      }


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

fileLayout :: Record FileLayoutProps -> R.Element
fileLayout props = R.createElement fileLayoutCpt props []

fileLayoutCpt :: R.Component FileLayoutProps
73
fileLayoutCpt = R.hooksComponentWithModule thisModule "fileLayout" cpt
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  where
    cpt { nodeId, session } _ = do
      useLoader { nodeId } (loadFile session) $ \loaded ->
        fileLayoutLoaded { loaded, nodeId, session }

type LoadFileProps = (
  nodeId :: Int
  )

loadFile :: Session -> Record LoadFileProps -> Aff File
loadFile session { nodeId } = get session $ NodeAPI T.Node (Just nodeId) ""

type FileLayoutLoadedProps = (
  loaded :: File
  | FileLayoutProps
  )

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

fileLayoutLoadedCpt :: R.Component FileLayoutLoadedProps
95
fileLayoutLoadedCpt = R.hooksComponentWithModule thisModule "fileLayoutLoaded" cpt
96 97 98 99 100 101 102 103 104 105
  where
    cpt { loaded: File { hyperdata: HyperdataFile hyperdata }, nodeId, session } _ = do
      R.useEffect' $ do
        log2 "[fileLayoutLoaded] hyperdata" hyperdata

      pure $ H.div { className: "col-md-12" } [
          H.div { className: "row" } [
            H.h2 {} [ H.text hyperdata.name ]
          ]
        , H.div { className: "row" } [
106
            H.div { className: "btn btn-secondary" } [
107
               H.a { href: toUrl session ("node/" <> show nodeId <> "/file/download")
108 109 110 111 112
                   , target: "_blank"
                   } [ H.text "Download" ]
               ]
          ]
      ]