-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Bind EditorPageOrder to Redux state
- Loading branch information
Showing
3 changed files
with
61 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,78 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import React, { PropTypes } from 'react'; | ||
import PureRenderMixin from 'react-pure-render/mixin'; | ||
import isNaN from 'lodash/isNaN'; | ||
import React, { Component, PropTypes } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { localize } from 'i18n-calypso'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import TextInput from 'components/forms/form-text-input'; | ||
import postActions from 'lib/posts/actions'; | ||
import { recordEvent, recordStat } from 'lib/posts/stats'; | ||
import { getSelectedSiteId } from 'state/ui/selectors'; | ||
import { getEditorPostId } from 'state/ui/editor/selectors'; | ||
import { getEditedPostValue } from 'state/posts/selectors'; | ||
import { editPost } from 'state/posts/actions'; | ||
|
||
export default React.createClass( { | ||
displayName: 'EditorPageOrder', | ||
class EditorPageOrder extends Component { | ||
static propTypes = { | ||
siteId: PropTypes.number, | ||
postId: PropTypes.number, | ||
menuOrder: PropTypes.number | ||
}; | ||
|
||
mixins: [ PureRenderMixin ], | ||
static defaultProps = { | ||
menuOrder: 0 | ||
}; | ||
|
||
propTypes: { | ||
menuOrder: PropTypes.oneOfType( [ | ||
PropTypes.number, | ||
PropTypes.string | ||
] ) | ||
}, | ||
|
||
getDefaultProps() { | ||
return { | ||
menuOrder: 0 | ||
}; | ||
}, | ||
|
||
updateMenuOrder( event ) { | ||
let newOrder = parseInt( event.target.value ); | ||
|
||
if ( isNaN( newOrder ) ) { | ||
newOrder = ''; | ||
} | ||
|
||
this.editMenuOrder( newOrder ); | ||
}, | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
validateMenuOrder( event ) { | ||
let newOrder = parseInt( event.target.value ); | ||
this.editMenuOrder = this.editMenuOrder.bind( this ); | ||
} | ||
|
||
if ( isNaN( newOrder ) ) { | ||
newOrder = 0; | ||
editMenuOrder( event ) { | ||
const newOrder = parseInt( event.target.value ) || 0; | ||
const { menuOrder, siteId, postId } = this.props; | ||
if ( newOrder === menuOrder ) { | ||
return; | ||
} | ||
|
||
this.editMenuOrder( newOrder ); | ||
}, | ||
|
||
editMenuOrder( newOrder ) { | ||
if ( newOrder !== this.props.menuOrder ) { | ||
recordStat( 'advanced_menu_order_changed' ); | ||
recordEvent( 'Changed page menu order' ); | ||
|
||
// TODO: REDUX - remove flux actions when whole post-editor is reduxified | ||
postActions.edit( { | ||
menu_order: newOrder | ||
} ); | ||
} | ||
}, | ||
recordStat( 'advanced_menu_order_changed' ); | ||
recordEvent( 'Changed page menu order' ); | ||
this.props.editPost( { menu_order: newOrder }, siteId, postId ); | ||
} | ||
|
||
render() { | ||
const { translate, menuOrder } = this.props; | ||
|
||
return ( | ||
<div className="editor-page-order"> | ||
<label> | ||
<span className="editor-page-order__label-text">{ this.translate( 'Order', { context: 'Editor: Field label for page menu order.' } ) }</span> | ||
<TextInput value={ this.props.menuOrder } onChange={ this.updateMenuOrder } onBlur={ this.validateMenuOrder } /> | ||
<span className="editor-page-order__label-text"> | ||
{ translate( 'Order', { context: 'Editor: Field label for page menu order.' } ) } | ||
</span> | ||
<TextInput | ||
type="number" | ||
value={ menuOrder } | ||
pattern="[0-9]*" | ||
onChange={ this.editMenuOrder } | ||
onBlur={ this.editMenuOrder } | ||
className="editor-page-order__input" /> | ||
</label> | ||
</div> | ||
); | ||
} | ||
} ); | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
const siteId = getSelectedSiteId( state ); | ||
const postId = getEditorPostId( state ); | ||
const menuOrder = getEditedPostValue( state, siteId, postId, 'menu_order' ); | ||
|
||
return { siteId, postId, menuOrder }; | ||
}, | ||
{ editPost } | ||
)( localize( EditorPageOrder ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters