Skip to content

Commit

Permalink
fix: indefinite loading on manage licenses screen
Browse files Browse the repository at this point in the history
  • Loading branch information
vikrantgupta25 committed Dec 10, 2024
1 parent dadee01 commit 59a48a0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
9 changes: 1 addition & 8 deletions frontend/src/container/Licenses/ApplyLicenseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import apply from 'api/licenses/apply';
import { useNotifications } from 'hooks/useNotifications';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { QueryObserverResult, RefetchOptions } from 'react-query';
import { ErrorResponse, SuccessResponse } from 'types/api';
import { PayloadProps } from 'types/api/licenses/getAll';
import { requireErrorMessage } from 'utils/form/requireErrorMessage';

import {
Expand Down Expand Up @@ -96,11 +93,7 @@ function ApplyLicenseForm({
}

interface ApplyLicenseFormProps {
licenseRefetch: (
options?: RefetchOptions,
) => Promise<
QueryObserverResult<SuccessResponse<PayloadProps> | ErrorResponse, unknown>
>;
licenseRefetch: () => void;
}

interface FormValues {
Expand Down
17 changes: 5 additions & 12 deletions frontend/src/container/Licenses/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Tabs, Typography } from 'antd';
import { Tabs } from 'antd';
import Spinner from 'components/Spinner';
import useLicense from 'hooks/useLicense';
import { useAppContext } from 'providers/App/App';
import { useTranslation } from 'react-i18next';

Expand All @@ -9,26 +8,20 @@ import ListLicenses from './ListLicenses';

function Licenses(): JSX.Element {
const { t, ready: translationsReady } = useTranslation(['licenses']);
const { isLoggedIn } = useAppContext();
// we need explicit license fetch here and reload so no need to extract from licenses context
const { data, isError, isLoading, refetch } = useLicense(isLoggedIn);
const { licenses, licensesRefetch } = useAppContext();

if (isError || data?.error) {
return <Typography>{data?.error}</Typography>;
}

if (isLoading || data?.payload === undefined || !translationsReady) {
if (!translationsReady) {
return <Spinner tip={t('loading_licenses')} height="90vh" />;
}

const allValidLicense =
data?.payload?.licenses?.filter((license) => license.isCurrent) || [];
licenses?.licenses?.filter((license) => license.isCurrent) || [];

const tabs = [
{
label: t('tab_current_license'),
key: 'licenses',
children: <ApplyLicenseForm licenseRefetch={refetch} />,
children: <ApplyLicenseForm licenseRefetch={licensesRefetch} />,
},
{
label: t('tab_license_history'),
Expand Down
34 changes: 23 additions & 11 deletions frontend/src/providers/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export function AppProvider({ children }: PropsWithChildren): JSX.Element {
data: licenseData,
isFetching: isFetchingLicenses,
error: licensesFetchError,
refetch: licensesRefetch,
} = useLicense(isLoggedIn);
useEffect(() => {
if (!isFetchingLicenses && licenseData && licenseData.payload) {
Expand Down Expand Up @@ -191,6 +192,15 @@ export function AppProvider({ children }: PropsWithChildren): JSX.Element {
...org.slice(orgIndex + 1, org.length),
];
setOrg(updatedOrg);
setUser((prev) => {
if (prev.orgId === orgId) {
return {
...prev,
organization: updatedOrgName,
};
}
return prev;
});
}
},
[org],
Expand Down Expand Up @@ -223,25 +233,26 @@ export function AppProvider({ children }: PropsWithChildren): JSX.Element {
// return value for the context
const value: IAppContext = useMemo(
() => ({
activeLicenseV3,
isFetchingActiveLicenseV3,
activeLicenseV3FetchError,
user,
licenses,
activeLicenseV3,
featureFlags,
orgPreferences,
isLoggedIn,
org,
isFetchingUser,
userFetchError,
licenses,
isFetchingLicenses,
licensesFetchError,
featureFlags,
isFetchingActiveLicenseV3,
isFetchingFeatureFlags,
featureFlagsFetchError,
orgPreferences,
isFetchingOrgPreferences,
userFetchError,
licensesFetchError,
activeLicenseV3FetchError,
featureFlagsFetchError,
orgPreferencesFetchError,
isLoggedIn,
setUserFlags,
licensesRefetch,
updateUser,
setUserFlags,
updateOrgPreferences,
updateOrg,
}),
Expand All @@ -258,6 +269,7 @@ export function AppProvider({ children }: PropsWithChildren): JSX.Element {
isLoggedIn,
licenses,
licensesFetchError,
licensesRefetch,
org,
orgPreferences,
orgPreferencesFetchError,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/providers/App/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IAppContext {
activeLicenseV3FetchError: unknown;
featureFlagsFetchError: unknown;
orgPreferencesFetchError: unknown;
licensesRefetch: () => void;
updateUser: (user: IUser) => void;
setUserFlags: (flags: UserFlags) => void;
updateOrgPreferences: (orgPreferences: OrgPreference[]) => void;
Expand Down

0 comments on commit 59a48a0

Please sign in to comment.