DonationRowActions.tsx
3 years ago
DonationsListTable.tsx
2 years ago
ListTable.module.scss
3 years ago
DonationsListTable.tsx
261 lines
| 1 | import {__} from '@wordpress/i18n'; |
| 2 | import {ListTablePage} from '@givewp/components'; |
| 3 | import {DonationRowActions} from './DonationRowActions'; |
| 4 | import ListTableApi from '@givewp/components/ListTable/api'; |
| 5 | import tableStyles from '@givewp/components/ListTable/ListTablePage/ListTablePage.module.scss'; |
| 6 | import styles from './ListTable.module.scss'; |
| 7 | import {IdBadge} from '@givewp/components/ListTable/TableCell'; |
| 8 | import {BulkActionsConfig, FilterConfig} from '@givewp/components/ListTable/ListTablePage'; |
| 9 | import {Interweave} from 'interweave'; |
| 10 | import BlankSlate from '@givewp/components/ListTable/BlankSlate'; |
| 11 | import ProductRecommendations from '@givewp/components/ListTable/ProductRecommendations'; |
| 12 | import {RecommendedProductData} from '@givewp/promotions/hooks/useRecommendations'; |
| 13 | |
| 14 | declare global { |
| 15 | interface Window { |
| 16 | GiveDonations: { |
| 17 | apiNonce: string; |
| 18 | apiRoot: string; |
| 19 | adminUrl: string; |
| 20 | forms: Array<{value: string; text: string}>; |
| 21 | table: {columns: Array<object>}; |
| 22 | paymentMode: boolean; |
| 23 | manualDonations: boolean; |
| 24 | pluginUrl: string; |
| 25 | dismissedRecommendations: Array<string>; |
| 26 | addonsBulkActions: Array<BulkActionsConfig>; |
| 27 | }; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | const API = new ListTableApi(window.GiveDonations); |
| 32 | |
| 33 | const filters: Array<FilterConfig> = [ |
| 34 | { |
| 35 | name: 'search', |
| 36 | type: 'search', |
| 37 | inlineSize: '14rem', |
| 38 | text: __('Name, Email, or Donation ID', 'give'), |
| 39 | ariaLabel: __('search donations', 'give'), |
| 40 | }, |
| 41 | { |
| 42 | name: 'form', |
| 43 | type: 'formselect', |
| 44 | text: __('Select Form', 'give'), |
| 45 | ariaLabel: __('filter donation forms by status', 'give'), |
| 46 | options: window.GiveDonations.forms, |
| 47 | }, |
| 48 | { |
| 49 | name: 'toggle', |
| 50 | type: 'checkbox', |
| 51 | text: __('Test', 'give'), |
| 52 | ariaLabel: __('View Test Donations', 'give'), |
| 53 | }, |
| 54 | ]; |
| 55 | |
| 56 | const bulkActions: Array<BulkActionsConfig> = [ |
| 57 | { |
| 58 | label: __('Delete', 'give'), |
| 59 | value: 'delete', |
| 60 | type: 'danger', |
| 61 | action: async (selected) => { |
| 62 | const response = await API.fetchWithArgs('/delete', {ids: selected.join(',')}, 'DELETE'); |
| 63 | return response; |
| 64 | }, |
| 65 | confirm: (selected, names) => ( |
| 66 | <> |
| 67 | <p>{__('Really delete the following donations?', 'give')}</p> |
| 68 | <ul role="document" tabIndex={0}> |
| 69 | {selected.map((donationId, index) => ( |
| 70 | <li key={donationId}> |
| 71 | <IdBadge id={donationId} />{' '} |
| 72 | <span> |
| 73 | {__('from ', 'give')} <Interweave content={names[index]} /> |
| 74 | </span> |
| 75 | </li> |
| 76 | ))} |
| 77 | </ul> |
| 78 | </> |
| 79 | ), |
| 80 | }, |
| 81 | ...(() => { |
| 82 | const donationStatuses = { |
| 83 | publish: __('Set To Completed', 'give'), |
| 84 | pending: __('Set To Pending', 'give'), |
| 85 | processing: __('Set To Processing', 'give'), |
| 86 | refunded: __('Set To Refunded', 'give'), |
| 87 | revoked: __('Set To Revoked', 'give'), |
| 88 | failed: __('Set To Failed', 'give'), |
| 89 | cancelled: __('Set To Cancelled', 'give'), |
| 90 | abandoned: __('Set To Abandoned', 'give'), |
| 91 | preapproval: __('Set To Preapproval', 'give'), |
| 92 | }; |
| 93 | |
| 94 | return Object.entries(donationStatuses).map(([value, label]) => { |
| 95 | return { |
| 96 | label, |
| 97 | value, |
| 98 | action: async (selected) => |
| 99 | await API.fetchWithArgs( |
| 100 | '/setStatus', |
| 101 | { |
| 102 | ids: selected.join(','), |
| 103 | status: value, |
| 104 | }, |
| 105 | 'POST' |
| 106 | ), |
| 107 | confirm: (selected, names) => ( |
| 108 | <> |
| 109 | <p>{__('Set status for the following donations?', 'give')}</p> |
| 110 | <ul role="document" tabIndex={0}> |
| 111 | {selected.map((donationId, index) => ( |
| 112 | <li key={donationId}> |
| 113 | <IdBadge id={donationId} /> <span>{__('from', 'give')}</span> |
| 114 | <Interweave content={names[index]} /> |
| 115 | </li> |
| 116 | ))} |
| 117 | </ul> |
| 118 | </> |
| 119 | ), |
| 120 | }; |
| 121 | }); |
| 122 | })(), |
| 123 | { |
| 124 | label: __('Resend Email Receipts', 'give'), |
| 125 | value: 'resendEmailReceipt', |
| 126 | action: async (selected) => await API.fetchWithArgs('/resendEmailReceipt', {ids: selected.join(',')}, 'POST'), |
| 127 | confirm: (selected, names) => ( |
| 128 | <> |
| 129 | <p>{__('Resend Email Receipts for following donations?', 'give')}</p> |
| 130 | <ul role="document" tabIndex={0}> |
| 131 | {selected.map((donationId, index) => ( |
| 132 | <li key={donationId}> |
| 133 | <IdBadge id={donationId} /> <span>{__('from', 'give')}</span> |
| 134 | <Interweave content={names[index]} /> |
| 135 | </li> |
| 136 | ))} |
| 137 | </ul> |
| 138 | </> |
| 139 | ), |
| 140 | }, |
| 141 | ]; |
| 142 | |
| 143 | /** |
| 144 | * Displays a blank slate for the Donations table. |
| 145 | * @since 2.27.0 |
| 146 | */ |
| 147 | const ListTableBlankSlate = ( |
| 148 | <BlankSlate |
| 149 | imagePath={`${window.GiveDonations.pluginUrl}/assets/dist/images/list-table/blank-slate-donations-icon.svg`} |
| 150 | description={__('No donations found', 'give')} |
| 151 | href={'https://docs.givewp.com/donations'} |
| 152 | linkText={__('GiveWP Donations.', 'give')} |
| 153 | /> |
| 154 | ); |
| 155 | |
| 156 | interface DonationTableRecommendations { |
| 157 | recurring: RecommendedProductData; |
| 158 | feeRecovery: RecommendedProductData; |
| 159 | designatedFunds: RecommendedProductData; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @since 2.27.1 |
| 164 | */ |
| 165 | const RecommendationConfig: DonationTableRecommendations = { |
| 166 | recurring: { |
| 167 | enum: 'givewp_donations_recurring_recommendation_dismissed', |
| 168 | documentationPage: 'https://docs.givewp.com/recurring-donations-list', |
| 169 | message: __('Increase your fundraising revenue by over 30% with recurring giving campaigns.', 'give'), |
| 170 | innerHtml: __('Get More Donations', 'give'), |
| 171 | }, |
| 172 | feeRecovery: { |
| 173 | enum: 'givewp_donations_fee_recovery_recommendation_dismissed', |
| 174 | documentationPage: 'https://docs.givewp.com/feerecovery-donations-list', |
| 175 | message: __( |
| 176 | 'Raise more money per donation by providing donors with the option to help cover the credit card processing fees.', |
| 177 | 'give' |
| 178 | ), |
| 179 | innerHtml: __('Get Fee Recovery', 'give'), |
| 180 | }, |
| 181 | designatedFunds: { |
| 182 | enum: 'givewp_donations_designated_funds_recommendation_dismissed', |
| 183 | documentationPage: 'https://docs.givewp.com/funds-donations-list', |
| 184 | message: __( |
| 185 | 'Elevate your fundraising campaigns with unlimited donation fund designations, and tailored fundraising reports.', |
| 186 | 'give' |
| 187 | ), |
| 188 | innerHtml: __('Start creating designated funds', 'give'), |
| 189 | }, |
| 190 | }; |
| 191 | |
| 192 | const rotatingRecommendation = ( |
| 193 | <ProductRecommendations |
| 194 | options={[ |
| 195 | RecommendationConfig.recurring, |
| 196 | RecommendationConfig.feeRecovery, |
| 197 | RecommendationConfig.designatedFunds, |
| 198 | ]} |
| 199 | apiSettings={window.GiveDonations} |
| 200 | /> |
| 201 | ); |
| 202 | |
| 203 | export default function DonationsListTable() { |
| 204 | return ( |
| 205 | <ListTablePage |
| 206 | title={__('Donations', 'give')} |
| 207 | singleName={__('donation', 'give')} |
| 208 | pluralName={__('donations', 'give')} |
| 209 | rowActions={DonationRowActions} |
| 210 | bulkActions={bulkActions} |
| 211 | apiSettings={window.GiveDonations} |
| 212 | filterSettings={filters} |
| 213 | paymentMode={!!window.GiveDonations.paymentMode} |
| 214 | listTableBlankSlate={ListTableBlankSlate} |
| 215 | productRecommendation={rotatingRecommendation} |
| 216 | > |
| 217 | {window.GiveDonations.manualDonations ? ( |
| 218 | <a |
| 219 | className={tableStyles.addFormButton} |
| 220 | href={`${window.GiveDonations.adminUrl}edit.php?post_type=give_forms&page=give-manual-donation`} |
| 221 | > |
| 222 | {__('New Donation', 'give')} |
| 223 | </a> |
| 224 | ) : ( |
| 225 | <a |
| 226 | className={styles.manualDonationsNotice} |
| 227 | href={'https://docs.givewp.com/enterdonation'} |
| 228 | target={'_blank'} |
| 229 | > |
| 230 | <span className={styles.manualDonationsAddOn}>{__('ADD-ON', 'give')}</span> |
| 231 | {__('Enter Donations', 'give')} |
| 232 | <span className={styles.manualDonationsMessage}> |
| 233 | <img |
| 234 | src={`${window.GiveDonations.pluginUrl}/assets/dist/images/admin/triangle-tip.svg`} |
| 235 | alt={'manual donations'} |
| 236 | />{' '} |
| 237 | {__( |
| 238 | 'Need to add in a record for a donation received elsewhere, or reconcile with the payment gateway? Add donation records with the Manual Donations add-on!', |
| 239 | 'give' |
| 240 | )} |
| 241 | </span> |
| 242 | </a> |
| 243 | )} |
| 244 | <a |
| 245 | className={tableStyles.addFormButton} |
| 246 | href={` ${window.GiveDonations.adminUrl}edit.php?post_type=give_forms&page=give-tools&tab=import&importer-type=import_donations`} |
| 247 | > |
| 248 | {__('Import Donations', 'give')} |
| 249 | </a> |
| 250 | <button className={tableStyles.addFormButton} onClick={showLegacyDonations}> |
| 251 | {__('Switch to Legacy View', 'give')} |
| 252 | </button> |
| 253 | </ListTablePage> |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | const showLegacyDonations = async (event) => { |
| 258 | await API.fetchWithArgs('/view', {isLegacy: 1}); |
| 259 | window.location.reload(); |
| 260 | }; |
| 261 |