Skip to content

Commit

Permalink
Issue #34 - pass and expect post_type for server side rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Feb 1, 2020
1 parent c1e7923 commit 1aa3df7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
11 changes: 9 additions & 2 deletions blocks/oik-content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const shortcode_attributes =
default: '',
},

postType: {
post_type: {
type: 'string',
default: '',
}
Expand Down Expand Up @@ -152,6 +152,11 @@ export default registerBlockType(
setAttributes( { [key] : value } );
};

const onChangePostType = ( value ) => {
attributes = getAttributes( value );
setAttributes( { post_type: value });
}


/*
<GenericAttrs value={attributes.shortcode} />
Expand All @@ -177,7 +182,7 @@ export default registerBlockType(
placeholder={ __( 'Enter your shortcode content' ) }
onChange={onChangeContent}
/>
<PostTypes />
<PostTypes value={ attributes.post_type } onChange={onChangePostType } />



Expand All @@ -195,9 +200,11 @@ export default registerBlockType(


</div>
<div>
<ServerSideRender
block="oik/content-block" attributes={ attributes }
/>
</div>
</Fragment>

);
Expand Down
36 changes: 34 additions & 2 deletions blocks/oik-content/post_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
///>


import {bw_shortcodes} from "./bw_shortcodes";

const { withSelect, select, subscribe } = wp.data;
const { Component } = wp.element;
//const { getPostTypes } = subscribe( 'core' );
//const bw_post_types = subscribe( 'core' ).getPostTypes();

const { SelectControl } = wp.components;



export class PostTypes extends Component {
constructor() {
super( ...arguments );
this.state = {
postTypes: [],
postType: "",

};
console.log( this.state);
console.log( this );
}

componentDidMount() {
Expand All @@ -45,13 +51,39 @@ export class PostTypes extends Component {
}
}


postTypeListSelect( props ) {
var postTypes = this.state.postTypes;
if ( postTypes ) {
var options = postTypes.map(( postType ) => this.postTypeOption( postType ) );
return(
<SelectControl label="Post Type" value={this.props.postType}
options={options}
onChange={ this.props.onChange}
/>
);
} else {
return( <p>Loading post types</p>);
}
}

/**
* Map the postTypes to a select list
* @param postType
* @returns {*}
*/

postTypeMap( postType ) {
console.log( postType );
return( <li>{postType.slug}</li>);
}

postTypeOption( postType ) {
return( { value: postType.slug, label: postType.name });
}

render() {
return( this.postTypeList()
return( this.postTypeListSelect()
);
}

Expand Down
1 change: 1 addition & 0 deletions oik-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ function oik_blocks_register_dynamic_blocks() {
[ 'shortcode' => [ 'type'=>'string' ]
,'content' => [ 'type' => 'string ']
, 'parameters' => [ 'type' => 'string']
, 'post_type' => ['type' => 'string']

]
] );
Expand Down

0 comments on commit 1aa3df7

Please sign in to comment.