Skip to content

Commit

Permalink
Added Posts and Taxonomies Dropdown for #5
Browse files Browse the repository at this point in the history
  • Loading branch information
andreilupu committed Apr 19, 2017
1 parent 59543f6 commit 4488b9f
Show file tree
Hide file tree
Showing 7 changed files with 6,101 additions and 3,520 deletions.
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ var reactdom = require('react-dom');
var babel = require('babelify');

function compile_admin(watch) {
var bundler = watchify(browserify('./src/socket.js', { debug: false }).transform(babel, { presets: ["es2015", "stage-0", "react"]}));
var bundler = watchify(browserify('./src/socket.js', { debug: true }).transform(babel, { presets: ["es2015", "stage-0", "react"]}));

function rebundle_admin() {
bundler.bundle()
.on('error', function(err) { console.error(err); this.emit('end'); })
.pipe(source('socket.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: false }))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./js'));
}
Expand Down
9,307 changes: 5,813 additions & 3,494 deletions js/socket.js

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function socket_options_page() {
<div class="socket-wrapper">
<header class="title">
<h1 class="page-title"><?php echo $this->page_title ?></h1>
<div class="description"><?php echo $this->description ?></div>
<!-- <div class="description">--><?php //echo $this->description ?><!--</div>-->
</header>
<div class="content">
<div id="socket_dashboard"></div>
Expand Down Expand Up @@ -143,9 +143,13 @@ public function enqueue_styles() {
*/
public function enqueue_scripts() {
if ( $this->is_socket_dashboard() ) {

wp_enqueue_media();

wp_enqueue_script( 'socket-dashboard', plugin_dir_url( __FILE__ ) . 'js/socket.js', array(
'jquery',
'wp-util'
'wp-util',
'wp-api'
),
filemtime( plugin_dir_path( __FILE__ ) . 'js/socket.js' ), true );

Expand All @@ -172,20 +176,21 @@ function localize_js_data( $script ) {

function add_rest_routes_api() {
//The Following registers an api route with multiple parameters.
register_rest_route( $this->plugin . '/v1', '/option', array(
$route = 'socket';
register_rest_route( $route . '/v1', '/option', array(
'methods' => 'GET',
'callback' => array( $this, 'rest_get_state' ),
'permission_callback' => array( $this, 'permission_nonce_callback' )
) );

register_rest_route( $this->plugin . '/v1', '/option', array(
register_rest_route( $route . '/v1', '/option', array(
'methods' => 'POST',
'callback' => array( $this, 'rest_set_state' ),
'permission_callback' => array( $this, 'permission_nonce_callback' )
) );

// debug tools
register_rest_route( $this->plugin . '/v1', '/cleanup', array(
register_rest_route( $route . '/v1', '/cleanup', array(
'methods' => 'POST',
'callback' => array( $this, 'rest_cleanup' ),
'permission_callback' => array( $this, 'permission_nonce_callback' ),
Expand Down Expand Up @@ -222,6 +227,7 @@ function rest_set_state() {
$option_value = $_POST['value'];

$this->values[ $option_name ] = $option_value;

wp_send_json_success( $this->save_values() );
}

Expand Down
12 changes: 4 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@
"gulp-ruby-sass": "~0.7.1",
"gulp-sass": "^3.1.0",
"gulp-sourcemaps": "^2.4.0",
"material-ui": "^0.16.7",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-tap-event-plugin": "^2.0.1",
"rtlcss": "*",
"semantic-ui": "^2.2.9",
"semantic-ui": "^2.2.10",
"semantic-ui-react": "^0.68.0",
"susy": "^2.2.12",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.9.0"
},
"dependencies": {
"semantic-ui-react": "^0.66.0"
}
}
47 changes: 36 additions & 11 deletions src/components/dashboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from "react";
import ReactDOM from "react-dom";
import SocketPostSelect from "./postSelect.js";
import SocketTaxSelect from "./taxSelect.js";

import {
Grid,
Expand Down Expand Up @@ -36,6 +38,7 @@ class SocketDashboard extends React.Component {
this.radioHandleChange = this.radioHandleChange.bind(this);
this.multicheckboxHandleChange = this.multicheckboxHandleChange.bind(this);
this.clean_the_house = this.clean_the_house.bind(this);
this.setup_loading_flag = this.setup_loading_flag.bind(this);
}

render() {
Expand All @@ -45,7 +48,7 @@ class SocketDashboard extends React.Component {

{ ( component.state.loading === true ) ?
<div style={{"position": 'absolute', "top": 0, "bottom": 0, "right": 0, "left": 0}}>
<Dimmer active>
<Dimmer active inverted>
<Loader size='big'/>
<Divider inverted/>
<Divider inverted/>
Expand Down Expand Up @@ -168,18 +171,40 @@ class SocketDashboard extends React.Component {
case 'select' : {
let dropDownOptions = [];

{
Object.keys(field.options).map(function (opt) {
dropDownOptions.push({key: opt, value: opt, text: field.options[opt]});
})
}
{Object.keys(field.options).map(function (opt) {
dropDownOptions.push({key: opt, value: opt, text: field.options[opt]});
})}

output = <Form.Field key={field_key}>
<Dropdown placeholder={placeholder} search selection defaultValue={value}
options={dropDownOptions} onChange={component.radioHandleChange}/>
<Dropdown
placeholder={placeholder}
search
selection
defaultValue={value}
options={dropDownOptions}
onChange={component.radioHandleChange}/>
</Form.Field>
break;
}

case 'post_select' : {
if ( '' === value ) {
value = []
}

output = <SocketPostSelect key={field_key} name={field_key} value={value} field={field} placeholder={placeholder} setup_loading_flag={component.setup_loading_flag} />
break;
}

case 'tax_select' : {
if ( '' === value ) {
value = []
}

output = <SocketTaxSelect key={field_key} name={field_key} value={value} field={field} placeholder={placeholder} setup_loading_flag={component.setup_loading_flag} />
break;
}

default:
break
}
Expand All @@ -202,7 +227,6 @@ class SocketDashboard extends React.Component {
}

validate_options_for_checkboxes(value) {

if (typeof value === 'number') {
return ( value == 1 );
}
Expand Down Expand Up @@ -306,10 +330,8 @@ class SocketDashboard extends React.Component {
loading: true,
});
});

});
}

}

checkboxHandleChange(e) {
Expand Down Expand Up @@ -445,6 +467,9 @@ class SocketDashboard extends React.Component {
return notices;
}

setup_loading_flag( $val ){
this.setState( { loading: $val })
}

clean_the_house = () => {
let component = this,
Expand Down
120 changes: 120 additions & 0 deletions src/components/postSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import React from "react"
import ReactDOM from "react-dom"
import PropTypes from 'prop-types'
// import _ from 'lodash'

import {
Form,
Dropdown
} from 'semantic-ui-react'

export default class SocketPostSelect extends React.Component {

static propTypes = {
name: PropTypes.string,
value: PropTypes.array,
setup_loading_flag: PropTypes.func
}

constructor(props) {
// this makes the this
super(props);

// get the current state localized by wordpress
this.state = {
loading: true,
posts: [],
name: null,
value: this.props.value
};

this.handleClose = this.handleClose.bind(this);
}

render() {
let component = this,
output = null,
value = this.props.value;

if ( '' === value ) {
value = []
}

output = <Form.Field className="post_type_select" >
<Dropdown
placeholder={this.props.placeholder}
search
selection
closeOnBlur={false}
multiple={true}
loading={this.state.loading}
defaultValue={value}
options={this.state.posts}
onChange={component.handleChange}
onClose={component.handleClose}
/>
</Form.Field>

return output;
}

handleChange = (e, { value }) => {
this.setState({ value });
}

// on close we want to save the data
handleClose(e){
let component = this,
value = this.state.value

if ( '' === value || [] === value ) {
return;
}

component.props.setup_loading_flag( true );

jQuery.ajax({
url: socket.wp_rest.root + 'socket/v1/option',
method: 'POST',
beforeSend: function (xhr) {
xhr.setRequestHeader('X-WP-Nonce', socket.wp_rest.nonce);
},
data: {
'socket_nonce': socket.wp_rest.socket_nonce,
name: this.props.name,
value: value
}
}).done(function (response) {
// let new_values = component.state.values;
console.log(response);
component.props.setup_loading_flag( false );
}).error(function (err) {
console.log(err);
component.props.setup_loading_flag( false );
});
}

componentWillMount(){
if ( ! this.state.loading ) {
return false;
}

let component = this;

// load all the posts
wp.api.loadPromise.done( function() {
var wpPosts = new wp.api.collections.Posts(),
posts = [];

wpPosts.fetch( { data: { per_page: 100 } } ).done(function (models) {
{Object.keys(models).map(function ( i ) {
let model = models[i];

posts.push({ key: model.id, value: model.id.toString(), text: model.title.rendered });
})}

component.setState( { posts: posts, loading: false } );
});
});
}
}
Loading

0 comments on commit 4488b9f

Please sign in to comment.