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

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

14
import Gargantext.Config (publicBackend)
15
import Gargantext.Config.REST (get)
16
import Gargantext.Ends (backendUrl)
17
import Gargantext.Hooks.Loader (useLoader)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
18
import Gargantext.Prelude
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 37 38 39
derive instance Eq PublicData
derive instance Generic PublicData _
instance JSON.ReadForeign PublicData where readImpl = JSONG.untaggedSumRep
instance Show PublicData where
40 41 42 43 44 45
  show = genericShow

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

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

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

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

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

James Laver's avatar
James Laver committed
69
renderPublic :: R2.Leaf ()
70
renderPublic props = R.createElement renderPublicCpt props []
James Laver's avatar
James Laver committed
71 72 73
renderPublicCpt :: R.Component ()
renderPublicCpt = here.component "renderPublic" cpt where
  cpt _ _ = do
74
    useLoader { reload: 0 } loadPublicData loaded where
James Laver's avatar
James Laver committed
75
      loaded publicData = publicLayout { publicData }
76

77
publicLayout :: Record PublicDataProps -> R.Element
78 79
publicLayout props = R.createElement publicLayoutCpt props []

80
publicLayoutCpt :: R.Component PublicDataProps
James Laver's avatar
James Laver committed
81
publicLayoutCpt = here.component "publicLayout" cpt
82
  where
James Laver's avatar
James Laver committed
83 84 85 86 87 88 89 90 91 92 93 94
    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
95

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

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