Skip to content

Commit 8f8a73a

Browse files
feat: donate button show amount (#557)
* add amount to button * update jest snapshots * adjust Cypress tests * add feature to no money buy forms * more snaps * add better Cypress tests * remove Button as input, make button be button * adjust cypress tests from input to button * snaps * create useClickOutside hook to reset value inside button * create useClickOutside hook to fire default moneyBuy value * create functional deal with button text * check tests and clean up * clean up * lint * move function outside useEffect * bring function back inside useEffect * change button colours of examples as too hard to read * move function outside useEffect with useCallback * add value to amountDonate * add buttonRef check to click outside function * snaps --------- Co-authored-by: Andy E Phipps <a.phipps@comicrelief.com>
1 parent def92e2 commit 8f8a73a

4 files changed

Lines changed: 145 additions & 49 deletions

File tree

cypress/integration/components/Organisms/Donate.spec.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ describe('Donate component', () => {
7676
.find('p.error--amount')
7777
.contains('Please enter an amount between £1 and £25000 and up to 2 decimal places');
7878
cy.get('@container')
79-
.find('form > fieldset > input')
80-
.contains('Donate monthly');
79+
.find('[id="mship-1--MoneyBuy-userInput"]')
80+
.clear()
81+
.type('16');
82+
cy.get('@container')
83+
.find('form > fieldset > button')
84+
.contains('Donate £16 monthly');
8185
cy.get('@container')
8286
.find('h2')
8387
.should('exist');
@@ -138,8 +142,12 @@ describe('Donate component', () => {
138142
.find('p.error--amount')
139143
.should('contain', 'Please enter an amount between £1 and £25000 and up to 2 decimal places');
140144
cy.get('@container')
141-
.find('form > fieldset > input')
142-
.contains('Donate now');
145+
.find('[id="mship-2--MoneyBuy-userInput"]')
146+
.clear()
147+
.type('179');
148+
cy.get('@container')
149+
.find('form > fieldset > button')
150+
.contains('Donate £179 now');
143151
cy.get('@container')
144152
.find('h2')
145153
.should('exist');
@@ -199,8 +207,12 @@ describe('Donate component', () => {
199207
.find('p.error--amount')
200208
.contains('Please enter an amount between £1 and £25000 and up to 2 decimal places');
201209
cy.get('@container')
202-
.find('form > fieldset > input')
203-
.contains('Donate now');
210+
.find('[id="mship-3--MoneyBuy-userInput"]')
211+
.clear()
212+
.type('898');
213+
cy.get('@container')
214+
.find('form > fieldset > button')
215+
.contains('Donate £898 now');
204216
cy.get('@container')
205217
.find('h2')
206218
.should('exist');
@@ -258,7 +270,7 @@ describe('Donate component', () => {
258270
.type('5')
259271
.should('not.have', 'Please enter an amount between £1 and £250000 and up to 2 decimal places');
260272
cy.get('@container')
261-
.find('form > fieldset > input')
273+
.find('form > fieldset > button')
262274
.contains('Donate');
263275
cy.get('@container')
264276
.find('h2')

src/components/Organisms/Donate/Donate.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
1111
alt="Background image"
1212
mobileBackgroundColor="deep_violet_dark"
1313
desktopOverlayColor="red"
14-
submitButtonColor="blue_dark"
14+
submitButtonColor="blue_dark"
1515
formAlignRight={true}
1616
imageLow={desktopPictures.imageLow}
1717
images={desktopPictures.images}
@@ -35,6 +35,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
3535

3636
<Donate
3737
mobileBackgroundColor="blue_dark"
38+
submitButtonColor="blue_dark"
3839
desktopOverlayColor="blue_dark"
3940
formAlignRight={false}
4041
imageLow={desktopPictures.imageLow}
@@ -61,6 +62,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
6162

6263
<Donate
6364
mobileBackgroundColor="blue_dark"
65+
submitButtonColor="blue_dark"
6466
desktopOverlayColor="blue_dark"
6567
formAlignRight={false}
6668
imageLow={desktopPictures.imageLow}
@@ -86,6 +88,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
8688

8789
<Donate
8890
mobileBackgroundColor="blue_dark"
91+
submitButtonColor="blue_dark"
8992
desktopOverlayColor="blue_dark"
9093
formAlignRight={false}
9194
imageLow={desktopPictures.imageLow}
@@ -209,6 +212,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
209212
<Donate
210213
alt="Background image"
211214
mobileBackgroundColor="transparent"
215+
submitButtonColor="blue_dark"
212216
desktopOverlayColor="transparent"
213217
formAlignRight={true}
214218
imageLow={desktopPictures.imageLow}
@@ -234,6 +238,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
234238

235239
<Donate
236240
mobileBackgroundColor="transparent"
241+
submitButtonColor="blue_dark"
237242
formAlignRight={false}
238243
imageLow={desktopPictures.imageLow}
239244
images={desktopPictures.images}
@@ -258,6 +263,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
258263

259264
<Donate
260265
mobileBackgroundColor="blue_dark"
266+
submitButtonColor="blue_dark"
261267
formAlignRight={false}
262268
imageLow={desktopPictures.imageLow}
263269
images={desktopPictures.images}
@@ -282,6 +288,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
282288

283289
<Donate
284290
mobileBackgroundColor="blue_dark"
291+
submitButtonColor="blue_dark"
285292
formAlignRight={false}
286293
imageLow={desktopPictures.imageLow}
287294
images={desktopPictures.images}
@@ -305,6 +312,7 @@ import data from './dev-data/data-single';
305312

306313
<Donate
307314
desktopOverlayColor="blue_dark"
315+
submitButtonColor="blue_dark"
308316
mobileBackgroundColor="blue_dark"
309317
formAlignRight={false}
310318
data={data}
@@ -329,6 +337,7 @@ const desktopPictures = require('../../../styleguide/data/data').defaultData;
329337
<Donate
330338
alt="Background image"
331339
mobileBackgroundColor="red"
340+
submitButtonColor="blue_dark"
332341
desktopOverlayColor="red"
333342
formAlignRight={true}
334343
imageLow={desktopPictures.imageLow}

src/components/Organisms/Donate/Form/Form.js

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, {
2+
useState, useRef, useEffect, useCallback
3+
} from 'react';
24
import PropTypes from 'prop-types';
35

46
import PopUpComponent from './PopUpComponent';
@@ -40,7 +42,7 @@ const Signup = ({
4042
}) => {
4143
const [givingType, setGivingType] = useState('single');
4244
const [errorMsg, setErrorMsg] = useState(false);
43-
const [amountDonate, setAmountDonate] = useState(null);
45+
const [amountDonate, setAmountDonate] = useState(10);
4446
const [moneyBuyCopy, setMoneyBuyCopy] = useState(true);
4547
const [popOpen, setPopOpen] = useState(false);
4648
// In order to keep track of whether the user has ever been shown the popup
@@ -142,6 +144,53 @@ const Signup = ({
142144
const givingData = givingType === 'single' ? singleGiving : regularGiving;
143145
const showGivingSelector = singleGiving !== null && regularGiving !== null;
144146

147+
// Create ref for amount input
148+
const amountRef = useRef(null);
149+
// Create ref for amount button
150+
const buttonRef = useRef(null);
151+
152+
const handleClickOutside = useCallback(event => {
153+
if (!errorMsg) {
154+
return;
155+
}
156+
157+
if (buttonRef.current && event.target === buttonRef.current) {
158+
return;
159+
}
160+
161+
if (amountRef.current && !amountRef.current.contains(event.target)) {
162+
// Check the 2nd moneybuy exists before using it;
163+
// 'philantrophy' carts have been set up to use a single moneybuy.
164+
// See ENG-1685 for more details
165+
const thisAmount = givingData.moneybuys[1]
166+
? givingData.moneybuys[1].value
167+
: givingData.moneybuys[0].value;
168+
169+
setAmountDonate(parseFloat(thisAmount));
170+
}
171+
}, [errorMsg, givingData.moneybuys]);
172+
173+
// Listen for click outside custom amount input if there is no value entered.
174+
useEffect(() => {
175+
// Bind the event listener
176+
document.addEventListener('mousedown', handleClickOutside);
177+
return () => {
178+
// Unbind the event listener on clean up
179+
document.removeEventListener('mousedown', handleClickOutside);
180+
};
181+
}, [errorMsg, handleClickOutside]);
182+
183+
// Create function to conditionally render button text
184+
const renderButtonText = () => {
185+
if (errorMsg) {
186+
return 'Donate';
187+
}
188+
if (givingType === 'single') {
189+
return `Donate £${amountFormatter(amountDonate)} now`;
190+
}
191+
return `Donate £${amountFormatter(amountDonate)} monthly`;
192+
};
193+
145194
return (
146195
<FormWrapper>
147196
{showGivingSelector && (
@@ -210,6 +259,7 @@ const Signup = ({
210259
placeholder="0.00"
211260
onChange={e => setAmountDonate(parseFloat(e.target.value))}
212261
aria-label="Input a different amount"
262+
ref={amountRef}
213263
/>
214264
</FormFieldset>
215265
{amountDonate >= 1 && !noMoneyBuys && moneyBuyCopy && (
@@ -227,14 +277,23 @@ const Signup = ({
227277
)}
228278

229279
{noMoneyBuys ? (
230-
<Button type="submit" as="input" value="Donate" color={submitButtonColor} />
280+
<Button
281+
type="submit"
282+
color={submitButtonColor}
283+
>
284+
{errorMsg
285+
? 'Donate'
286+
: `Donate £${amountFormatter(amountDonate)}`}
287+
</Button>
231288
) : (
232289
<Button
233290
type="submit"
234-
as="input"
235-
value={givingType === 'single' ? 'Donate now' : 'Donate monthly'}
236291
color={submitButtonColor}
237-
/>
292+
ref={buttonRef}
293+
>
294+
{renderButtonText()}
295+
</Button>
296+
238297
)}
239298

240299
</OuterFieldset>

0 commit comments

Comments
 (0)