Listing fixes

- fixed bulk actions
- fixed paging behavior
- fixed filtering issues
This commit is contained in:
Jonathan Labreuille
2015-10-21 12:56:24 +02:00
parent da322cae4b
commit d4bfb49415
24 changed files with 200 additions and 97 deletions

View File

@ -14,11 +14,29 @@ function(
} }
}, },
componentWillMount: function() { componentWillMount: function() {
this.loadCachedItems();
}, },
componentDidMount: function() { componentDidMount: function() {
jQuery('#'+this.props.field.id).select2() this.loadCachedItems();
.on('change', this.handleChange); },
setupSelect2: function() {
if(this.props.field.select2 && Object.keys(this.props.item).length > 0) {
console.log('do it!');
jQuery('#'+this.props.field.id).select2({
width: this.props.field.width
}).select2(
'val',
this.props.item[this.props.field.name]
).on('change', this.handleChange);
// set values
/*jQuery('#'+this.props.field.id).select2(
'val',
this.props.item[this.props.field.name]
);*/
console.log(this.props.item[this.props.field.name]);
}
}, },
loadCachedItems: function() { loadCachedItems: function() {
if(typeof(window['mailpoet_'+this.props.field.endpoint]) !== 'undefined') { if(typeof(window['mailpoet_'+this.props.field.endpoint]) !== 'undefined') {
@ -29,35 +47,49 @@ function(
} }
}, },
handleChange: function() { handleChange: function() {
return this.props.onValueChange({ if(this.props.onValueChange !== undefined) {
target: { this.props.onValueChange({
value: jQuery('#'+this.props.field.id).select2('val'), target: {
name: this.props.field.name value: jQuery('#'+this.props.field.id).select2('val'),
} name: this.props.field.name
}); }
});
}
return true;
}, },
render: function() { render: function() {
var options = this.state.items.map(function(item, index) { if(this.state.items.length === 0) {
return ( return false;
<option } else {
key={ item.id } var options = this.state.items.map(function(item, index) {
value={ item.id } return (
> <option
{ item.name } key={ item.id }
</option> value={ item.id }
); >
}); { item.name }
</option>
);
});
return ( var default_value = (
<select (this.props.item !== undefined && this.props.field.name !== undefined)
ref="selection" ? this.props.item[this.props.field.name]
id={ this.props.field.id } : null
placeholder={ this.props.field.placeholder } );
multiple={ this.props.field.multiple }
onChange={ this.handleChange } this.setupSelect2();
defaultValue={ this.props.item[this.props.field.name] }
>{ options }</select> return (
); <select
id={ this.props.field.id }
placeholder={ this.props.field.placeholder }
multiple={ this.props.field.multiple }
onChange={ this.handleChange }
defaultValue={ default_value }
>{ options }</select>
);
}
} }
}); });

View File

@ -37,7 +37,7 @@ define(
loading: false, loading: false,
item: {} item: {}
}); });
this.refs.form.getDOMNode().reset(); this.refs.form.reset();
} else { } else {
this.loadItem(props.params.id); this.loadItem(props.params.id);
} }

View File

@ -55,7 +55,7 @@ function(
}); });
}, },
getSelectedAction: function() { getSelectedAction: function() {
var selected_action = jQuery(this.refs.action.getDOMNode()).val(); var selected_action = this.refs.action.value;
if(selected_action.length > 0) { if(selected_action.length > 0) {
var action = this.props.bulk_actions.filter(function(action) { var action = this.props.bulk_actions.filter(function(action) {
return (action.name === selected_action); return (action.name === selected_action);

View File

@ -7,7 +7,7 @@ function(
var ListingFilters = React.createClass({ var ListingFilters = React.createClass({
handleFilterAction: function() { handleFilterAction: function() {
var filters = this.props.filters.map(function(filter, index) { var filters = this.props.filters.map(function(filter, index) {
var value = this.refs['filter-'+index].getDOMNode().value; var value = this.refs['filter-'+index].value;
if(value) { if(value) {
return { return {
'name': filter.name, 'name': filter.name,

View File

@ -3,7 +3,7 @@ define(['react', 'classnames'], function(React, classNames) {
var ListingHeader = React.createClass({ var ListingHeader = React.createClass({
handleSelectItems: function() { handleSelectItems: function() {
return this.props.onSelectItems( return this.props.onSelectItems(
this.refs.toggle.getDOMNode().checked this.refs.toggle.checked
); );
}, },
render: function() { render: function() {

View File

@ -161,7 +161,15 @@ define(
this.props.columns.length this.props.columns.length
+ (this.props.is_selectable ? 1 : 0) + (this.props.is_selectable ? 1 : 0)
}> }>
{ MailPoetI18n.selectAllLabel }&nbsp; {
(this.props.selection !== 'all')
? MailPoetI18n.selectAllLabel
: MailPoetI18n.selectedAllLabel.replace(
'%d',
this.props.count
)
}
&nbsp;
<a <a
onClick={ this.props.onSelectAll } onClick={ this.props.onSelectAll }
href="javascript:;">{ href="javascript:;">{
@ -202,7 +210,7 @@ define(
search: '', search: '',
page: 1, page: 1,
count: 0, count: 0,
limit: 10, limit: 5,
sort_by: 'id', sort_by: 'id',
sort_order: 'desc', sort_order: 'desc',
items: [], items: [],
@ -247,7 +255,10 @@ define(
}.bind(this)); }.bind(this));
}, },
handleDeleteItem: function(id) { handleDeleteItem: function(id) {
this.setState({ loading: true }); this.setState({
loading: true,
page: 1
});
MailPoet.Ajax.post({ MailPoet.Ajax.post({
endpoint: this.props.endpoint, endpoint: this.props.endpoint,
@ -289,6 +300,7 @@ define(
handleSearch: function(search) { handleSearch: function(search) {
this.setState({ this.setState({
search: search, search: search,
page: 1,
selection: false, selection: false,
selected_ids: [] selected_ids: []
}, function() { }, function() {
@ -340,6 +352,7 @@ define(
}, },
handleSelectAll: function() { handleSelectAll: function() {
if(this.state.selection === 'all') { if(this.state.selection === 'all') {
this.clearSelection();
} else { } else {
this.setState({ this.setState({
selection: 'all', selection: 'all',
@ -375,6 +388,7 @@ define(
}.bind(this)); }.bind(this));
}, },
handleSetPage: function(page) { handleSetPage: function(page) {
console.log('listing > handleSetPage('+page+')');
this.setState({ this.setState({
page: page, page: page,
selection: false, selection: false,

View File

@ -1,25 +1,39 @@
define(['react', 'classnames'], function(React, classNames) { define(['react', 'classnames'], function(React, classNames) {
var ListingPages = React.createClass({ var ListingPages = React.createClass({
getInitialState: function() {
return {
page: null
}
},
setPage: function(page) {
this.props.onSetPage(page);
},
setFirstPage: function() { setFirstPage: function() {
this.props.onSetPage(1); this.setPage(1);
}, },
setLastPage: function() { setLastPage: function() {
this.props.onSetPage(this.getLastPage()); this.setPage(this.getLastPage());
}, },
setPreviousPage: function() { setPreviousPage: function() {
this.props.onSetPage(this.constrainPage(this.props.page - 1)); this.setPage(this.constrainPage(this.props.page - 1));
}, },
setNextPage: function() { setNextPage: function() {
this.props.onSetPage(this.constrainPage(this.props.page + 1)); this.setPage(this.constrainPage(this.props.page + 1));
}, },
constrainPage: function(page) { constrainPage: function(page) {
return Math.min(Math.max(1, Math.abs(~~page)), this.getLastPage()); return Math.min(Math.max(1, Math.abs(~~page)), this.getLastPage());
}, },
handleSetPage: function() { handleSetManualPage: function(e) {
this.props.onSetPage( if(e.which === 13) {
this.constrainPage(this.refs.page.getDOMNode().value) this.setPage(this.state.page);
); this.setState({ page: null });
}
},
handleChangeManualPage: function(e) {
this.setState({
page: this.constrainPage(e.target.value)
});
}, },
getLastPage: function() { getLastPage: function() {
return Math.ceil(this.props.count / this.props.limit); return Math.ceil(this.props.count / this.props.limit);
@ -46,7 +60,7 @@ define(['react', 'classnames'], function(React, classNames) {
if(this.props.page > 1) { if(this.props.page > 1) {
previousPage = ( previousPage = (
<a href="javascript:;" <a href="javascript:;"
onClick={this.setPreviousPage} onClick={ this.setPreviousPage }
className="prev-page"> className="prev-page">
<span className="screen-reader-text">Previous page</span> <span className="screen-reader-text">Previous page</span>
<span aria-hidden="true"></span> <span aria-hidden="true"></span>
@ -57,7 +71,7 @@ define(['react', 'classnames'], function(React, classNames) {
if(this.props.page > 2) { if(this.props.page > 2) {
firstPage = ( firstPage = (
<a href="javascript:;" <a href="javascript:;"
onClick={this.setFirstPage} onClick={ this.setFirstPage }
className="first-page"> className="first-page">
<span className="screen-reader-text">First page</span> <span className="screen-reader-text">First page</span>
<span aria-hidden="true">«</span> <span aria-hidden="true">«</span>
@ -68,7 +82,7 @@ define(['react', 'classnames'], function(React, classNames) {
if(this.props.page < this.getLastPage()) { if(this.props.page < this.getLastPage()) {
nextPage = ( nextPage = (
<a href="javascript:;" <a href="javascript:;"
onClick={this.setNextPage} onClick={ this.setNextPage }
className="next-page"> className="next-page">
<span className="screen-reader-text">Next page</span> <span className="screen-reader-text">Next page</span>
<span aria-hidden="true"></span> <span aria-hidden="true"></span>
@ -79,7 +93,7 @@ define(['react', 'classnames'], function(React, classNames) {
if(this.props.page < this.getLastPage() - 1) { if(this.props.page < this.getLastPage() - 1) {
lastPage = ( lastPage = (
<a href="javascript:;" <a href="javascript:;"
onClick={this.setLastPage} onClick={ this.setLastPage }
className="last-page"> className="last-page">
<span className="screen-reader-text">Last page</span> <span className="screen-reader-text">Last page</span>
<span aria-hidden="true">»</span> <span aria-hidden="true">»</span>
@ -98,11 +112,12 @@ define(['react', 'classnames'], function(React, classNames) {
htmlFor="current-page-selector">Current Page</label> htmlFor="current-page-selector">Current Page</label>
<input <input
type="text" type="text"
onChange={this.handleSetPage} onChange={ this.handleChangeManualPage }
onKeyUp={ this.handleSetManualPage }
aria-describedby="table-paging" aria-describedby="table-paging"
size="1" size="1"
ref="page" ref="page"
value={this.props.page} value={ this.state.page || this.props.page }
name="paged" name="paged"
id="current-page-selector" id="current-page-selector"
className="current-page" /> className="current-page" />
@ -124,7 +139,7 @@ define(['react', 'classnames'], function(React, classNames) {
); );
return ( return (
<div className={classes}> <div className={ classes }>
<span className="displaying-num">{ this.props.count } item(s)</span> <span className="displaying-num">{ this.props.count } item(s)</span>
{ pagination } { pagination }
</div> </div>

View File

@ -4,7 +4,7 @@ define(['react'], function(React) {
handleSearch: function(e) { handleSearch: function(e) {
e.preventDefault(); e.preventDefault();
this.props.onSearch( this.props.onSearch(
this.refs.search.getDOMNode().value this.refs.search.value
); );
}, },
render: function() { render: function() {

View File

@ -3,13 +3,15 @@ define(
'react', 'react',
'react-router', 'react-router',
'listing/listing.jsx', 'listing/listing.jsx',
'classnames' 'classnames',
'jquery'
], ],
function( function(
React, React,
Router, Router,
Listing, Listing,
classNames classNames,
jQuery
) { ) {
var Link = Router.Link; var Link = Router.Link;
@ -19,6 +21,10 @@ define(
label: 'Subject', label: 'Subject',
sortable: true sortable: true
}, },
{
name: 'segments',
label: 'Lists'
},
{ {
name: 'created_at', name: 'created_at',
label: 'Created on', label: 'Created on',
@ -60,6 +66,12 @@ define(
'has-row-actions' 'has-row-actions'
); );
var segments = mailpoet_segments.filter(function(segment) {
return (jQuery.inArray(segment.id, newsletter.segments) !== -1);
}).map(function(segment) {
return segment.name;
}).join(', ');
return ( return (
<div> <div>
<td className={ rowClasses }> <td className={ rowClasses }>
@ -68,6 +80,9 @@ define(
</strong> </strong>
{ actions } { actions }
</td> </td>
<td className="column" data-colname="Lists">
{ segments }
</td>
<td className="column-date" data-colname="Subscribed on"> <td className="column-date" data-colname="Subscribed on">
<abbr>{ newsletter.created_at }</abbr> <abbr>{ newsletter.created_at }</abbr>
</td> </td>

View File

@ -15,7 +15,7 @@ const App = React.createClass({
} }
}); });
let container = document.getElementById('newsletters'); let container = document.getElementById('newsletters_container');
if(container) { if(container) {
ReactDOM.render(( ReactDOM.render((

View File

@ -34,7 +34,8 @@ define(
placeholder: "Select a list", placeholder: "Select a list",
id: "mailpoet_segments", id: "mailpoet_segments",
endpoint: "segments", endpoint: "segments",
multiple: true multiple: true,
select2: true
}, },
{ {
name: 'sender', name: 'sender',

View File

@ -13,7 +13,7 @@ const App = React.createClass({
} }
}); });
let container = document.getElementById('segments'); let container = document.getElementById('segments_container');
if(container) { if(container) {
ReactDOM.render(( ReactDOM.render((

View File

@ -42,7 +42,7 @@ define(
sortable: true sortable: true
}, },
{ {
name: 'lists', name: 'segments',
label: 'Lists', label: 'Lists',
sortable: false sortable: false
}, },
@ -64,10 +64,13 @@ define(
name: 'moveToList', name: 'moveToList',
label: 'Move to list...', label: 'Move to list...',
onSelect: function() { onSelect: function() {
var field = {
id: 'move_to_segment',
endpoint: 'segments'
};
return ( return (
<Selection <Selection field={ field }/>
endpoint="segments"
id="move_to_segment" />
); );
}, },
getData: function() { getData: function() {
@ -80,10 +83,13 @@ define(
name: 'addToList', name: 'addToList',
label: 'Add to list...', label: 'Add to list...',
onSelect: function() { onSelect: function() {
var field = {
id: 'add_to_segment',
endpoint: 'segments'
};
return ( return (
<Selection <Selection field={ field }/>
endpoint="segments"
id="add_to_segment" />
); );
}, },
getData: function() { getData: function() {
@ -96,10 +102,13 @@ define(
name: 'removeFromList', name: 'removeFromList',
label: 'Remove from list...', label: 'Remove from list...',
onSelect: function() { onSelect: function() {
var field = {
id: 'remove_from_segment',
endpoint: 'segments'
};
return ( return (
<Selection <Selection field={ field }/>
endpoint="segments"
id="remove_from_segment" />
); );
}, },
getData: function() { getData: function() {

View File

@ -13,7 +13,7 @@ const App = React.createClass({
} }
}); });
let container = document.getElementById('subscribers'); let container = document.getElementById('subscribers_container');
if(container) { if(container) {
ReactDOM.render(( ReactDOM.render((

View File

@ -6,14 +6,14 @@ if(!defined('ABSPATH')) exit;
class BulkAction { class BulkAction {
private $listing = null; private $listing = null;
private $data = null; private $data = null;
private $model = null; private $model_class = null;
function __construct($model, $data) { function __construct($model_class, $data) {
$this->model = $model; $this->model_class = $model_class;
$this->data = $data; $this->data = $data;
$this->listing = new Handler( $this->listing = new Handler(
\Model::factory($this->model), $this->model_class,
$this->data['listing'] $this->data['listing']
); );
return $this; return $this;
@ -21,7 +21,7 @@ class BulkAction {
function apply() { function apply() {
return call_user_func_array( return call_user_func_array(
array($this->model, $this->data['action']), array($this->model_class, $this->data['action']),
array($this->listing, $this->data) array($this->listing, $this->data)
); );
} }

View File

@ -8,8 +8,12 @@ class Handler {
private $data = array(); private $data = array();
private $model = null; private $model = null;
function __construct($model, $data = array()) { function __construct($model_class, $data = array()) {
$this->model = $model; $class = new \ReflectionClass($model_class);
$this->table_name = $class->getStaticPropertyValue('_table');
$this->model = \Model::factory($model_class);
$this->data = array( $this->data = array(
// pagination // pagination
'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0), 'offset' => (isset($data['offset']) ? (int)$data['offset'] : 0),
@ -27,7 +31,7 @@ class Handler {
'selection' => (isset($data['selection']) ? $data['selection'] : null) 'selection' => (isset($data['selection']) ? $data['selection'] : null)
); );
$this->setFilter(); $this->model = $this->setFilter();
$this->setSearch(); $this->setSearch();
$this->setGroup(); $this->setGroup();
$this->setOrder(); $this->setOrder();
@ -42,10 +46,8 @@ class Handler {
private function setOrder() { private function setOrder() {
return $this->model return $this->model
->tableAlias('model')
->{'order_by_'.$this->data['sort_order']}( ->{'order_by_'.$this->data['sort_order']}(
'model.'.$this->data['sort_by'] $this->table_name.'.'.$this->data['sort_by']);
);
} }
private function setGroup() { private function setGroup() {
@ -57,20 +59,20 @@ class Handler {
private function setFilter() { private function setFilter() {
if($this->data['filter'] === null) { if($this->data['filter'] === null) {
return; return $this->model;
} }
return $this->model->filter('filterBy', $this->data['filter']); return $this->model->filter('filterBy', $this->data['filter']);
} }
function getSelection() { function getSelection() {
if(!empty($this->data['selection'])) { if(!empty($this->data['selection'])) {
$this->model->whereIn('model.id', $this->data['selection']); $this->model->whereIn('id', $this->data['selection']);
} }
return $this->model; return $this->model;
} }
function getSelectionIds() { function getSelectionIds() {
$models = $this->getSelection()->select('model.id')->findMany(); $models = $this->getSelection()->select('id')->findMany();
return array_map(function($model) { return array_map(function($model) {
return (int)$model->id; return (int)$model->id;
}, $models); }, $models);

View File

@ -62,11 +62,11 @@ class Newsletter extends Model {
$segment = Segment::findOne($filter['value']); $segment = Segment::findOne($filter['value']);
if($segment !== false) { if($segment !== false) {
$orm = $orm $orm = $orm
->select('model.*') ->select(MP_NEWSLETTERS_TABLE.'.*')
->select('newsletter_segment.id', 'newsletter_segment_id') ->select('newsletter_segment.id', 'newsletter_segment_id')
->join( ->join(
MP_NEWSLETTER_SEGMENT_TABLE, MP_NEWSLETTER_SEGMENT_TABLE,
'model.id = newsletter_segment.newsletter_id', MP_NEWSLETTERS_TABLE.'.id = newsletter_segment.newsletter_id',
'newsletter_segment' 'newsletter_segment'
) )
->where('newsletter_segment.segment_id', (int)$filter['value']); ->where('newsletter_segment.segment_id', (int)$filter['value']);

View File

@ -72,15 +72,16 @@ class Subscriber extends Model {
$segment = Segment::findOne($filter['value']); $segment = Segment::findOne($filter['value']);
if($segment !== false) { if($segment !== false) {
$orm = $orm /*$orm = $orm
->select('model.*') ->select(MP_SUBSCRIBERS_TABLE.'.*')
->select('subscriber_segment.id', 'subscriber_segment_id') ->select('subscriber_segment.id', 'subscriber_segment_id')
->join( ->join(
MP_SUBSCRIBER_SEGMENT_TABLE, MP_SUBSCRIBER_SEGMENT_TABLE,
'model.id = subscriber_segment.subscriber_id', MP_SUBSCRIBERS_TABLE.'.id = subscriber_segment.subscriber_id',
'subscriber_segment' 'subscriber_segment'
) )
->where('subscriber_segment.segment_id', (int)$filter['value']); ->where('subscriber_segment.segment_id', (int)$filter['value']);*/
$orm = $segment->subscribers();
} }
} }
} }

View File

@ -134,10 +134,23 @@ class Newsletters {
function listing($data = array()) { function listing($data = array()) {
$listing = new Listing\Handler( $listing = new Listing\Handler(
\Model::factory('\MailPoet\Models\Newsletter'), '\MailPoet\Models\Newsletter',
$data $data
); );
wp_send_json($listing->get());
$listing_data = $listing->get();
// fetch segments relations for each returned item
foreach($listing_data['items'] as &$item) {
$segments = NewsletterSegment::select('segment_id')
->where('newsletter_id', $item['id'])
->findMany();
$item['segments'] = array_map(function($relation) {
return $relation->segment_id;
}, $segments);
}
wp_send_json($listing_data);
} }
function bulk_action($data = array()) { function bulk_action($data = array()) {

View File

@ -22,7 +22,7 @@ class Segments {
function listing($data = array()) { function listing($data = array()) {
$listing = new Listing\Handler( $listing = new Listing\Handler(
\Model::factory('\MailPoet\Models\Segment'), '\MailPoet\Models\Segment',
$data $data
); );
wp_send_json($listing->get()); wp_send_json($listing->get());

View File

@ -24,7 +24,7 @@ class Subscribers {
function listing($data = array()) { function listing($data = array()) {
$listing = new Listing\Handler( $listing = new Listing\Handler(
\Model::factory('\MailPoet\Models\Subscriber'), '\MailPoet\Models\Subscriber',
$data $data
); );

View File

@ -1,7 +1,7 @@
<% extends 'layout.html' %> <% extends 'layout.html' %>
<% block content %> <% block content %>
<div id="newsletters"></div> <div id="newsletters_container"></div>
<%= localize({ <%= localize({
'pageTitle': __('Newsletters'), 'pageTitle': __('Newsletters'),

View File

@ -1,7 +1,7 @@
<% extends 'layout.html' %> <% extends 'layout.html' %>
<% block content %> <% block content %>
<div id="segments"></div> <div id="segments_container"></div>
<%= localize({ <%= localize({
'pageTitle': __('Segments'), 'pageTitle': __('Segments'),

View File

@ -1,7 +1,7 @@
<% extends 'layout.html' %> <% extends 'layout.html' %>
<% block content %> <% block content %>
<div id="subscribers"></div> <div id="subscribers_container"></div>
<%= localize({ <%= localize({
'pageTitle': __('Subscribers'), 'pageTitle': __('Subscribers'),
@ -9,6 +9,7 @@
'loadingItems': __('Loading subscribers...'), 'loadingItems': __('Loading subscribers...'),
'noItemsFound': __('No subscribers found.'), 'noItemsFound': __('No subscribers found.'),
'selectAllLabel': __('All subscribers on this page are selected.'), 'selectAllLabel': __('All subscribers on this page are selected.'),
'selectedAllLabel': __('All %d subscribers are selected.'),
'selectAllLink': __('Select all pages.'), 'selectAllLink': __('Select all pages.'),
'clearSelection': __('Clear selection.') 'clearSelection': __('Clear selection.')
}) %> }) %>