ContactList.purs 6.39 KB
Newer Older
arturo's avatar
arturo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
module Gargantext.Components.GraphExplorer.Sidebar.ContactList
  ( contactListWrapper
  ) where

import Gargantext.Prelude

import Data.Array (concat, head)
import Data.Foldable (intercalate)
import Data.Map as Map
import Data.Maybe (Maybe(..))
import Data.Sequence as Seq
import Data.Set as Set
import Data.Tuple.Nested ((/\))
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.FacetsTable (ContactsView(..), Rows(..), initialPagePath, loadPage)
import Gargantext.Components.GraphExplorer.Store as GraphStore
import Gargantext.Components.GraphExplorer.Types (CorpusId, ListId)
import Gargantext.Components.GraphExplorer.Types as GET
import Gargantext.Components.RandomText (words)
import Gargantext.Components.Search (HyperdataRowContact(..), SearchQuery(..), SearchType(..))
import Gargantext.Config (defaultFrontends)
import Gargantext.Config.REST (RESTError(..))
import Gargantext.Ends (Frontends, url)
import Gargantext.Hooks.Loader (useLoaderEffect)
import Gargantext.Hooks.Session (useSession)
import Gargantext.Hooks.Sigmax.Types as SigmaxT
import Gargantext.Hooks.UpdateEffect (useUpdateEffect1')
import Gargantext.Routes as Routes
import Gargantext.Sessions (Session, sessionId)
import Gargantext.Utils (nbsp)
import Gargantext.Utils.Reactix as R2
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T

here :: R2.Here
here = R2.here "Gargantext.Components.GraphExplorer.Sidebar.ContactList"

type Props =
  ( metaData :: GET.MetaData
  )

contactListWrapper :: R2.Leaf Props
contactListWrapper = R2.leaf contactListWrapperCpt

contactListWrapperCpt :: R.Component Props
contactListWrapperCpt = here.component "wrapper" cpt where
  cpt { metaData: GET.MetaData metaData
      } _ = do
    -- | States
    -- |
    session <- useSession

    { graph
    , selectedNodeIds
    } <- GraphStore.use

    graph'            <- R2.useLive' graph
    selectedNodeIds'  <- R2.useLive' selectedNodeIds

    query' /\ query <- R2.useBox' Nothing

    -- | Helpers
    -- |
    let
      frontends = defaultFrontends

      nodesMap = SigmaxT.nodesGraphMap graph'

      toSearchQuery ids = SearchQuery
        { expected: SearchContact
        , query: concat $ toQuery <$> Set.toUnfoldable ids
        }

      toQuery id = case Map.lookup id nodesMap of
        Nothing -> []
        Just n -> words n.label

    -- | Hooks
    -- |
    R.useEffect1' selectedNodeIds' $
      T.write_ (selectedNodeIds' # toSearchQuery >>> Just) query

    -- | Render
    -- |
    pure $

      R.fragment
      [
        case (head metaData.corpusId) /\ query' of

          (Just corpusId) /\ (Just q') ->
            contactList
            { frontends
            , query: q'
            , session
            , corpusId
            , listId: metaData.list.listId
            }

          _ /\ _ ->
            B.caveat
            {}
            [
              H.text "You can link an annuaire to retrieve relative contacts about your selection"
            ]

      ]

-------------------------------------------------------------------

type ListProps =
  ( query           :: SearchQuery
  , corpusId        :: CorpusId
  , listId          :: ListId
  , frontends       :: Frontends
  , session         :: Session
  )

contactList :: R2.Leaf ListProps
contactList = R2.leaf contactListCpt

contactListCpt :: R.Component ListProps
contactListCpt = here.component "main" cpt where
  -- | Helpers
  -- |
  errorHandler err = do
    here.warn2 "[pageLayout] RESTError" err
    case err of
      ReadJSONError err' ->
        here.warn2 "[pageLayout] ReadJSONError" $ show err'
      _ -> pure unit
  -- | Component
  -- |
  cpt { frontends
      , query
      , session
      , corpusId: nodeId
      , listId
      } _ = do
    -- | States
    -- |

    path' /\ path
      <- R2.useBox' $ initialPagePath { nodeId, listId, query, session }

    state' /\ state <-
      R2.useBox' Nothing

    rows' /\ rows <-
      R2.useBox' Nothing

    -- | Hooks
    -- |

    useLoaderEffect
      { errorHandler
      , state
      , loader: loadPage
      , path: path'
      }

    -- | Effects
    -- |

    -- (on query change, reload fetched docs)
    useUpdateEffect1' query $
      flip T.write_ path $ initialPagePath { nodeId, listId, query, session }

    -- (on fetch success, extract existing docs)
    useUpdateEffect1' state' case state' of
      Nothing -> T.write_ (Just Seq.empty) rows
      Just r -> case r of
        Contacts { contacts } -> T.write_ (Just contacts) rows
        _                     -> T.write_ (Just Seq.empty) rows

    -- | Render
    -- |
    pure $

arturo's avatar
arturo committed
181
      R2.fromMaybe rows' \results ->
arturo's avatar
arturo committed
182 183 184

        R.fragment
        [
arturo's avatar
arturo committed
185
          R2.when (results == Seq.empty) $
arturo's avatar
arturo committed
186 187 188 189 190 191 192

            B.caveat
            {}
            [
              H.text "No contact found in your corpus for your selected terms"
            ]
        ,
arturo's avatar
arturo committed
193
          R2.when (not $ eq results Seq.empty) $
arturo's avatar
arturo committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

            H.ul
            { className: intercalate " "
                [ "graph-contact-list"
                , "list-group"
                ]
            } $
            Seq.toUnfoldable $ flip Seq.map results \r ->

              item
              { frontends
              , session
              , contactView: (r :: ContactsView)
              }
        ]


---------------------------------------------------------

type ItemProps =
  ( contactView  :: ContactsView
  , frontends    :: Frontends
  , session      :: Session
  )

item :: R2.Leaf ItemProps
item = R2.leaf itemCpt

itemCpt :: R.Component ItemProps
itemCpt = here.component "item" cpt where
  cpt { contactView: ContactsView
          { id
          , annuaireId
          , hyperdata: HyperdataRowContact
              { firstname
              , lastname
              , labs
              }
          }
      , frontends
      , session
      } _ = do
    -- Computed
    let

      -- Creating a href link
      contactUrl id'
        = url frontends $ Routes.ContactPage (sessionId session) annuaireId id'

    -- Render
    pure $

      H.div
      { className: intercalate " "
          [ "graph-contact-list__item"
          , "list-group-item"
          ]
      }
      [
        H.a
        { className: "graph-contact-list__item__title"
        , target: "_blank"
        , href: contactUrl id
        }
        [
          H.text $ firstname
        ,
          H.text $ nbsp 1
        ,
          H.text $ lastname
        ]
      ,
        B.div'
        { className: "graph-contact-list__item__subtitle" }
        labs
      ]