Table.purs 28.6 KB
Newer Older
Nicolas Pouillard's avatar
Nicolas Pouillard committed
1 2
module Gargantext.Components.Table where

Karen Konou's avatar
Karen Konou committed
3 4
import Gargantext.Prelude

5
import Data.Array as A
6
import Data.Either (Either(..))
7
import Data.Foldable (intercalate)
8
import Data.Maybe (Maybe(..))
9
import Data.Sequence as Seq
10
import Data.Tuple.Nested ((/\))
11
import Effect (Effect)
12
import Effect.Aff (Milliseconds(..), delay, launchAff_)
13
import Effect.Class (liftEffect)
14
import Gargantext.Components.App.Store as AppStore
15
import Gargantext.Components.Bootstrap as B
16
import Gargantext.Components.Bootstrap.Types (ButtonVariant(..), ComponentStatus(..), Variant(..))
arturo's avatar
arturo committed
17
import Gargantext.Components.Corpus.CodeSection (saveCorpus)
18
import Gargantext.Components.FolderView as FV
19
import Gargantext.Components.Forest.Tree.Node.Action.Rename (RenameValue(..), rename)
20
import Gargantext.Components.Nodes.Corpus.Types (CorpusInfo(..), Hyperdata(..), getCorpusInfo, saveCorpusInfo)
21
import Gargantext.Components.Nodes.Lists.Types as NT
22
import Gargantext.Components.Nodes.Types (FTFieldList)
23
import Gargantext.Components.Search (SearchType(..))
24
import Gargantext.Components.Table.Types (ColumnName(..), OrderBy, OrderByDirection(..), Params, Props, TableContainerProps, columnName)
25
import Gargantext.Hooks.FirstEffect (useFirstEffect')
26
import Gargantext.Sessions.Types (Session)
27 28
import Gargantext.Types (NodeID, defaultCacheParams)
import Gargantext.Utils (setter, (?))
29
import Gargantext.Utils.Reactix (effectLink)
Karen Konou's avatar
Karen Konou committed
30 31 32 33
import Gargantext.Utils.Reactix as R2
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
Nicolas Pouillard's avatar
Nicolas Pouillard committed
34

35 36
here :: R2.Here
here = R2.here "Gargantext.Components.Table"
37

38 39
type Page = Int

Nicolas Pouillard's avatar
Nicolas Pouillard committed
40
type State =
41
  { page       :: Page
42 43
  , pageSize   :: PageSizes
  , orderBy    :: OrderBy
44
  , searchType :: SearchType
Nicolas Pouillard's avatar
Nicolas Pouillard committed
45 46
  }

47
paramsState :: Params -> State
48
paramsState {offset, limit, orderBy, searchType} = {pageSize, page, orderBy, searchType}
49 50 51 52
  where
    pageSize = int2PageSizes limit
    page = offset / limit + 1

53
stateParams :: State -> Params
54
stateParams {pageSize, page, orderBy, searchType} = {offset, limit, orderBy, searchType}
55 56 57 58
  where
    limit = pageSizes2Int pageSize
    offset = limit * (page - 1)

59
type TableHeaderLayoutProps = (
60
    cacheState :: T.Box NT.CacheState
61
  , date  :: String
62
  , desc  :: String
63
  , key   :: String
64
  , query :: String
65
  , title :: String
66 67
  , user  :: String
  )
Nicolas Pouillard's avatar
Nicolas Pouillard committed
68

69
type TableHeaderWithRenameLayoutProps = (
70 71 72 73
    cacheState  :: T.Box NT.CacheState
  , session     :: Session
  , hyperdata   :: Hyperdata
  , nodeId      :: NodeID
74
  , name        :: String
75 76 77 78 79 80 81 82 83
  , date        :: String
  , key         :: String
)

type TableHeaderWithRenameBoxedLayoutProps = (
    cacheState  :: T.Box NT.CacheState
  , session     :: Session
  , hyperdata   :: Hyperdata
  , nodeId      :: NodeID
84
  , name        :: String
85 86
  , date        :: String
  , corpusInfoS :: T.Box CorpusInfo
87 88
)

89
initialParams :: Params
90
initialParams = stateParams {page: 1, pageSize: PS10, orderBy: Just (DESC (ColumnName "Date")), searchType: SearchDoc}
91 92
-- TODO: Not sure this is the right place for this

arturo's avatar
arturo committed
93 94
tableHeaderWithRenameLayout :: R2.Leaf TableHeaderWithRenameLayoutProps
tableHeaderWithRenameLayout = R2.leaf tableHeaderWithRenameLayoutCpt
95 96 97 98

tableHeaderWithRenameLayoutCpt :: R.Component TableHeaderWithRenameLayoutProps
tableHeaderWithRenameLayoutCpt = here.component "tableHeaderWithRenameLayoutCpt" cpt
  where
arturo's avatar
arturo committed
99
    cpt { hyperdata: Hyperdata h, nodeId, session, cacheState, name, date } _ = do
100 101 102
      let corpusInfo = getCorpusInfo h.fields
      corpusInfoS <- T.useBox corpusInfo

arturo's avatar
arturo committed
103
      pure $ tableHeaderWithRenameBoxedLayout {hyperdata: Hyperdata h, nodeId, session, cacheState, name, date, corpusInfoS} []
104 105 106 107 108 109 110

tableHeaderWithRenameBoxedLayout :: R2.Component TableHeaderWithRenameBoxedLayoutProps
tableHeaderWithRenameBoxedLayout = R.createElement tableHeaderWithRenameBoxedLayoutCpt

tableHeaderWithRenameBoxedLayoutCpt :: R.Component TableHeaderWithRenameBoxedLayoutProps
tableHeaderWithRenameBoxedLayoutCpt = here.component "tableHeaderWithRenameBoxedLayoutCpt" cpt
  where
111 112 113 114 115 116 117
    cpt p@{ nodeId
          , session
          , cacheState
          , name
          , date
          , corpusInfoS
          } _ = do
118 119
      -- | States
      -- |
120
      cacheState' <- T.useLive T.unequal cacheState
121
      CorpusInfo {title, desc, query, authors} <- T.read corpusInfoS
122

123 124 125 126 127
      { expandTableEdition
      } <- AppStore.use

      expandTableEdition' <- R2.useLive' expandTableEdition

128 129 130 131 132 133 134
      -- | Hooks
      -- |

      topBarPortalKey <- pure $ "portal-topbar::" <> show nodeId

      mTopBarHost <- R.unsafeHooksEffect $ R2.getElementById "portal-topbar"

135 136 137 138 139 140 141 142 143 144 145 146
      -- | Effects
      -- |

      -- transfer local Component change to Local Storage cache
      useFirstEffect' $
        flip T.listen expandTableEdition onExpandTableEditionChange

      -- | Behaviors
      -- |
      let
        onExpandClick _ = T.modify_ (not) expandTableEdition

147 148
      -- | Render
      -- |
149 150 151 152 153
      pure $

        H.div
        { className: "table-header-rename" }
        [
154 155 156
          -- [To Topbar portal]
          -- @NOTE #446: UI flicker artfact when user toggle the CTA
          --             This is due to a re-render + portal input focus --             lost
Alexandre Delanoë's avatar
Alexandre Delanoë committed
157
{-
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
          R2.createPortal' mTopBarHost
          [
            R2.fragmentWithKey topBarPortalKey
            [
              B.button
              { className: "table-header-rename__cache-toolbar"
              , callback: cacheClick cacheState
              , variant: cacheState' == NT.CacheOn ?
                  ButtonVariant Light $
                  OutlinedButtonVariant Light
              }
              [
                H.text $ cacheText cacheState'
              ]
            ]
          ]
        ,
Alexandre Delanoë's avatar
Alexandre Delanoë committed
175
-}
176
          H.div
177
          { className: "table-header-rename__title" }
178
          [
179 180 181
            B.div'
            { className: "table-header-rename__title__text" }
            name
182 183
          ,
            H.hr
184 185 186 187 188 189 190 191 192
            { className: "table-header-rename__title__line" }
          ,
            B.iconButton
            { name: expandTableEdition' ?
                "caret-up" $
                "caret-down"
            , className: "table-header-rename__title__expand"
            , callback: onExpandClick
            }
193
          ]
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        ,
          R2.when expandTableEdition' $

          tableHeaderEditionBlock
          { hyperdata: p.hyperdata
          , nodeId
          , session
          , corpusInfoS
          , defaultData:
            { name
            , title
            , query
            , desc
            , authors
            , date
            }
          }
        ]

    cacheText NT.CacheOn = "Cache On"
    cacheText NT.CacheOff = "Cache Off"

Alexandre Delanoë's avatar
Alexandre Delanoë committed
216
--    cacheClick cacheState _ = T.modify_ cacheStateToggle cacheState
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

    cacheStateToggle NT.CacheOn = NT.CacheOff
    cacheStateToggle NT.CacheOff = NT.CacheOn

onExpandTableEditionChange :: T.Change Boolean -> Effect Unit
onExpandTableEditionChange { new } = do
  cache <- R2.loadLocalStorageState' R2.appParamsKey defaultCacheParams
  let update = setter (_ { expandTableEdition = new }) cache
  R2.setLocalStorageState R2.appParamsKey update

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

type TableHeaderEditionBlockDefaultData =
  ( name    :: String
  , title   :: String
  , desc    :: String
  , query   :: String
  , authors :: String
  , date    :: String
  )

type TableHeaderEditionBlockProps =
  ( defaultData :: Record TableHeaderEditionBlockDefaultData
  , corpusInfoS :: T.Box CorpusInfo
  , session     :: Session
  , hyperdata   :: Hyperdata
  , nodeId      :: NodeID
  )

tableHeaderEditionBlock :: R2.Leaf TableHeaderEditionBlockProps
tableHeaderEditionBlock = R2.leaf tableHeaderEditionBlockCpt

tableHeaderEditionBlockCpt :: R.Component TableHeaderEditionBlockProps
tableHeaderEditionBlockCpt = here.component "tableHeaderEditionBlock" cpt where
  cpt { defaultData
      , corpusInfoS
      , hyperdata: Hyperdata h
      , nodeId
      , session
      } _ = do
    -- | States
    -- |
    name' /\ name
      <- R2.useBox' defaultData.name

    title' /\ title
      <- R2.useBox' defaultData.title

    desc' /\ desc
      <- R2.useBox' defaultData.desc

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

    authors' /\ authors
      <- R2.useBox' defaultData.authors

    date' /\ _
      <- R2.useBox' defaultData.date

    onNamePending' /\ onNamePending
      <- R2.useBox' false

    onTitlePending' /\ onTitlePending
      <- R2.useBox' false

    onDescPending' /\ onDescPending
      <- R2.useBox' false

    onQueryPending' /\ onQueryPending
      <- R2.useBox' false

    onAuthorsPending' /\ onAuthorsPending
      <- R2.useBox' false

    -- | Behaviors
    -- |
    let
      onRenameCorpus newName = do
        saveCorpusName { name: newName
                       , session
                       , nodeId
                       , onPending: onNamePending
                       }

      onRenameTitle newTitle = do
        _ <- T.modify (\(CorpusInfo c) -> CorpusInfo $ c {title = newTitle}) corpusInfoS
        corpusInfo <- T.read corpusInfoS
        let newFields = saveCorpusInfo corpusInfo h.fields
        save { fields: newFields
             , session
             , nodeId
             , onPending: onTitlePending
             }

      onRenameDesc newDesc = do
        _ <- T.modify (\(CorpusInfo c) -> CorpusInfo $ c {desc = newDesc}) corpusInfoS
        corpusInfo <- T.read corpusInfoS
        let newFields = saveCorpusInfo corpusInfo h.fields
        save { fields: newFields
             , session
             , nodeId
             , onPending: onDescPending
             }

      onRenameQuery newQuery = do
        _ <- T.modify (\(CorpusInfo c) -> CorpusInfo $ c {query = newQuery}) corpusInfoS
        corpusInfo <- T.read corpusInfoS
        let newFields = saveCorpusInfo corpusInfo h.fields
        save { fields: newFields
             , session
             , nodeId
             , onPending: onQueryPending
             }

      onRenameAuthors newAuthors = do
        _ <- T.modify (\(CorpusInfo c) -> CorpusInfo $ c {authors = newAuthors}) corpusInfoS
        corpusInfo <- T.read corpusInfoS
        let newFields = saveCorpusInfo corpusInfo h.fields
        save { fields: newFields
             , session
             , nodeId
             , onPending: onAuthorsPending
             }

    -- | Render
    -- |
    pure $

      H.div
      { className: "table-header-rename-edition" }
      [
        B.wad
        [ "d-flex gap-4 " ]
        [
          B.wad
          [ "flex-grow-1" ]
          [
            -- Corpus name
            H.form
            { className: intercalate " "
                [ "form-group"
                , "input-group input-group-sm"
                ]
            }
362 363
            [
              H.div
364 365 366 367 368 369 370 371 372 373 374 375
              { className: "form-group__label" }
              [
                B.icon
                { name: "book" }
              ]
            ,
              H.div
              { className: intercalate " "
                  [ "form-group__field"
                  , "input-group input-group-sm"
                  ]
              }
376
              [
377 378 379
                B.formInput
                { callback: flip T.write_ name
                , value: name'
380 381
                }
              ,
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
                H.div
                { className: "input-group-append" }
                [
                  B.button
                  {
                    variant: ButtonVariant Light
                  , type: "submit"
                  , callback: const $ onRenameCorpus name'
                  , className: "input-group-text"
                  , status: onNamePending' ?
                      Disabled $
                      Enabled
                  }
                  [
                    B.icon
                    { name: "floppy-o"
                    , className: "text-darker"
                    }
                  ]
                ]
              ]
            ]
          ,
            -- Node title
            H.form
            { className: intercalate " "
                [ "form-group"
                , "input-group input-group-sm"
                ]
            }
            [
              H.div
              { className: "form-group__label" }
              [
                B.icon
                { name: "header" }
              ]
            ,
              H.div
              { className: intercalate " "
                  [ "form-group__field"
                  , "input-group input-group-sm"
                  ]
              }
              [
                B.formInput
                { callback: flip T.write_ title
                , value: title'
430 431
                }
              ,
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
                H.div
                { className: "input-group-append" }
                [
                  B.button
                  { variant: ButtonVariant Light
                  , type: "submit"
                  , callback: const $ onRenameTitle title'
                  , className: "input-group-text"
                  , status: onTitlePending' ?
                      Disabled $
                      Enabled
                  }
                  [
                    B.icon
                    { name: "floppy-o"
                    , className: "text-darker"
                    }
                  ]
                ]
              ]
            ]
          ,
            -- Description
            H.form
            { className: intercalate " "
                [ "form-group"
                , "input-group input-group-sm"
                ]
            }
            [
              H.div
              { className: "form-group__label" }
              [
                B.icon
                { name: "info" }
              ]
            ,
              H.div
              { className: intercalate " "
                  [ "form-group__field"
                  , "input-group input-group-sm"
                  ]
              }
              [
                B.formInput
                { callback: flip T.write_ desc
                , value: desc'
479 480
                }
              ,
481 482
                H.div
                { className: "input-group-append" }
483
                [
484 485 486 487 488 489 490 491 492
                  B.button
                  { variant: ButtonVariant Light
                  , type: "submit"
                  , callback: const $ onRenameDesc desc'
                  , className: "input-group-text"
                  , status: onDescPending' ?
                      Disabled $
                      Enabled
                  }
493
                  [
494 495 496
                    B.icon
                    { name: "floppy-o"
                    , className: "text-darker"
497
                    }
498
                  ]
499 500
                ]
              ]
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
            ]
          ]
        ,
          B.wad
          [ "flex-grow-1"]
          [
            -- Search query
            H.form
            { className: intercalate " "
                [ "form-group"
                , "input-group input-group-sm"
                ]
            }
            [
              H.div
              { className: "form-group__label" }
              [
                B.icon
                { name: "search-plus" }
              ]
521
            ,
522 523 524 525 526 527
              H.div
              { className: intercalate " "
                  [ "form-group__field"
                  , "input-group input-group-sm"
                  ]
              }
528
              [
529 530 531
                B.formInput
                { callback: flip T.write_ query
                , value: query'
532 533 534
                }
              ,
                H.div
535
                { className: "input-group-append" }
arturo's avatar
arturo committed
536
                [
537 538 539 540 541 542 543 544
                  B.button
                  { variant: ButtonVariant Light
                  , type: "submit"
                  , callback: const $ onRenameQuery query'
                  , className: "input-group-text"
                  , status: onQueryPending' ?
                      Disabled $
                      Enabled
545
                  }
546 547 548 549 550 551
                  [
                    B.icon
                    { name: "floppy-o"
                    , className: "text-darker"
                    }
                  ]
552 553 554
                ]
              ]
            ]
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
          ,
            -- Authors
            H.form
            { className: intercalate " "
                [ "form-group"
                , "input-group input-group-sm"
                ]
            }
            [
              H.div
              { className: "form-group__label" }
              [
                B.icon
                { name: "user" }
              ]
            ,
              H.div
              { className: intercalate " "
                  [ "form-group__field"
                  , "input-group input-group-sm"
                  ]
              }
              [
                B.formInput
                { callback: flip T.write_ authors
                , value: authors'
                }
              ,
                H.div
                { className: "input-group-append" }
                [
                  B.button
                  { variant: ButtonVariant Light
                  , type: "submit"
                  , callback: const $ onRenameAuthors authors'
                  , className: "input-group-text"
                  , status: onAuthorsPending' ?
                      Disabled $
                      Enabled
                  }
                  [
                    B.icon
                    { name: "floppy-o"
                    , className: "text-darker"
                    }
                  ]
                ]
              ]
            ]
          ,
            -- Date
            H.form
            { className: intercalate " "
                [ "form-group"
                , "input-group input-group-sm"
                ]
            }
            [
              H.div
              { className: "form-group__label" }
              [
                B.icon
                { name: "calendar" }
              ]
            ,
              H.div
              { className: intercalate " "
                  [ "form-group__field"
                  , "input-group input-group-sm"
                  ]
              }
              [
                B.formInput
                { callback: const $ pure unit
                , value: date'
                , status: Idled
                }
              ]
            ]
634
          ]
635 636 637 638 639 640 641 642 643 644 645 646 647 648
        ]
      ]

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

save ::
      { fields    :: FTFieldList
      , session   :: Session
      , nodeId    :: Int
      , onPending :: T.Box Boolean
      }
  ->  Effect Unit
save { fields, session, nodeId, onPending } = do
  T.write_ true onPending
649 650 651
  launchAff_ do
    res <- saveCorpus $ {hyperdata: Hyperdata {fields}, session, nodeId}
    liftEffect $ do
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
      case res of
        Left err -> here.warn2 "[corpusLayoutView] onClickSave RESTError" err
        _ -> pure unit
    -- add a human minimal cognitive delay telling something has being executed
    delay $ Milliseconds 200.0
    liftEffect $ T.write_ false onPending

saveCorpusName ::
      { name      :: String
      , session   :: Session
      , nodeId    :: Int
      , onPending :: T.Box Boolean
      }
  ->  Effect Unit
saveCorpusName { name, session, nodeId, onPending } = do
  T.write_ true onPending
668 669 670
  launchAff_ do
    res <- rename session nodeId $ RenameValue {text: name}
    liftEffect $ do
671 672 673 674 675 676
      case res of
        Left err -> here.warn2 "[corpusLayoutView] onClickSave RESTError" err
        _ -> pure unit
    -- add a human minimal cognitive delay telling something has being executed
    delay $ Milliseconds 200.0
    liftEffect $ T.write_ false onPending
677

678 679
tableHeaderLayout :: R2.Component TableHeaderLayoutProps
tableHeaderLayout = R.createElement tableHeaderLayoutCpt
680
tableHeaderLayoutCpt :: R.Component TableHeaderLayoutProps
681
tableHeaderLayoutCpt = here.component "tableHeaderLayout" cpt
682
  where
683 684 685
    cpt { cacheState, date, desc, query, title, user } _ = do
      cacheState' <- T.useLive T.unequal cacheState

686
      pure $ R.fragment
687
        [ R2.row [FV.backButton {} []]
688 689
        ,
          R2.row
690
          [ H.div {className: "col-md-3"} [ H.h3 {} [H.text title] ]
691 692
          , H.div {className: "col-md-9"}
            [ H.hr {style: {height: "2px", backgroundColor: "black"}} ]
693
          ]
694 695 696 697
          , R2.row
            [ H.div {className: "col-md-8 content"}
              [ H.p {}
                [ H.span {className: "fa fa-globe"} []
698
                , H.text $ " " <> desc
699 700 701
                ]
              , H.p {}
                [ H.span {className: "fa fa-search-plus"} []
702
                , H.text $ " " <> query
703 704 705 706 707 708 709 710 711 712
                ]
              , H.p { className: "cache-toggle"
                    , on: { click: cacheClick cacheState } }
                [ H.span { className: "fa " <> (cacheToggle cacheState') } []
                , H.text $ cacheText cacheState'
                ]
              ]
            , H.div {className: "col-md-4 content"}
              [ H.p {}
                [ H.span {className: "fa fa-user"} []
713
                , H.text $ " " <> user
714
                ]
715 716 717 718
              , H.p {}
                [ H.span {className: "fa fa-calendar"} []
                , H.text $ " " <> date
                ]
719
              ]
720 721
            ]
          ]
722

723 724
    cacheToggle NT.CacheOn = "fa-toggle-on"
    cacheToggle NT.CacheOff = "fa-toggle-off"
725

726 727
    cacheText NT.CacheOn = "Cache On"
    cacheText NT.CacheOff = "Cache Off"
728

729 730
    cacheClick cacheState _ = do
      T.modify cacheStateToggle cacheState
731 732 733

    cacheStateToggle NT.CacheOn = NT.CacheOff
    cacheStateToggle NT.CacheOff = NT.CacheOn
734

735
table :: R2.Leaf Props
736
table = R2.leaf tableCpt
737
tableCpt :: R.Component Props
738
tableCpt = here.component "table" cpt
Nicolas Pouillard's avatar
Nicolas Pouillard committed
739
  where
740 741 742 743 744 745
    cpt { colNames
        , container
        , params
        , rows
        , syncResetButton
        , totalRecords
746 747
        , wrapColElts
        } _ = do
748 749
      params' <- T.useLive T.unequal params

750
      let
751
        state = paramsState params'
752
        ps = pageSizes2Int state.pageSize
753 754 755 756
        totalPages = (totalRecords / ps) + min 1 (totalRecords `mod` ps)
        colHeader :: ColumnName -> R.Element
        colHeader c = H.th {scope: "col"} [ H.b {} cs ]
          where
757
            lnk mc = effectLink $ void $ T.modify (_ { orderBy = mc }) params
758 759
            cs :: Array R.Element
            cs =
760
              wrapColElts c $
761
              case state.orderBy of
762 763
                Just (ASC d)  | c == d -> [lnk (Just (DESC c)) "ASC " , lnk Nothing (columnName c)]
                Just (DESC d) | c == d -> [lnk (Just (ASC  c)) "DESC ", lnk Nothing (columnName c)]
764
                _ -> [lnk (Just (ASC c)) (columnName c)]
765
      pure $ container
766
        { pageSizeControl: sizeDD { params }
767
        , pageSizeDescription: textDescription state.page state.pageSize totalRecords
768
        , paginationLinks: pagination { params, totalPages }
769
        , syncResetButton
770
        , tableBody: map _.row $ A.fromFoldable rows
771
        , tableHead: H.tr {} (colHeader <$> colNames)
772
        }
Nicolas Pouillard's avatar
Nicolas Pouillard committed
773

774 775 776
makeRow :: Array R.Element -> R.Element
makeRow els = H.tr {} $ (\c -> H.td {} [c]) <$> els

777 778
makeRow' :: forall r. Record r -> Array R.Element -> R.Element
makeRow' p els = H.tr p $ (\c -> H.td {} [c]) <$> els
779 780 781 782 783 784

type FilterRowsParams =
  (
    params :: Params
  )

785
filterRows :: forall a. Record FilterRowsParams -> Seq.Seq a -> Seq.Seq a
786
filterRows { params: { limit, offset } } rs = newRs
787
  where
788
    newRs = Seq.take limit $ Seq.drop offset $ rs
789

790
defaultContainer :: Record TableContainerProps -> R.Element
791
defaultContainer props = R.fragment $ props.syncResetButton <> table_data
792
  where
793 794 795
    table_data = [
                    controls
                  , R2.row [
796
                      H.table {className: "table table-hover table-responsive"}
797 798 799
                      [ H.thead {className: ""} [ props.tableHead ]
                      , H.tbody {} props.tableBody
                      ]
800
                    ]
801
                  , controls
802
                 ]
803 804 805 806 807 808 809 810 811 812 813 814
      where
        controls = H.div { className: "table-container__navigation d-flex align-items-center mb-2" }
                    [ H.div {className: "col-md-4"} [ props.pageSizeDescription ]
                    , H.div {className: "col-md-4"} [ props.paginationLinks ]
                    , H.div {className: "col-md-4"} 
                      [ B.wad [ "d-flex", "align-items-center", "justify-content-end" ]
                          [ B.label_ "per page"
                          , B.wad_ [ "virtual-space", "w-1" ]
                          , props.pageSizeControl
                          ]
                      ]
                    ]
Nicolas Pouillard's avatar
Nicolas Pouillard committed
815

816
-- TODO: this needs to be in Gargantext.Pages.Corpus.Graph.Tabs
817 818
graphContainer :: Record TableContainerProps -> R.Element
graphContainer props =
819
  -- TODO title in tabs name (above)
820
  H.table {className: "table table-hover table-responsive"}
821
  [ H.thead {className: ""} [ props.tableHead ]
822 823
  , H.tbody {} props.tableBody
  ]
824 825 826 827
   -- TODO better rendering of the paginationLinks
   -- , props.pageSizeControl
   -- , props.pageSizeDescription
   -- , props.paginationLinks
828

829 830
type SizeDDProps =
  (
831
    params :: T.Box Params
832 833 834 835
  )

sizeDD :: Record SizeDDProps -> R.Element
sizeDD p = R.createElement sizeDDCpt p []
836
sizeDDCpt :: R.Component SizeDDProps
837
sizeDDCpt = here.component "sizeDD" cpt
838
  where
839 840 841 842
    cpt { params } _ = do
      params' <- T.useLive T.unequal params
      let { pageSize } = paramsState params'

843 844 845 846
      pure $ H.span {} [
        R2.select { className, defaultValue: show pageSize, on: {change} } sizes
      ]
      where
847
        className = "form-control form-control-sm custom-select"
848
        change e = do
849
          let ps = string2PageSize $ R.unsafeEventValue e
850 851
          _ <- T.modify (\p -> stateParams $ (paramsState p) { pageSize = ps }) params
          changePage 1 params
852 853 854
        sizes = map option pageSizes
        option size = H.option {value} [H.text value]
          where value = show size
Nicolas Pouillard's avatar
Nicolas Pouillard committed
855

856 857
textDescription :: Int -> PageSizes -> Int -> R.Element
textDescription currPage pageSize totalRecords =
858
  H.div {className: ""} [ H.text msg ] -- TODO or col-md-6 ?
859
  where
860 861 862 863 864
    start = (currPage - 1) * pageSizes2Int pageSize + 1
    end' = currPage * pageSizes2Int pageSize
    end  = if end' > totalRecords then totalRecords else end'
    msg = "Showing " <> show start <> " to " <> show end <> " of " <> show totalRecords

865 866 867 868 869 870 871 872 873
changePage :: Page -> T.Box Params -> Effect Unit
changePage page params =
  void $ T.modify (\p -> stateParams $ (paramsState p) { page = page }) params

type PaginationProps =
  ( params     :: T.Box Params
  , totalPages :: Int )

pagination :: R2.Leaf PaginationProps
874
pagination = R2.leaf paginationCpt
875 876 877 878 879 880 881 882
paginationCpt :: R.Component PaginationProps
paginationCpt = here.component "pagination" cpt
  where
    cpt { params, totalPages } _ = do
      params' <- T.useLive T.unequal params
      let { page } = paramsState params'
          prev = if page == 1 then
                  H.text " Prev. "
Nicolas Pouillard's avatar
Nicolas Pouillard committed
883
                else
884 885 886
                  changePageLink (page - 1) "Prev."
          next = if page == totalPages then
                  H.text " Next "
Nicolas Pouillard's avatar
Nicolas Pouillard committed
887
                else
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
                  changePageLink (page + 1) "Next"
          first = if page == 1 then
                    H.text ""
                  else
                    changePageLink' 1
          last = if page == totalPages then
                  H.text ""
                else
                  changePageLink' totalPages
          ldots = if page >= 5 then
                    H.text " ... "
                    else
                    H.text ""
          rdots = if page + 3 < totalPages then
                    H.text " ... "
                    else
                    H.text ""
          lnums = map changePageLink' $ A.filter (1  < _) [page - 2, page - 1]
          rnums = map changePageLink' $ A.filter (totalPages > _) [page + 1, page + 2]


      pure $ H.span {} $
        [ H.text " ", prev, first, ldots]
        <>
        lnums
        <>
        [H.b {} [H.text $ " " <> show page <> " "]]
        <>
        rnums
        <>
        [ rdots, last, next ]
        where
          changePageLink :: Int -> String -> R.Element
          changePageLink i s =
            H.span {}
              [ H.text " "
              , effectLink (changePage i params) s
              , H.text " "
              ]
927

928 929
          changePageLink' :: Int -> R.Element
          changePageLink' i = changePageLink i (show i)
Nicolas Pouillard's avatar
Nicolas Pouillard committed
930

931
data PageSizes = PS10 | PS20 | PS50 | PS100 | PS200
Nicolas Pouillard's avatar
Nicolas Pouillard committed
932

933
derive instance Eq PageSizes
Nicolas Pouillard's avatar
Nicolas Pouillard committed
934

935
instance Show PageSizes where
Nicolas Pouillard's avatar
Nicolas Pouillard committed
936 937 938 939
  show PS10  = "10"
  show PS20  = "20"
  show PS50  = "50"
  show PS100 = "100"
940
  show PS200 = "200"
Nicolas Pouillard's avatar
Nicolas Pouillard committed
941

942 943 944
int2PageSizes :: Int -> PageSizes
int2PageSizes i = string2PageSize $ show i

Nicolas Pouillard's avatar
Nicolas Pouillard committed
945 946 947 948 949
pageSizes2Int :: PageSizes -> Int
pageSizes2Int PS10  = 10
pageSizes2Int PS20  = 20
pageSizes2Int PS50  = 50
pageSizes2Int PS100 = 100
950
pageSizes2Int PS200 = 200
Nicolas Pouillard's avatar
Nicolas Pouillard committed
951

952 953
pageSizes :: Array PageSizes
pageSizes = [PS10, PS20, PS50, PS100, PS200]
Nicolas Pouillard's avatar
Nicolas Pouillard committed
954 955 956 957 958 959

string2PageSize :: String -> PageSizes
string2PageSize "10" = PS10
string2PageSize "20" = PS20
string2PageSize "50" = PS50
string2PageSize "100" = PS100
960
string2PageSize "200" = PS200
Nicolas Pouillard's avatar
Nicolas Pouillard committed
961
string2PageSize _    = PS10