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

refactor: [M3-7437] - Use @lukemorales/query-key-factory for Profile Queries #10241

Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,5 @@
---
"@linode/manager": Tech Stories
---

Use `@lukemorales/query-key-factory` for Profile Queries ([#10241](https://github.com/linode/manager/pull/10241))
1 change: 1 addition & 0 deletions packages/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@emotion/styled": "^11.11.0",
"@linode/api-v4": "*",
"@linode/validation": "*",
"@lukemorales/query-key-factory": "^1.3.4",
"@mui/icons-material": "^5.14.7",
"@mui/material": "^5.14.7",
"@paypal/react-paypal-js": "^7.8.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { LinkButton } from 'src/components/LinkButton';
import { TextField } from 'src/components/TextField';
import { Typography } from 'src/components/Typography';
import {
queryKey,
profileQueries,
updateProfileData,
useProfile,
useSendPhoneVerificationCodeMutation,
Expand Down Expand Up @@ -98,7 +98,7 @@ export const PhoneVerification = ({
);
} else {
// Cloud Manager does not know about the country, so lets refetch the user's phone number so we know it's displaying correctly
queryClient.invalidateQueries([queryKey]);
queryClient.invalidateQueries(profileQueries.profile().queryKey);
}

// reset form states
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useQueryClient } from '@tanstack/react-query';
import { StyledLinkButton } from 'src/components/Button/StyledLinkButton';
import { Notice } from 'src/components/Notice/Notice';
import { Typography } from 'src/components/Typography';
import { queryKey } from 'src/queries/profile';
import { useSecurityQuestions } from 'src/queries/securityQuestions';
import { getAPIErrorOrDefault } from 'src/utilities/errorUtils';
import { getAPIErrorFor } from 'src/utilities/getAPIErrorFor';
Expand All @@ -20,6 +19,7 @@ import {
StyledRootContainer,
} from './TwoFactor.styles';
import { TwoFactorToggle } from './TwoFactorToggle';
import { profileQueries } from 'src/queries/profile';

export interface TwoFactorProps {
disabled?: boolean;
Expand Down Expand Up @@ -61,7 +61,7 @@ export const TwoFactor = (props: TwoFactorProps) => {
*/
const handleEnableSuccess = (scratchCode: string) => {
// Refetch Profile with React Query so profile is up to date
queryClient.invalidateQueries([queryKey]);
queryClient.invalidateQueries(profileQueries.profile().queryKey);
setSuccess('Two-factor authentication has been enabled.');
setShowQRCode(false);
setTwoFactorEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import {
withQueryClient,
} from 'src/containers/withQueryClient.container';
import { StackScriptForm } from 'src/features/StackScripts/StackScriptForm/StackScriptForm';
import { queryKey } from 'src/queries/profile';
import { filterImagesByType } from 'src/store/image/image.helpers';
import { getAPIErrorFor } from 'src/utilities/getAPIErrorFor';
import { scrollErrorIntoView } from 'src/utilities/scrollErrorIntoView';
import { storage } from 'src/utilities/storage';
import { profileQueries } from 'src/queries/profile';

interface State {
apiResponse?: StackScript;
Expand Down Expand Up @@ -359,7 +359,7 @@ export class StackScriptCreate extends React.Component<CombinedProps, State> {
return;
}
if (profile.data?.restricted) {
queryClient.invalidateQueries([queryKey, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
}
this.setState({ isSubmitting: false });
this.resetAllFields();
Expand Down
10 changes: 4 additions & 6 deletions packages/manager/src/hooks/useInitialRequests.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { getAccountInfo, getAccountSettings } from '@linode/api-v4/lib/account';
import { getProfile, getUserPreferences } from '@linode/api-v4/lib/profile';
import * as React from 'react';
import { getUserPreferences } from '@linode/api-v4/lib/profile';
import { useQueryClient } from '@tanstack/react-query';
import * as React from 'react';

import { useAuthentication } from 'src/hooks/useAuthentication';
import { usePendingUpload } from 'src/hooks/usePendingUpload';
import { queryKey as accountQueryKey } from 'src/queries/account';
import { profileQueries } from 'src/queries/profile';
import { redirectToLogin } from 'src/session';

/**
Expand Down Expand Up @@ -66,10 +67,7 @@ export const useInitialRequests = () => {
}),

// Username and whether a user is restricted
queryClient.prefetchQuery({
queryFn: () => getProfile(),
queryKey: ['profile'],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing two fetches to GET /v4/profile because we were loading data into ['profile'] and that query key isn't used anymore. Because of parent-child, there are now arguments in the profile query key.

}),
queryClient.prefetchQuery(profileQueries.profile()),

// Is a user managed
queryClient.prefetchQuery({
Expand Down
5 changes: 1 addition & 4 deletions packages/manager/src/queries/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import {
QueryClient,
QueryKey,
UseMutationOptions,
UseQueryOptions,
} from '@tanstack/react-query';

// =============================================================================
// Config
// =============================================================================
type QueryConfigTypes = 'longLived' | 'noRetry' | 'oneTimeFetch' | 'shortLived';

export const queryPresets: Record<QueryConfigTypes, UseQueryOptions<any>> = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UseQueryOptions<any> is less type-safe than letting typescript do the inference

export const queryPresets = {
longLived: {
cacheTime: 10 * 60 * 1000,
refetchOnMount: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/manager/src/queries/databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { EventHandlerData } from 'src/hooks/useEventHandlers';
import { getAll } from 'src/utilities/getAll';

import { queryPresets, updateInPaginatedStore } from './base';
import { queryKey as PROFILE_QUERY_KEY } from './profile';
import { profileQueries } from './profile';

export const queryKey = 'databases';

Expand Down Expand Up @@ -116,7 +116,7 @@ export const useCreateDatabaseMutation = () => {
// Add database to the cache
queryClient.setQueryData([queryKey, data.id], data);
// If a restricted user creates an entity, we must make sure grants are up to date.
queryClient.invalidateQueries([PROFILE_QUERY_KEY, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
},
}
);
Expand Down
4 changes: 2 additions & 2 deletions packages/manager/src/queries/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';

import { getAll } from 'src/utilities/getAll';

import { queryKey as PROFILE_QUERY_KEY } from './profile';
import { EventHandlerData } from 'src/hooks/useEventHandlers';
import { profileQueries } from './profile';

export const queryKey = 'domains';

Expand Down Expand Up @@ -57,7 +57,7 @@ export const useCreateDomainMutation = () => {
queryClient.invalidateQueries([queryKey, 'paginated']);
queryClient.setQueryData([queryKey, 'domain', domain.id], domain);
// If a restricted user creates an entity, we must make sure grants are up to date.
queryClient.invalidateQueries([PROFILE_QUERY_KEY, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
},
});
};
Expand Down
4 changes: 2 additions & 2 deletions packages/manager/src/queries/firewalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { queryKey as linodesQueryKey } from 'src/queries/linodes/linodes';
import { getAll } from 'src/utilities/getAll';

import { updateInPaginatedStore } from './base';
import { queryKey as PROFILE_QUERY_KEY } from './profile';
import { profileQueries } from './profile';

export const queryKey = 'firewall';

Expand Down Expand Up @@ -123,7 +123,7 @@ export const useCreateFirewall = () => {
queryClient.invalidateQueries([queryKey, 'paginated']);
queryClient.setQueryData([queryKey, 'firewall', firewall.id], firewall);
// If a restricted user creates an entity, we must make sure grants are up to date.
queryClient.invalidateQueries([PROFILE_QUERY_KEY, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
},
}
);
Expand Down
6 changes: 3 additions & 3 deletions packages/manager/src/queries/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
useQueryClient,
} from '@tanstack/react-query';

import { EventHandlerData } from 'src/hooks/useEventHandlers';
import { getAll } from 'src/utilities/getAll';

import { doesItemExistInPaginatedStore, updateInPaginatedStore } from './base';
import { queryKey as PROFILE_QUERY_KEY } from './profile';
import { EventHandlerData } from 'src/hooks/useEventHandlers';
import { profileQueries } from './profile';

export const queryKey = 'images';

Expand Down Expand Up @@ -55,7 +55,7 @@ export const useCreateImageMutation = () => {
onSuccess() {
queryClient.invalidateQueries([`${queryKey}-list`]);
// If a restricted user creates an entity, we must make sure grants are up to date.
queryClient.invalidateQueries([PROFILE_QUERY_KEY, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
},
}
);
Expand Down
4 changes: 2 additions & 2 deletions packages/manager/src/queries/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { getAll } from 'src/utilities/getAll';

import { queryPresets, updateInPaginatedStore } from './base';
import { queryKey as PROFILE_QUERY_KEY } from './profile';
import { profileQueries } from './profile';

export const queryKey = `kubernetes`;

Expand Down Expand Up @@ -141,7 +141,7 @@ export const useCreateKubernetesClusterMutation = () => {
onSuccess() {
queryClient.invalidateQueries([`${queryKey}-list`]);
// If a restricted user creates an entity, we must make sure grants are up to date.
queryClient.invalidateQueries([PROFILE_QUERY_KEY, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
},
}
);
Expand Down
4 changes: 2 additions & 2 deletions packages/manager/src/queries/linodes/linodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { manuallySetVPCConfigInterfacesToActive } from 'src/utilities/configs';

import { queryKey as accountNotificationsQueryKey } from '../accountNotifications';
import { queryPresets } from '../base';
import { queryKey as PROFILE_QUERY_KEY } from '../profile';
import { profileQueries } from '../profile';
import { getAllLinodeKernelsRequest, getAllLinodesRequest } from './requests';

export const queryKey = 'linodes';
Expand Down Expand Up @@ -159,7 +159,7 @@ export const useCreateLinodeMutation = () => {
linode
);
// If a restricted user creates an entity, we must make sure grants are up to date.
queryClient.invalidateQueries([PROFILE_QUERY_KEY, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
},
});
};
Expand Down
6 changes: 3 additions & 3 deletions packages/manager/src/queries/nodebalancers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import {
Params,
ResourcePage,
} from '@linode/api-v4/lib/types';
import { DateTime } from 'luxon';
import {
useInfiniteQuery,
useMutation,
useQuery,
useQueryClient,
} from '@tanstack/react-query';
import { DateTime } from 'luxon';

import { EventHandlerData } from 'src/hooks/useEventHandlers';
import { queryKey as firewallsQueryKey } from 'src/queries/firewalls';
Expand All @@ -38,7 +38,7 @@ import { getAll } from 'src/utilities/getAll';

import { queryPresets } from './base';
import { itemInListCreationHandler, itemInListMutationHandler } from './base';
import { queryKey as PROFILE_QUERY_KEY } from './profile';
import { profileQueries } from './profile';

export const queryKey = 'nodebalancers';

Expand Down Expand Up @@ -114,7 +114,7 @@ export const useNodebalancerCreateMutation = () => {
queryClient.invalidateQueries([queryKey]);
queryClient.setQueryData([queryKey, 'nodebalancer', data.id], data);
// If a restricted user creates an entity, we must make sure grants are up to date.
queryClient.invalidateQueries([PROFILE_QUERY_KEY, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
},
}
);
Expand Down
4 changes: 2 additions & 2 deletions packages/manager/src/queries/placementGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';

import { getAll } from 'src/utilities/getAll';

import { queryKey as PROFILE_QUERY_KEY } from './profile';
import { profileQueries } from './profile';

import type {
AssignLinodesToPlacementGroupPayload,
Expand Down Expand Up @@ -76,7 +76,7 @@ export const useCreatePlacementGroup = () => {
placementGroup
);
// If a restricted user creates an entity, we must make sure grants are up to date.
queryClient.invalidateQueries([PROFILE_QUERY_KEY, 'grants']);
queryClient.invalidateQueries(profileQueries.grants.queryKey);
},
});
};
Expand Down
Loading
Loading