Skip to content
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

feat: support plus unavailable #4101

Merged
merged 7 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 6 additions & 2 deletions packages/shared/src/components/plus/PlusDesktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { useRouter } from 'next/router';
import { usePaymentContext } from '../../contexts/PaymentContext';

import { PlusInfo } from './PlusInfo';
import { PlusUnavailable } from './PlusUnavailable';

export const PlusDesktop = (): ReactElement => {
const { openCheckout, paddle, productOptions } = usePaymentContext();
const { openCheckout, paddle, productOptions, isPlusAvailable } =
usePaymentContext();
const {
query: { selectedPlan },
} = useRouter();
Expand Down Expand Up @@ -52,7 +54,9 @@ export const PlusDesktop = (): ReactElement => {
<div
ref={ref}
className="checkout-container min-h-40 w-[28.5rem] rounded-16 border border-border-subtlest-tertiary bg-background-default p-5"
/>
>
{!isPlusAvailable && <PlusUnavailable className="h-[35rem]" />}
</div>
</div>
);
};
36 changes: 36 additions & 0 deletions packages/shared/src/components/plus/PlusUnavailable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { ReactElement } from 'react';
import React from 'react';
import classNames from 'classnames';
import { IconSize } from '../Icon';
import { SitesIcon } from '../icons';
import {
TypographyType,
TypographyColor,
Typography,
} from '../typography/Typography';

export type PlusUnavailableProps = {
className?: string;
};

export const PlusUnavailable = ({
className,
}: PlusUnavailableProps): ReactElement => {
return (
<div
className={classNames(
'flex flex-1 flex-col items-center justify-center gap-4 text-center',
className,
)}
>
<SitesIcon size={IconSize.XXXLarge} />
<Typography
type={TypographyType.Callout}
bold
color={TypographyColor.Primary}
>
Unfortunately, this service is not available in your region.
</Typography>
</div>
);
};
18 changes: 14 additions & 4 deletions packages/shared/src/contexts/PaymentContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { useQuery } from '@tanstack/react-query';
import { useRouter } from 'next/router';
import { useAuthContext } from './AuthContext';
import { plusSuccessUrl } from '../lib/constants';
import { invalidPlusRegions, plusSuccessUrl } from '../lib/constants';
import { LogEvent } from '../lib/log';
import { usePlusSubscription } from '../hooks';
import { logPixelPayment } from '../components/Pixels';
Expand All @@ -38,9 +38,10 @@ export interface PaymentContextData {
paddle?: Paddle | undefined;
productOptions?: ProductOption[];
earlyAdopterPlanId?: string | null;
isPlusAvailable: boolean;
}

const PaymentContext = React.createContext<PaymentContextData>({});
const PaymentContext = React.createContext<PaymentContextData>(undefined);
export default PaymentContext;

export type PaymentContextProviderProps = {
Expand All @@ -58,6 +59,10 @@ export const PaymentContextProvider = ({
const logRef = useRef<typeof logSubscriptionEvent>();
logRef.current = logSubscriptionEvent;

const isPlusAvailable = useMemo(() => {
return !invalidPlusRegions.includes(geo?.region);
}, [geo?.region]);

// Download and initialize Paddle instance from CDN
useEffect(() => {
const existingPaddleInstance = getPaddleInstance();
Expand Down Expand Up @@ -120,6 +125,10 @@ export const PaymentContextProvider = ({

const openCheckout = useCallback(
({ priceId }: { priceId: string }) => {
if (!isPlusAvailable) {
return;
}

paddle?.Checkout.open({
items: [{ priceId, quantity: 1 }],
customer: {
Expand All @@ -139,7 +148,7 @@ export const PaymentContextProvider = ({
},
});
},
[paddle, user],
[paddle, user, isPlusAvailable],
);

const getPrices = useCallback(async () => {
Expand Down Expand Up @@ -194,8 +203,9 @@ export const PaymentContextProvider = ({
paddle,
productOptions,
earlyAdopterPlanId,
isPlusAvailable,
}),
[earlyAdopterPlanId, openCheckout, paddle, productOptions],
[earlyAdopterPlanId, openCheckout, paddle, productOptions, isPlusAvailable],
);

return (
Expand Down
12 changes: 12 additions & 0 deletions packages/shared/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ export const feedRangeFilters: RadioItemProps[] = [
];

export const customFeedsPlusDate = new Date('2024-12-11');

export const invalidPlusRegions = [
'CU',
'IR',
'MM',
'SD',
'SY',
'KP',
'BY',
'ZW',
'RU',
];
4 changes: 2 additions & 2 deletions packages/shared/src/lib/featureManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const feature = {
onboardingChecklist: new Feature('onboarding_checklist', true),
showCodeSnippets: new Feature('show_code_snippets', false),
pricingIds: new Feature('pricing_ids', {
pri_01jbsccbdbcwyhdy8hy3c2etyn: PlusPriceType.Monthly,
pri_01jbscda57910yvwjtyapnrrzc: PlusPriceType.Yearly,
pri_01jcdp5ef4yhv00p43hr2knrdg: PlusPriceType.Monthly,
pri_01jcdn6enr5ap3ekkddc6fv6tq: PlusPriceType.Yearly,
capJavert marked this conversation as resolved.
Show resolved Hide resolved
}),
};

Expand Down
8 changes: 6 additions & 2 deletions packages/webapp/pages/plus/payment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { useRouter } from 'next/router';
import { useViewSize, ViewSize } from '@dailydotdev/shared/src/hooks';
import { webappUrl } from '@dailydotdev/shared/src/lib/constants';
import { NextSeo } from 'next-seo';
import { PlusUnavailable } from '@dailydotdev/shared/src/components/plus/PlusUnavailable';

import { getPlusLayout } from '../../components/layouts/PlusLayout/PlusLayout';

const PlusPaymentPage = (): ReactElement => {
const isLaptop = useViewSize(ViewSize.Laptop);
const { openCheckout } = usePaymentContext();
const { openCheckout, isPlusAvailable } = usePaymentContext();
const router = useRouter();
const { pid } = router.query;

Expand All @@ -36,7 +38,9 @@ const PlusPaymentPage = (): ReactElement => {
openCheckout({ priceId: pid as string });
}}
className="checkout-container h-full w-full bg-background-default p-5"
/>
>
{!isPlusAvailable && <PlusUnavailable className="h-full" />}
</div>
</div>
</>
);
Expand Down
Loading