11import React , { Component } from 'react' ;
22import { connect } from 'react-redux' ;
3+ import { withRouter } from 'react-router' ;
34import PageHeader from 'react-bootstrap/lib/PageHeader' ;
5+ import Resources from '../../../constants/Resources' ;
46import todoAPI from '../../../api/todo' ;
57import { pushErrors } from '../../../actions/errorActions' ;
68import {
79 setTodo ,
810 addTodo ,
911 removeTodo ,
1012} from '../../../actions/todoActions' ;
13+ import { setCrrentPage , setPage } from '../../../actions/pageActions' ;
1114import PageLayout from '../../layouts/PageLayout' ;
15+ import Pagination from '../../utils/BsPagination' ;
1216
1317class TodoItem extends Component {
1418 constructor ( ) {
@@ -84,25 +88,29 @@ class ListPage extends Component {
8488 }
8589
8690 componentDidMount ( ) {
87- let { todos } = this . props ;
88-
89- if ( todos . length === 0 ) {
90- this . fetchTodos ( ) ;
91- }
91+ let { dispatch, location } = this . props ;
92+ dispatch ( setCrrentPage ( Resources . TODO , location . query . page || 1 ) ) ;
9293 }
9394
94- fetchTodos ( ) {
95- let { dispatch, apiEngine } = this . props ;
96-
97- todoAPI ( apiEngine )
98- . list ( )
99- . catch ( ( err ) => {
100- dispatch ( pushErrors ( err ) ) ;
101- throw err ;
102- } )
103- . then ( ( json ) => {
104- dispatch ( setTodo ( json . todos ) ) ;
105- } ) ;
95+ componentDidUpdate ( prevProps ) {
96+ let { dispatch, apiEngine, page, router, location } = this . props ;
97+
98+ if ( prevProps . page . current !== page . current ) {
99+ todoAPI ( apiEngine )
100+ . list ( { page : page . current } )
101+ . catch ( ( err ) => {
102+ dispatch ( pushErrors ( err ) ) ;
103+ throw err ;
104+ } )
105+ . then ( ( json ) => {
106+ dispatch ( setTodo ( json . todos ) ) ;
107+ dispatch ( setPage ( Resources . TODO , json . page ) ) ;
108+ router . push ( {
109+ pathname : location . pathname ,
110+ query : { page : json . page . current } ,
111+ } ) ;
112+ } ) ;
113+ }
106114 }
107115
108116 _handleAddClick ( ) {
@@ -148,9 +156,11 @@ class ListPage extends Component {
148156 }
149157
150158 render ( ) {
159+ let { page } = this . props ;
160+
151161 return (
152162 < PageLayout >
153- < PageHeader > Todo List</ PageHeader >
163+ < PageHeader > Todo List ( { ` ${ page . current } / ${ page . total } ` } ) </ PageHeader >
154164 < input type = "text" ref = "todotext" />
155165 < button onClick = { this . handleAddClick } > Add Todo</ button >
156166 < ul >
@@ -161,12 +171,14 @@ class ListPage extends Component {
161171 onSaveClick = { this . handleSaveClick . bind ( this , todo . _id ) }
162172 text = { todo . text } /> ) }
163173 </ ul >
174+ < Pagination resourceName = { Resources . TODO } />
164175 </ PageLayout >
165176 ) ;
166177 }
167178} ;
168179
169- export default connect ( state => ( {
180+ export default withRouter ( connect ( state => ( {
170181 apiEngine : state . apiEngine ,
171182 todos : state . todos ,
172- } ) ) ( ListPage ) ;
183+ page : state . pages [ Resources . TODO ] || { } ,
184+ } ) ) ( ListPage ) ) ;
0 commit comments