index.js
83 lines
| 1 | import {Fragment, useState} from 'react'; |
| 2 | import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; |
| 3 | import {__} from '@wordpress/i18n'; |
| 4 | import Table from '../table'; |
| 5 | import DonationRow from '../donation-row'; |
| 6 | |
| 7 | import './style.scss'; |
| 8 | |
| 9 | const DonationTable = ({donations, perPage}) => { |
| 10 | const [page, setPage] = useState(1); |
| 11 | |
| 12 | const getStartIndex = () => { |
| 13 | return (page - 1) * perPage; |
| 14 | }; |
| 15 | |
| 16 | const getEndIndex = () => { |
| 17 | return start + perPage <= donationsArray.length ? start + perPage : donationsArray.length; |
| 18 | }; |
| 19 | |
| 20 | const getDonationRows = () => { |
| 21 | return donationsArray.reduce((rows, donation, index) => { |
| 22 | if (index >= start && index < end) { |
| 23 | rows.push(<DonationRow key={index} donation={donation} />); |
| 24 | } |
| 25 | return rows; |
| 26 | }, []); |
| 27 | }; |
| 28 | |
| 29 | let donationRows = []; |
| 30 | const donationsArray = []; |
| 31 | let start = 0; |
| 32 | let end = perPage; |
| 33 | let lastPage = 1; |
| 34 | |
| 35 | if (donations) { |
| 36 | Object.entries(donations).forEach((donation) => { |
| 37 | donationsArray[donation[0]] = donation[1]; |
| 38 | }); |
| 39 | start = getStartIndex(); |
| 40 | end = getEndIndex(); |
| 41 | lastPage = Math.ceil(donationsArray.length / perPage) - 1; |
| 42 | donationRows = getDonationRows(); |
| 43 | } |
| 44 | |
| 45 | return ( |
| 46 | <Table |
| 47 | header={ |
| 48 | <Fragment> |
| 49 | <div className="give-donor-dashboard-table__column">{__('Donation', 'give')}</div> |
| 50 | <div className="give-donor-dashboard-table__column">{__('Campaign', 'give')}</div> |
| 51 | <div className="give-donor-dashboard-table__column">{__('Date', 'give')}</div> |
| 52 | <div className="give-donor-dashboard-table__column">{__('Status', 'give')}</div> |
| 53 | </Fragment> |
| 54 | } |
| 55 | rows={<Fragment>{donationRows}</Fragment>} |
| 56 | footer={ |
| 57 | <Fragment> |
| 58 | <div className="give-donor-dashboard-table__footer-text"> |
| 59 | {donations && |
| 60 | `${__('Showing', 'give')} ${start + 1} - ${end} ${__('of', 'give')} ${ |
| 61 | donationsArray.length |
| 62 | } ${__('Donations', 'give')}`} |
| 63 | </div> |
| 64 | <div className="give-donor-dashboard-table__footer-nav"> |
| 65 | {page - 1 >= 1 && ( |
| 66 | <a onClick={() => setPage(page - 1)}> |
| 67 | <FontAwesomeIcon icon="chevron-left" /> |
| 68 | </a> |
| 69 | )} |
| 70 | {page <= lastPage && ( |
| 71 | <a onClick={() => setPage(page + 1)}> |
| 72 | <FontAwesomeIcon icon="chevron-right" /> |
| 73 | </a> |
| 74 | )} |
| 75 | </div> |
| 76 | </Fragment> |
| 77 | } |
| 78 | /> |
| 79 | ); |
| 80 | }; |
| 81 | |
| 82 | export default DonationTable; |
| 83 |