-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed errors when adding duplicate categories. #4372
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import { unescape as unescapeString, without, map, repeat, find } from 'lodash'; | ||
import { unescape as unescapeString, without, map, repeat, find, some } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -28,6 +28,7 @@ const DEFAULT_QUERY = { | |
class HierarchicalTermSelector extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.findTerm = this.findTerm.bind( this ); | ||
this.onChange = this.onChange.bind( this ); | ||
this.onChangeFormName = this.onChangeFormName.bind( this ); | ||
this.onChangeFormParent = this.onChangeFormParent.bind( this ); | ||
|
@@ -69,12 +70,35 @@ class HierarchicalTermSelector extends Component { | |
} ) ); | ||
} | ||
|
||
findTerm( terms, parent, name ) { | ||
return find( terms, term => { | ||
return ( ( ! term.parent && ! parent ) || parseInt( term.parent ) === parseInt( parent ) ) && | ||
term.name === name; | ||
} ); | ||
} | ||
|
||
onAddTerm( event ) { | ||
event.preventDefault(); | ||
const { formName, formParent, adding } = this.state; | ||
const { onUpdateTerms, restBase, terms, slug } = this.props; | ||
const { formName, formParent, adding, availableTerms } = this.state; | ||
if ( formName === '' || adding ) { | ||
return; | ||
} | ||
|
||
// check if the term we are adding already exists | ||
const existingTerm = this.findTerm( availableTerms, formParent, formName ); | ||
if ( existingTerm ) { | ||
// if the term we are adding exists but is not selected select it | ||
if ( ! some( terms, term => term === existingTerm.id ) ) { | ||
onUpdateTerms( [ ...terms, existingTerm.id ], restBase ); | ||
} | ||
this.setState( { | ||
formName: '', | ||
formParent: '', | ||
} ); | ||
return; | ||
} | ||
|
||
const findOrCreatePromise = new Promise( ( resolve, reject ) => { | ||
this.setState( { | ||
adding: true, | ||
|
@@ -89,8 +113,13 @@ class HierarchicalTermSelector extends Component { | |
.then( resolve, ( xhr ) => { | ||
const errorCode = xhr.responseJSON && xhr.responseJSON.code; | ||
if ( errorCode === 'term_exists' ) { | ||
this.addRequest = new Model( { id: xhr.responseJSON.data } ).fetch(); | ||
return this.addRequest.then( resolve, reject ); | ||
// search the new category created since last fetch | ||
this.addRequest = new Model().fetch( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you change the request here? Thinking that requesting using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bug was that the server when returns the 409 answer does not return the term. So we were generating an invalid request GET http://src.wordpress-develop.test/wp-json/wp/v2/categories/[object%20Object] 404 (Not Found). Requesting by id would be better but I think we can not do that because we don't know the id yet :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you try to inspect the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I tried, and unfortunately, we are out of luck the response just contains "{"code":"term_exists","message":"A term with the name provided already exists with this parent.","data":{"status":409}}". Maybe something changed in the API meanwhile. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this means we may have an issue here, what guarantees that the received category is the right one? I think we should compare the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right, we should add a client-side filter on the search results. I will make the change :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change was applied, we should be handling all the cases now. |
||
{ data: { ...DEFAULT_QUERY, parent: formParent || 0, search: formName } } | ||
); | ||
return this.addRequest.then( searchResult => { | ||
resolve( this.findTerm( searchResult, formParent, formName ) ); | ||
}, reject ); | ||
} | ||
reject( xhr ); | ||
} ); | ||
|
@@ -99,7 +128,6 @@ class HierarchicalTermSelector extends Component { | |
.then( ( term ) => { | ||
const hasTerm = !! find( this.state.availableTerms, ( availableTerm ) => availableTerm.id === term.id ); | ||
const newAvailableTerms = hasTerm ? this.state.availableTerms : [ term, ...this.state.availableTerms ]; | ||
const { onUpdateTerms, restBase, terms, slug } = this.props; | ||
const termAddedMessage = slug === 'category' ? __( 'Category added' ) : __( 'Term added' ); | ||
this.props.speak( termAddedMessage, 'assertive' ); | ||
this.setState( { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: Could this have been consolidated into the
findOrCreatePromise
as an early return value for theterm
argument of the resolved promise callback?