From 172cc7dbddfc90eae65d0536775e382045a286e3 Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Fri, 25 Feb 2022 18:19:03 +0100 Subject: [PATCH 01/29] feat(data-integrity): add checks-selector --- i18n/en.pot | 13 +- .../FormFields/DataIntegrityChecksField.js | 183 ++++++++++++++++++ .../DataIntegrityChecksField.module.css | 43 ++++ src/components/FormFields/ParameterFields.js | 16 +- src/components/Forms/JobEditForm.js | 2 +- src/components/Store/Store.js | 6 +- src/hooks/jobs/use-submit-job.js | 7 +- .../dataIntegrityChecks.js | 119 ++++++++++++ 8 files changed, 379 insertions(+), 10 deletions(-) create mode 100644 src/components/FormFields/DataIntegrityChecksField.js create mode 100644 src/components/FormFields/DataIntegrityChecksField.module.css create mode 100644 src/services/server-translations/dataIntegrityChecks.js diff --git a/i18n/en.pot b/i18n/en.pot index 6ba116585..6ed698154 100644 --- a/i18n/en.pot +++ b/i18n/en.pot @@ -5,8 +5,8 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" -"POT-Creation-Date: 2021-11-18T16:21:37.799Z\n" -"PO-Revision-Date: 2021-11-18T16:21:37.799Z\n" +"POT-Creation-Date: 2022-02-16T11:35:29.342Z\n" +"PO-Revision-Date: 2022-02-16T11:35:29.342Z\n" msgid "Not authorized" msgstr "Not authorized" @@ -30,6 +30,15 @@ msgstr "Something went wrong whilst creating your job" msgid "CRON Expression" msgstr "CRON Expression" +msgid "Severity" +msgstr "Severity" + +msgid "Checks to run" +msgstr "Checks to run" + +msgid "All checks will be run if none are selected." +msgstr "All checks will be run if none are selected." + msgid "Delay" msgstr "Delay" diff --git a/src/components/FormFields/DataIntegrityChecksField.js b/src/components/FormFields/DataIntegrityChecksField.js new file mode 100644 index 000000000..6caae4439 --- /dev/null +++ b/src/components/FormFields/DataIntegrityChecksField.js @@ -0,0 +1,183 @@ +import React, { useCallback, useRef, useState } from 'react' +import { PropTypes } from '@dhis2/prop-types' +import i18n from '@dhis2/d2-i18n' +import { + FieldGroup, + Radio, + Transfer, + TransferOption, + ReactFinalForm, + InputFieldFF, + Help, +} from '@dhis2/ui' +import cx from 'classnames' +import { hooks } from '../Store' +import { + getCheckName, + severityMap, +} from '../../services/server-translations/dataIntegrityChecks' +import styles from './DataIntegrityChecksField.module.css' + +const { Field, useField } = ReactFinalForm + +const VALIDATOR = value => + value && value.length < 1 + ? i18n.t('Please select checks to run.') + : undefined + +const DataIntegrityChecksField = ({ label, name }) => { + const options = hooks.useParameterOptions('dataIntegrityChecks') + const { + input: { value, onChange }, + } = useField(name) + + const hasValue = value && value.length > 0 + const [runSelected, setRunSelected] = useState(hasValue) + + const translatedOptions = options + .map(option => ({ + ...option, + value: option.name, + label: getCheckName(option.name), + severity: severityMap[option.severity], + })) + .sort((a, b) => a.label.localeCompare(b.label)) + + const toggle = () => { + if (!runSelected) { + // clear checks when "Run all" is selected + onChange([]) + } + setRunSelected(!runSelected) + } + + return ( + + + + {runSelected && ( + + )} + + ) +} + +// Mostly taken from https://github.com/dhis2/ui/blob/master/components/transfer/src/transfer-option.js +// TODO: Change PropType of label in ui to accept "node" so this won't be necessary +const Option = ({ + disabled, + dataTest, + highlighted, + onClick, + onDoubleClick, + label, + value, + severity, +}) => { + const doubleClickTimeout = useRef(null) + + const handleClick = useCallback(() => { + if (disabled) { + return + } + if (doubleClickTimeout.current) { + clearTimeout(doubleClickTimeout.current) + doubleClickTimeout.current = null + + onDoubleClick({ value }, event) + } else { + doubleClickTimeout.current = setTimeout(() => { + clearTimeout(doubleClickTimeout.current) + doubleClickTimeout.current = null + }, 500) + + onClick({ value }, event) + } + }, [onClick, disabled, onDoubleClick, value]) + + return ( +
+
{label}
+
{`${i18n.t( + 'Severity' + )}: ${severity}`}
+
+ ) +} + +Option.propTypes = TransferOption.propTypes + +const renderOption = option =>