@@ -7,7 +7,6 @@ import { action } from '@storybook/addon-actions';
7
7
import omitEmpty from 'omit-empty' ;
8
8
import withReadme from 'storybook-readme/with-readme' ;
9
9
import { withKnobs , select } from '@storybook/addon-knobs' ;
10
- import mapValues from 'lodash.mapvalues' ;
11
10
import { FormikBox , Section } from '../.storybook/decorators' ;
12
11
import {
13
12
Text ,
@@ -182,11 +181,17 @@ const validate = (formValues, locale) => {
182
181
errors . slug . missing = true ;
183
182
} else {
184
183
const isValidSlug = value => / ^ [ a - z A - Z 0 - 9 _ - ] { 2 , 256 } $ / . test ( value ) ;
185
- const translationErrors = mapValues (
186
- LocalizedTextInput . omitEmptyTranslations ( formValues . slug ) ,
187
- // more validation errors could be added in theory
188
- slug => ( isValidSlug ( slug ) ? { } : { hasForbiddenChars : true } )
189
- ) ;
184
+ const translationErrors = Object . keys (
185
+ LocalizedTextInput . omitEmptyTranslations ( formValues . slug )
186
+ ) . reduce ( ( acc , language ) => {
187
+ const value = isValidSlug ( formValues . slug [ language ] )
188
+ ? { }
189
+ : { hasForbiddenChars : true } ;
190
+ return {
191
+ ...acc ,
192
+ [ language ] : value ,
193
+ } ;
194
+ } , { } ) ;
190
195
191
196
errors . slug . translations = translationErrors ;
192
197
}
@@ -349,24 +354,31 @@ class ProductForm extends React.Component {
349
354
// specific messages in code means we can make use of our existing
350
355
// translation mechanism. We could easily render
351
356
// <FormattedMessage /> instead of rendering the warning string.
352
- mapValues (
353
- // We only show errors once the field has been touched
354
- LocalizedTextInput . isTouched ( this . props . formik . touched . slug ) &&
355
- this . props . formik . errors . slug
356
- ? // We map on the per-field errors which are present on
357
- // the "translations" field
358
- this . props . formik . errors . slug . translations
359
- : { } ,
360
- error => {
361
- if ( error . hasForbiddenChars ) {
362
- return < ErrorMessage > This slug is not valid.</ ErrorMessage > ;
363
- }
364
- // we could map other errors here as well
357
+ ( ( ) => {
358
+ const allLocales =
359
+ LocalizedTextInput . isTouched (
360
+ this . props . formik . touched . slug
361
+ ) && this . props . formik . errors . slug
362
+ ? // We map on the per-field errors which are present on
363
+ // the "translations" field
364
+ this . props . formik . errors . slug . translations
365
+ : { } ;
366
+ return Object . entries ( allLocales ) . reduce (
367
+ ( acc , [ key , error ] ) => {
368
+ const value = error . hasForbiddenChars ? (
369
+ < ErrorMessage > This slug is not valid.</ ErrorMessage >
370
+ ) : (
371
+ undefined
372
+ ) ;
365
373
366
- // returning undefined results in no error for that field
367
- return undefined ;
368
- }
369
- )
374
+ return {
375
+ [ key ] : value ,
376
+ ...acc ,
377
+ } ;
378
+ } ,
379
+ { }
380
+ ) ;
381
+ } ) ( )
370
382
}
371
383
/>
372
384
{ LocalizedTextInput . isTouched ( this . props . formik . touched . slug ) &&
0 commit comments