SubscriptionsListTable.tsx
138 lines
| 1 | import {__, sprintf} from '@wordpress/i18n'; |
| 2 | import {ListTablePage} from '@givewp/components'; |
| 3 | import ListTableApi from '@givewp/components/ListTable/api'; |
| 4 | import tableStyles from '@givewp/components/ListTable/ListTablePage/ListTablePage.module.scss'; |
| 5 | import {BulkActionsConfig, FilterConfig} from '@givewp/components/ListTable/ListTablePage'; |
| 6 | import {SubscriptionsRowActions} from './SubscriptionsRowActions'; |
| 7 | import {IdBadge} from '@givewp/components/ListTable/TableCell'; |
| 8 | import {Interweave} from 'interweave'; |
| 9 | |
| 10 | declare global { |
| 11 | interface Window { |
| 12 | GiveSubscriptions: { |
| 13 | apiNonce: string; |
| 14 | apiRoot: string; |
| 15 | table: {columns: Array<object>}; |
| 16 | forms: Array<{value: string; text: string}>; |
| 17 | paymentMode: boolean; |
| 18 | }; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | const API = new ListTableApi(window.GiveSubscriptions); |
| 23 | |
| 24 | const filters: Array<FilterConfig> = [ |
| 25 | { |
| 26 | name: 'search', |
| 27 | type: 'search', |
| 28 | inlineSize: '14rem', |
| 29 | text: __('Name, Email, or ID', 'give'), |
| 30 | ariaLabel: __('search donations', 'give'), |
| 31 | }, |
| 32 | { |
| 33 | name: 'form', |
| 34 | type: 'formselect', |
| 35 | text: __('Select Form', 'give'), |
| 36 | ariaLabel: __('filter donation forms by status', 'give'), |
| 37 | options: window.GiveSubscriptions.forms, |
| 38 | }, |
| 39 | { |
| 40 | name: 'toggle', |
| 41 | type: 'checkbox', |
| 42 | text: __('Test', 'give'), |
| 43 | ariaLabel: __('View Test Subscriptions', 'give'), |
| 44 | }, |
| 45 | ]; |
| 46 | |
| 47 | const bulkActions: Array<BulkActionsConfig> = [ |
| 48 | { |
| 49 | label: __('Delete', 'give'), |
| 50 | value: 'delete', |
| 51 | type: 'danger', |
| 52 | action: async (selected) => { |
| 53 | const response = await API.fetchWithArgs('/delete', {ids: selected.join(',')}, 'DELETE'); |
| 54 | return response; |
| 55 | }, |
| 56 | confirm: (selected, names) => ( |
| 57 | <> |
| 58 | <p>{__('Really delete the following subscriptions?', 'give')}</p> |
| 59 | <ul role="document" tabIndex={0}> |
| 60 | {selected.map((donationId, index) => ( |
| 61 | <li key={donationId}> |
| 62 | <IdBadge id={donationId} />{' '} |
| 63 | <span> |
| 64 | {__('from ', 'give')} <Interweave content={names[index]} /> |
| 65 | </span> |
| 66 | </li> |
| 67 | ))} |
| 68 | </ul> |
| 69 | </> |
| 70 | ), |
| 71 | }, |
| 72 | ...(() => { |
| 73 | const subscriptionStatuses = { |
| 74 | active: __('Set To Active', 'give'), |
| 75 | expired: __('Set To Expired', 'give'), |
| 76 | completed: __('Set To Completed', 'give'), |
| 77 | cancelled: __('Set To Cancelled', 'give'), |
| 78 | pending: __('Set To Pending', 'give'), |
| 79 | failing: __('Set To Failing', 'give'), |
| 80 | suspended: __('Set To Suspended', 'give'), |
| 81 | abandoned: __('Set To Abandoned', 'give'), |
| 82 | }; |
| 83 | |
| 84 | return Object.entries(subscriptionStatuses).map(([value, label]) => { |
| 85 | return { |
| 86 | label, |
| 87 | value, |
| 88 | action: async (selected) => |
| 89 | await API.fetchWithArgs( |
| 90 | '/setStatus', |
| 91 | { |
| 92 | ids: selected.join(','), |
| 93 | status: value, |
| 94 | }, |
| 95 | 'POST' |
| 96 | ), |
| 97 | confirm: (selected, names) => ( |
| 98 | <> |
| 99 | <p>{__('Set status for the following donations?', 'give')}</p> |
| 100 | <ul role="document" tabIndex={0}> |
| 101 | {selected.map((donationId, index) => ( |
| 102 | <li key={donationId}> |
| 103 | <IdBadge id={donationId} /> <span>{__('from', 'give')}</span> |
| 104 | <Interweave content={names[index]} /> |
| 105 | </li> |
| 106 | ))} |
| 107 | </ul> |
| 108 | </> |
| 109 | ), |
| 110 | }; |
| 111 | }); |
| 112 | })(), |
| 113 | ]; |
| 114 | |
| 115 | export default function SubscriptionsListTable() { |
| 116 | return ( |
| 117 | <ListTablePage |
| 118 | title={__('Subscriptions', 'give')} |
| 119 | singleName={__('subscription', 'give')} |
| 120 | pluralName={__('subscriptions', 'give')} |
| 121 | rowActions={SubscriptionsRowActions} |
| 122 | bulkActions={bulkActions} |
| 123 | apiSettings={window.GiveSubscriptions} |
| 124 | filterSettings={filters} |
| 125 | paymentMode={!!window.GiveSubscriptions.paymentMode} |
| 126 | > |
| 127 | <button className={tableStyles.addFormButton} onClick={showLegacyDonations}> |
| 128 | {__('Switch to Legacy View')} |
| 129 | </button> |
| 130 | </ListTablePage> |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | const showLegacyDonations = async (event) => { |
| 135 | await API.fetchWithArgs('/view', {isLegacy: 1}); |
| 136 | window.location.reload(); |
| 137 | }; |
| 138 |