-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): implement cancel subscription flow
- Loading branch information
1 parent
bc9dae1
commit 2fef77f
Showing
19 changed files
with
326 additions
and
27 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/components/CancelSubscriptionForm/CancelSubscriptionForm.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@use '../../styles/variables'; | ||
@use '../../styles/theme'; | ||
|
||
.title { | ||
margin-bottom: 8px; | ||
font-family: theme.$body-font-family; | ||
font-weight: 700; | ||
font-size: 24px; | ||
} | ||
|
||
.paragraph { | ||
font-family: theme.$body-font-family; | ||
} | ||
|
||
.confirmButton { | ||
margin-bottom: 8px; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/components/CancelSubscriptionForm/CancelSubscriptionForm.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import CancelSubscriptionForm from './CancelSubscriptionForm'; | ||
|
||
describe('<CancelSubscriptionForm>', () => { | ||
test('renders and matches snapshot', () => { | ||
const { container } = render(<CancelSubscriptionForm error={null} onCancel={jest.fn()} onConfirm={jest.fn()} />); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
src/components/CancelSubscriptionForm/CancelSubscriptionForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import Button from '../Button/Button'; | ||
import FormFeedback from '../FormFeedback/FormFeedback'; | ||
|
||
import styles from './CancelSubscriptionForm.module.scss'; | ||
|
||
type Props = { | ||
onConfirm: () => void; | ||
onCancel: () => void; | ||
error: string | null; | ||
}; | ||
|
||
const CancelSubscriptionForm: React.FC<Props> = ({ onConfirm, onCancel, error }: Props) => { | ||
const { t } = useTranslation('account'); | ||
|
||
return ( | ||
<div> | ||
{error ? <FormFeedback variant="error">{error}</FormFeedback> : null} | ||
<h2 className={styles.title}>{t('cancel_subscription.title')}</h2> | ||
<p className={styles.paragraph}> | ||
{t('cancel_subscription.explanation')} | ||
</p> | ||
<Button className={styles.confirmButton} label="Unsubscribe" color="primary" variant="contained" onClick={onConfirm} fullWidth /> | ||
<Button label="No, thanks" variant="outlined" onClick={onCancel} fullWidth /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CancelSubscriptionForm; |
32 changes: 32 additions & 0 deletions
32
src/components/CancelSubscriptionForm/__snapshots__/CancelSubscriptionForm.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<CancelSubscriptionForm> renders and matches snapshot 1`] = ` | ||
<div> | ||
<div> | ||
<h2 | ||
class="title" | ||
> | ||
cancel_subscription.title | ||
</h2> | ||
<p | ||
class="paragraph" | ||
> | ||
cancel_subscription.explanation | ||
</p> | ||
<button | ||
class="button confirmButton primary fullWidth" | ||
> | ||
<span> | ||
Unsubscribe | ||
</span> | ||
</button> | ||
<button | ||
class="button default outlined fullWidth" | ||
> | ||
<span> | ||
No, thanks | ||
</span> | ||
</button> | ||
</div> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/components/SubscriptionCancelled/SubscriptionCancelled.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@use '../../styles/variables'; | ||
@use '../../styles/theme'; | ||
|
||
.title { | ||
margin-bottom: 8px; | ||
font-family: theme.$body-font-family; | ||
font-weight: 700; | ||
font-size: 24px; | ||
} | ||
|
||
.paragraph { | ||
font-family: theme.$body-font-family; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/components/SubscriptionCancelled/SubscriptionCancelled.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import SubscriptionCancelled from './SubscriptionCancelled'; | ||
|
||
describe('<SubscriptionCancelled>', () => { | ||
test('renders and matches snapshot', () => { | ||
const { container } = render(<SubscriptionCancelled onClose={jest.fn()} expiresDate="12/12/2021" />); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
src/components/SubscriptionCancelled/SubscriptionCancelled.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import Button from '../Button/Button'; | ||
|
||
import styles from './SubscriptionCancelled.module.scss'; | ||
|
||
type Props = { | ||
expiresDate: string; | ||
onClose: () => void; | ||
}; | ||
|
||
const SubscriptionCancelled: React.FC<Props> = ({ expiresDate, onClose }: Props) => { | ||
const { t } = useTranslation('account'); | ||
|
||
return ( | ||
<div className={styles.SubscriptionCancelled}> | ||
<h2 className={styles.title}>{t('subscription_cancelled.title')}</h2> | ||
<p className={styles.paragraph}> | ||
{t('subscription_cancelled.message', { date: expiresDate})} | ||
</p> | ||
<Button label="Back to profile" variant="outlined" onClick={onClose} fullWidth /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SubscriptionCancelled; |
25 changes: 25 additions & 0 deletions
25
src/components/SubscriptionCancelled/__snapshots__/SubscriptionCancelled.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`<SubscriptionCancelled> renders and matches snapshot 1`] = ` | ||
<div> | ||
<div> | ||
<h2 | ||
class="title" | ||
> | ||
subscription_cancelled.title | ||
</h2> | ||
<p | ||
class="paragraph" | ||
> | ||
subscription_cancelled.message | ||
</p> | ||
<button | ||
class="button default outlined fullWidth" | ||
> | ||
<span> | ||
Back to profile | ||
</span> | ||
</button> | ||
</div> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useHistory } from 'react-router'; | ||
|
||
import CancelSubscriptionForm from '../../../components/CancelSubscriptionForm/CancelSubscriptionForm'; | ||
import { removeQueryParam } from '../../../utils/history'; | ||
import LoadingOverlay from '../../../components/LoadingOverlay/LoadingOverlay'; | ||
import { AccountStore, cancelSubscription } from '../../../stores/AccountStore'; | ||
import SubscriptionCancelled from '../../../components/SubscriptionCancelled/SubscriptionCancelled'; | ||
import { formatDate } from '../../../utils/formatting'; | ||
|
||
const CancelSubscription = () => { | ||
const { t } = useTranslation('account'); | ||
const history = useHistory(); | ||
const subscription = AccountStore.useState((s) => s.subscription); | ||
const [cancelled, setCancelled] = useState(false); | ||
const [loading, setLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const cancelSubscriptionConfirmHandler = async () => { | ||
setLoading(true); | ||
setError(null); | ||
|
||
try { | ||
await cancelSubscription(); | ||
setCancelled(true); | ||
} catch (error: unknown) { | ||
setError(t('cancel_subscription.unknown_error_occurred')); | ||
} | ||
|
||
setLoading(false); | ||
}; | ||
|
||
const closeHandler = () => { | ||
history.replace(removeQueryParam(history, 'u')); | ||
}; | ||
|
||
if (!subscription) return null; | ||
|
||
return ( | ||
<React.Fragment> | ||
{cancelled ? ( | ||
<SubscriptionCancelled expiresDate={formatDate(subscription.expiresAt)} onClose={closeHandler} /> | ||
) : ( | ||
<CancelSubscriptionForm onConfirm={cancelSubscriptionConfirmHandler} onCancel={closeHandler} error={error} /> | ||
)} | ||
{loading ? <LoadingOverlay inline /> : null} | ||
</React.Fragment> | ||
); | ||
}; | ||
export default CancelSubscription; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.