From f9be68f98af3d22e7939e6fc1d97d1f034742a51 Mon Sep 17 00:00:00 2001 From: Liordino Neto Date: Tue, 6 Oct 2020 17:59:27 -0300 Subject: [PATCH] fix: make table fields show a '-' instead of an empty string when the col content is null fix: make all notifications fields optional fix: make the notifications overrides fields valid only if the machine/ cryptocurrency and at least one of the other options are filled --- .../src/components/editableTable/Row.js | 2 +- .../components/SingleFieldEditableNumber.js | 4 +- .../src/pages/Notifications/helper.js | 6 +++ .../sections/CryptoBalanceOverrides.js | 41 +++++++++++++------ .../sections/FiatBalanceAlerts.js | 12 ++++-- .../sections/FiatBalanceOverrides.js | 41 +++++++++++++------ 6 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 new-lamassu-admin/src/pages/Notifications/helper.js diff --git a/new-lamassu-admin/src/components/editableTable/Row.js b/new-lamassu-admin/src/components/editableTable/Row.js index f6b76eb5..5c2112e9 100644 --- a/new-lamassu-admin/src/components/editableTable/Row.js +++ b/new-lamassu-admin/src/components/editableTable/Row.js @@ -103,7 +103,7 @@ const ECol = ({ textAlign, suffix, SuffixComponent = TL2, - view = it => it?.toString(), + view = it => (!it ? '-' : it.toString()), inputProps = {} } = config diff --git a/new-lamassu-admin/src/pages/Notifications/components/SingleFieldEditableNumber.js b/new-lamassu-admin/src/pages/Notifications/components/SingleFieldEditableNumber.js index e01c05ad..f3600db0 100644 --- a/new-lamassu-admin/src/pages/Notifications/components/SingleFieldEditableNumber.js +++ b/new-lamassu-admin/src/pages/Notifications/components/SingleFieldEditableNumber.js @@ -5,6 +5,7 @@ import * as Yup from 'yup' import PromptWhenDirty from 'src/components/PromptWhenDirty' import NotificationsCtx from '../NotificationsContext' +import { transformNumber } from '../helper' import Header from './EditHeader' import EditableNumber from './EditableNumber' @@ -43,10 +44,11 @@ const SingleFieldEditableNumber = ({ const schema = Yup.object().shape({ [name]: Yup.number() + .transform(transformNumber) .integer() .min(min) .max(max) - .required() + .nullable() }) return ( diff --git a/new-lamassu-admin/src/pages/Notifications/helper.js b/new-lamassu-admin/src/pages/Notifications/helper.js new file mode 100644 index 00000000..0ac9a01f --- /dev/null +++ b/new-lamassu-admin/src/pages/Notifications/helper.js @@ -0,0 +1,6 @@ +import _ from 'lodash/fp' + +const transformNumber = value => + _.isNumber(value) && !_.isNaN(value) ? value : null + +export { transformNumber } diff --git a/new-lamassu-admin/src/pages/Notifications/sections/CryptoBalanceOverrides.js b/new-lamassu-admin/src/pages/Notifications/sections/CryptoBalanceOverrides.js index 26ff880a..013a5257 100644 --- a/new-lamassu-admin/src/pages/Notifications/sections/CryptoBalanceOverrides.js +++ b/new-lamassu-admin/src/pages/Notifications/sections/CryptoBalanceOverrides.js @@ -7,6 +7,7 @@ import { NumberInput } from 'src/components/inputs/formik' import Autocomplete from 'src/components/inputs/formik/Autocomplete.js' import NotificationsCtx from '../NotificationsContext' +import { transformNumber } from '../helper' const HIGH_BALANCE_KEY = 'highBalance' const LOW_BALANCE_KEY = 'lowBalance' @@ -51,20 +52,34 @@ const CryptoBalanceOverrides = ({ section }) => { [HIGH_BALANCE_KEY]: '' } + const notesMin = 0 const currencyMax = 9999999 - const validationSchema = Yup.object().shape({ - [CRYPTOCURRENCY_KEY]: Yup.string().required(), - [LOW_BALANCE_KEY]: Yup.number() - .integer() - .min(0) - .max(currencyMax) - .required(), - [HIGH_BALANCE_KEY]: Yup.number() - .integer() - .min(0) - .max(currencyMax) - .required() - }) + const validationSchema = Yup.object().shape( + { + [CRYPTOCURRENCY_KEY]: Yup.string().required(), + [LOW_BALANCE_KEY]: Yup.number() + .when(HIGH_BALANCE_KEY, { + is: HIGH_BALANCE_KEY => !HIGH_BALANCE_KEY, + then: Yup.number().required() + }) + .transform(transformNumber) + .integer() + .min(notesMin) + .max(currencyMax) + .nullable(), + [HIGH_BALANCE_KEY]: Yup.number() + .when(LOW_BALANCE_KEY, { + is: LOW_BALANCE_KEY => !LOW_BALANCE_KEY, + then: Yup.number().required() + }) + .transform(transformNumber) + .integer() + .min(notesMin) + .max(currencyMax) + .nullable() + }, + [LOW_BALANCE_KEY, HIGH_BALANCE_KEY] + ) const viewCrypto = it => R.compose( diff --git a/new-lamassu-admin/src/pages/Notifications/sections/FiatBalanceAlerts.js b/new-lamassu-admin/src/pages/Notifications/sections/FiatBalanceAlerts.js index c914fda6..58a5a7e7 100644 --- a/new-lamassu-admin/src/pages/Notifications/sections/FiatBalanceAlerts.js +++ b/new-lamassu-admin/src/pages/Notifications/sections/FiatBalanceAlerts.js @@ -10,6 +10,7 @@ import { Cashbox } from '../../../components/inputs/cashbox/Cashbox' import NotificationsCtx from '../NotificationsContext' import Header from '../components/EditHeader' import EditableNumber from '../components/EditableNumber' +import { transformNumber } from '../helper' import styles from './FiatBalanceAlerts.styles.js' @@ -19,6 +20,7 @@ const NAME = 'fiatBalanceAlerts' const FiatBalance = ({ section, + min = 0, max = Number.MAX_SAFE_INTEGER, fieldWidth = 80 }) => { @@ -31,15 +33,17 @@ const FiatBalance = ({ const schema = Yup.object().shape({ fiatBalanceCassette1: Yup.number() + .transform(transformNumber) .integer() - .min(0) + .min(min) .max(max) - .required(), + .nullable(), fiatBalanceCassette2: Yup.number() + .transform(transformNumber) .integer() - .min(0) + .min(min) .max(max) - .required() + .nullable() }) const fiatBalanceCassette1Percent = diff --git a/new-lamassu-admin/src/pages/Notifications/sections/FiatBalanceOverrides.js b/new-lamassu-admin/src/pages/Notifications/sections/FiatBalanceOverrides.js index ab92a19d..47e345ef 100644 --- a/new-lamassu-admin/src/pages/Notifications/sections/FiatBalanceOverrides.js +++ b/new-lamassu-admin/src/pages/Notifications/sections/FiatBalanceOverrides.js @@ -7,6 +7,7 @@ import { NumberInput } from 'src/components/inputs/formik/' import Autocomplete from 'src/components/inputs/formik/Autocomplete' import NotificationsCtx from '../NotificationsContext' +import { transformNumber } from '../helper' const CASSETTE_1_KEY = 'cassette1' const CASSETTE_2_KEY = 'cassette2' @@ -38,20 +39,34 @@ const FiatBalanceOverrides = ({ section }) => { [CASSETTE_2_KEY]: '' } + const notesMin = 0 const notesMax = 9999999 - const validationSchema = Yup.object().shape({ - [MACHINE_KEY]: Yup.string().required(), - [CASSETTE_1_KEY]: Yup.number() - .integer() - .min(0) - .max(notesMax) - .required(), - [CASSETTE_2_KEY]: Yup.number() - .integer() - .min(0) - .max(notesMax) - .required() - }) + const validationSchema = Yup.object().shape( + { + [MACHINE_KEY]: Yup.string().required(), + [CASSETTE_1_KEY]: Yup.number() + .when(CASSETTE_2_KEY, { + is: CASSETTE_2_KEY => !CASSETTE_2_KEY, + then: Yup.number().required() + }) + .transform(transformNumber) + .integer() + .min(notesMin) + .max(notesMax) + .nullable(), + [CASSETTE_2_KEY]: Yup.number() + .when(CASSETTE_1_KEY, { + is: CASSETTE_1_KEY => !CASSETTE_1_KEY, + then: Yup.number().required() + }) + .transform(transformNumber) + .integer() + .min(notesMin) + .max(notesMax) + .nullable() + }, + [CASSETTE_1_KEY, CASSETTE_2_KEY] + ) const viewMachine = it => R.compose(R.path(['name']), R.find(R.propEq('deviceId', it)))(machines)