diff --git a/src/modules/advanced/components/AdvancedFilter/index.js b/src/modules/advanced/components/AdvancedFilter/index.js
index 1a823d79..3551b63c 100644
--- a/src/modules/advanced/components/AdvancedFilter/index.js
+++ b/src/modules/advanced/components/AdvancedFilter/index.js
@@ -5,93 +5,48 @@ import PropTypes from 'prop-types';
import React from 'react';
const getIsCheckboxFilterChecked = ({ advancedFilter }) => {
- const hasActiveFilter = advancedFilter.activeFilters?.length > 0;
+ const { activeFilters, conditions } = advancedFilter;
+ const hasActiveFilters = activeFilters?.length > 0;
- if (!hasActiveFilter && advancedFilter.conditions.default === 'checked') {
- return true;
- }
-
- if (hasActiveFilter && advancedFilter.activeFilters[0] === advancedFilter.conditions.checked) {
- return true;
- }
-
- return false;
+ return (!hasActiveFilters && conditions.default === 'checked')
+ || (hasActiveFilters && activeFilters[0] === conditions.checked);
};
const getDateRangeValue = ({ beginDateQuery, endDateQuery, selectedRange }) => {
- switch (selectedRange) {
- case 'Before':
- if (endDateQuery) {
- return `before ${endDateQuery}`;
- }
- return null;
- case 'After':
- if (beginDateQuery) {
- return `after ${beginDateQuery}`;
- }
- return null;
- case 'Between':
- if (beginDateQuery && endDateQuery) {
- return `${beginDateQuery} to ${endDateQuery}`;
- }
- return null;
- case 'In':
- if (beginDateQuery) {
- return beginDateQuery;
- }
- return null;
- default:
- return null;
- }
+ const dateRanges = {
+ After: beginDateQuery && `after ${beginDateQuery}`,
+ Before: endDateQuery && `before ${endDateQuery}`,
+ Between: (beginDateQuery && endDateQuery) && `${beginDateQuery} to ${endDateQuery}`,
+ In: beginDateQuery
+ };
+
+ return dateRanges[selectedRange] || null;
};
const getStateDateRangeValues = ({ advancedFilter }) => {
- if (advancedFilter.activeFilters?.length > 0) {
- const [filterValue] = advancedFilter.activeFilters;
-
- // Before
- if (filterValue.indexOf('before') !== -1) {
- const values = filterValue.split('before');
-
- return {
- stateEndQuery: values[1],
- stateSelectedRangeOption: 0
- };
- }
-
- // After
- if (filterValue.indexOf('after') !== -1) {
- const values = filterValue.split('after');
-
- return {
- stateBeginQuery: values[1],
- stateSelectedRangeOption: 1
- };
- }
-
- // Between
- if (filterValue.indexOf('to') !== -1) {
- const values = filterValue.split('to');
-
- return {
- stateBeginQuery: values[0],
- stateEndQuery: values[1],
- stateSelectedRangeOption: 2
- };
- }
+ const rangeValues = {
+ stateSelectedRangeOption: 0
+ };
+ const dates = [null, null];
- // In or other
- return {
- stateBeginQuery: filterValue,
- stateSelectedRangeOption: 3
- };
+ if (advancedFilter?.activeFilters?.length) {
+ const ranges = ['before', 'after', 'to'];
+ rangeValues.stateSelectedRangeOption = ranges.length;
+ const [filterValue] = advancedFilter.activeFilters;
+ dates.unshift(...filterValue.match(/\d+/gu));
+ ranges.forEach((range, index) => {
+ if (filterValue.includes(range)) {
+ rangeValues.stateSelectedRangeOption = index;
+ if (range === ranges[0]) {
+ dates.unshift(null);
+ }
+ }
+ });
}
- return {
- stateBeginQuery: '',
- stateEndQuery: '',
- stateSelectedRangeOption: 0
- };
+ [rangeValues.stateBeginQuery, rangeValues.stateEndQuery] = dates;
+
+ return rangeValues;
};
const AdvancedFilter = ({ advancedFilter, changeAdvancedFilter }) => {
diff --git a/src/modules/advanced/components/AdvancedSearchForm/index.js b/src/modules/advanced/components/AdvancedSearchForm/index.js
index e2d1bec0..7382ea46 100644
--- a/src/modules/advanced/components/AdvancedSearchForm/index.js
+++ b/src/modules/advanced/components/AdvancedSearchForm/index.js
@@ -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 }) => {
@@ -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
};
@@ -110,7 +107,7 @@ const AdvancedSearchForm = ({ datastore }) => {
]);
window.scrollTo(0, 0);
}
- }, [navigate, institution, booleanTypes, fieldedSearches, activeFilters, datastore]);
+ }, [navigate, institution, booleanTypes, fieldedSearches, activeFilters(), datastore]);
return (
);
};
diff --git a/src/modules/advanced/components/FieldInput/index.js b/src/modules/advanced/components/FieldInput/index.js
index dd4dba3d..91ba5c65 100644
--- a/src/modules/advanced/components/FieldInput/index.js
+++ b/src/modules/advanced/components/FieldInput/index.js
@@ -5,6 +5,7 @@ import SearchByOptions from '../../../search/components/SearchByOptions';
const FieldInput = ({
activeDatastore,
+ booleanTypes,
changeFieldedSearch,
fieldedSearch,
fieldedSearchIndex,
@@ -19,7 +20,7 @@ const FieldInput = ({
{notFirst && (