0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor(console): improve onboarding data and subscription fetching (#5960)

This commit is contained in:
Gao Sun 2024-05-31 19:21:47 +08:00 committed by GitHub
parent e989f08065
commit 11f05ceb1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 11 additions and 36 deletions

View file

@ -34,12 +34,6 @@ const reservedRoutes: Readonly<string[]> = Object.freeze([
...Object.values(GlobalRoute),
]);
/**
* The current tenant status of access validation. When it's `validated`, it indicates that a
* valid Access Token for the current tenant is available.
*/
type CurrentTenantStatus = 'pending' | 'validating' | 'validated';
/** @see {@link TenantsProvider} for why `useSWR()` is not applicable for this context. */
type Tenants = {
tenants: readonly TenantResponse[];

View file

@ -6,10 +6,13 @@ import { isCloud } from '@/consts/env';
const useSubscription = (tenantId: string) => {
const cloudApi = useCloudApi();
return useSWR<Subscription, Error>(isCloud && `/api/tenants/${tenantId}/subscription`, async () =>
cloudApi.get('/api/tenants/:tenantId/subscription', {
params: { tenantId },
})
return useSWR<Subscription, Error>(
// `tenantId` could be an empty string which may cause the request to fail
isCloud && tenantId && `/api/tenants/${tenantId}/subscription`,
async () =>
cloudApi.get('/api/tenants/:tenantId/subscription', {
params: { tenantId },
})
);
};

View file

@ -1,15 +1,12 @@
import {
adminTenantId,
Project,
type UserOnboardingData,
userOnboardingDataGuard,
userOnboardingDataKey,
} from '@logto/schemas';
import { useCallback, useContext, useMemo } from 'react';
import { useCallback, useMemo } from 'react';
import { z } from 'zod';
import { isCloud } from '@/consts/env';
import { TenantsContext } from '@/contexts/TenantsProvider';
import useCurrentUser from '@/hooks/use-current-user';
const useUserOnboardingData = (): {
@ -18,11 +15,9 @@ const useUserOnboardingData = (): {
isLoading: boolean;
isLoaded: boolean;
isOnboarding: boolean;
isBusinessPlan: boolean;
update: (data: Partial<UserOnboardingData>) => Promise<void>;
} => {
const { customData, error, isLoading, isLoaded, updateCustomData } = useCurrentUser();
const { currentTenantId } = useContext(TenantsContext);
const userOnboardingData = useMemo(() => {
const parsed = z
@ -37,24 +32,8 @@ const useUserOnboardingData = (): {
return false;
}
if (currentTenantId === adminTenantId) {
return false;
}
return !userOnboardingData.isOnboardingDone;
}, [currentTenantId, userOnboardingData.isOnboardingDone]);
const isBusinessPlan = useMemo(() => {
if (!isCloud) {
return false;
}
if (currentTenantId === adminTenantId) {
return true;
}
return userOnboardingData.questionnaire?.project === Project.Company;
}, [currentTenantId, userOnboardingData.questionnaire?.project]);
}, [userOnboardingData.isOnboardingDone]);
const update = useCallback(
async (data: Partial<UserOnboardingData>) => {
@ -74,7 +53,6 @@ const useUserOnboardingData = (): {
isLoading,
isLoaded,
isOnboarding,
isBusinessPlan,
update,
};
};

View file

@ -3,6 +3,7 @@ import { conditional } from '@silverhand/essentials';
import { useEffect } from 'react';
import { Controller, useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import Case from '@/assets/icons/case.svg';
import ActionBar from '@/components/ActionBar';
@ -11,7 +12,6 @@ import Button from '@/ds-components/Button';
import FormField from '@/ds-components/FormField';
import OverlayScrollbar from '@/ds-components/OverlayScrollbar';
import TextInput from '@/ds-components/TextInput';
import useTenantPathname from '@/hooks/use-tenant-pathname';
import useUserOnboardingData from '@/onboarding/hooks/use-user-onboarding-data';
import * as pageLayout from '@/onboarding/scss/layout.module.scss';
import { trySubmitSafe } from '@/utils/form';
@ -25,7 +25,7 @@ import { stageOptions, additionalFeaturesOptions, projectOptions } from './optio
function Welcome() {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const { navigate } = useTenantPathname();
const navigate = useNavigate();
const {
data: { questionnaire },