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