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

3 4
import Gargantext.Prelude

5 6
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
Alexandre Delanoë's avatar
Alexandre Delanoë committed
7
import Data.Show.Generic (genericShow)
8
import Data.String (take)
9
import Gargantext.Config (publicBackend)
10
import Gargantext.Config.REST (AffRESTError, get, logRESTError)
11
import Gargantext.Ends (backendUrl)
12
import Gargantext.Hooks.Loader (useLoader)
13
import Gargantext.Utils.Reactix as R2
14
import Gargantext.Utils.SimpleJSON as GUSJ
15 16 17
import Reactix as R
import Reactix.DOM.HTML as H
import Simple.JSON as JSON
18

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

James Laver's avatar
James Laver committed
22
type PublicDataProps = ( publicData :: Array PublicData )
23

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

34 35
derive instance Eq PublicData
derive instance Generic PublicData _
36 37
instance JSON.ReadForeign PublicData where readImpl = GUSJ.taggedSumRep
instance Show PublicData where show = genericShow
38 39

------------------------------------------------------------------------
Alexandre Delanoë's avatar
Alexandre Delanoë committed
40
type LoadData :: forall k. Row k
41
type LoadData  = ()
Alexandre Delanoë's avatar
Alexandre Delanoë committed
42

43 44
type LoadProps = (reload :: Int)

45
-- | WIP still finding the right way to chose the default public backend
46
loadPublicData :: Record LoadProps -> AffRESTError (Array PublicData)
47
loadPublicData _l = do
48
  -- This solution is error prone (url needs to be cleaned)
James Laver's avatar
James Laver committed
49
  --backend <- liftEffect public
50 51 52 53

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

56
{- | Another solution: get all data
Alexandre Delanoë's avatar
Alexandre Delanoë committed
57 58
  let
    ok = ["local.cnrs", "devel.inshs.cnrs"]
James Laver's avatar
James Laver committed
59 60 61
    backends = Array.filter
      (\(Backend {name}) -> Array.elem name ok)
     (toArray defaultBackends)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
62

James Laver's avatar
James Laver committed
63 64 65
  Array.concat <$> traverse
    (\backend -> get Nothing (backendUrl backend "public"))
    backends
Alexandre Delanoë's avatar
Alexandre Delanoë committed
66 67
-}

James Laver's avatar
James Laver committed
68
renderPublic :: R2.Leaf ()
69
renderPublic = R2.leafComponent renderPublicCpt
James Laver's avatar
James Laver committed
70 71 72
renderPublicCpt :: R.Component ()
renderPublicCpt = here.component "renderPublic" cpt where
  cpt _ _ = do
73 74 75 76 77 78
    useLoader { errorHandler
              , loader: loadPublicData
              , path: { reload: 0 }
              , render:  loaded }
      where
        loaded publicData = publicLayout { publicData }
79
        errorHandler = logRESTError here "[renderPublic]"
80

81
publicLayout :: Record PublicDataProps -> R.Element
82
publicLayout props = R.createElement publicLayoutCpt props []
83
publicLayoutCpt :: R.Component PublicDataProps
James Laver's avatar
James Laver committed
84
publicLayoutCpt = here.component "publicLayout" cpt
85
  where
James Laver's avatar
James Laver committed
86 87 88 89
    cpt { publicData } _ = do
      pure $
        H.span {}
        [ H.div { className: "text-center" }
arturo's avatar
arturo committed
90
          [ H.div { style: { marginBottom: "15px" }}
James Laver's avatar
James Laver committed
91 92 93 94 95 96 97
            [ 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
98

99
album :: Array PublicData -> R.Element
James Laver's avatar
James Laver committed
100 101 102 103 104
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 ]
105 106

tableau :: PublicData -> R.Element
James Laver's avatar
James Laver committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
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" ]]]]