Skip to content

Commit 2cdb534

Browse files
author
Amir Tocker
committed
Pass all properties to children of CloudinaryContext.
1 parent fe07047 commit 2cdb534

File tree

3 files changed

+76
-35
lines changed

3 files changed

+76
-35
lines changed
Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import React, {Component, PropTypes} from 'react';
2+
import cloudinary from 'cloudinary-core';
23

34
export default class CloudinaryContext extends React.Component {
4-
constructor(props) {
5-
super(props);
5+
constructor(props, context) {
6+
super(props, context);
67
this.state = {};
78
}
89

910
getChildContext() {
10-
return {cloud_name: "demo"};
11+
let {children, ...otherProps} = this.props;
12+
return Object.assign({}, this.context, otherProps);
1113
}
1214

13-
componentWillReceiveProps(nextProps) {
15+
componentWillReceiveProps(nextProps, nextContext) {
1416
}
1517

1618
componentWillMount() {
@@ -22,27 +24,35 @@ export default class CloudinaryContext extends React.Component {
2224
componentWillUnmount() {
2325
}
2426

25-
componentWillUpdate(nextProps, nextState) {
27+
componentWillUpdate(nextProps, nextState, nextContext) {
2628
}
2729

28-
componentDidUpdate(prevProps, prevState) {
30+
componentDidUpdate(prevProps, prevState, prevContext) {
2931
}
3032

31-
shouldComponentUpdate(nextProps, nextState) {
33+
shouldComponentUpdate(nextProps, nextState, nextContext) {
3234
return true;
3335
}
3436

3537
render() {
3638
return (
37-
<span>CloudinaryContext Not Implemented yet</span>
39+
<div>{this.props.children}</div>
3840
);
3941
}
4042
}
41-
CloudinaryContext.propTypes = {initialCount: React.PropTypes.number};
42-
CloudinaryContext.defaultProps = {initialCount: 0};
43+
CloudinaryContext.propTypes = {};
44+
CloudinaryContext.defaultProps = {};
4345
CloudinaryContext.contextTypes = {
44-
cloud_name: PropTypes.string.isRequired
46+
cloudName: PropTypes.string.isRequired
4547
};
4648
CloudinaryContext.childContextTypes = {
47-
cloud_name: PropTypes.string.isRequired
48-
};
49+
cloudName: PropTypes.string.isRequired
50+
};
51+
for (let key of cloudinary.Configuration.CONFIG_PARAMS) {
52+
CloudinaryContext.childContextTypes[cloudinary.Util.camelCase(key)] = PropTypes.string;
53+
}
54+
for (let key of cloudinary.Transformation.new().PARAM_NAMES) {
55+
CloudinaryContext.childContextTypes[cloudinary.Util.camelCase(key)] = PropTypes.string;
56+
}
57+
58+
console.log(CloudinaryContext.childContextTypes);

src/components/Image/Image.js

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,40 @@ import React, {Component, PropTypes} from 'react';
22
import cloudinary from 'cloudinary-core';
33

44
export default class Image extends React.Component {
5-
constructor(props) {
6-
super(props);
7-
this.state = {bar: props.initialBar};
5+
constructor(props, context) {
6+
super(props, context);
7+
var options = Object.assign({}, context, props);
8+
var options2 = this.snakeCase(options);
9+
let cl = cloudinary.Cloudinary.new(options2);
10+
// let tr = cloudinary.Transformation.new(options);
11+
//
12+
console.log("trying url", props, options2);
13+
var url = cl.url(props.publicId, options2);
14+
console.log(url);
15+
this.state = {url: url};
816
// this.foo = this.foo.bind(this);
917
}
1018

19+
snakeCase(options) {
20+
let res = {};
21+
for(let key of Object.keys(options)) {
22+
res[cloudinary.Util.snakeCase(key)] = options[key];
23+
}
24+
return res;
25+
}
26+
1127
componentWillReceiveProps(nextProps, nextContext) {
12-
var config = {};
13-
cloudinary.Configuration.CONFIG_PARAMS.forEach((param)=> {
14-
if(nextProps[param]){
15-
config[param] = nextProps[param];
16-
}
17-
});
28+
var options = Object.assign({}, nextContext, nextProps);
1829

30+
let cl = cloudinary.Cloudinary.new(options);
31+
let tr = cloudinary.Transformation.new(options);
32+
var url = cl.url(this.props.publicId, options);
33+
console.log(url);
34+
var transformation = tr.toString();
35+
console.log(transformation);
1936
this.setState({
20-
url: ""
37+
url: url,
38+
transformation: transformation
2139
})
2240
}
2341

@@ -41,11 +59,11 @@ export default class Image extends React.Component {
4159
}
4260

4361
render() {
44-
let cl = cloudinary.Cloudinary.new({cloud_name: "demo"});
62+
var options = Object.assign({}, this.context, this.props);
4563
var {publicId, transformation, ...other} = this.props;
46-
var url = cl.url(publicId, transformation);
64+
var attributes = cloudinary.Transformation.new(options).toHtmlAttributes();
4765
return (
48-
<img {...other} src={url} />
66+
<img {...attributes} src={this.state.url} />
4967
);
5068
}
5169
}
@@ -57,16 +75,17 @@ Image.propTypes = {
5775
transformation: PropTypes.oneOfType([
5876
PropTypes.string,
5977
PropTypes.object,
60-
PropTypes.arrayOf(React.PropTypes.object)
78+
PropTypes.arrayOf(PropTypes.object)
6179
]),
6280
handleLoad: PropTypes.func,
6381
breakpoints: PropTypes.oneOfType([
6482
PropTypes.func,
65-
PropTypes.arrayOf(React.PropTypes.number)
83+
PropTypes.arrayOf(PropTypes.number)
6684
])
6785

6886
};
6987
Image.defaultProps = {};
7088
Image.contextTypes = {
71-
config: React.PropTypes.object
89+
cloudName: PropTypes.string,
90+
angle: PropTypes.string
7291
};

stories/index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ storiesOf('Cloudinary', module)
1010
.add('image', ()=> {
1111
let t = { width: 0.5, crop: "scale"};
1212
return (
13-
<Image publicId="sample" transformation={t}/>
13+
<Image cloudName="demo" publicId="sample" transformation={t}/>
1414
)})
1515
.add('image with alt', ()=> {
1616
let t = { width: 0.5, crop: "scale"};
1717
return(
18-
<Image publicId="does-not-exist" alt="This image is intentionally missing" transformation={t}/>
18+
<Image cloudName="demo" publicId="does-not-exist" alt="This image is intentionally missing" transformation={t}/>
1919
)
2020
})
2121
.add('image with style', ()=> {
2222
let t = { width: 0.5, crop: "scale"};
2323
return(
24-
<Image publicId="sample" style={{border: "20px solid"}} transformation={t}/>
24+
<Image cloudName="demo" publicId="sample" style={{border: "20px solid"}} transformation={t}/>
2525
)
2626
})
2727
.add('Video', ()=> {
@@ -31,10 +31,22 @@ storiesOf('Cloudinary', module)
3131
.add('CloudinaryContext', ()=> {
3232
let t = { width: 0.5, crop: "scale"};
3333
return (
34-
<CloudinaryContext >
35-
<div>
34+
<CloudinaryContext cloudName="demo" >
35+
<ul><li>
36+
<Image publicId="sample" transformation={t} width="50"/>
37+
</li></ul>
38+
39+
<Image publicId="sample" transformation={t}/>
40+
</CloudinaryContext>
41+
)})
42+
.add('CloudinaryContext - nested', ()=> {
43+
let t = { width: 0.5, crop: "scale"};
44+
return (
45+
<CloudinaryContext cloudName="demo" angle="30">
46+
<Image publicId="sample" width="50"/>
47+
<CloudinaryContext cloudName="demo" angle="20" width="100">
3648
<Image publicId="sample" transformation={t}/>
37-
</div>
49+
</CloudinaryContext>
3850
</CloudinaryContext>
3951
)})
4052
;

0 commit comments

Comments
 (0)