From bf5d7f273befbb4736645b46cd6aaefac7f709dc Mon Sep 17 00:00:00 2001 From: Jonathan Labreuille Date: Fri, 21 Aug 2015 13:12:14 +0200 Subject: [PATCH] Settings JSX - fixed issue with token in MailPoet Ajax / JSX - added settings form with get/set - reformated some code in subscribers.jsx --- assets/js/src/ajax.js | 10 ++- assets/js/src/settings.jsx | 121 ++++++++++++++++++++++++++++++---- assets/js/src/subscribers.jsx | 48 ++++++-------- lib/Router/Router.php | 2 +- views/settings.html | 52 +-------------- views/subscribers.html | 3 +- 6 files changed, 143 insertions(+), 93 deletions(-) diff --git a/assets/js/src/ajax.js b/assets/js/src/ajax.js index e5dec0ebfc..9b8cf420d6 100644 --- a/assets/js/src/ajax.js +++ b/assets/js/src/ajax.js @@ -1,12 +1,13 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) { "use strict"; MailPoet.Ajax = { - version: 0.1, + version: 0.5, options: {}, defaults: { url: null, endpoint: null, action: null, + token: null, data: {}, onSuccess: function(data, textStatus, xhr) {}, onError: function(xhr, textStatus, errorThrown) {} @@ -31,7 +32,7 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) { // set default token if(this.options.token === null) { - this.options.token = mailpoet_token; + this.options.token = window.mailpoet_token; } }, request: function(method, options) { @@ -44,7 +45,7 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) { token: this.options.token, endpoint: this.options.endpoint, method: this.options.action, - data: this.options.data + data: this.options.data || {} }; // make ajax request depending on method @@ -65,6 +66,9 @@ define('ajax', ['mailpoet', 'jquery'], function(MailPoet, jQuery) { error : this.options.onError }); } + + // clear options + this.options = {}; } }; }); diff --git a/assets/js/src/settings.jsx b/assets/js/src/settings.jsx index f3db38e0f1..36504c9359 100644 --- a/assets/js/src/settings.jsx +++ b/assets/js/src/settings.jsx @@ -1,19 +1,118 @@ -define('settings', ['react'], function(React) { - var CommentBox = React.createClass({ +define('settings', ['react/addons', 'jquery', 'mailpoet'], function(React, jQuery, MailPoet) { + var SettingsForm = React.createClass({ + mixins: [React.addons.LinkedStateMixin], + load: function() { + this.setState({ loading: true }); + + MailPoet.Ajax.post({ + endpoint: 'settings', + action: 'get', + onSuccess: function(response) { + // index settings by key + var settings = {}; + + jQuery.each(response, function(i, setting) { + settings[setting.name] = setting.value; + }); + + this.setState({ + loading: false, + settings: settings + }); + }.bind(this) + }); + }, + save: function(e) { + this.setState({ loading: true }); + + e.preventDefault(); + + // format data + var settings = []; + + jQuery.each(this.state.settings, function(name, value) { + settings.push({ + name: name, + value: value + }); + }); + + MailPoet.Ajax.post({ + endpoint: 'settings', + action: 'set', + data: settings, + onSuccess: function(response) { + this.setState({ loading: false }); + }.bind(this) + }) + }, + getInitialState: function() { + return { + loading: false, + settings: {} + }; + }, + linkSettingValue: function(key) { + return { + value: this.state['settings'][key], + requestChange: function(newValue) { + var settings = this.state.settings; + settings[key] = newValue; + this.setState({ 'settings': settings }); + }.bind(this) + }; + }, + componentDidMount: function() { + this.load(); + }, render: function() { return ( -
- Hello, world! I am a CommentBox. -
- ); +
+

+ +

+ +

+ +

+ +

+ +

+ + +
+ ); } }); - var element = document.getElementById('settings-container'); - if (element) { + var element = jQuery('#mailpoet_settings'); + + if(element.length > 0) { React.render( - , - document.getElementById('settings-container') - ); + , + element[0] + ); } }); diff --git a/assets/js/src/subscribers.jsx b/assets/js/src/subscribers.jsx index 6f7f267643..a9e858ff6f 100644 --- a/assets/js/src/subscribers.jsx +++ b/assets/js/src/subscribers.jsx @@ -1,4 +1,4 @@ -define('subscribers', ['react', 'jquery'], function(React, jQuery) { +define('subscribers', ['react', 'jquery', 'mailpoet'], function(React, jQuery, MailPoet) { var data = [ { @@ -16,29 +16,23 @@ define('subscribers', ['react', 'jquery'], function(React, jQuery) { var Subscriber = React.createClass({ render: function() { return ( -
+

- {this.props.subscriber.first_name} {this.props.subscriber.last_name} + {this.props.subscriber.first_name} {this.props.subscriber.last_name}

{this.props.subscriber.email} -
- ); +
+ ); } }); var SubscribersList = React.createClass({ load: function() { - jQuery.ajax({ - url: ajaxurl, - type: 'post', - data: { - action: 'mailpoet', - token: mailpoet_token, - endpoint: 'subscribers', - method: 'get', - data: '' - }, - success : function(response) { + MailPoet.Ajax.post({ + endpoint: 'subscribers', + action: 'get', + data: {}, + onSuccess: function(response) { this.setState({data: response}); }.bind(this) }); @@ -53,23 +47,23 @@ define('subscribers', ['react', 'jquery'], function(React, jQuery) { render: function() { var nodes = this.state.data.map(function (subscriber) { return ( - - ); + + ); }); return ( -
- {nodes} -
- ); +
+ {nodes} +
+ ); } }); + var element = jQuery('#mailpoet_subscribers'); - var element = document.getElementById('mailpoet_subscribers'); - if (element) { + if(element.length > 0) { React.render( - , - document.getElementById('mailpoet_subscribers') - ); + , + element[0] + ); } }); diff --git a/lib/Router/Router.php b/lib/Router/Router.php index 6ec0a0f206..2ee08f6bb9 100644 --- a/lib/Router/Router.php +++ b/lib/Router/Router.php @@ -24,7 +24,7 @@ class Router { $class = ucfirst($_POST['endpoint']); $endpoint = __NAMESPACE__ . "\\" . $class; $method = $_POST['method']; - $data = $_POST['data']; + $data = isset($_POST['data']) ? $_POST['data'] : array(); $endpoint = new $endpoint(); $endpoint->$method($data); } diff --git a/views/settings.html b/views/settings.html index 7a4240ba01..09f7d7e4be 100644 --- a/views/settings.html +++ b/views/settings.html @@ -1,55 +1,7 @@ <% extends 'layout.html' %> <% block content %> -

<%= 'Settings' %>

+

<%= __('Settings') %>

-
-

- -

- -

- -

- -

- -

- -

- -

-
- - +
<% endblock %> diff --git a/views/subscribers.html b/views/subscribers.html index 319341e1d6..55db151e66 100644 --- a/views/subscribers.html +++ b/views/subscribers.html @@ -1,6 +1,7 @@ <% extends 'layout.html' %> <% block content %> -

<%= 'Subscribers' %>

+

<%= __('Subscribers') %>

+
<% endblock %>