11import React , { Component } from "react" ;
2- import { Button , Form , Image } from "react-bootstrap" ;
2+ import { Button , Form , Image , Pagination } from "react-bootstrap" ;
33import "./DashboardContent.scss" ;
44import userIcon2 from "../../../../assets/images/userIcon2.jpg" ;
55import { Link } from "react-router-dom" ;
@@ -15,9 +15,13 @@ class DashboardContent extends Component {
1515 this . state = {
1616 userId : localStorage . getItem ( "userId" ) ,
1717 proposals : [ ] ,
18- displayItems : [ ] ,
18+ displayItems : [ 0 ] ,
1919 allProposals : [ ] ,
2020 userProposals : [ ] ,
21+
22+ pageCount : 0 ,
23+ pageSize : 4 ,
24+ currentPage : 0 ,
2125 } ;
2226 }
2327
@@ -29,21 +33,25 @@ class DashboardContent extends Component {
2933 componentWillReceiveProps ( nextProps ) {
3034 const { allProposals, userProposals } = nextProps ;
3135
36+ let pageCount = Math . ceil ( allProposals . length / this . state . pageSize ) ;
37+
3238 this . setState ( {
3339 allProposals : allProposals ,
3440 userProposals : userProposals ,
3541 displayItems : allProposals ,
42+ pageCount : pageCount ,
3643 } ) ;
3744 }
3845
3946 handleSearchBarChange = ( evt ) => {
4047 const value = evt . target . value . toLowerCase ( ) ;
41- const proposals = this . state . proposals ;
48+ const proposals = this . props . allProposals ;
4249 let results = [ ] ;
4350
4451 if ( value . length === 0 ) {
4552 this . setState ( {
4653 displayItems : proposals ,
54+ pageCount : this . calculatePageCount ( proposals . length ) ,
4755 } ) ;
4856 } else {
4957 proposals . forEach ( ( item ) => {
@@ -54,20 +62,27 @@ class DashboardContent extends Component {
5462
5563 this . setState ( {
5664 displayItems : results ,
65+ pageCount : this . calculatePageCount ( results . length ) ,
5766 } ) ;
5867 }
5968 } ;
6069
70+ calculatePageCount = ( length ) => {
71+ return Math . ceil ( length / this . state . pageSize ) ;
72+ } ;
73+
6174 handleButtonClick = ( name ) => {
6275 const proposals = this . state . userProposals ;
6376 const allProposals = this . state . allProposals ;
6477
6578 let results = [ ] ;
79+ let pageCount ;
6680
6781 switch ( name ) {
6882 case "All" :
6983 this . setState ( {
7084 displayItems : allProposals ,
85+ pageCount : this . calculatePageCount ( allProposals . length ) ,
7186 } ) ;
7287 break ;
7388 case "Accepted" :
@@ -76,51 +91,59 @@ class DashboardContent extends Component {
7691 results . push ( item ) ;
7792 }
7893 } ) ;
79- this . setState ( {
80- displayItems : results ,
81- } ) ;
94+ this . handleStateChange ( results )
8295 break ;
8396 case "Submitted" :
8497 proposals . forEach ( ( item ) => {
8598 if ( item . proposalStatus === "SUBMITTED" ) {
8699 results . push ( item ) ;
87100 }
88101 } ) ;
89- this . setState ( {
90- displayItems : results ,
91- } ) ;
102+ this . handleStateChange ( results )
92103 break ;
93104 case "Draft" :
94105 proposals . forEach ( ( item ) => {
95106 if ( item . proposalStatus === "DRAFT" ) {
96107 results . push ( item ) ;
97108 }
98109 } ) ;
99- this . setState ( {
100- displayItems : results ,
101- } ) ;
110+ this . handleStateChange ( results )
102111 break ;
103112 case "Rejected" :
104113 proposals . forEach ( ( item ) => {
105114 if ( item . proposalStatus === "REJECTED" ) {
106115 results . push ( item ) ;
107116 }
108117 } ) ;
109- this . setState ( {
110- displayItems : results ,
111- } ) ;
118+ this . handleStateChange ( results )
112119 break ;
113120 }
114121 } ;
115122
123+ handleStateChange = ( results ) => {
124+ this . setState ( {
125+ displayItems : results ,
126+ pageCount : this . calculatePageCount ( results . length ) ,
127+ } ) ;
128+ }
129+
130+ handlePaginationClick = ( e , index ) => {
131+ e . preventDefault ( ) ;
132+
133+ this . setState ( {
134+ currentPage : index ,
135+ } ) ;
136+ } ;
137+
116138 render ( ) {
139+ const { currentPage, pageSize, displayItems } = this . state ;
117140 return (
118141 < div className = "dashboard-content" >
119142 < div className = "searchbar-container" >
120143 < Form >
121144 < Form . Control
122145 as = "input"
123- placeholder = "search "
146+ placeholder = "Search "
124147 className = "searchbar"
125148 onChange = { this . handleSearchBarChange }
126149 />
@@ -178,90 +201,121 @@ class DashboardContent extends Component {
178201
179202 < div className = "proposal-container" >
180203 < div className = "proposals" >
181- { this . state . displayItems . map ( ( proposalItem , index ) => {
182- return (
183- < div className = "single-proposal" key = { index } >
184- < div className = "user-info" >
185- < div className = "image" >
186- < Image src = { userIcon2 } alt = "icon" rounded / >
187- </ div >
188- < div className = "img-desc" >
189- < h2 > { proposalItem . title } </ h2 >
190- < p className = "proposal-date" > { proposalItem . createdAt } </ p >
191- </ div >
192- { proposalItem . proposalStatus === "DRAFT" ? (
193- < Button
194- variant = "primary"
195- className = "status-btn"
196- size = "sm"
197- >
198- < span className = "btn-content" >
199- { proposalItem . proposalStatus }
200- </ span >
201- </ Button >
202- ) : (
203- < Button
204- variant = "primary"
205- className = "status-btn-submitted"
206- size = "sm"
207- >
208- < span className = "btn-content" >
209- { proposalItem . proposalStatus }
210- </ span >
211- </ Button >
212- ) }
213- </ div >
214- < div className = "proposal-content" >
215- < div className = "proposal-details" >
216- { proposalItem . proposalDescription }
204+ { displayItems
205+ . slice ( currentPage * pageSize , ( currentPage + 1 ) * pageSize )
206+ . map ( ( proposalItem , index ) => {
207+ return (
208+ < div className = "single-proposal" key = { index } >
209+ < div className = "user-info" >
210+ < div className = "image" >
211+ < Image src = { userIcon2 } alt = "icon" rounded / >
212+ </ div >
213+ < div className = "img-desc" >
214+ < h2 > { proposalItem . title } </ h2 >
215+ < p className = "proposal-date" >
216+ { proposalItem . createdAt }
217+ </ p >
218+ </ div >
219+ { proposalItem . proposalStatus === "DRAFT" ? (
220+ < Button
221+ variant = "primary"
222+ className = "status-btn"
223+ size = "sm"
224+ >
225+ < span className = "btn-content" >
226+ { proposalItem . proposalStatus }
227+ </ span >
228+ </ Button >
229+ ) : (
230+ < Button
231+ variant = "primary"
232+ className = "status-btn-submitted"
233+ size = "sm"
234+ >
235+ < span className = "btn-content" >
236+ { proposalItem . proposalStatus }
237+ </ span >
238+ </ Button >
239+ ) }
217240 </ div >
218- < div className = "proposal-options" >
219- { proposalItem . creator === this . state . userId ? (
241+ < div className = "proposal-content" >
242+ < div className = "proposal-details" >
243+ { proposalItem . proposalDescription }
244+ </ div >
245+ < div className = "proposal-options" >
246+ { proposalItem . creator === this . state . userId ? (
247+ < Link
248+ to = { {
249+ pathname : "/proposaleditor" ,
250+ state : {
251+ proposalId : proposalItem . _id ,
252+ } ,
253+ } }
254+ >
255+ < Button
256+ variant = "primary"
257+ className = "option-btn"
258+ size = "sm"
259+ >
260+ < span className = "option-text" > Edit</ span >
261+ </ Button >
262+ </ Link >
263+ ) : null }
220264 < Link
221265 to = { {
222- pathname : "/proposaleditor " ,
266+ pathname : "/proposaldiscussion " ,
223267 state : {
224268 proposalId : proposalItem . _id ,
269+ isAdmin : false ,
270+ userId : this . state . userId ,
271+ isCreator :
272+ proposalItem . creator === this . state . userId ,
225273 } ,
226274 } }
227275 >
228276 < Button
277+ active
229278 variant = "primary"
230279 className = "option-btn"
231280 size = "sm"
232281 >
233- < span className = "option-text" > Edit </ span >
282+ < span className = "option-text" > View </ span >
234283 </ Button >
235284 </ Link >
236- ) : null }
237- < Link
238- to = { {
239- pathname : "/proposaldiscussion" ,
240- state : {
241- proposalId : proposalItem . _id ,
242- isAdmin : false ,
243- userId : this . state . userId ,
244- isCreator :
245- proposalItem . creator === this . state . userId ,
246- } ,
247- } }
248- >
249- < Button
250- active
251- variant = "primary"
252- className = "option-btn"
253- size = "sm"
254- >
255- < span className = "option-text" > View</ span >
256- </ Button >
257- </ Link >
285+ </ div >
258286 </ div >
259287 </ div >
260- </ div >
288+ ) ;
289+ } ) }
290+ </ div >
291+ </ div >
292+ { this . state . pageCount !== 0 ? (
293+ < div className = "pagination-container" >
294+ < Pagination . Prev
295+ disabled = { currentPage <= 0 }
296+ onClick = { ( e ) => this . handlePaginationClick ( e , currentPage - 1 ) }
297+ className = "pagination-item"
298+ />
299+
300+ { [ ...Array ( this . state . pageCount ) ] . map ( ( page , index ) => {
301+ return (
302+ < Pagination . Item
303+ className = "pagination-item"
304+ active = { index === currentPage }
305+ onClick = { ( e ) => this . handlePaginationClick ( e , index ) }
306+ >
307+ { index + 1 }
308+ </ Pagination . Item >
261309 ) ;
262310 } ) }
311+
312+ < Pagination . Next
313+ disabled = { currentPage >= this . state . pageCount - 1 }
314+ onClick = { ( e ) => this . handlePaginationClick ( e , currentPage + 1 ) }
315+ className = "pagination-item"
316+ />
263317 </ div >
264- </ div >
318+ ) : null }
265319 </ div >
266320 ) ;
267321 }
0 commit comments