Skip to content

Commit

Permalink
Fix react-query lint errors (and some warnings)
Browse files Browse the repository at this point in the history
Fix the reported errors for the eslint plugins:

  - `"@tanstack/query/prefer-query-object-syntax`: fix the `useQuery()`
    to use the query object syntax throughout the code base.

  - `@tanstack/query/exhaustive-deps`:

    - Fix the `queryKey` params to include all of the variables used
      in the `queryFn`.  This is similar behavior to the `useMemo()`
      lint rule.

    - Where a `params` prop was being transformed to a string in the
      `queryKey`, the transform was removed.  The bare `params` object
      is allowed to be used as part of the queryKey.

Signed-off-by: Scott J Dickerson <[email protected]>
  • Loading branch information
sjd78 committed Oct 13, 2023
1 parent b9b7b33 commit 506dac3
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 223 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { useTranslation } from "react-i18next";
import { AxiosError, AxiosResponse } from "axios";
import { AxiosError } from "axios";
import { object, string } from "yup";

import {
Expand Down Expand Up @@ -70,11 +70,11 @@ export const JobFunctionForm: React.FC<JobFunctionFormProps> = ({
mode: "all",
});

const onCreateJobFunctionSuccess = (response: AxiosResponse<JobFunction>) => {
const onCreateJobFunctionSuccess = (data: JobFunction) => {
pushNotification({
title: t("toastr.success.createWhat", {
type: t("terms.jobFunction"),
what: response.data.name,
what: data.name,
}),
variant: "success",
});
Expand All @@ -91,7 +91,7 @@ export const JobFunctionForm: React.FC<JobFunctionFormProps> = ({
onClose();
};

const onCreateJobFunctionError = (error: AxiosError) => {
const onCreateJobFunctionError = (_error: AxiosError) => {
pushNotification({
title: t("toastr.fail.create", {
type: t("terms.jobFunction").toLowerCase(),
Expand All @@ -105,7 +105,7 @@ export const JobFunctionForm: React.FC<JobFunctionFormProps> = ({
onCreateJobFunctionError
);

const onUpdateJobFunctionError = (error: AxiosError) => {
const onUpdateJobFunctionError = (_error: AxiosError) => {
pushNotification({
title: t("toastr.fail.save", {
type: t("terms.jobFunction").toLowerCase(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,17 @@ export const AdoptionCandidateGraph: React.FC = () => {
refetch: refreshChart,
isFetching,
error: fetchError,
} = useQuery<AssessmentConfidence[]>(
["assessmentconfidence"],
async () =>
} = useQuery<AssessmentConfidence[]>({
queryKey: ["assessmentConfidence"],
queryFn: async () =>
// (
// await getAssessmentConfidence(
// applications.length > 0 ? applications.map((f) => f.id!) : []
// )
// ).data,
[],
{
onError: (error) => console.log("error, ", error),
}
);
onError: (error) => console.log("error, ", error),
});

useEffect(() => {
refreshChart();
Expand All @@ -164,6 +162,8 @@ export const AdoptionCandidateGraph: React.FC = () => {
const appConfidence = confidences.find(
(elem) => elem.applicationId === current.id
);

// TODO: This hook should be pulled outside of the useMemo
const { review: appReview } = useFetchReviewById(current?.review?.id);

if (appConfidence && appReview) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { RISK_LIST } from "@app/Constants";
import { Application, Assessment, Ref, Risk } from "@app/api/models";

import { useFetchReviews } from "@app/queries/reviews";
import { AppTableWithControls } from "@app/components/AppTableWithControls";
import {
FilterCategory,
Expand Down Expand Up @@ -54,7 +53,6 @@ export const AdoptionCandidateTable: React.FC<IAdoptionCandidateTable> = () => {
// Table data
// const { reviews } = useFetchReviews();
const { assessments } = useFetchAssessments();
const { reviews } = useFetchReviews();

const allRows = useMemo(() => {
return assessments.map((assessment: Assessment) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useContext, useEffect, useMemo } from "react";
import React, { useContext, useEffect, useMemo } from "react";
import Measure from "react-measure";
import { useTranslation } from "react-i18next";

Expand Down Expand Up @@ -41,18 +41,16 @@ export const AdoptionPlan: React.FC = () => {
refetch: refreshChart,
error: fetchError,
isFetching,
} = useQuery<ApplicationAdoptionPlan[]>(
["adoptionplan"],
async () =>
} = useQuery<ApplicationAdoptionPlan[]>({
queryKey: ["adoptionPlan", applications.length],
queryFn: async () =>
(
await getApplicationAdoptionPlan(
applications.length > 0 ? applications.map((f) => f.id!) : []
)
).data,
{
onError: (error) => console.log("error, ", error),
}
);
onError: (error) => console.log("error, ", error),
});

useEffect(() => {
refreshChart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,17 @@ export const IdentifiedRisksTable: React.FC<
refetch: refreshTable,
error: fetchError,
isFetching,
} = useQuery<AssessmentQuestionRisk[]>(
["assessmentquestionrisks"],
async () =>
} = useQuery<AssessmentQuestionRisk[]>({
queryKey: ["assessmentQuestionRisks"],
queryFn: async () =>
// (
// await getAssessmentIdentifiedRisks(
// allApplications.length > 0 ? allApplications.map((f) => f.id!) : []
// )
// ).data,
[],
{
onError: (error) => console.log("error, ", error),
}
);
onError: (error) => console.log("error, ", error),
});

const tableData: ITableRowData[] = useMemo(() => {
return (assessmentQuestionRisks || []).map((risk) => ({
Expand Down Expand Up @@ -160,8 +158,10 @@ export const IdentifiedRisksTable: React.FC<
filterCategories
);

const { currentPageItems, setPageNumber, paginationProps } =
useLegacyPaginationState(filteredItems, 10);
const { currentPageItems, paginationProps } = useLegacyPaginationState(
filteredItems,
10
);

const rows: IRow[] = [];
currentPageItems.forEach((item) => {
Expand Down
51 changes: 25 additions & 26 deletions client/src/app/queries/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ export const useDeleteApplicationMutation = (
onError: (err: AxiosError) => void
) => {
const queryClient = useQueryClient();
return useMutation(({ id }: { id: number }) => deleteApplication(id), {
onSuccess: (res) => {
return useMutation({
mutationFn: ({ id }: { id: number }) => deleteApplication(id),
onSuccess: (_res) => {

Check warning on line 122 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L120-L122

Added lines #L120 - L122 were not covered by tests
onSuccess(1);
queryClient.invalidateQueries([ApplicationsQueryKey]);
},
Expand All @@ -131,16 +132,14 @@ export const useBulkDeleteApplicationMutation = (
onError: (err: AxiosError) => void
) => {
const queryClient = useQueryClient();
return useMutation(
({ ids }: { ids: number[] }) => deleteBulkApplications(ids),
{
onSuccess: (res, vars) => {
onSuccess(vars.ids.length);
queryClient.invalidateQueries([ApplicationsQueryKey]);
},
onError: onError,
}
);
return useMutation({
mutationFn: ({ ids }: { ids: number[] }) => deleteBulkApplications(ids),
onSuccess: (res, vars) => {
onSuccess(vars.ids.length);
queryClient.invalidateQueries([ApplicationsQueryKey]);

Check warning on line 139 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L135-L139

Added lines #L135 - L139 were not covered by tests
},
onError: onError,
});
};

export const downloadStaticReport = async ({
Expand Down Expand Up @@ -182,7 +181,7 @@ export const downloadStaticReport = async ({
};

export const useDownloadStaticReport = () => {
return useMutation(downloadStaticReport);
return useMutation({ mutationFn: downloadStaticReport });

Check warning on line 184 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L184

Added line #L184 was not covered by tests
};

export const useFetchApplicationDependencies = (
Expand All @@ -193,23 +192,21 @@ export const useFetchApplicationDependencies = (
error: northError,
isLoading: isLoadingNorth,
refetch: refetchNorth,
} = useQuery<ApplicationDependency[], AxiosError>(
[ApplicationDependencyQueryKey, "north"],
() => getApplicationDependencies({ to: [`${applicationId}`] }),
{
enabled: !!applicationId,
}
);
} = useQuery<ApplicationDependency[], AxiosError>({

Check warning on line 195 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L195

Added line #L195 was not covered by tests
queryKey: [ApplicationDependencyQueryKey, applicationId, "north"],
queryFn: () => getApplicationDependencies({ to: [`${applicationId}`] }),

Check warning on line 197 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L197

Added line #L197 was not covered by tests
enabled: !!applicationId,
});

const {
data: southData,
error: southError,
isLoading: isLoadingSouth,
refetch: refetchSouth,
} = useQuery<ApplicationDependency[], AxiosError>(
[ApplicationDependencyQueryKey, "south"],
() => getApplicationDependencies({ from: [`${applicationId}`] })
);
} = useQuery<ApplicationDependency[], AxiosError>({

Check warning on line 206 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L206

Added line #L206 was not covered by tests
queryKey: [ApplicationDependencyQueryKey, applicationId, "south"],
queryFn: () => getApplicationDependencies({ from: [`${applicationId}`] }),

Check warning on line 208 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L208

Added line #L208 was not covered by tests
});

const isFetching = isLoadingNorth || isLoadingSouth;
const fetchError = northError || southError;
Expand Down Expand Up @@ -241,7 +238,8 @@ export const useCreateApplicationDependency = ({
}: UseCreateApplicationDependencyOptions = {}) => {
const queryClient = useQueryClient();

return useMutation(createApplicationDependency, {
return useMutation({

Check warning on line 241 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L241

Added line #L241 was not covered by tests
mutationFn: createApplicationDependency,
onSuccess: () => {
queryClient.invalidateQueries([ApplicationDependencyQueryKey]);
if (onSuccess) {
Expand All @@ -259,7 +257,8 @@ export const useCreateApplicationDependency = ({
export const useDeleteApplicationDependency = () => {
const queryClient = useQueryClient();

return useMutation(deleteApplicationDependency, {
return useMutation({

Check warning on line 260 in client/src/app/queries/applications.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/applications.ts#L260

Added line #L260 was not covered by tests
mutationFn: deleteApplicationDependency,
onSuccess: () => {
queryClient.invalidateQueries([ApplicationDependencyQueryKey]);
},
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/queries/assessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ export const useFetchAssessmentsByItemId = (
itemId?: number | string
) => {
const { data, isLoading, error } = useQuery({
queryKey: [assessmentsByItemIdQueryKey, itemId],
queryKey: [assessmentsByItemIdQueryKey, itemId, isArchetype],
queryFn: () => getAssessmentsByItemId(isArchetype, itemId),
onError: (error: AxiosError) => console.log("error, ", error),
onSuccess: (data) => {},
onSuccess: (_data) => {},

Check warning on line 135 in client/src/app/queries/assessments.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/assessments.ts#L135

Added line #L135 was not covered by tests
enabled: !!itemId,
});

Expand Down
13 changes: 3 additions & 10 deletions client/src/app/queries/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
HubRequestParams,
} from "@app/api/models";
import { getAppDependencies, getDependencies } from "@app/api/rest";
import { serializeRequestParamsForHub } from "@app/hooks/table-controls/getHubRequestParams";

export interface IDependenciesFetchState {
result: HubPaginatedResult<AnalysisDependency>;
Expand All @@ -22,16 +21,13 @@ export interface IAppDependenciesFetchState {
}

export const DependenciesQueryKey = "dependencies";
export const AppDependenciesQueryKey = "appdependencies";
export const AppDependenciesQueryKey = "appDependencies";

export const useFetchDependencies = (
params: HubRequestParams = {}
): IDependenciesFetchState => {
const { data, isLoading, error, refetch } = useQuery({
queryKey: [
DependenciesQueryKey,
serializeRequestParamsForHub(params).toString(),
],
queryKey: [DependenciesQueryKey, params],
queryFn: async () => await getDependencies(params),
onError: (error) => console.log("error, ", error),
keepPreviousData: true,
Expand All @@ -48,10 +44,7 @@ export const useFetchAppDependencies = (
params: HubRequestParams = {}
): IAppDependenciesFetchState => {
const { data, isLoading, error, refetch } = useQuery({
queryKey: [
AppDependenciesQueryKey,
serializeRequestParamsForHub(params).toString(),
],
queryKey: [AppDependenciesQueryKey, params],
queryFn: async () => await getAppDependencies(params),
onError: (error) => console.log("error, ", error),
keepPreviousData: true,
Expand Down
18 changes: 8 additions & 10 deletions client/src/app/queries/facts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import { Fact } from "@app/api/models";
export const FactsQueryKey = "facts";

export const useFetchFacts = (applicationID: number | string | undefined) => {
const { data, isLoading, error, refetch } = useQuery(
[FactsQueryKey, applicationID],
{
queryFn: () => getFacts(applicationID),
enabled: !!applicationID,
onError: (error: AxiosError) => console.log("error, ", error),
select: (facts): Fact[] =>
Object.keys(facts).map((fact) => ({ name: fact, data: facts[fact] })),
}
);
const { data, isLoading, error, refetch } = useQuery({
queryKey: [FactsQueryKey, applicationID],
queryFn: () => getFacts(applicationID),
enabled: !!applicationID,
onError: (error: AxiosError) => console.log("error, ", error),
select: (facts): Fact[] =>
Object.keys(facts).map((fact) => ({ name: fact, data: facts[fact] })),
});

return {
facts: data || [],
Expand Down
18 changes: 9 additions & 9 deletions client/src/app/queries/identities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const useUpdateIdentityMutation = (
onError: (err: AxiosError) => void
) => {
const queryClient = useQueryClient();
const { isLoading, mutate, error } = useMutation(updateIdentity, {
const { isLoading, mutate, error } = useMutation({
mutationFn: updateIdentity,
onSuccess: (res) => {
onSuccess(res);
queryClient.invalidateQueries([IdentitiesQueryKey]);
Expand All @@ -37,7 +38,8 @@ export const useCreateIdentityMutation = (
onError: (err: AxiosError) => void
) => {
const queryClient = useQueryClient();
const { isLoading, mutate, error } = useMutation(createIdentity, {
const { isLoading, mutate, error } = useMutation({
mutationFn: createIdentity,
onSuccess: (res) => {
onSuccess(res);
queryClient.invalidateQueries([IdentitiesQueryKey]);
Expand All @@ -54,13 +56,11 @@ export const useCreateIdentityMutation = (
};

export const useFetchIdentities = () => {
const { data, isLoading, error, refetch } = useQuery(
[IdentitiesQueryKey],
async () => (await getIdentities()).data,
{
onError: (error) => console.log("error, ", error),
}
);
const { data, isLoading, error, refetch } = useQuery({
queryKey: [IdentitiesQueryKey],
queryFn: async () => (await getIdentities()).data,
onError: (error) => console.log("error, ", error),

Check warning on line 62 in client/src/app/queries/identities.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/identities.ts#L62

Added line #L62 was not covered by tests
});
return {
identities: data || [],
isFetching: isLoading,
Expand Down
Loading

0 comments on commit 506dac3

Please sign in to comment.