Download.purs 5.03 KB
Newer Older
1 2
module Gargantext.Components.Forest.Tree.Node.Action.Download where

3
import Data.Generic.Rep (class Generic)
4
import Data.Maybe (Maybe(..))
5 6
import Data.Show.Generic (genericShow)
import Data.String.Common (toLower)
7
import Gargantext.Components.Forest.Tree.Node.Action.Types (Action(DownloadNode))
8
import Gargantext.Components.Forest.Tree.Node.Tools (fragmentPT, panel, submitButtonHref)
9
import Gargantext.Ends (url)
10
import Gargantext.Prelude
11 12
import Gargantext.Routes as Routes
import Gargantext.Sessions (Session)
13
import Gargantext.Types (ID)
14
import Gargantext.Types as GT
15
import Gargantext.Utils.Reactix as R2
16 17 18
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
19 20

here :: R2.Here
21
here = R2.here "Gargantext.Components.Forest.Tree.Node.Action.Download"
22 23

-- | Action : Download
24 25 26 27 28 29 30 31 32
type ActionDownload =
  ( id       :: ID
  , nodeType :: GT.NodeType
  , session  :: Session )

actionDownload :: R2.Component ActionDownload
actionDownload = R.createElement actionDownloadCpt
actionDownloadCpt :: R.Component ActionDownload
actionDownloadCpt = here.component "actionDownload" cpt where
33 34 35 36 37
  cpt props@{ nodeType: GT.Corpus } _    = pure $ actionDownloadCorpus props []
  cpt props@{ nodeType: GT.Graph } _     = pure $ actionDownloadGraph props []
  cpt props@{ nodeType: GT.NodeList }  _ = pure $ actionDownloadNodeList props []
  cpt props@{ nodeType: GT.NodeTexts } _ = pure $ actionDownloadNodeTexts props []
  cpt props@{ nodeType: _ } _            = pure $ actionDownloadOther props []
38 39 40 41 42 43 44 45

actionDownloadCorpus :: R2.Component ActionDownload
actionDownloadCorpus = R.createElement actionDownloadCorpusCpt
actionDownloadCorpusCpt :: R.Component ActionDownload
actionDownloadCorpusCpt = here.component "actionDownloadCorpus" cpt where
  cpt { id, session } _ = do
    pure $ panel [H.div {} [H.text info]]
      (submitButtonHref DownloadNode href)
46
    where
47 48
      href  = url session $ Routes.NodeAPI GT.Corpus (Just id) "export"
      info  = "Download as JSON"
49

50 51 52 53 54 55 56
actionDownloadGraph :: R2.Component ActionDownload
actionDownloadGraph = R.createElement actionDownloadGraphCpt
actionDownloadGraphCpt :: R.Component ActionDownload
actionDownloadGraphCpt = here.component "actionDownloadGraph" cpt where
  cpt { id, session } _ = do
    pure $ panel [H.div {} [H.text info]]
      (submitButtonHref DownloadNode href)
57 58 59 60
    where
      href  = url session $ Routes.NodeAPI GT.Graph (Just id) "gexf"
      info  = "Info about the Graph as GEXF format"

61 62 63 64 65 66 67
actionDownloadNodeList :: R2.Component ActionDownload
actionDownloadNodeList = R.createElement actionDownloadNodeListCpt
actionDownloadNodeListCpt :: R.Component ActionDownload
actionDownloadNodeListCpt = here.component "actionDownloadNodeList" cpt where
  cpt { id, session } _ = do
    pure $ panel [ H.div {} [H.text info] ]
      (submitButtonHref DownloadNode href)
68
    where
69 70
      href  = url session $ Routes.NodeAPI GT.NodeList (Just id) ""
      info  = "Info about the List as JSON format"
71

72 73 74 75 76
data NodeTextsDownloadFormat = CSV | JSON
derive instance Eq NodeTextsDownloadFormat
derive instance Generic NodeTextsDownloadFormat _
instance Show NodeTextsDownloadFormat where show = genericShow

77 78 79 80 81
readDownloadFormat :: String -> NodeTextsDownloadFormat
readDownloadFormat "CSV" = CSV
readDownloadFormat "JSON" = JSON
readDownloadFormat _ = JSON

82 83 84 85 86
actionDownloadNodeTexts :: R2.Component ActionDownload
actionDownloadNodeTexts = R.createElement actionDownloadNodeTextsCpt
actionDownloadNodeTextsCpt :: R.Component ActionDownload
actionDownloadNodeTextsCpt = here.component "actionDownloadNodeTexts" cpt where
  cpt { id, session } _ = do
87 88 89 90
    downloadFormat <- T.useBox JSON
    downloadFormat' <- T.useLive T.unequal downloadFormat
    
    pure $ panel
91 92 93 94 95
      [ R2.select { className: "form-control"
                  , defaultValue: show downloadFormat'
                  , on: { change: onChange downloadFormat } }
        [ opt CSV downloadFormat
        , opt JSON downloadFormat ]
96 97 98
      , H.div {} [ H.text $ info downloadFormat' ]
      ]
      (submitButtonHref DownloadNode $ href downloadFormat')
99
    where
100
      opt t downloadFormat = H.option { value: show t } [ H.text $ show t ]
101 102
        where
          onClick _ = T.write_ t downloadFormat
103
      onChange downloadFormat e = T.write_ (readDownloadFormat $ R.unsafeEventValue e) downloadFormat
104 105
      href t  = url session $ Routes.NodeAPI GT.NodeTexts (Just id) ("export/" <> (toLower $ show t))
      info t  = "Info about the Documents as " <> show t <> " format"
106

107 108 109 110
{-
-- TODO fix the route
actionDownload GT.Texts id session = pure $ panel [H.div {} [H.text info]]
                                           (submitButtonHref DownloadNode href)
111 112 113
    where
      href  = url session $ Routes.NodeAPI GT.Texts (Just id) ""
      info  = "TODO: fix the backend route. What is the expected result ?"
114
      -}
115 116 117 118
actionDownloadOther :: R2.Component ActionDownload
actionDownloadOther = R.createElement actionDownloadOtherCpt
actionDownloadOtherCpt :: R.Component ActionDownload
actionDownloadOtherCpt = here.component "actionDownloadOther" cpt where
119
  cpt _ _ = do
120
    pure $ fragmentPT $ "Soon, you will be able to download your file here "