Skip to content

Enable coupon redemption #285

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

Merged
merged 4 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const api = {
getNextInvoice: endpoint('get', '/v1/billing/next_invoice'),
hasUnpaidInvoices: endpoint('get', '/v1/billing/has_unpaid_invoices'),
getUsageCsv: endpoint('get', '/v1/usages/details'),
redeemCoupon: endpoint('post', '/v1/coupons'),

// invitations
listInvitations: endpoint('get', '/v1/organization_invitations'),
Expand Down
9 changes: 9 additions & 0 deletions src/intl/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"back": "Back",
"next": "Next",
"import": "Import",
"send": "Send",
"override": "Override",
"noValue": "--",
"notApplicable": "N/A",
Expand Down Expand Up @@ -2389,6 +2390,14 @@
"upgradeRequired": "You have no invoices or payment methods",
"cta": "Manage billing"
},
"coupon": {
"title": "Coupon code",
"description": "Redeem a coupon code",
"placeholder": "COUPON_CODE",
"submit": "Redeem",
"couponSent": "The coupon code has been applied to your organization",
"hobbyPlanUpgrade": "Do you have a coupon? <upgrade>Upgrade to the Starter plan</upgrade> to apply it."
},
"billingInformation": {
"title": "Billing information",
"description": "This information appears on your monthly invoice and should be the legal address of your home or business",
Expand Down
6 changes: 6 additions & 0 deletions src/pages/settings/organization/billing/billing.page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { FeatureFlag } from 'src/hooks/feature-flag';

import { BillingInformation } from './billing-information';
import { Coupon } from './coupon';
import { StripePortal } from './stripe-portal';
import { Usage } from './usage';

Expand All @@ -7,6 +10,9 @@ export function BillingPage() {
<>
<Usage />
<StripePortal />
<FeatureFlag feature="coupons">
<Coupon />
</FeatureFlag>
<BillingInformation />
</>
);
Expand Down
62 changes: 62 additions & 0 deletions src/pages/settings/organization/billing/coupon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useMutation } from '@tanstack/react-query';
import { useForm } from 'react-hook-form';

import { Button } from '@koyeb/design-system';
import { useOrganization } from 'src/api/hooks/session';
import { useApiMutationFn } from 'src/api/use-api';
import { notify } from 'src/application/notify';
import { ControlledInput } from 'src/components/controlled';
import { SectionHeader } from 'src/components/section-header';
import { FormValues, handleSubmit } from 'src/hooks/form';
import { useSearchParams } from 'src/hooks/router';
import { createTranslate } from 'src/intl/translate';

const T = createTranslate('pages.organizationSettings.billing.coupon');

export function Coupon() {
const params = useSearchParams();
const organization = useOrganization();
const t = T.useTranslate();

const form = useForm({
defaultValues: {
code: params.get('coupon') ?? '',
},
});

const mutation = useMutation({
...useApiMutationFn('redeemCoupon', ({ code }: FormValues<typeof form>) => ({
body: { code },
})),
onSuccess() {
form.reset();
notify.success(t('couponSent'));
},
});

return (
<section className="col gap-6">
<SectionHeader title={<T id="title" />} description={<T id="description" />} />

<form onSubmit={handleSubmit(form, mutation.mutateAsync)} className="row max-w-md items-center gap-4">
<ControlledInput
control={form.control}
disabled={organization.plan === 'hobby'}
name="code"
placeholder={t('placeholder')}
className="w-full"
/>

<Button type="submit" disabled={organization.plan === 'hobby'} loading={form.formState.isSubmitting}>
<T id="submit" />
</Button>
</form>

{organization.plan === 'hobby' && (
<p className="border-l-4 border-green/50 pl-3 text-xs">
<T id="hobbyPlanUpgrade" />
</p>
)}
</section>
);
}
Loading