Skip to content

Commit

Permalink
Resolving useSelector warning. Adding booleanTypes for FieldInput.
Browse files Browse the repository at this point in the history
  • Loading branch information
erinesullivan committed Oct 15, 2024
1 parent f4f4f37 commit b74e284
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 11 additions & 13 deletions src/modules/advanced/components/AdvancedSearchForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,24 @@ const AdvancedSearchForm = ({ datastore }) => {
const [errors, setErrors] = useState([]);
const dispatch = useDispatch();

const fields = useSelector((state) => {
return state.advanced[datastore.uid].fields;
const { activeFilters: advancedFilters = {}, fieldedSearches, fields } = useSelector((state) => {
return state.advanced[datastore.uid] || {};
});
const booleanTypes = useSelector((state) => {
return state.advanced.booleanTypes;
});
const fieldedSearches = useSelector((state) => {
return state.advanced[datastore.uid].fieldedSearches;
const { booleanTypes } = useSelector((state) => {
return state.advanced;
});
const institution = useSelector((state) => {
return state.institution;
});
const activeFilters = useSelector((state) => {
const currentFilters = state.advanced[datastore.uid].activeFilters || {};
const activeFilters = () => {
const currentFilters = advancedFilters;
Object.keys(currentFilters).forEach((filter) => {
if (!currentFilters[filter]) {
delete currentFilters[filter];
}
});
return currentFilters;
});
};

// Functions wrapped with useCallback to prevent unnecessary re-creation
const changeFieldedSearch = useCallback(({ booleanType, fieldedSearchIndex, query, selectedFieldUid }) => {
Expand Down Expand Up @@ -87,9 +84,9 @@ const AdvancedSearchForm = ({ datastore }) => {
}, [])
.join(' ');

if (query.length > 0 || (Object.keys(activeFilters).length > 0)) {
if (query.length > 0 || (Object.keys(activeFilters()).length > 0)) {
const search = {
filter: { ...activeFilters },
filter: { ...activeFilters() },
query
};

Expand All @@ -110,7 +107,7 @@ const AdvancedSearchForm = ({ datastore }) => {
]);
window.scrollTo(0, 0);
}
}, [navigate, institution, booleanTypes, fieldedSearches, activeFilters, datastore]);
}, [navigate, institution, booleanTypes, fieldedSearches, activeFilters(), datastore]);

return (
<form className='y-spacing' onSubmit={handleSubmit}>
Expand All @@ -132,6 +129,7 @@ const AdvancedSearchForm = ({ datastore }) => {
return (
<FieldInput
key={index}
booleanTypes={booleanTypes}
fieldedSearchIndex={index}
fieldedSearch={fs}
fields={fields}
Expand Down
4 changes: 3 additions & 1 deletion src/modules/advanced/components/FieldInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SearchByOptions from '../../../search/components/SearchByOptions';

const FieldInput = ({
activeDatastore,
booleanTypes,
changeFieldedSearch,
fieldedSearch,
fieldedSearchIndex,
Expand All @@ -19,7 +20,7 @@ const FieldInput = ({
{notFirst && (
<fieldset className='flex__responsive'>
<legend className='visually-hidden'>Boolean operator for field {fieldedSearchIndex} and field {fieldedSearchIndex + 1}</legend>
{['AND', 'OR', 'NOT'].map((option, index) => {
{booleanTypes.map((option, index) => {
return (
<label key={index}>
<input
Expand Down Expand Up @@ -89,6 +90,7 @@ const FieldInput = ({

FieldInput.propTypes = {
activeDatastore: PropTypes.object,
booleanTypes: PropTypes.array,
changeFieldedSearch: PropTypes.func,
fieldedSearch: PropTypes.object,
fieldedSearchIndex: PropTypes.number,
Expand Down

0 comments on commit b74e284

Please sign in to comment.