@@ -6,6 +6,7 @@ import { gql } from 'apollo-boost';
66import { slugify } from 'utilities/urlHelpers' ;
77import { Link } from 'react-router-dom' ;
88import Truncate from 'react-truncate' ;
9+ import ReactPaginate from 'react-paginate'
910let moment = require ( 'moment' ) ;
1011
1112export interface ArticleListProps {
@@ -36,8 +37,39 @@ export const ArticleList: React.FC<ArticleListProps> = ({ env }) => {
3637 }
3738 } , [ ! loading && data ] ) ;
3839
40+ const [ articlesNumber , setPageNumber ] = useState ( 0 ) ;
41+ const articlesPerPage = 10 ;
42+ const articlesVisited = articlesNumber * articlesPerPage ;
43+ const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
44+
45+ const displayArticles = articleList
46+ . slice ( articlesVisited , articlesVisited + articlesPerPage ) ;
47+
48+ const pageCount = Math . ceil ( articleList . length / articlesPerPage ) ;
49+
50+ const newsFocus = ( ) => {
51+ let content = document . querySelector ( '#newsList' ) ;
52+ if ( ! content ) return ;
53+
54+ const focusable = content . querySelectorAll < HTMLElement > (
55+ 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
56+ ) ;
57+
58+ const first = focusable [ 0 ] ;
59+
60+ if ( first ) {
61+ first . focus ( ) ;
62+ }
63+ } ;
64+
65+ const changePage = ( { selected } : { selected : any } ) => {
66+ let currentPage = selected + 1 ;
67+ setPageNumber ( selected ) ;
68+ setCurrentPage ( selected + 1 ) ;
69+ } ;
70+
3971 return (
40- < div className = "news-list" >
72+ < div className = "news-list" id = 'newsList' >
4173 < ul >
4274 { loading && (
4375 < span className = "text-5 loading" > { i18n . t ( 'common|loading' ) } </ span >
@@ -48,7 +80,7 @@ export const ArticleList: React.FC<ArticleListProps> = ({ env }) => {
4880 </ span >
4981 ) }
5082 { ! loading &&
51- articleList . map ( ( n , index ) => {
83+ displayArticles . map ( ( n , index ) => {
5284 return (
5385 < li key = { index } >
5486 < span className = "text-6" >
@@ -57,20 +89,41 @@ export const ArticleList: React.FC<ArticleListProps> = ({ env }) => {
5789 < Link
5890 className = "text-4"
5991 onClick = { ( ) => console . log ( 'hello' ) }
60- to = { `/${ i18n . languages [ 0 ] } /${ i18n . t ( 'routes|news|path' ) } /${
61- n . id
62- } /${ slugify ( n . heading ) } `}
92+ to = { `/${ i18n . languages [ 0 ] } /${ i18n . t ( 'routes|news|path' ) } /${ n . id
93+ } /${ slugify ( n . heading ) } `}
6394 >
6495 { n . heading }
6596 </ Link >
6697
67- < p className = "text-5 " >
98+ < p className = "text-6 " >
6899 < Truncate lines = { 2 } > { n . preambleHTML } </ Truncate >
69100 </ p >
70101 </ li >
71102 ) ;
72103 } ) }
73104 </ ul >
105+ { /* Show pagination only when there is more than one page */ }
106+ { ( pageCount > 1 ) && (
107+ < div className = 'currentpage-tracker' >
108+ < span className = 'text-6' >
109+ { i18n . t ( 'pages|search|page' ) } { currentPage } { i18n . t ( 'common|of' ) } { pageCount }
110+ </ span >
111+ </ div >
112+ ) }
113+ { ( pageCount > 1 ) && (
114+ < ReactPaginate
115+ previousLabel = "Föregående"
116+ nextLabel = "Nästa"
117+ onClick = { newsFocus }
118+ pageCount = { pageCount }
119+ onPageChange = { changePage }
120+ containerClassName = "pagination-wrapper"
121+ previousLinkClassName = "pagination-prev_btn default-button"
122+ previousAriaLabel = "Föregående sida"
123+ nextLinkClassName = "pagination-nex_btn default-button"
124+ nextAriaLabel = "Nästa sida"
125+ />
126+ ) }
74127 </ div >
75128 ) ;
76129} ;
0 commit comments