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(sdk): custom plan change message support #625

Merged
merged 2 commits into from
May 20, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import styles from '../../organization.module.css';
import { useNavigate, useParams } from '@tanstack/react-router';
import cross from '~/react/assets/cross.svg';
import { useFrontier } from '~/react/contexts/FrontierContext';
import { useCallback, useEffect, useMemo, useState } from 'react';
import dayjs, { ManipulateType } from 'dayjs';
import { DEFAULT_DATE_FORMAT } from '~/react/utils/constants';
import { useCallback, useEffect, useState } from 'react';
import dayjs from 'dayjs';
import {
DEFAULT_DATE_FORMAT,
DEFAULT_PLAN_UPGRADE_MESSAGE
} from '~/react/utils/constants';
import { V1Beta1Plan } from '~/src';
import Skeleton from 'react-loading-skeleton';
import { getPlanChangeAction, getPlanNameWithInterval } from '~/react/utils';
import planStyles from '../plans.module.css';
import { usePlans } from '../hooks/usePlans';
import { toast } from 'sonner';
import * as _ from 'lodash';

export default function ConfirmPlanChange() {
const navigate = useNavigate({ from: '/plans/confirm-change/$planId' });
Expand Down Expand Up @@ -46,6 +50,17 @@ export default function ConfirmPlanChange() {
fetchActiveSubsciption();
}, [fetchActiveSubsciption]);

const planChangeSlug =
activePlan?.name && newPlan?.name
? `${activePlan?.name}:${newPlan?.name}`
: '';

const planChangeMessage = planChangeSlug
? _.get(config, ['messages', 'billing', 'plan_change', planChangeSlug])
: '';

const isUpgrade = planAction.btnLabel === 'Upgrade';

// const expiryDate = useMemo(() => {
// if (activePlan?.created_at && activePlan?.interval) {
// return dayjs(activePlan?.created_at)
Expand Down Expand Up @@ -142,6 +157,7 @@ export default function ConfirmPlanChange() {
// @ts-ignore
src={cross}
onClick={cancel}
data-test-id="frontier-sdk-confirm-plan-change-close-button"
/>
</Flex>
<Separator />
Expand Down Expand Up @@ -179,24 +195,29 @@ export default function ConfirmPlanChange() {
)}
{isLoading ? (
<Skeleton count={2} />
) : planAction?.btnLabel === 'Upgrade' ? (
) : (
<Text size={2} style={{ color: 'var(--foreground-muted)' }}>
Any remaining balance from your current plan will be prorated and
credited to your account in future billing cycles.
{planChangeMessage || (isUpgrade && DEFAULT_PLAN_UPGRADE_MESSAGE)}
</Text>
) : null}
)}
</Flex>

<Separator />
<Flex justify={'end'} gap="medium" style={{ padding: 'var(--pd-16)' }}>
<Button variant={'secondary'} onClick={cancel} size={'medium'}>
<Button
variant={'secondary'}
onClick={cancel}
size={'medium'}
data-test-id="frontier-sdk-confirm-plan-change-cancel-button"
>
Cancel
</Button>
<Button
variant={'primary'}
size={'medium'}
onClick={onConfirm}
disabled={isLoading || isChangePlanLoading}
data-test-id="frontier-sdk-confirm-plan-change-submit-button"
>
{isChangePlanLoading
? `${planAction?.btnLoadingLabel}...`
Expand Down
2 changes: 2 additions & 0 deletions sdks/js/packages/core/react/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const DEFAULT_DATE_FORMAT = 'DD MMM YYYY';
export const DEFAULT_DATE_SHORT_FORMAT = 'DD MMM';
export const DEFAULT_TOKEN_PRODUCT_NAME = 'token';
export const DEFAULT_PLAN_UPGRADE_MESSAGE =
'Any remaining balance from your current plan will be prorated and credited to your account in future billing cycles.';

export const SUBSCRIPTION_STATES = {
ACTIVE: 'active',
Expand Down
5 changes: 5 additions & 0 deletions sdks/js/packages/core/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface FrontierClientOptions {
dateFormat?: string;
shortDateFormat?: string;
billing?: FrontierClientBillingOptions;
messages?: {
billing?: {
plan_change?: Record<string, string>;
};
};
}

export interface InitialState {
Expand Down
Loading