Skip to content
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

Editor: Bind EditorPageOrder to Redux state #7166

Merged
merged 2 commits into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/post-editor/editor-drawer/page-options.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function EditorDrawerPageOptions( { translate, siteId, postId, post, postType, h
<PageTemplates post={ post } />
</PageTemplatesData>
) }
<PageOrder menuOrder={ get( post, 'menu_order', 0 ) } />
<PageOrder />
</Accordion>
);
}
Expand Down
107 changes: 58 additions & 49 deletions client/post-editor/editor-page-order/index.jsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,84 @@
/**
* 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 AccordionSection from 'components/accordion/section';
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 );
this.statTracked = false;
}

if ( isNaN( newOrder ) ) {
newOrder = 0;
editMenuOrder( event ) {
const { menuOrder, siteId, postId } = this.props;
const newOrder = parseInt( event.target.value, 10 ) || 0;
if ( newOrder === menuOrder ) {
return;
}

this.editMenuOrder( newOrder );
},

editMenuOrder( newOrder ) {
if ( newOrder !== this.props.menuOrder ) {
if ( ! this.statTracked ) {
this.statTracked = true;
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
} );
}
},

this.props.editPost( { menu_order: newOrder }, siteId, postId );
}

render() {
const { translate, menuOrder } = this.props;

return (
<div className="editor-page-order">
<AccordionSection 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 menu order.' } ) }
</span>
<TextInput
type="number"
value={ menuOrder }
pattern="[0-9]*"
onChange={ this.editMenuOrder }
onBlur={ this.editMenuOrder }
className="editor-page-order__input" />
</label>
</div>
</AccordionSection>
);
}
} );
}

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 ) );
8 changes: 7 additions & 1 deletion client/post-editor/editor-page-order/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
text-transform: uppercase;
}

.editor-page-order input[type="text"] {
input[type="number"].editor-page-order__input {
-moz-appearance: textfield;
font-size: 13px;
width: 50px;

&::-webkit-outer-spin-button,
&::-webkit-inner-spin-button {
-webkit-appearance: none;
}
}