Skip to content

Commit ac7b33f

Browse files
author
Amir Tocker
committed
Refactor url generation to CloudinaryComponent.getUrl(). Add all properties to Image.
1 parent 3e55334 commit ac7b33f

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

src/components/CloudinaryComponent/CloudinaryComponent.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, {Component, PropTypes} from 'react';
2-
import cloudinary from 'cloudinary-core';
2+
import {Cloudinary, Configuration, Transformation, Util} from 'cloudinary-core';
33

4-
const camelCase = cloudinary.Util.camelCase;
5-
const snakeCase = cloudinary.Util.snakeCase;
4+
const camelCase = Util.camelCase;
5+
const snakeCase = Util.snakeCase;
66

77
export default class CloudinaryComponent extends Component {
88
constructor(props, context) {
@@ -63,8 +63,15 @@ export default class CloudinaryComponent extends Component {
6363
}
6464
return options;
6565
}
66+
67+
getUrl(props, context = {}) {
68+
var options = this.getOptions(props, context);
69+
let cl = Cloudinary.new(options);
70+
return cl.url(props.publicId, options);
71+
}
72+
6673
}
67-
CloudinaryComponent.VALID_OPTIONS = cloudinary.Configuration.CONFIG_PARAMS.concat(cloudinary.Transformation.new().PARAM_NAMES).map( camelCase);
74+
CloudinaryComponent.VALID_OPTIONS = Configuration.CONFIG_PARAMS.concat(Transformation.new().PARAM_NAMES).map( camelCase);
6875
CloudinaryComponent.contextTypes = typesFrom(CloudinaryComponent.VALID_OPTIONS);
6976

7077
CloudinaryComponent.propTypes = CloudinaryComponent.contextTypes ;
@@ -73,13 +80,13 @@ CloudinaryComponent.childContextTypes = {};
7380

7481
/**
7582
* Create a React type definition object. All items are PropTypes.string.
76-
* @param {Array} configparams a list of parameter names
83+
* @param {Array} configParams a list of parameter names
7784
* @returns {Object}
7885
*/
79-
function typesFrom(configparams) {
80-
configparams = configparams || [];
86+
function typesFrom(configParams) {
87+
configParams = configParams || [];
8188
const types = {};
82-
for (let key of configparams) {
89+
for (let key of configParams) {
8390
types[camelCase(key)] = PropTypes.string;
8491
}
8592
return types;

src/components/Image/Image.js

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
import React, {Component, PropTypes} from 'react';
2-
import cloudinary from 'cloudinary-core';
2+
import {Transformation} from 'cloudinary-core';
33
import CloudinaryComponent from '../CloudinaryComponent';
44

55
export default class Image extends CloudinaryComponent {
66
constructor(props, context) {
77
super(props, context);
8-
var options = this.getOptions(props, context);
9-
let cl = cloudinary.Cloudinary.new(options);
10-
var url = cl.url(props.publicId, options);
11-
this.state = {url: url};
8+
let url = this.getUrl(props, context);
9+
this.state = {url};
1210
}
1311

1412
componentWillReceiveProps(nextProps, nextContext) {
15-
var options = this.getOptions(nextProps, nextContext);
16-
17-
let cl = cloudinary.Cloudinary.new(options);
18-
var url = cl.url(this.props.publicId, options);
19-
if(url != this.state.url){
20-
this.setState({
21-
url: url
22-
})
13+
let url = this.getUrl(nextProps, nextContext);
14+
if(url !== this.state.url){
15+
this.setState({url});
2316
}
2417
}
2518

@@ -44,28 +37,13 @@ export default class Image extends CloudinaryComponent {
4437

4538
render() {
4639
var options = this.getOptions(this.props, this.context);
47-
var attributes = cloudinary.Transformation.new(options).toHtmlAttributes();
40+
var attributes = Transformation.new(options).toHtmlAttributes();
4841
return (
4942
<img {...attributes} src={this.state.url} />
5043
);
5144
}
5245
}
5346

54-
Image.propTypes = {
55-
publicId: PropTypes.string.isRequired,
56-
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
57-
height: PropTypes.number,
58-
transformation: PropTypes.oneOfType([
59-
PropTypes.string,
60-
PropTypes.object,
61-
PropTypes.arrayOf(PropTypes.object)
62-
]),
63-
handleLoad: PropTypes.func,
64-
breakpoints: PropTypes.oneOfType([
65-
PropTypes.func,
66-
PropTypes.arrayOf(PropTypes.number)
67-
])
68-
69-
};
7047
Image.defaultProps = {};
71-
Image.contextTypes = CloudinaryComponent.contextTypes;
48+
Image.contextTypes = CloudinaryComponent.contextTypes;
49+
Image.propTypes = CloudinaryComponent.propTypes;

0 commit comments

Comments
 (0)