Lists.purs 2.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
module Gargantext.Pages.Lists where

import Data.Array (head)
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff, throwError)
import Effect.Exception (error)
import Reactix as R
import Thermite (Spec)
--------------------------------------------------------
import Gargantext.Prelude
import Gargantext.Components.Node (NodePoly(..), HyperdataList)
import Gargantext.Components.Loader2 (useLoader)
import Gargantext.Components.Table as Table
import Gargantext.Config      (toUrl, Path(..), NodeType(..), End(..))
import Gargantext.Config.REST (get)
import Gargantext.Pages.Lists.Tabs.Types (CorpusData, CorpusInfo(..))
import Gargantext.Pages.Lists.Tabs.Specs (elt) as Tabs
import Gargantext.Utils.Reactix as R2

------------------------------------------------------------------------
layout :: Spec {} {nodeId :: Int} Void
layout =
  R2.elSpec $ R.hooksComponent "ListsLoader" \{nodeId} _ ->
24 25 26
    useLoader nodeId getCorpus $ \{loaded: corpusData} ->
      let {corpusId
          ,corpusNode:
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
            NodePoly { name: title
                     , date: date'
                     , hyperdata: CorpusInfo corpus
                     }
          } = corpusData in
      R2.toElement $
        Table.renderTableHeaderLayout
          { title: "Corpus " <> title
          , desc:  corpus.desc
          , query: corpus.query
          , date:  date'
          , user:  corpus.authors
          }
        <> [Tabs.elt {corpusId, corpusData}]
------------------------------------------------------------------------

getCorpus :: Int -> Aff CorpusData
44 45 46
getCorpus listId = do
  -- fetch corpus via lists parentId
  (NodePoly {parentId: corpusId} :: NodePoly {})       <- get $ toUrl Back Corpus $ Just listId
47 48 49 50
  corpusNode     <- get $ toUrl Back Corpus $ Just corpusId
  defaultListIds <- get $ toUrl Back (Children NodeList 0 1 Nothing) $ Just corpusId
  case (head defaultListIds :: Maybe (NodePoly HyperdataList)) of
    Just (NodePoly { id: defaultListId }) ->
51
      pure {corpusId, corpusNode, defaultListId}
52 53
    Nothing ->
      throwError $ error "Missing default list"