Skip to content

Commit

Permalink
feat(payment): add checkout welcome component
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Jul 28, 2021
1 parent bd9902c commit 9ba4589
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/components/Welcome/Welcome.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@use '../../styles/variables';
@use '../../styles/theme';

.welcome {
h2 {
font-family: theme.$body-font-family;
font-weight: 700;
font-size: 24px;
}

p {
font-family: theme.$body-font-family;
}
}
38 changes: 38 additions & 0 deletions src/components/Welcome/Welcome.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { act, fireEvent, render } from '@testing-library/react';

import Welcome from './Welcome';

describe('<Welcome>', () => {
test('renders and matches snapshot', () => {
const { container } = render(<Welcome />);

expect(container).toMatchSnapshot();
});

test('calls the onCloseButtonClick callback when clicking the close button', () => {
const onCloseButtonClick = jest.fn();
const { getByText } = render(<Welcome onCloseButtonClick={onCloseButtonClick} />);

fireEvent.click(getByText('checkout.start_watching'));

expect(onCloseButtonClick).toBeCalled();
});

test('calls the onCloseButtonClick callback when clicking the close button', () => {
jest.useFakeTimers();
const onCountdownCompleted = jest.fn();

render(<Welcome onCountdownCompleted={onCountdownCompleted} />);

let i = 10;
while(i--) {
act(() => {
jest.runAllTimers()
});
}

expect(onCountdownCompleted).toBeCalled();
jest.useRealTimers();
});
});
29 changes: 29 additions & 0 deletions src/components/Welcome/Welcome.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';

import Button from '../Button/Button';
import useCountdown from '../../hooks/useCountdown';
import { ConfigStore } from '../../stores/ConfigStore';

import styles from './Welcome.module.scss';
import { useTranslation } from 'react-i18next';

type Props = {
onCloseButtonClick?: () => void;
onCountdownCompleted?: () => void;
};

const Welcome: React.FC<Props> = ({ onCloseButtonClick, onCountdownCompleted }) => {
const { t } = useTranslation('account');
const config = ConfigStore.useState(s => s.config);
const countdown = useCountdown(10, 1, onCountdownCompleted);

return (
<div className={styles.welcome}>
<h2>{t('checkout.welcome_title', { siteName: config.siteName })}</h2>
<p>{t('checkout.welcome_description', { siteName: config.siteName })}</p>
<Button variant="contained" color="primary" label={t('checkout.start_watching', { countdown })} onClick={onCloseButtonClick} size="large" fullWidth />
</div>
);
};

export default Welcome;
23 changes: 23 additions & 0 deletions src/components/Welcome/__snapshots__/Welcome.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<Welcome> renders and matches snapshot 1`] = `
<div>
<div
class="welcome"
>
<h2>
checkout.welcome_title
</h2>
<p>
checkout.welcome_description
</p>
<button
class="button primary fullWidth large"
>
<span>
checkout.start_watching
</span>
</button>
</div>
</div>
`;
2 changes: 2 additions & 0 deletions src/containers/AccountModal/AccountModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Dialog from '../../components/Dialog/Dialog';
import useQueryParam from '../../hooks/useQueryParam';
import { removeQueryParam } from '../../utils/history';
import PaymentFailed from '../../components/PaymentFailed/PaymentFailed';
import Welcome from '../../components/Welcome/Welcome';

import styles from './AccountModal.module.scss';
import Login from './forms/Login';
Expand Down Expand Up @@ -39,6 +40,7 @@ const AccountModal = () => {
{view === 'checkout' ? <Checkout /> : null}
{view === 'paypal-error' ? <PaymentFailed type="error" message={message} onCloseButtonClick={closeHandler} /> : null}
{view === 'paypal-cancelled' ? <PaymentFailed type="cancelled" onCloseButtonClick={closeHandler} /> : null}
{view === 'welcome' ? <Welcome onCloseButtonClick={closeHandler} onCountdownCompleted={closeHandler} /> : null}
</Dialog>
);
};
Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useCountdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect, useRef, useState } from 'react';

const useCountdown = (durationSeconds: number, intervalSeconds: number = 1, completeHandler?: () => void) => {
const timerRef = useRef<number>();
const [countdown, setCountdown] = useState(durationSeconds);

useEffect(() => {
window.clearTimeout(timerRef.current);
if (countdown === 0) {
if (completeHandler) completeHandler();
return;
}

timerRef.current = window.setTimeout(() => {
setCountdown(count => count - intervalSeconds);
}, intervalSeconds * 1000);
}, [countdown]);

return countdown;
};

export default useCountdown;
3 changes: 3 additions & 0 deletions src/i18n/locales/en_US/account.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"paypal": "PayPal",
"paypal_instructions": "Clicking 'continue' will bring you to the PayPal site.",
"redeem_coupon": "Redeem coupon",
"start_watching": "Start watching ({{countdown}})",
"total_price": "Total",
"welcome_description": "Thank you for subscribing to {{siteName}}. Please enjoy all our content.",
"welcome_title": "Welcome to {{siteName}}",
"yearly": "Yearly subscription"
},
"choose_offer": {
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/locales/nl_NL/account.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"paypal": "",
"paypal_instructions": "",
"redeem_coupon": "",
"start_watching": "",
"total_price": "",
"welcome_description": "",
"welcome_title": "",
"yearly": ""
},
"choose_offer": {
Expand Down

0 comments on commit 9ba4589

Please sign in to comment.