give
/
src
/
DonorDashboards
/
resources
/
js
/
app
/
components
/
subscription-manager
Last commit date
amount-control
4 years ago
payment-method-control
3 years ago
utils
3 years ago
index.js
4 years ago
style.scss
4 years ago
index.js
123 lines
| 1 | import {Fragment, useMemo, useRef, useState} from 'react'; |
| 2 | import FieldRow from '../field-row'; |
| 3 | import Button from '../button'; |
| 4 | import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; |
| 5 | |
| 6 | import {__} from '@wordpress/i18n'; |
| 7 | |
| 8 | import AmountControl from './amount-control'; |
| 9 | import PaymentMethodControl from './payment-method-control'; |
| 10 | |
| 11 | import {updateSubscriptionWithAPI} from './utils'; |
| 12 | |
| 13 | import './style.scss'; |
| 14 | |
| 15 | /** |
| 16 | * Normalize an amount |
| 17 | * |
| 18 | * @param {string} float |
| 19 | * @param {number} decimals |
| 20 | * @return {string|NaN} |
| 21 | */ |
| 22 | const normalizeAmount = (float, decimals) => Number.parseFloat(float).toFixed(decimals); |
| 23 | |
| 24 | // There is no error handling whatsoever, that will be necessary. |
| 25 | const SubscriptionManager = ({id, subscription}) => { |
| 26 | const gatewayRef = useRef(); |
| 27 | |
| 28 | const [amount, setAmount] = useState(() => |
| 29 | normalizeAmount(subscription.payment.amount.raw, subscription.payment.currency.numberDecimals) |
| 30 | ); |
| 31 | const [isUpdating, setIsUpdating] = useState(false); |
| 32 | const [updated, setUpdated] = useState(false); |
| 33 | |
| 34 | // Prepare data for amount control |
| 35 | const {max, min, options} = useMemo(() => { |
| 36 | const {numberDecimals} = subscription.payment.currency; |
| 37 | const {custom_amount} = subscription.form; |
| 38 | |
| 39 | const options = subscription.form.amounts.map((amount) => ({ |
| 40 | value: normalizeAmount(amount.raw, numberDecimals), |
| 41 | label: amount.formatted, |
| 42 | })); |
| 43 | |
| 44 | if (custom_amount) { |
| 45 | options.push({ |
| 46 | value: 'custom_amount', |
| 47 | label: __('Custom Amount', 'give'), |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | return { |
| 52 | max: normalizeAmount(custom_amount?.maximum, numberDecimals), |
| 53 | min: normalizeAmount(custom_amount?.minimum, numberDecimals), |
| 54 | options, |
| 55 | }; |
| 56 | }, [subscription]); |
| 57 | |
| 58 | const handleUpdate = async () => { |
| 59 | if (isUpdating) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | setIsUpdating(true); |
| 64 | |
| 65 | const paymentMethod = gatewayRef.current ? |
| 66 | await gatewayRef.current.getPaymentMethod() : |
| 67 | {}; |
| 68 | |
| 69 | if ('error' in paymentMethod) { |
| 70 | setIsUpdating(false); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | await updateSubscriptionWithAPI({ |
| 75 | id, |
| 76 | amount, |
| 77 | paymentMethod, |
| 78 | }); |
| 79 | |
| 80 | setUpdated(true); |
| 81 | setIsUpdating(false); |
| 82 | }; |
| 83 | |
| 84 | return ( |
| 85 | <Fragment> |
| 86 | <AmountControl |
| 87 | currency={subscription.payment.currency} |
| 88 | options={options} |
| 89 | max={max} |
| 90 | min={min} |
| 91 | value={amount} |
| 92 | onChange={setAmount} |
| 93 | /> |
| 94 | <PaymentMethodControl |
| 95 | forwardedRef={gatewayRef} |
| 96 | label={__('Payment Method', 'give')} |
| 97 | gateway={subscription.gateway} |
| 98 | /> |
| 99 | <FieldRow> |
| 100 | <div> |
| 101 | <Button onClick={handleUpdate}> |
| 102 | {updated ? ( |
| 103 | <Fragment> |
| 104 | {__('Updated', 'give')} <FontAwesomeIcon icon="check" fixedWidth/> |
| 105 | </Fragment> |
| 106 | ) : ( |
| 107 | <Fragment> |
| 108 | {__('Update Subscription', 'give')}{' '} |
| 109 | <FontAwesomeIcon |
| 110 | className={isUpdating ? 'give-donor-dashboard__subscription-manager-spinner' : ''} |
| 111 | icon={isUpdating ? 'spinner' : 'save'} |
| 112 | fixedWidth |
| 113 | /> |
| 114 | </Fragment> |
| 115 | )} |
| 116 | </Button> |
| 117 | </div> |
| 118 | </FieldRow> |
| 119 | </Fragment> |
| 120 | ); |
| 121 | }; |
| 122 | export default SubscriptionManager; |
| 123 |