(function( element, blockEditor, blocksConfig ) {
const el = element.createElement;
const __ = wp.i18n.__;
const availableBlocks = blocksConfig.blockTypes;
const { SelectControl } = wp.components;
const knownBlocks = {
'sociallocker': {
'title': __('Social Locker', 'bizpanda'),
'description': __('Hides content inside the block behind a social locker.', 'bizpanda'),
'keywords': [ 'locker', 'sociallocker', 'social locker', 'social', 'lock' ],
'transformsFrom': ['bizpanda/signinlocker', 'bizpanda/emaillocker'],
'icon':
},
'signinlocker': {
'title': __('Sign-In Locker', 'bizpanda'),
'description': __('Hides content inside the block behind a sign-in locker.', 'bizpanda'),
'keywords': [ 'locker', 'signinlocker', 'signin locker', 'social', 'lock' ],
'transformsFrom': ['bizpanda/sociallocker', 'bizpanda/emaillocker'],
'icon':
},
'emaillocker': {
'title': __('Email Locker', 'bizpanda'),
'description': __('Hides content inside the block behind an email locker.', 'bizpanda'),
'keywords': [ 'locker', 'emaillocker', 'email locker', 'optin', 'opt-in', 'lock' ],
'transformsFrom': ['bizpanda/sociallocker', 'bizpanda/signinlocker'],
'icon':
}
};
/**
* Locker Select
*/
class LockerSelect extends wp.element.Component {
constructor() {
super(...arguments);
this.state = {
isLoading: true,
selectedId: this.props.lockerId ? parseInt( this.props.lockerId ) : 0,
options: []
};
this.handleSelectChange = this.handleSelectChange.bind(this);
}
componentDidMount() {
const self = this;
const request = jQuery.ajax(window.ajaxurl, {
type: 'post',
dataType: 'json',
data: {
action: 'get_opanda_lockers',
shortcode: self.props.shortcode
}
});
request.done(function( data ){
const options = [];
let hasSelected = false;
let defaultLocker = false;
for ( let locker of data ) {
if ( self.state.selectedId && self.state.selectedId === parseInt( locker.id ) ) {
hasSelected = true;
}
const item = { label: locker.title, value: locker.id, shortcode: locker.shortcode };
if ( !defaultLocker ) defaultLocker = item;
options.push(item);
}
if ( !hasSelected && defaultLocker ) {
console.log(defaultLocker);
self.props.onChange(defaultLocker);
}
self.setState({
isLoading: false,
options: options,
selectedId: hasSelected ? self.state.selectedId : defaultLocker.value
});
});
}
handleSelectChange( lockerId ) {
let option = null;
for ( let optionItem of this.state.options ) {
if ( parseInt( optionItem.value ) === parseInt( lockerId ) ) {
option = optionItem;
this.setState({
selectedId: lockerId ? parseInt( lockerId ) : 0
});
break;
}
}
this.props.onChange( option );
}
render() {
const self = this;
let options = this.state.options;
if ( this.state.isLoading ) {
options = [{label: __('Loading...', 'bizpanda'), value: 0}];
}
const hasLockers = options.length > 0;
if ( !hasLockers ) {
options = [{label: __('[ - empty - ]', 'bizpanda'), value: 0}];
}
const hasEditableItem = ( !this.state.isLoading && hasLockers && this.state.selectedId ) ? true : false;
const hasAddAbility = !this.state.isLoading;
return (
<>