Skip to content

Commit 27a2305

Browse files
MichaelDeBoeySaraVieira
authored andcommitted
🔨 Switch ChangeSubscription to use useOvermind (#3055)
1 parent 1a25f3d commit 27a2305

File tree

5 files changed

+72
-68
lines changed

5 files changed

+72
-68
lines changed

‎packages/app/src/app/overmind/namespaces/patron/actions.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ export const createSubscriptionClicked: AsyncAction<{
4545
state.patron.isUpdatingSubscription = false;
4646
};
4747

48-
export const updateSubscriptionClicked: AsyncAction<{
49-
coupon: string;
50-
}> = async ({ state, effects }, { coupon }) => {
48+
export const updateSubscriptionClicked: AsyncAction<string> = async (
49+
{ state, effects },
50+
coupon
51+
) => {
5152
effects.analytics.track('Update Patron Subscription');
5253
state.patron.error = null;
5354
state.patron.isUpdatingSubscription = true;

‎packages/app/src/app/pages/Patron/PricingModal/PricingChoice/ChangeSubscription/elements.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import styled from 'styled-components';
2-
import { Button } from '@codesandbox/common/lib/components/Button';
1+
import { Button as ButtonBase } from '@codesandbox/common/lib/components/Button';
32
import Input from '@codesandbox/common/lib/components/Input';
3+
import styled from 'styled-components';
44

55
export const SmallText = styled.div`
66
text-align: center;
@@ -17,7 +17,7 @@ export const Buttons = styled.div`
1717
margin-top: 1rem;
1818
`;
1919

20-
export const StyledButton = styled(Button)`
20+
export const Button = styled(ButtonBase)`
2121
margin: 1rem;
2222
`;
2323

@@ -30,6 +30,10 @@ export const StripeInput = styled(Input)`
3030
height: 32.8px;
3131
`;
3232

33+
export const StripeInputContainer = styled.div`
34+
margin: 2rem 5rem 0;
35+
`;
36+
3337
export const Centered = styled.div`
3438
display: flex;
3539
flex-direction: row;

‎packages/app/src/app/pages/Patron/PricingModal/PricingChoice/ChangeSubscription/index.tsx‎

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,112 @@
1-
import React, { useState } from 'react';
2-
import { useOvermind } from 'app/overmind';
31
import { format } from 'date-fns';
2+
import React, {
3+
ChangeEvent,
4+
FunctionComponent,
5+
MouseEvent,
6+
useState,
7+
} from 'react';
8+
49
import { LinkButton } from 'app/components/LinkButton';
10+
import { useOvermind } from 'app/overmind';
511

612
import {
7-
SmallText,
13+
Button,
814
Buttons,
9-
StyledButton,
10-
StripeInput,
1115
CancelText,
1216
Centered,
17+
SmallText,
18+
StripeInput,
19+
StripeInputContainer,
1320
} from './elements';
1421

15-
interface IChangeSubscriptionProps {
16-
date: string;
17-
markedAsCancelled: boolean;
18-
cancelSubscription: () => void;
19-
updateSubscription: (params: { coupon: string }) => void;
20-
}
21-
22-
export const ChangeSubscription: React.FC<IChangeSubscriptionProps> = ({
23-
date,
24-
markedAsCancelled,
25-
cancelSubscription,
26-
updateSubscription,
27-
}) => {
22+
export const ChangeSubscription: FunctionComponent = () => {
2823
const {
29-
state: {
30-
patron: { isUpdatingSubscription, error },
31-
},
3224
actions: {
3325
modalOpened,
34-
patron: { tryAgainClicked },
26+
patron: {
27+
cancelSubscriptionClicked,
28+
tryAgainClicked,
29+
updateSubscriptionClicked,
30+
},
31+
},
32+
state: {
33+
patron: { error, isUpdatingSubscription },
34+
user: { subscription },
3535
},
3636
} = useOvermind();
37-
3837
const [coupon, setCoupon] = useState('');
3938

4039
if (error) {
4140
return (
4241
<div>
43-
There was a problem updating this subscription.
42+
<span>There was a problem updating this subscription.</span>
43+
4444
<SmallText>{error}</SmallText>
45+
4546
<Buttons>
46-
<StyledButton onClick={() => tryAgainClicked()}>
47-
Try again
48-
</StyledButton>
47+
<Button onClick={() => tryAgainClicked()}>Try again</Button>
4948
</Buttons>
5049
</div>
5150
);
5251
}
5352

5453
let buttons = (
5554
<>
56-
<div style={{ margin: '0 5rem', marginTop: '2rem' }}>
55+
<StripeInputContainer>
5756
<StripeInput
58-
onChange={e => setCoupon(e.target.value)}
59-
value={coupon}
57+
onChange={({ target: { value } }: ChangeEvent<HTMLInputElement>) =>
58+
setCoupon(value)
59+
}
6060
placeholder="Apply Coupon Code"
61+
value={coupon}
6162
/>
62-
</div>
63+
</StripeInputContainer>
64+
6365
<Buttons>
64-
<StyledButton onClick={() => updateSubscription({ coupon })}>
66+
<Button onClick={() => updateSubscriptionClicked(coupon)}>
6567
Update
66-
</StyledButton>
68+
</Button>
6769
</Buttons>
70+
6871
<Centered>
69-
<CancelText onClick={() => cancelSubscription()}>
72+
<CancelText onClick={() => cancelSubscriptionClicked()}>
7073
Cancel my subscription
7174
</CancelText>
7275
</Centered>
7376
</>
7477
);
7578

76-
if (markedAsCancelled) {
79+
if (subscription.cancelAtPeriodEnd) {
7780
buttons = (
7881
<Buttons>
79-
<StyledButton onClick={() => updateSubscription({ coupon: '' })}>
82+
<Button onClick={() => updateSubscriptionClicked('')}>
8083
Reactivate Subscription
81-
</StyledButton>
84+
</Button>
8285
</Buttons>
8386
);
8487
}
8588

8689
if (isUpdatingSubscription) {
8790
buttons = (
8891
<Buttons>
89-
<StyledButton disabled>Processing...</StyledButton>
92+
<Button disabled>Processing...</Button>
9093
</Buttons>
9194
);
9295
}
9396

9497
return (
9598
<div>
9699
{buttons}
100+
97101
<SmallText>
98-
You will be billed every <strong>{format(new Date(date), 'do')}</strong>{' '}
99-
of the month, you can change or cancel your subscription at any time.
100-
You can change your payment method in{' '}
102+
You will be billed every{' '}
103+
<strong>{format(new Date(subscription.since), 'do')}</strong> of the
104+
month, you can change or cancel your subscription at any time. You can
105+
change your payment method in{' '}
101106
<LinkButton
102-
onClick={e => {
103-
e.preventDefault();
107+
onClick={(event: MouseEvent<HTMLButtonElement>) => {
108+
event.preventDefault();
109+
104110
modalOpened({ modal: 'preferences', itemId: 'paymentInfo' });
105111
}}
106112
>

‎packages/app/src/app/pages/Patron/PricingModal/PricingChoice/index.tsx‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { FunctionComponent } from 'react';
22
import { format } from 'date-fns';
33
import { PatronBadge } from '@codesandbox/common/lib/types';
44
import Centered from '@codesandbox/common/lib/components/flex/Centered';
@@ -20,34 +20,30 @@ import {
2020
StyledSignInButton,
2121
} from './elements';
2222

23-
interface IPricingChoiceProps {
23+
type Props = {
2424
badge: PatronBadge;
25-
}
26-
27-
export const PricingChoice: React.FC<IPricingChoiceProps> = ({ badge }) => {
25+
};
26+
export const PricingChoice: FunctionComponent<Props> = ({ badge }) => {
2827
const {
29-
state: { isLoggedIn, isPatron, user, patron },
3028
actions: {
31-
patron: {
32-
priceChanged,
33-
createSubscriptionClicked,
34-
updateSubscriptionClicked,
35-
cancelSubscriptionClicked,
36-
},
29+
patron: { priceChanged, createSubscriptionClicked },
3730
},
31+
state: { isLoggedIn, isPatron, user, patron },
3832
} = useOvermind();
3933

4034
return (
4135
<Container>
4236
<Centered horizontal vertical={false}>
4337
<Title>Pay what you want</Title>
38+
4439
{isPatron && (
4540
<ThankYou
4641
price={user.subscription.amount}
4742
color={badges[badge].colors[0]}
4843
markedAsCancelled={user.subscription.cancelAtPeriodEnd}
4944
/>
5045
)}
46+
5147
<Relative>
5248
<Currency>$</Currency>
5349
<PriceInput
@@ -58,6 +54,7 @@ export const PricingChoice: React.FC<IPricingChoiceProps> = ({ badge }) => {
5854
/>
5955
<Month>/month</Month>
6056
</Relative>
57+
6158
<RangeContainer>
6259
<Range
6360
onChange={value => priceChanged({ price: Number(value) })}
@@ -68,14 +65,10 @@ export const PricingChoice: React.FC<IPricingChoiceProps> = ({ badge }) => {
6865
color={badges[badge].colors[0]}
6966
/>
7067
</RangeContainer>
68+
7169
{isLoggedIn ? ( // eslint-disable-line no-nested-ternary
7270
isPatron ? (
73-
<ChangeSubscription
74-
updateSubscription={props => updateSubscriptionClicked(props)}
75-
cancelSubscription={() => cancelSubscriptionClicked()}
76-
date={user.subscription.since}
77-
markedAsCancelled={user.subscription.cancelAtPeriodEnd}
78-
/>
71+
<ChangeSubscription />
7972
) : (
8073
<Centered style={{ marginTop: '2rem' }} horizontal>
8174
<SubscribeForm

‎packages/common/src/types/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export type CurrentUser = {
117117
plan?: 'pro' | 'patron';
118118
} | null;
119119
curatorAt: string;
120-
badges: Array<Badge>;
120+
badges: Badge[];
121121
integrations: {
122122
zeit?: {
123123
token: string;

0 commit comments

Comments
 (0)