Skip to content

Commit

Permalink
Fix issue where selecting an already-selected variation clears the re…
Browse files Browse the repository at this point in the history
…st of the selected variations
  • Loading branch information
ryelle committed Nov 8, 2017
1 parent 24a6c25 commit 69a0cea
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions client/extensions/woocommerce/components/product-search/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,7 @@ import PropTypes from 'prop-types';
import { bindActionCreators } from 'redux';
import classNames from 'classnames';
import { connect } from 'react-redux';
import {
filter,
find,
forEach,
get,
head,
intersection,
noop,
reduce,
uniqBy,
values,
} from 'lodash';
import { filter, find, get, intersection, noop, reduce, uniqBy, values } from 'lodash';
import { localize } from 'i18n-calypso';

/**
Expand Down Expand Up @@ -95,14 +84,7 @@ class ProductSearchRow extends Component {

onChange = event => {
const productId = Number( event.target.value );
this.setState(
prevState => ( {
variations: filter( prevState.variations, item => item.id !== productId ),
} ),
() => {
this.props.onChange( productId );
}
);
this.props.onChange( productId );
};

toggleCustomizeForm = event => {
Expand Down Expand Up @@ -139,20 +121,20 @@ class ProductSearchRow extends Component {
} );
if ( matchingVariations.length === 1 ) {
// We found a match.
const variation = head( matchingVariations );
if ( this.props.singular ) {
this.setState( { variations: [ variation ] } );
this.props.onChange( variation.id );
return;
this.setState( prevState => {
// For singular selects, we can replace the old selected variation, but for
// multi-selects, we want to merge the new variation into the existing list
const newVariations = this.props.singular
? matchingVariations
: uniqBy( [ ...prevState.variations, ...matchingVariations ], 'id' );
return {
variations: newVariations,
};
} );
const variationId = get( matchingVariations, '[0].id', false );
if ( variationId && ! this.isSelected( variationId ) ) {
this.props.onChange( variationId );
}
this.setState(
prevState => ( {
variations: uniqBy( [ ...prevState.variations, variation ], 'id' ),
} ),
() => {
forEach( this.state.variations, v => this.props.onChange( v.id ) );
}
);
}
};

Expand Down

0 comments on commit 69a0cea

Please sign in to comment.