Skip to content

Commit

Permalink
chore: refactor import warning messages into dict of uris
Browse files Browse the repository at this point in the history
Signed-off-by: David Wallace <[email protected]>
  • Loading branch information
MyPyDavid committed Jan 29, 2024
1 parent 38996b7 commit c35469e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const ImportElement = ({ config, element, importActions }) => {
element.show && <>
<Form config={config} element={element} updateElement={updateElement} />
<Fields element={element} />
<Warnings element={element} />
<Warnings element={element} success={false}/>
<Errors element={element} />
</>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import uniqueId from 'lodash/uniqueId'

import { codeClass, verboseNames } from '../../constants/elements'
import { isEmpty } from 'lodash'
import Warnings from './common/Warnings'

const ImportSuccessElement = ({ element }) => {
return (
Expand All @@ -23,7 +24,7 @@ const ImportSuccessElement = ({ element }) => {
}
{'.'}
</p>
{element.warnings.map(message => <p key={uniqueId()} className="text-warning">{message}</p>)}
<Warnings element={element} success={true}/>
{element.errors.map(message => <p key={uniqueId()} className="text-danger">{message}</p>)}
</li>
)
Expand Down
27 changes: 22 additions & 5 deletions rdmo/management/assets/js/components/import/common/Warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@ import React from 'react'
import PropTypes from 'prop-types'
import isEmpty from 'lodash/isEmpty'
import uniqueId from 'lodash/uniqueId'
import {codeClass} from '../../../constants/elements'

const Warnings = ({ element }) => {
return !isEmpty(element.warnings) && <div className="row text-warning mt-10">
<div className="col-sm-3 text-right">
const Warnings = ({ element, success = false }) => {
return !isEmpty(element.warnings) && <div className="row mt-10">
{ (success === true) &&
<div className="col-sm-3 text-warning text-right">
{gettext('Warnings')}
</div>
}
<div className="col-sm-9">
<ul className="list-unstyled">
{
element.warnings.map(message => <li key={uniqueId('error-')}>{message}</li>)
Object.entries(element.warnings).map(([uri, messages]) => {
return (
<li key={uniqueId('warning-uri-')}><code className={codeClass['domain.attribute']}>{uri}</code>
<div className="col-sm-9">
<ul className="list-unstyled">
{
messages.map(message => (
<li className="text-warning" key={uniqueId('warning-uri-message')}>{message}</li>))
}
</ul>
</div>
</li>
)
})
}
</ul>
</div>
</div>
}

Warnings.propTypes = {
element: PropTypes.object.isRequired
element: PropTypes.object.isRequired,
success: PropTypes.bool.isRequired
}

export default Warnings
14 changes: 12 additions & 2 deletions rdmo/management/assets/js/components/main/Import.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ const Import = ({ config, imports, configActions, importActions }) => {
const updatedAndChangedElements = elements.filter(element => element.updated && !isEmpty(element.updated_and_changed))
// const updatedAndSameElements = elements.filter(element => element.updated && isEmpty(element.updated_and_changed))
const importErrors = elements.filter(element => !isEmpty(element.errors))
// const importWarnings = elements.filter(element => !isEmpty(element.warnings))
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 selectFilterChanged = () => {
const configBool = get(config, 'filter.import.elements.changed', false)
if (configBool === true && updatedAndChangedElements.length === 0) {
return false
}
else {
return configBool
}
}

const filterByChanged = (elements, selectFilterChanged) => {
if (selectFilterChanged) {
if (selectFilterChanged === true) {
return updatedAndChangedElements
} else {
return elements
Expand Down
2 changes: 1 addition & 1 deletion rdmo/management/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def import_elements(uploaded_elements: List[Dict], save: bool = True, request: O
element = import_element(element=uploaded_element, save=save, uploaded_uris=uploaded_uris,
request=request, current_site=current_site,
questions_widget_types=questions_widget_types)
element['warnings'] = [val for k, val in element['warnings'].items() if k not in uploaded_uris]
element['warnings'] = {k: val for k, val in element['warnings'].items() if k not in uploaded_uris}
imported_elements.append(element)
return imported_elements

Expand Down

0 comments on commit c35469e

Please sign in to comment.