Skip to content

Add a billing tab to the settings #2811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f0cd0b9
Add a billing tab to the settings
Arpan-206 Apr 23, 2025
22a50d9
ofc I forgot about the linter
Arpan-206 Apr 23, 2025
fdf9480
most tests are working
Arpan-206 Apr 25, 2025
7646797
tests are working and are extensive
Arpan-206 Apr 25, 2025
ef59d5a
making the linter a little less upset
Arpan-206 Apr 25, 2025
d2a4e06
[percy] Let's check what this means
Arpan-206 Apr 25, 2025
a744efb
Remove /membership (test, templates etc), also redirect to billing
Arpan-206 Apr 25, 2025
b8e5abe
Rabbit suggestion + Linter
Arpan-206 Apr 25, 2025
4586692
Minor fix to the test
Arpan-206 Apr 25, 2025
492ea65
Bring back the pay thing
Arpan-206 Apr 25, 2025
28c49d4
maintain consistancy
Arpan-206 Apr 25, 2025
80fd584
Move the Container inwards and input user as a whole
Arpan-206 Apr 25, 2025
54847e9
Pauls' changes
Arpan-206 Apr 25, 2025
fc9bfe3
[percy] forgot this
Arpan-206 Apr 25, 2025
ff65f50
[percy] linter?
Arpan-206 Apr 25, 2025
a23e813
Switch back to convention
Arpan-206 Apr 25, 2025
5e42545
remove .settled calls which are unnecessary
Arpan-206 Apr 25, 2025
e44227a
addressed comments except VIP
Arpan-206 Apr 28, 2025
2324df5
Show VIP + Active
Arpan-206 Apr 28, 2025
f387a3b
[percy] forgot this
Arpan-206 Apr 28, 2025
31426ec
[percy] linter too
Arpan-206 Apr 28, 2025
c370b91
Added Dark Methods
Arpan-206 Apr 28, 2025
0fb27af
linter ffs
Arpan-206 Apr 28, 2025
5fa3c1a
Duplicate selectors again
Arpan-206 Apr 28, 2025
96700ab
Added test for email client
Arpan-206 Apr 28, 2025
324665f
[percy] addressed comments
Arpan-206 Apr 30, 2025
bd80766
[percy] Update the dot to be just a dot
Arpan-206 Apr 30, 2025
ac350c2
Update app/components/settings/billing-page/payment-history-section.ts
Arpan-206 May 1, 2025
bc73fba
Addressed Paul's Comments
Arpan-206 May 3, 2025
547b45b
[percy] linter + other minor changes
Arpan-206 May 3, 2025
263cab5
[percy] removed active plan one
Arpan-206 May 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/components/billing-status-badge/member-badge.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<PrimaryLinkButton @size={{this.linkButtonSize}} @route="membership" data-test-member-badge ...attributes>
<PrimaryLinkButton @size={{this.linkButtonSize}} @route="settings.billing" data-test-member-badge ...attributes>

<span class="flex items-center">
Member
Expand Down
2 changes: 1 addition & 1 deletion app/components/header/account-dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
@action
async handleManageSubscriptionClick(dropdownActions: { close: () => void }) {
dropdownActions.close();
this.router.transitionTo('membership');
this.router.transitionTo('settings.billing');

Check warning on line 55 in app/components/header/account-dropdown.ts

View check run for this annotation

Codecov / codecov/patch

app/components/header/account-dropdown.ts#L55

Added line #L55 was not covered by tests
}

@action
Expand Down
80 changes: 80 additions & 0 deletions app/components/settings/billing-page/membership-section.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<Settings::FormSection @title="Membership" @description="Your CodeCrafters membership details" data-test-membership-section>
{{#if @user.hasActiveSubscription}}
<div class="inline-flex items-center gap-2 rounded-full bg-teal-50 dark:bg-teal-900 px-2 py-1 border border-teal-500 dark:border-teal-400">
{{svg-jar "circle" class="h-5 w-5 text-teal-500"}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of svg let's just do HTML (h-5 w-5 rounded-full), that's what we use for simple cases like this

<div class="text-teal-500 font-semibold uppercase text-sm">
Membership active
</div>
</div>
<div class="text-gray-500 dark:text-gray-400 text-xs mt-2">
{{#if (and @user.isVip (gt @user.vipStatusExpiresAt @user.activeSubscription.cancelAt))}}
<p class="line-through">
You have access to all CodeCrafters content, valid until
<b class="font-semibold">{{date-format @user.activeSubscription.cancelAt format="PPPp"}}</b>.
</p>
<p>
🎉 You have VIP access to all CodeCrafters content, valid until
<b class="font-semibold">{{date-format @user.vipStatusExpiresAt format="PPPp"}}</b>.
</p>
{{else}}
<p>
You have access to all CodeCrafters content, valid until
<b class="font-semibold">{{date-format @user.activeSubscription.cancelAt format="PPPp"}}</b>.
</p>
{{/if}}
</div>
{{else if @user.isVip}}
<div class="inline-flex items-center gap-2 rounded-full bg-teal-50 px-2 py-1 border border-teal-500">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arpan-206 no dark mode styles. The same green pill above has different styles added.

Maybe you'd want to extract this into a component? Something like BillingPage::MembershipSection::StatusPill?

{{svg-jar "circle" class="h-5 w-5 text-teal-500"}}
<div class="text-teal-500 font-semibold uppercase text-sm">
VIP Access
</div>
</div>
<div class="text-gray-500 dark:text-gray-400 text-xs mt-2">
<p>
{{#if @user.vipStatusExpiresAt}}
🎉 You have VIP access to all CodeCrafters content, valid until
<b class="font-semibold">{{date-format @user.vipStatusExpiresAt format="PPPp"}}</b>.
{{else}}
🎉 You have VIP access to all CodeCrafters content.
{{/if}}
</p>
</div>
{{else if @user.expiredSubscription}}
<div class="inline-flex items-center gap-2 rounded-full bg-red-50 px-2 py-1 border border-red-500">
{{svg-jar "circle" class="h-5 w-5 text-red-700 dark:text-red-300"}}
<div class="text-red-700 dark:text-red-300 font-semibold uppercase text-sm">
Membership inactive
</div>
</div>
<div class="text-gray-500 dark:text-gray-400 text-xs mt-2">
<p>
Your CodeCrafters membership is
<b class="text-red-600 font-semibold">currently inactive</b>.
</p>
<p>
Start a new membership to get access to
<a href="https://docs.codecrafters.io/membership" target="_blank" rel="noopener noreferrer">membership benefits</a>.
</p>
</div>
<PrimaryLinkButton @size="small" @route="pay" class="mt-3">
Start membership →
</PrimaryLinkButton>
{{else}}
<div class="inline-flex items-center gap-2 rounded-full bg-yellow-50 px-2 py-1 border border-yellow-500">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No dark mode styles (bg-yellow-50 will look extremely bright in dark mode)

{{svg-jar "circle" class="h-5 w-5 text-yellow-500"}}
<div class="text-yellow-500 font-semibold uppercase text-sm">
No membership found
</div>
</div>
<div class="text-gray-500 dark:text-gray-400 text-xs mt-2">
<p>
You don't have a CodeCrafters membership. Start one to get access to
<a href="https://docs.codecrafters.io/membership" target="_blank" rel="noopener noreferrer">membership benefits</a>.
</p>
</div>
<PrimaryLinkButton @size="small" @route="pay" class="mt-3">
Start membership →
</PrimaryLinkButton>
{{/if}}
</Settings::FormSection>
18 changes: 18 additions & 0 deletions app/components/settings/billing-page/membership-section.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Component from '@glimmer/component';
import type UserModel from 'codecrafters-frontend/models/user';

interface Signature {
Element: HTMLDivElement;

Args: {
user: UserModel;
};
}

export default class MembershipSectionComponent extends Component<Signature> {}

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
'Settings::BillingPage::MembershipSection': typeof MembershipSectionComponent;
}
}
63 changes: 63 additions & 0 deletions app/components/settings/billing-page/payment-history-section.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<Settings::FormSection @title="Payment history" @description="Details of your previous payments" data-test-payment-history-section>
{{#if this.isLoading}}
<div class="flex justify-center py-8">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stay consistent with loading states that we have in other places:

Screenshot 2025-04-30 at 15 28 39 Screenshot 2025-04-30 at 15 28 32 Screenshot 2025-04-30 at 15 28 22

(I searched for isLoading across *.hbs files to find out what we do in other cases)

<div class="animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900 dark:border-gray-100"></div>
</div>
{{else if this.errorMessage}}
<div class="bg-red-50 dark:bg-red-900 border border-red-200 dark:border-red-400 rounded-md p-4">
<div class="flex gap-3">
<div class="flex-shrink-0">
{{svg-jar "x-circle" class="h-5 w-5 text-red-700 dark:text-red-300"}}
</div>
<p class="text-sm text-red-700 dark:text-red-300">{{this.errorMessage}}</p>
</div>
</div>
{{else if (gt this.charges.length 0)}}
<div class="grid grid-cols-3 gap-y-3">
<div class="border-b pb-2 text-gray-500 dark:text-gray-400 text-sm">Date</div>
<div class="border-b pb-2 text-gray-500 dark:text-gray-400 text-sm text-right">Amount</div>
<div class="border-b pb-2"></div>

{{#each this.charges as |charge|}}
<div class="text-gray-600 dark:text-gray-300 text-sm">
{{date-format charge.createdAt format="PPP"}}
</div>
<div
class="{{if charge.statusIsFailed 'text-red-600 dark:text-red-400' 'text-gray-600 dark:text-gray-300'}} text-sm text-right"
data-test-payment-history-item
>
<span class="font-semibold" data-test-amount>{{charge.displayString}}</span>
{{#if (gt charge.amountRefunded 0)}}
{{#if charge.isFullyRefunded}}
<span class="text-red-600 dark:text-red-400" data-test-refund-text>(refunded)</span>
{{else}}
<span class="text-red-600 dark:text-red-400" data-test-refund-text>(<span
class="font-semibold"
>{{charge.refundedAmountDisplayString}}</span>
refunded)</span>
{{/if}}
{{/if}}
</div>
<div class="flex items-center justify-end">
{{#if (and charge.invoiceId charge.statusIsSucceeded)}}
<a
href={{charge.invoiceDownloadUrl}}
target="_blank"
class="text-teal-500 dark:text-teal-400 hover:text-teal-600 dark:hover:text-teal-300 font-semibold text-sm"
data-test-download-invoice-link
rel="noopener noreferrer"
>
Download Invoice
</a>
{{else if charge.statusIsFailed}}
<span class="text-gray-600 dark:text-gray-300 text-sm">Payment failed</span>
{{/if}}
</div>
{{/each}}
</div>
{{else}}
<div class="text-gray-700 dark:text-gray-200">
No payment history found.
</div>
{{/if}}
</Settings::FormSection>
52 changes: 52 additions & 0 deletions app/components/settings/billing-page/payment-history-section.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import * as Sentry from '@sentry/ember';
import type Store from '@ember-data/store';
import type UserModel from 'codecrafters-frontend/models/user';
import type ChargeModel from 'codecrafters-frontend/models/charge';

interface Signature {
Element: HTMLDivElement;
Args: {
user: UserModel;
};
}

export default class PaymentHistorySectionComponent extends Component<Signature> {
@service declare store: Store;

@tracked charges: ChargeModel[] = [];
@tracked errorMessage: string | null = null;
@tracked isLoading = true;

Check warning on line 22 in app/components/settings/billing-page/payment-history-section.ts

View check run for this annotation

Codecov / codecov/patch

app/components/settings/billing-page/payment-history-section.ts#L20-L22

Added lines #L20 - L22 were not covered by tests
constructor(owner: unknown, args: Signature['Args']) {
super(owner, args);
this.loadCharges();
}

async loadCharges() {
this.isLoading = true;
this.errorMessage = null;

try {
const result = await this.store.query('charge', {
filter: { user_id: this.args.user.id },
});
this.charges = result.toArray();
} catch (error) {
console.error('Failed to fetch charges:', error);
this.errorMessage = 'Failed to load payment history. Please try again later.';
Sentry.captureException(error);
this.charges = [];
} finally {

Check warning on line 42 in app/components/settings/billing-page/payment-history-section.ts

View check run for this annotation

Codecov / codecov/patch

app/components/settings/billing-page/payment-history-section.ts#L39-L42

Added lines #L39 - L42 were not covered by tests
this.isLoading = false;
}
}
}

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
'Settings::BillingPage::PaymentHistorySection': typeof PaymentHistorySectionComponent;
}
}
12 changes: 12 additions & 0 deletions app/components/settings/billing-page/renewal-section.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Settings::FormSection @title="Renewal" @description="Details of your upcoming payment">
<div class="inline-flex items-center gap-2 rounded-full bg-gray-50 dark:bg-gray-800 px-2 py-1 border border-gray-300 dark:border-gray-600">
{{svg-jar "circle" class="h-5 w-5 text-gray-400"}}
<div class="text-gray-400 font-semibold uppercase text-sm">
Auto-renew disabled
</div>
</div>

<div class="text-gray-500 dark:text-gray-400 text-xs mt-2">
Your membership does not renew automatically. Once your membership expires, you'll be able to make a new one-time payment.
</div>
</Settings::FormSection>
13 changes: 13 additions & 0 deletions app/components/settings/billing-page/renewal-section.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Component from '@glimmer/component';

interface Signature {
Element: HTMLDivElement;
}

export default class RenewalSectionComponent extends Component<Signature> {}

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
'Settings::BillingPage::RenewalSection': typeof RenewalSectionComponent;
}
}
14 changes: 14 additions & 0 deletions app/components/settings/billing-page/support-section.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Settings::FormSection @title="Support" @description="How to get help with billing">
<a href="mailto:[email protected]?subject=Billing help (account: {{@user.username}})" class="inline-block rounded" data-test-support-section>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<a href="mailto:[email protected]?subject=Billing help (account: {{@user.username}})" class="inline-block rounded" data-test-support-section>
<a href="mailto:[email protected]?subject=Billing help (account: {{@user.username}})" class="inline-block rounded">

(Place on the outer FormSection, to be consistent with other components)

<PrimaryButton @size="small" data-test-support-contact-button>
Get help
</PrimaryButton>
</a>
<div class="text-gray-500 dark:text-gray-400 text-xs mt-3">
<p>
Questions? Click the button above or write to us at
<a href="mailto:[email protected]" class="underline text-blue-500 dark:text-blue-400">[email protected]</a>
and we'll help sort things out.
</p>
</div>
</Settings::FormSection>
17 changes: 17 additions & 0 deletions app/components/settings/billing-page/support-section.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Component from '@glimmer/component';
import type UserModel from 'codecrafters-frontend/models/user';

interface Signature {
Element: HTMLDivElement;
Args: {
user: UserModel;
};
}

export default class SupportSectionComponent extends Component<Signature> {}

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
'Settings::BillingPage::SupportSection': typeof SupportSectionComponent;
}
}
1 change: 1 addition & 0 deletions app/controllers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class SettingsController extends Controller {
get tabs() {
return [
{ route: 'settings.profile', label: 'Profile' },
{ route: 'settings.billing', label: 'Billing' },
{ route: 'settings.account', label: 'Account' },
];
}
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/settings/billing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Controller from '@ember/controller';
import type ChargeModel from 'codecrafters-frontend/models/charge';
import type { ModelType as SettingsModelType } from 'codecrafters-frontend/routes/settings';

interface BillingModelType extends SettingsModelType {
charges: ChargeModel[];
}

export default class BillingController extends Controller {
declare model: BillingModelType;
}
1 change: 1 addition & 0 deletions app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Router.map(function () {

this.route('settings', function () {
this.route('profile');
this.route('billing');
this.route('account');
});

Expand Down
21 changes: 0 additions & 21 deletions app/routes/membership.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/routes/pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class PayRoute extends BaseRoute {
await this.authenticator.authenticate();

if (this.authenticator.currentUser && this.authenticator.currentUser.hasActiveSubscription) {
this.router.transitionTo('membership');
this.router.transitionTo('settings.billing');

return;
}
Expand Down
25 changes: 0 additions & 25 deletions app/templates/membership.hbs

This file was deleted.

7 changes: 7 additions & 0 deletions app/templates/settings/billing.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Settings::BillingPage::MembershipSection @user={{@model.user}} />
<Settings::FormDivider />
<Settings::BillingPage::RenewalSection />
<Settings::FormDivider />
<Settings::BillingPage::SupportSection @user={{@model.user}} />
<Settings::FormDivider />
<Settings::BillingPage::PaymentHistorySection @user={{@model.user}} />
3 changes: 3 additions & 0 deletions public/assets/images/svg-icons/circle.svg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be needed, just use HTML with rounded-full

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading