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

fix(console): fix the plan title for subscription plan selector #6348

Merged
merged 1 commit into from
Jul 29, 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
14 changes: 12 additions & 2 deletions packages/console/src/components/PlanName/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { type TFuncKey } from 'i18next';
import { useTranslation } from 'react-i18next';

import { ReservedPlanName } from '@/types/subscriptions';
import { ReservedPlanName, ReservedSkuId } from '@/types/subscriptions';

const registeredPlanNamePhraseMap: Record<
string,
Expand All @@ -15,6 +15,16 @@
[ReservedPlanName.Enterprise]: 'enterprise',
};

const registeredSkuIdNamePhraseMap: Record<
string,
TFuncKey<'translation', 'admin_console.subscription'> | undefined
> = {
quotaKey: undefined,
[ReservedSkuId.Free]: 'free_plan',
[ReservedSkuId.Pro]: 'pro_plan',
[ReservedSkuId.Enterprise]: 'enterprise',
};

type Props = {
/** Temporarily use optional for backward compatibility. */
readonly skuId?: string;
Expand All @@ -22,11 +32,11 @@
readonly name: string;
};

// TODO: rename the component once new pricing model is ready, should be `SkuName`.

Check warning on line 35 in packages/console/src/components/PlanName/index.tsx

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/console/src/components/PlanName/index.tsx#L35

[no-warning-comments] Unexpected 'todo' comment: 'TODO: rename the component once new...'.
function PlanName({ skuId, name }: Props) {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console.subscription' });
const planNamePhrase =
conditional(skuId && registeredPlanNamePhraseMap[skuId]) ?? registeredPlanNamePhraseMap[name];
conditional(skuId && registeredSkuIdNamePhraseMap[skuId]) ?? registeredPlanNamePhraseMap[name];

/**
* Note: fallback to the plan name if the phrase is not registered.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import PlanName from '@/components/PlanName';
import QuotaGuardFooter from '@/components/QuotaGuardFooter';
import { isDevFeaturesEnabled } from '@/consts/env';
import { SubscriptionDataContext } from '@/contexts/SubscriptionDataProvider';
import { TenantsContext } from '@/contexts/TenantsProvider';
import Button from '@/ds-components/Button';
import FormField from '@/ds-components/FormField';
import ModalLayout from '@/ds-components/ModalLayout';
import TextInput from '@/ds-components/TextInput';
import useApi from '@/hooks/use-api';
import useNewSubscriptionScopeUsage from '@/hooks/use-new-subscription-scopes-usage';
import modalStyles from '@/scss/modal.module.scss';
import { trySubmitSafe } from '@/utils/form';
import { hasReachedQuotaLimit, hasReachedSubscriptionQuotaLimit } from '@/utils/quota';
Expand All @@ -35,6 +37,10 @@ function CreatePermissionModal({ resourceId, totalResourceCount, onClose }: Prop
currentSubscriptionQuota,
currentSubscriptionScopeResourceUsage,
} = useContext(SubscriptionDataContext);
const { currentTenantId } = useContext(TenantsContext);
const {
scopeResourceUsage: { mutate: mutateScopeResourceUsage },
} = useNewSubscriptionScopeUsage(currentTenantId);
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });

const {
Expand All @@ -55,6 +61,7 @@ function CreatePermissionModal({ resourceId, totalResourceCount, onClose }: Prop
.post(`api/resources/${resourceId}/scopes`, { json: formData })
.json<Scope>();

void mutateScopeResourceUsage();
onClose(createdScope);
})
);
Expand Down
9 changes: 8 additions & 1 deletion packages/console/src/pages/ApiResourceDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Resource } from '@logto/schemas';
import { isManagementApi, Theme } from '@logto/schemas';
import { conditionalArray } from '@silverhand/essentials';
import classNames from 'classnames';
import { useEffect, useState } from 'react';
import { useEffect, useState, useContext } from 'react';
import { toast } from 'react-hot-toast';
import { Trans, useTranslation } from 'react-i18next';
import { Outlet, useLocation, useParams } from 'react-router-dom';
Expand All @@ -19,11 +19,13 @@ import DetailsPageHeader, { type MenuItem } from '@/components/DetailsPage/Detai
import Drawer from '@/components/Drawer';
import PageMeta from '@/components/PageMeta';
import { ApiResourceDetailsTabs } from '@/consts/page-tabs';
import { TenantsContext } from '@/contexts/TenantsProvider';
import DeleteConfirmModal from '@/ds-components/DeleteConfirmModal';
import TabNav, { TabNavItem } from '@/ds-components/TabNav';
import type { RequestError } from '@/hooks/use-api';
import useApi from '@/hooks/use-api';
import useDocumentationUrl from '@/hooks/use-documentation-url';
import useNewSubscriptionScopeUsage from '@/hooks/use-new-subscription-scopes-usage';
import useTenantPathname from '@/hooks/use-tenant-pathname';
import useTheme from '@/hooks/use-theme';

Expand All @@ -41,6 +43,10 @@ const icons = {
function ApiResourceDetails() {
const { pathname } = useLocation();
const { id, guideId } = useParams();
const { currentTenantId } = useContext(TenantsContext);
const {
scopeResourceUsage: { mutate: mutateScopeResourceUsage },
} = useNewSubscriptionScopeUsage(currentTenantId);
const { navigate, match } = useTenantPathname();
const { getDocumentationUrl } = useDocumentationUrl();
const isGuideView = !!id && !!guideId && match(`/api-resources/${id}/guide/${guideId}`);
Expand Down Expand Up @@ -75,6 +81,7 @@ function ApiResourceDetails() {

try {
await api.delete(`api/resources/${data.id}`);
void mutateScopeResourceUsage();
toast.success(t('api_resource_details.api_resource_deleted', { name: data.name }));
navigate(`/api-resources`);
} finally {
Expand Down
7 changes: 7 additions & 0 deletions packages/console/src/types/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
Enterprise = 'Enterprise',
}

// TODO: use `ReservedPlanId` in the future.

Check warning on line 13 in packages/console/src/types/subscriptions.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/console/src/types/subscriptions.ts#L13

[no-warning-comments] Unexpected 'todo' comment: 'TODO: use `ReservedPlanId` in the...'.
export enum ReservedSkuId {
Free = 'free',
Pro = 'pro',
Enterprise = 'enterprise',
}

export type SubscriptionPlanQuota = Omit<
SubscriptionPlanResponse['quota'],
'builtInEmailConnectorEnabled'
Expand Down
Loading