feat: get plan name from subscriptions endpoint
This commit is contained in:
@@ -8,43 +8,35 @@ import { Button } from '@/components/Button';
|
|||||||
import { observer } from '@node_modules/mobx-react-lite';
|
import { observer } from '@node_modules/mobx-react-lite';
|
||||||
import { WebApplication } from '@/ui_models/application';
|
import { WebApplication } from '@/ui_models/application';
|
||||||
import { useEffect, useState } from 'preact/hooks';
|
import { useEffect, useState } from 'preact/hooks';
|
||||||
import { GetSubscriptionResponse } from '@standardnotes/snjs/dist/@types/services/api/responses';
|
import {
|
||||||
|
GetSubscriptionResponse,
|
||||||
|
GetSubscriptionsResponse,
|
||||||
|
} from '@standardnotes/snjs/dist/@types/services/api/responses';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
application: WebApplication;
|
application: WebApplication;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum PlanName {
|
|
||||||
CorePlan = 'CORE_PLAN',
|
|
||||||
PlusPlan = 'PLUS_PLAN',
|
|
||||||
ProPlan = 'PRO_PLAN',
|
|
||||||
}
|
|
||||||
|
|
||||||
type Subscription = {
|
type Subscription = {
|
||||||
|
planName: string;
|
||||||
cancelled: boolean;
|
cancelled: boolean;
|
||||||
planName: PlanName;
|
|
||||||
endsAt: number;
|
endsAt: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type AvailableSubscriptions = {
|
||||||
|
[key: string]: {
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
type SubscriptionInformationProps = {
|
type SubscriptionInformationProps = {
|
||||||
subscription?: Subscription;
|
subscription?: Subscription;
|
||||||
|
availableSubscriptions: AvailableSubscriptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ValidSubscriptionProps = {
|
type ValidSubscriptionProps = {
|
||||||
subscription: Subscription;
|
subscription: Subscription;
|
||||||
};
|
availableSubscriptions: AvailableSubscriptions;
|
||||||
|
|
||||||
const mapPlanNameToString = (planName: PlanName) => {
|
|
||||||
switch (planName) {
|
|
||||||
case 'CORE_PLAN':
|
|
||||||
return 'Core';
|
|
||||||
case 'PLUS_PLAN':
|
|
||||||
return 'Plus';
|
|
||||||
case 'PRO_PLAN':
|
|
||||||
return 'Pro';
|
|
||||||
default:
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const NoSubscription = () => (
|
const NoSubscription = () => (
|
||||||
@@ -67,12 +59,15 @@ const NoSubscription = () => (
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
const ActiveSubscription = ({ subscription }: ValidSubscriptionProps) => (
|
const ActiveSubscription = ({
|
||||||
|
subscription,
|
||||||
|
availableSubscriptions,
|
||||||
|
}: ValidSubscriptionProps) => (
|
||||||
<>
|
<>
|
||||||
<Text>
|
<Text>
|
||||||
Your{' '}
|
Your{' '}
|
||||||
<span className="font-bold">
|
<span className="font-bold">
|
||||||
Standard Notes {mapPlanNameToString(subscription.planName)}
|
Standard Notes {availableSubscriptions[subscription.planName]}
|
||||||
</span>{' '}
|
</span>{' '}
|
||||||
subscription will be{' '}
|
subscription will be{' '}
|
||||||
<span className="font-bold">
|
<span className="font-bold">
|
||||||
@@ -103,12 +98,15 @@ const ActiveSubscription = ({ subscription }: ValidSubscriptionProps) => (
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
const CancelledSubscription = ({ subscription }: ValidSubscriptionProps) => (
|
const CancelledSubscription = ({
|
||||||
|
subscription,
|
||||||
|
availableSubscriptions,
|
||||||
|
}: ValidSubscriptionProps) => (
|
||||||
<>
|
<>
|
||||||
<Text>
|
<Text>
|
||||||
Your{' '}
|
Your{' '}
|
||||||
<span className="font-bold">
|
<span className="font-bold">
|
||||||
Standard Notes {mapPlanNameToString(subscription.planName)}
|
Standard Notes {availableSubscriptions[subscription.planName]}
|
||||||
</span>{' '}
|
</span>{' '}
|
||||||
subscription has been{' '}
|
subscription has been{' '}
|
||||||
<span className="font-bold">
|
<span className="font-bold">
|
||||||
@@ -142,13 +140,20 @@ const CancelledSubscription = ({ subscription }: ValidSubscriptionProps) => (
|
|||||||
|
|
||||||
const SubscriptionInformation = ({
|
const SubscriptionInformation = ({
|
||||||
subscription,
|
subscription,
|
||||||
|
availableSubscriptions,
|
||||||
}: SubscriptionInformationProps) => {
|
}: SubscriptionInformationProps) => {
|
||||||
const now = new Date().getTime();
|
const now = new Date().getTime();
|
||||||
if (subscription && subscription.endsAt > now) {
|
if (subscription && subscription.endsAt > now) {
|
||||||
return subscription.cancelled ? (
|
return subscription.cancelled ? (
|
||||||
<CancelledSubscription subscription={subscription} />
|
<CancelledSubscription
|
||||||
|
subscription={subscription}
|
||||||
|
availableSubscriptions={availableSubscriptions}
|
||||||
|
/>
|
||||||
) : (
|
) : (
|
||||||
<ActiveSubscription subscription={subscription} />
|
<ActiveSubscription
|
||||||
|
subscription={subscription}
|
||||||
|
availableSubscriptions={availableSubscriptions}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return <NoSubscription />;
|
return <NoSubscription />;
|
||||||
@@ -157,16 +162,39 @@ const SubscriptionInformation = ({
|
|||||||
const Subscription = observer(({ application }: Props) => {
|
const Subscription = observer(({ application }: Props) => {
|
||||||
const [subscription, setSubscription] =
|
const [subscription, setSubscription] =
|
||||||
useState<Subscription | undefined>(undefined);
|
useState<Subscription | undefined>(undefined);
|
||||||
|
const [availableSubscriptions, setAvailableSubscriptions] =
|
||||||
|
useState<AvailableSubscriptions>({});
|
||||||
|
const [error, setError] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const getSubscription = async () => {
|
const getSubscriptions = async () => {
|
||||||
const result = await application.getUserSubscription();
|
try {
|
||||||
if (!result.error && result.data) {
|
const result = await application.getSubscriptions();
|
||||||
const data = (result as GetSubscriptionResponse).data;
|
if (result.data) {
|
||||||
const subscription = data!.subscription;
|
const data = (result as GetSubscriptionsResponse).data;
|
||||||
setSubscription(subscription);
|
setAvailableSubscriptions(data!);
|
||||||
|
} else {
|
||||||
|
setError(true);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
setError(true);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const getSubscription = async () => {
|
||||||
|
try {
|
||||||
|
const result = await application.getUserSubscription();
|
||||||
|
if (!result.error && result.data) {
|
||||||
|
const data = (result as GetSubscriptionResponse).data;
|
||||||
|
const subscription = data!.subscription;
|
||||||
|
setSubscription(subscription);
|
||||||
|
} else {
|
||||||
|
setError(true);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
setError(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
getSubscriptions();
|
||||||
getSubscription();
|
getSubscription();
|
||||||
}, [application]);
|
}, [application]);
|
||||||
|
|
||||||
@@ -176,7 +204,14 @@ const Subscription = observer(({ application }: Props) => {
|
|||||||
<div className="flex flex-row items-center">
|
<div className="flex flex-row items-center">
|
||||||
<div className="flex-grow flex flex-col">
|
<div className="flex-grow flex flex-col">
|
||||||
<Title>Subscription</Title>
|
<Title>Subscription</Title>
|
||||||
<SubscriptionInformation subscription={subscription} />
|
{error ? (
|
||||||
|
'No subscription information available.'
|
||||||
|
) : (
|
||||||
|
<SubscriptionInformation
|
||||||
|
subscription={subscription}
|
||||||
|
availableSubscriptions={availableSubscriptions}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</PreferencesSegment>
|
</PreferencesSegment>
|
||||||
|
|||||||
Reference in New Issue
Block a user