Skip to content

Instantly share code, notes, and snippets.

@jossmac
Created August 2, 2016 09:20
Show Gist options
  • Save jossmac/8fdec86622d87f3935f3ba4655486627 to your computer and use it in GitHub Desktop.
Save jossmac/8fdec86622d87f3935f3ba4655486627 to your computer and use it in GitHub Desktop.
Temporarily removed the CloudinaryImageField.js renderImageSelect method
renderImageSelect () {
var selectPrefix = this.props.selectPrefix;
var self = this;
var getOptions = function (input, callback) {
// build our url, accounting for selectPrefix
var uri = Keystone.adminPath + '/api/cloudinary/autocomplete';
if (selectPrefix) {
uri = uri + '?prefix=' + selectPrefix;
}
// make the request
xhr({
body: JSON.stringify({
q: input,
}),
uri: uri,
headers: {
'Content-Type': 'application/json',
},
}, function (err, resp, body) {
// callback with err
if (err) {
callback(null, {
options: [],
complete: false,
});
return;
}
// try and parse the response
try {
var data = JSON.parse(body);
var options = [];
data.items.forEach(function (item) {
options.push({
value: item.public_id,
label: item.public_id,
});
});
callback(null, {
options: options,
complete: true,
});
} catch (e) {
callback(null, {
options: [],
complete: false,
});
}
});
};
// listen for changes
var onChange = function onChange (data) {
if (data && data.value) {
self.setState({ selectedCloudinaryImage: data.value });
} else {
self.setState({ selectedCloudinaryImage: null });
}
};
return (
<div className="image-select">
<Select.Async
placeholder="Search for an image from Cloudinary ..."
name={this.props.paths.select}
value={this.state.selectedCloudinaryImage}
onChange={onChange}
id={'field_' + this.props.paths.select}
loadOptions={getOptions}
/>
</div>
);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment