Skip to content

Commit

Permalink
Fix species deletion from species manager table (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
azangru authored Jul 10, 2024
1 parent e865fc6 commit 4f8934f
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { useState, type FormEvent } from 'react';
import { useState, useEffect, useRef, type FormEvent } from 'react';

import { useAppSelector } from 'src/store';

Expand All @@ -25,15 +25,31 @@ import type { CommittedItem } from 'src/content/app/species-selector/types/commi
const useFilteredGenomes = () => {
const selectedGenomes = useAppSelector(getCommittedSpecies);
const [filteredGenomes, setFilteredGenomes] = useState(selectedGenomes);
const filterStringRef = useRef('');

// If the list of selected genomes changes (e.g. a species has been deleted),
// this should be reflected in the list of filtered genomes
useEffect(() => {
const filterString = filterStringRef.current;
const filteredGenomes = applyFilter(selectedGenomes, filterString);
setFilteredGenomes(filteredGenomes);
}, [selectedGenomes]);

const onFilterChange = (event: FormEvent<HTMLInputElement>) => {
const filterString = event.currentTarget.value;
const filteredGenomes = selectedGenomes.filter((genome) => {
return doesGenomeMatchQuery(genome, filterString);
});
filterStringRef.current = filterString;

const filteredGenomes = applyFilter(selectedGenomes, filterString);

setFilteredGenomes(filteredGenomes);
};

const applyFilter = (genomes: CommittedItem[], filter: string) => {
return genomes.filter((genome) => {
return doesGenomeMatchQuery(genome, filter);
});
};

return {
allSelectedGenomes: selectedGenomes,
filteredGenomes,
Expand Down

0 comments on commit 4f8934f

Please sign in to comment.