Texts.purs 2.56 KB
Newer Older
1 2
module Gargantext.Pages.Texts where

3
import Prelude ((<<<))
4 5
import Data.Array (head)
import Data.Maybe (Maybe(..))
James Laver's avatar
James Laver committed
6 7
import DOM.Simple.Console (log2)
import Effect.Class (liftEffect)
8 9 10 11 12 13 14 15
import Effect.Aff (Aff, throwError)
import Effect.Exception (error)
import Reactix as R
--------------------------------------------------------
import Gargantext.Prelude
import Gargantext.Components.Node (NodePoly(..), HyperdataList)
import Gargantext.Components.Table as Table
import Gargantext.Config.REST (get)
16
import Gargantext.Ends (url)
17
import Gargantext.Hooks.Loader (useLoader)
18 19
import Gargantext.Pages.Texts.Tabs (CorpusData, CorpusInfo(..))
import Gargantext.Pages.Texts.Tabs as Tabs
20 21 22
import Gargantext.Routes (SessionRoute(NodeAPI, Children))
import Gargantext.Sessions (Session)
import Gargantext.Types (NodeType(..))
23

24
type Props = ( session :: Session, nodeId :: Int )
25 26 27 28

textsLayout :: Record Props -> R.Element
textsLayout props = R.createElement textsLayoutCpt props []

29
------------------------------------------------------------------------
30 31 32
textsLayoutCpt :: R.Component Props
textsLayoutCpt = R.hooksComponent "TextsLoader" cpt
  where
33 34
    cpt {nodeId,session} _ =
      useLoader nodeId (getCorpus session) $
35 36 37 38
        \corpusData@{corpusId, corpusNode, defaultListId} ->
          let
            NodePoly { name, date, hyperdata: CorpusInfo corpus } = corpusNode
            {desc, query, authors: user} = corpus
39
            tabs = Tabs.tabs {session, corpusId, corpusData}
40 41 42 43 44 45
            title = "Corpus " <> name
            headerProps = { title, desc, query, date, user } in
          R.fragment [Table.tableHeaderLayout headerProps, tabs]

          

46 47
------------------------------------------------------------------------

48 49
getCorpus :: Session -> Int -> Aff CorpusData
getCorpus session textsId = do
James Laver's avatar
James Laver committed
50
  liftEffect $ log2 "nodepolyurl: " nodePolyUrl
51
  -- fetch corpus via texts parentId
52
  (NodePoly {parentId: corpusId} :: NodePoly {}) <- get nodePolyUrl
James Laver's avatar
James Laver committed
53
  liftEffect $ log2 "corpusnodeurl: " $ corpusNodeUrl corpusId
54
  corpusNode     <- get $ corpusNodeUrl corpusId
James Laver's avatar
James Laver committed
55
  liftEffect $ log2 "defaultlistidsurl: " $ defaultListIdsUrl corpusId
56
  defaultListIds <- get $ defaultListIdsUrl corpusId
57 58
  case (head defaultListIds :: Maybe (NodePoly HyperdataList)) of
    Just (NodePoly { id: defaultListId }) ->
59
      pure {corpusId, corpusNode, defaultListId}
60 61
    Nothing ->
      throwError $ error "Missing default list"
62
  where
James Laver's avatar
James Laver committed
63
    nodePolyUrl = url session $ NodeAPI Corpus (Just textsId)
64 65
    corpusNodeUrl = url session <<< NodeAPI Corpus <<< Just
    defaultListIdsUrl = url session <<< Children NodeList 0 1 Nothing <<< Just