create safe form validation logic fixes

This commit is contained in:
mmv 2019-07-19 18:17:12 +04:00
parent 57cc985730
commit 2d998b2f24
3 changed files with 14 additions and 4 deletions

View File

@ -45,6 +45,9 @@ const ScanQRModal = ({
}, []) }, [])
useEffect(() => { useEffect(() => {
// this fires only when the hasWebcam changes to false (null > false (user doesn't have webcam)
// , true > false (user switched from webcam to file upload))
// Doesn't fire on re-render
if (hasWebcam === false) { if (hasWebcam === false) {
openImageDialog() openImageDialog()
} }

View File

@ -58,8 +58,8 @@ export const calculateValuesAfterRemoving = (index: number, notRemovedOwners: nu
initialValues[FIELD_CONFIRMATIONS] = numOwnersAfterRemoving.toString() initialValues[FIELD_CONFIRMATIONS] = numOwnersAfterRemoving.toString()
} }
delete initialValues[getOwnerNameBy(numOwnersAfterRemoving)] delete initialValues[getOwnerNameBy(index)]
delete initialValues[getOwnerAddressBy(numOwnersAfterRemoving)] delete initialValues[getOwnerAddressBy(index)]
return initialValues return initialValues
} }
@ -87,7 +87,14 @@ const SafeOwners = (props: Props) => {
form.reset() form.reset()
} else { } else {
const initialValues = calculateValuesAfterRemoving(index, numOwners, values) const initialValues = calculateValuesAfterRemoving(index, numOwners, values)
updateInitialProps(initialValues)
if (Object.keys(initialValues).length === 4) {
// this means that the form is back to its inital state
// and if they're equal it won't update the form
form.reset()
} else {
updateInitialProps(initialValues)
}
} }
setNumOwners(numOwners - 1) setNumOwners(numOwners - 1)

View File

@ -9,7 +9,7 @@ export const getOwnerAddressBy = (index: number) => `owner${index}Address`
export const getNumOwnersFrom = (values: Object) => { export const getNumOwnersFrom = (values: Object) => {
const accounts = Object.keys(values) const accounts = Object.keys(values)
.sort() .sort()
.filter(key => /^owner\d+Name$/.test(key)) .filter(key => /^owner\d+Address$/.test(key) && !!values[key])
return accounts.length return accounts.length
} }