Files
developer-roadmap/src/queries/billing.ts
T
Kamran Ahmed 79c6e2be53 refactor: ai-courses (#8327)
* Refactor ai courses

* Refactor

* Regenerate roadmap functionality

* Title and difficulty to refresh also

* Add course regeneration

* Improve the non paid user headings

* Update

* Improve back button logic

* Is paid user checks
2025-03-14 03:05:07 +00:00

98 lines
2.3 KiB
TypeScript

import { queryOptions, useQuery } from '@tanstack/react-query';
import { httpGet } from '../lib/query-http';
import { isLoggedIn } from '../lib/jwt';
import { queryClient } from '../stores/query-client';
export const allowedSubscriptionStatus = [
'active',
'canceled',
'incomplete',
'incomplete_expired',
'past_due',
'paused',
'trialing',
'unpaid',
'none',
] as const;
export type AllowedSubscriptionStatus =
(typeof allowedSubscriptionStatus)[number];
export const USER_SUBSCRIPTION_PLAN_PRICES = [
{
name: 'Pay Monthly',
interval: 'month',
priceId: import.meta.env.PUBLIC_STRIPE_INDIVIDUAL_MONTHLY_PRICE_ID,
amount: import.meta.env.PUBLIC_STRIPE_INDIVIDUAL_MONTHLY_PRICE_AMOUNT,
},
{
name: 'Pay Yearly',
interval: 'year',
priceId: import.meta.env.PUBLIC_STRIPE_INDIVIDUAL_YEARLY_PRICE_ID,
amount: import.meta.env.PUBLIC_STRIPE_INDIVIDUAL_YEARLY_PRICE_AMOUNT,
},
] as const;
export type AllowedSubscriptionInterval =
(typeof USER_SUBSCRIPTION_PLAN_PRICES)[number]['interval'];
type BillingDetailsResponse = {
status: AllowedSubscriptionStatus;
planId?: string;
priceId?: string;
interval?: string;
currentPeriodEnd?: Date;
cancelAtPeriodEnd?: boolean;
};
export function billingDetailsOptions() {
return queryOptions({
queryKey: ['billing-details'],
queryFn: async () => {
return httpGet<BillingDetailsResponse>('/v1-billing-details');
},
enabled: !!isLoggedIn(),
});
}
export function useIsPaidUser() {
const { data, isLoading } = useQuery(
{
queryKey: ['billing-details'],
queryFn: async () => {
return httpGet<BillingDetailsResponse>('/v1-billing-details');
},
enabled: !!isLoggedIn(),
select: (data) => data.status === 'active',
},
queryClient,
);
return {
isPaidUser: data ?? false,
isLoading,
};
}
type CoursePriceParams = {
courseSlug: string;
};
type CoursePriceResponse = {
flag: string;
fullPrice: number;
regionalPrice: number;
regionalDiscountPercentage: number;
isEligibleForDiscount: boolean;
};
export function coursePriceOptions(params: CoursePriceParams) {
return queryOptions({
queryKey: ['course-price', params],
queryFn: async () => {
return httpGet<CoursePriceResponse>(
`/v1-course-price/${params.courseSlug}`,
);
},
});
}