From 1ea58fa1bd3d7b66f7a568f3fa0145885eddbf58 Mon Sep 17 00:00:00 2001 From: David Wallace Date: Wed, 7 Feb 2024 18:53:53 +0100 Subject: [PATCH] js: draft add ImportFilters.js component Signed-off-by: David Wallace --- .../components/import/common/ImportFilters.js | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 rdmo/management/assets/js/components/import/common/ImportFilters.js diff --git a/rdmo/management/assets/js/components/import/common/ImportFilters.js b/rdmo/management/assets/js/components/import/common/ImportFilters.js new file mode 100644 index 0000000000..4bbcdde7ee --- /dev/null +++ b/rdmo/management/assets/js/components/import/common/ImportFilters.js @@ -0,0 +1,87 @@ +import React from 'react' +import PropTypes from 'prop-types' +import {FilterString, FilterUriPrefix} from '../../common/Filter' +import get from 'lodash/get' +import {getUriPrefixes} from '../../../utils/filter' +import {Checkbox} from '../../common/Checkboxes' + +const ImportFilters = ({ config, elements, updatedAndChanged, configActions }) => { + + const updateFilterString = (value) => configActions.updateConfig('filter.import.elements.search', value) + const updateFilterUriPrefix = (value) => configActions.updateConfig('filter.import.elements.uri_prefix', value) + const updateFilterChanged = (value) => configActions.updateConfig('filter.import.elements.changed', value) + + const searchString = get(config, 'filter.import.elements.search', '') + const selectedUriPrefix = get(config, 'filter.import.elements.uri_prefix', '') + const selectFilterChanged = get(config, 'filter.import.elements.changed', false) + + const filterByChanged = (elements, selectFilterChanged) => { + if (selectFilterChanged === true && updatedAndChanged.length > 0) { + return updatedAndChanged + } else { + return elements + }} + const filterByUriSearch = (elements, searchString) => { + if (searchString) { + const lowercaseSearch = searchString.toLowerCase() + return elements.filter((element) => + element.uri.toLowerCase().includes(lowercaseSearch) + // || element.title.toLowerCase().includes(lowercaseSearch) + ) + } else { + return elements + } + } + const filterByUriPrefix = (elements, searchUriPrefix) => { + if (searchUriPrefix) { + return elements.filter((element) => + element.uri_prefix.toLowerCase().includes(searchUriPrefix) + // || element.title.toLowerCase().includes(lowercaseSearch) + ) + } else { + return elements + } + } + const filteredElements = filterByUriSearch( + filterByUriPrefix( + filterByChanged(elements, selectFilterChanged), + selectedUriPrefix), + searchString) + + + return ( +
+
+
+ +
+
+ +
+
+
+ { + updatedAndChanged.length > 0 &&
+ {gettext('Changed:')} + {gettext('Filter changed')}} + value={get(config, 'filter.import.elements.changed', false)} onChange={updateFilterChanged}/> +
+ } + {filteredElements.length} +
+
+ +) +} + +ImportFilters.propTypes = { + config: PropTypes.object.isRequired, + elements: PropTypes.object.isRequired, + updatedAndChanged: PropTypes.array, + configActions: PropTypes.object.isRequired, +} + +export default ImportFilters