Skip to content

Commit

Permalink
fix: crashes with autocomplete inputs in Campaigns wizard and Categor…
Browse files Browse the repository at this point in the history
…yAutocomplete (#1609)

* fix: crashes with autocomplete inputs in Campaigns wizard and CategoryAutocomplete

* refactor: easier-to-read token array filtering

* refactor: filter on the component, not the prop
  • Loading branch information
dkoo authored Apr 18, 2022
1 parent 51179a2 commit 101d1d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
11 changes: 7 additions & 4 deletions assets/components/src/category-autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,20 @@ class CategoryAutocomplete extends Component {
const { suggestions } = this.state;
// Categories that are already will be objects, while new additions will be strings (the name).
// allValues nomalizes the array so that they are all objects.
const allValues = tokens.map( token =>
typeof token === 'string' ? suggestions[ token ] : token
);
const allValues = tokens
.filter( token => 'undefined' !== typeof token ) // Ensure each token is a valid value.
.map( token => ( 'string' === typeof token ? suggestions[ token ] : token ) )
.filter( Boolean );
onChange( allValues );
};

getAvailableSuggestions = () => {
const { value } = this.props;
const { suggestions } = this.state;
const selectedIds = value.reduce( ( acc, item ) => {
acc.push( item.id );
if ( item?.id ) {
acc.push( item.id );
}
return acc;
}, [] );
const availableSuggestions = filter(
Expand Down
12 changes: 10 additions & 2 deletions assets/wizards/popups/components/settings-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ const PromptSettingsModal = ( { prompt, disabled, onClose, segments, updatePopup
value={ excludedCategories || [] }
onChange={ tokens =>
setPromptConfig( {
options: { excluded_categories: tokens.map( token => token.id ) },
options: {
excluded_categories: tokens.map( token => token.id ),
},
} )
}
description={ __(
Expand All @@ -191,7 +193,13 @@ const PromptSettingsModal = ( { prompt, disabled, onClose, segments, updatePopup
hideHelpFromVision
taxonomy="tags"
value={ excludedTags || [] }
onChange={ tokens => setPromptConfig( { options: { excluded_tags: tokens } } ) }
onChange={ tokens =>
setPromptConfig( {
options: {
excluded_tags: tokens.map( token => token.id ),
},
} )
}
description={ __(
'Prompt will not appear on posts with the specified tags.',
'newspack'
Expand Down

0 comments on commit 101d1d6

Please sign in to comment.