/**
 * Fork of the https://github.com/ignatevdev/final-form-submit-errors
 *
 * In react-admin, errors might be objects with `message` and `args` properties which
 * are used for translation.
 * The original final-form-submit-errors mutator was considering those errors as form
 * nested field like `name.message` and `name.args`.
 * This version detects those objects.
 */ export function resetSubmitErrors ( [ { prev , current } ] , state , { getIn , setIn } ) { if ( state . formState . submitError ) { delete state . formState . submitError ; } if ( ! isObjectEmpty ( state . formState . submitErrors ) ) { const flatErrors = flatten ( state . formState . submitErrors ) ; const changed = [ ] ; Object . keys ( flatErrors ) . forEach ( key => { if ( getIn ( prev , key ) !== getIn ( current , key ) ) { changed . push ( key ) ; } } ) ; if ( changed . length ) { let newErrors = state . formState . submitErrors ; changed . forEach ( key => { newErrors = setIn ( newErrors , key , null ) ; } ) ; const cleanErrors = clean ( newErrors ) ; state . formState . submitErrors = cleanErrors ; } } } export function clean ( obj ) { const newObj = Array . isArray ( obj ) ? [ ] : { } ; Object . keys ( obj ) . forEach ( key => { if ( obj [ key ] && typeof obj [ key ] === 'object' ) { const newVal = clean ( obj [ key ] ) ; if ( ! isObjectEmpty ( newVal ) && newVal . length !== 0 ) { newObj [ key ] = newVal ; } } else if ( obj [ key ] !== null ) { newObj [ key ] = obj [ key ] ; } } ) ; return newObj ; } export function flatten ( obj ) { const toReturn = { } ; for ( const i in obj ) { if ( ! obj . hasOwnProperty ( i ) ) { continue ; } if ( typeof obj [ i ] === 'object' && obj [ i ] !== null && ! isValidationError ( obj [ i ] ) ) { const flatObject = flatten ( obj [ i ] ) ; for ( const x in flatObject ) { if ( ! flatObject . hasOwnProperty ( x ) ) { continue ; } const key = `${ i }.${ x }` . split ( '.' ) . reduce ( ( str , value ) => { if ( /^\[\d\]/ . test ( value ) ) { return `${ str }${ value }` ; } if ( ! isNaN ( Number ( value ) ) ) { return `${ str }[${ value }]` ; } if ( str ) { return `${ str }.${ value }` ; } return value ; } , '' ) ; toReturn [ key ] = flatObject [ x ] ; } } else { toReturn [ i ] = obj [ i ] ; } } return toReturn ; } export const isValidationError = ( obj ) => obj . message && obj . args ; export function isObjectEmpty ( obj ) { if ( ! obj ) { return true ; } return Object . entries ( obj ) . length === 0 && obj . constructor === Object ; } export default { resetSubmitErrors , } ;