Skip to content

Commit

Permalink
Repositories: fix lint warning react-hooks/exhaustive-deps with addin…
Browse files Browse the repository at this point in the history
…g useCallback hook

this commit fix the exhaustive-deps by adding the useCallback hook.
after adding the missing dependencies to the useEffect,
I fix this problem  -gcaThe 'initializeRepositories' function makes the
dependencies of useMemo Hook (at line 256) change on every render.
Move it inside the useMemo callback. Alternatively,
wrap the definition of 'initializeRepositories' in its own useCallback() Hook  react-hooks/exhaustive-deps
  • Loading branch information
mgold1234 committed Jan 28, 2024
1 parent c9e124b commit 6e0f4f0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
72 changes: 39 additions & 33 deletions src/Components/CreateImageWizard/formComponents/Repositories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useState } from 'react';
import React, { useCallback, useMemo, useState } from 'react';

import {
useFieldApi,
Expand Down Expand Up @@ -155,42 +155,45 @@ const convertSchemaToContentSources = (repo) => {
};

const Repositories = (props) => {
const initializeRepositories = (contentSourcesReposList) => {
// Convert list of repositories into an object where key is repo URL
const contentSourcesRepos = contentSourcesReposList.reduce(
(accumulator, currentValue) => {
accumulator[currentValue.url] = currentValue;
return accumulator;
},
{}
);
const initializeRepositories = useCallback(
(contentSourcesReposList) => {
// Convert list of repositories into an object where key is repo URL
const contentSourcesRepos = contentSourcesReposList.reduce(
(accumulator, currentValue) => {
accumulator[currentValue.url] = currentValue;
return accumulator;
},
{}
);

// Repositories in the form state can be present when 'Recreate image' is used
// to open the wizard that are not necessarily in content sources.
const formStateReposList =
getState()?.values?.['original-payload-repositories'];
// Repositories in the form state can be present when 'Recreate image' is used
// to open the wizard that are not necessarily in content sources.
const formStateReposList =
getState()?.values?.['original-payload-repositories'];

const mergeRepositories = (contentSourcesRepos, formStateReposList) => {
const formStateRepos = {};
const mergeRepositories = (contentSourcesRepos, formStateReposList) => {
const formStateRepos = {};

for (const repo of formStateReposList) {
formStateRepos[repo.baseurl] = convertSchemaToContentSources(repo);
formStateRepos[repo.baseurl].name = '';
}
for (const repo of formStateReposList) {
formStateRepos[repo.baseurl] = convertSchemaToContentSources(repo);
formStateRepos[repo.baseurl].name = '';
}

// In case of duplicate repo urls, the repo from Content Sources overwrites the
// repo from the form state.
const mergedRepos = { ...formStateRepos, ...contentSourcesRepos };
// In case of duplicate repo urls, the repo from Content Sources overwrites the
// repo from the form state.
const mergedRepos = { ...formStateRepos, ...contentSourcesRepos };

return mergedRepos;
};
return mergedRepos;
};

const repositories = formStateReposList
? mergeRepositories(contentSourcesRepos, formStateReposList)
: contentSourcesRepos;
const repositories = formStateReposList
? mergeRepositories(contentSourcesRepos, formStateReposList)
: contentSourcesRepos;

return repositories;
};
return repositories;
},
[getState]
);

const { getState, change } = useFormApi();
const { input } = useFieldApi(props);
Expand Down Expand Up @@ -254,15 +257,18 @@ const Repositories = (props) => {

const repositories = useMemo(() => {
return data ? initializeRepositories(data.data) : {};
}, [firstRequest.data, followupRequest.data]);
}, [data, initializeRepositories]);

const handleToggleClick = (event) => {
const id = event.currentTarget.id;
setPage(1);
setToggleSelected(id);
};

const isRepoSelected = (repoURL) => selected.includes(repoURL);
const isRepoSelected = useCallback(
(repoURL) => selected.includes(repoURL),
[selected]
);

const handlePerPageSelect = (event, newPerPage, newPage) => {
setPerPage(newPerPage);
Expand Down Expand Up @@ -290,7 +296,7 @@ const Repositories = (props) => {
.filter((repo) => isRepoSelected(repo.url))
.map((repo) => repo.url);
}
}, [filterValue, repositories, toggleSelected]);
}, [filterValue, repositories, toggleSelected, isRepoSelected]);

const handleClearFilter = () => {
setFilterValue('');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useState } from 'react';
import React, { useCallback, useMemo, useState } from 'react';

import {
Alert,
Expand Down Expand Up @@ -213,9 +213,10 @@ const Repositories = () => {
setToggleSelected(id);
};

const isRepoSelected = (repoURL: string | undefined) =>
selected.includes(repoURL);

const isRepoSelected = useCallback(
(repoURL: string | undefined) => selected.includes(repoURL),
[selected]
);
const handlePerPageSelect = (
_: React.MouseEvent,
newPerPage: number,
Expand Down Expand Up @@ -252,7 +253,7 @@ const Repositories = () => {
.filter((repo: ApiRepositoryResponseRead) => isRepoSelected(repo.url!))
.map((repo: ApiRepositoryResponseRead) => repo.url);
}
}, [filterValue, data, toggleSelected]);
}, [filterValue, data, toggleSelected, isRepoSelected]);

const handleClearFilter = () => {
setFilterValue('');
Expand Down

0 comments on commit 6e0f4f0

Please sign in to comment.