Public.purs 4.61 KB
Newer Older
1 2
module Gargantext.Components.Nodes.Home.Public where

3 4 5 6
import Data.Argonaut as Argonaut
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Maybe (Maybe(..))
7
import Data.String (take)
James Laver's avatar
James Laver committed
8
import Data.Tuple (fst)
9
import Effect.Aff (Aff)
10 11 12
import Reactix as R
import Reactix.DOM.HTML as H

13
import Gargantext.Config (publicBackend)
14
import Gargantext.Config.REST (get)
15
import Gargantext.Ends (backendUrl)
16
import Gargantext.Hooks.Loader (useLoader)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
17
import Gargantext.Prelude
18
import Gargantext.Utils.Argonaut (genericSumDecodeJson, genericSumEncodeJson)
19 20
import Gargantext.Utils.Reactix as R2

James Laver's avatar
James Laver committed
21 22
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Home.Public"
23

James Laver's avatar
James Laver committed
24
type PublicDataProps = ( publicData :: Array PublicData )
25

26
data PublicData = PublicData
27 28 29 30 31 32 33
  { title    :: String
  , abstract  :: String
  , img      :: String
  , url      :: String
  , date     :: String
  , database :: String
  , author   :: String
34
  } | NoData { nodata :: String }
35

36
derive instance eqPublicData :: Eq PublicData
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
derive instance genericPublicData :: Generic PublicData _

instance showPublicData :: Show PublicData where
  show = genericShow

instance decodeJsonPublicData :: Argonaut.DecodeJson PublicData where
  decodeJson = genericSumDecodeJson

instance encodeJsonPublicData :: Argonaut.EncodeJson PublicData where
  encodeJson = genericSumEncodeJson

------------------------------------------------------------------------
type LoadData  = ()
type LoadProps = (reload :: Int)

53
-- | WIP still finding the right way to chose the default public backend
54
loadPublicData :: Record LoadProps -> Aff (Array PublicData)
55
loadPublicData _l = do
56
  -- This solution is error prone (url needs to be cleaned)
James Laver's avatar
James Laver committed
57
  --backend <- liftEffect public
58 59 60 61

  -- This solution for development only, with local backend
  -- let backend = head defaultBackends
  let backend = publicBackend
Alexandre Delanoë's avatar
Alexandre Delanoë committed
62
  get Nothing (backendUrl backend "public")
63

64
{- | Another solution: get all data
Alexandre Delanoë's avatar
Alexandre Delanoë committed
65 66
  let
    ok = ["local.cnrs", "devel.inshs.cnrs"]
James Laver's avatar
James Laver committed
67 68 69
    backends = Array.filter
      (\(Backend {name}) -> Array.elem name ok)
     (toArray defaultBackends)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
70

James Laver's avatar
James Laver committed
71 72 73
  Array.concat <$> traverse
    (\backend -> get Nothing (backendUrl backend "public"))
    backends
Alexandre Delanoë's avatar
Alexandre Delanoë committed
74 75
-}

James Laver's avatar
James Laver committed
76
renderPublic :: R2.Leaf ()
77
renderPublic props = R.createElement renderPublicCpt props []
78

James Laver's avatar
James Laver committed
79 80 81 82 83 84
renderPublicCpt :: R.Component ()
renderPublicCpt = here.component "renderPublic" cpt where
  cpt _ _ = do
    reload <- R.useState' 0
    useLoader { reload: fst reload } loadPublicData loaded where
      loaded publicData = publicLayout { publicData }
85

86
publicLayout :: Record PublicDataProps -> R.Element
87 88
publicLayout props = R.createElement publicLayoutCpt props []

89
publicLayoutCpt :: R.Component PublicDataProps
James Laver's avatar
James Laver committed
90
publicLayoutCpt = here.component "publicLayout" cpt
91
  where
James Laver's avatar
James Laver committed
92 93 94 95 96 97 98 99 100 101 102 103
    cpt { publicData } _ = do
      pure $
        H.span {}
        [ H.div { className: "text-center" }
          [ H.div { className:"container1", style: { marginBottom: "15px" }}
            [ H.h2 {}
              [ H.text "Discover knowledge"
              , H.p { className: "lead text-muted center"}
                [ H.text "maps made with "
                , H.span { className: "fa fa-heart" } [] ]]]]
        -- | TODO: browse maps, random maps
        , album publicData ]
Alexandre Delanoë's avatar
Alexandre Delanoë committed
104

105
album :: Array PublicData -> R.Element
James Laver's avatar
James Laver committed
106 107 108 109 110
album pd =
  H.div { className: "album py-5 bg-light" }
  [ H.div { className: "container" }
    [ H.div { className: "row" } (map content pd) ]]
  where content tab = H.div { className: "col-md-6 content"} [ tableau tab ]
111 112

tableau :: PublicData -> R.Element
James Laver's avatar
James Laver committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
tableau (NoData _) =
  H.div { className : "center" }
  [ H.h2 {} [ H.text "Create a corpus and publicize it" ]]
tableau (PublicData { title, abstract, img, url, date, database, author }) =
  H.div { className: "card mb-6 box-shadow" }
  [ H.a { target: "_blank", href: url }
    [ H.div { className:"center"} [ H.img { src: img, width: "50%" } ]]
  , H.div { className : "card-body"}
    [ H.h3 {} [H.text title]
    , H.p { className: "card-text"} [ H.text $ (take 252 abstract) <> "..." ]
    , H.div { className: "center justify-content-between align-items-center" }
      [ H.div { className: "btn-group" }
        [ H.div { className : "small text-muted flex-end" }
          [ H.text
            $  "Made by " <> author
            <> " on "     <> date
            <> " with "   <> database ]]]]]
        -- , H.button { className: "btn btn-primary flex-between"
        --            , href: url, role: "button" }
        --            [ H.text "View the map" ]
        -- , H.button { className: "btn btn-primary flex-start"
        --            , href: url, role: "button" }
        --            [ H.text "More like this" ]]]]