2017-05-26 14:18:09 +00:00
|
|
|
import React, { PropTypes } from 'react';
|
2017-05-25 16:00:53 +00:00
|
|
|
import { requireNativeComponent, View } from 'react-native';
|
2017-05-26 16:55:22 +00:00
|
|
|
import { statics } from './';
|
|
|
|
import { nativeToJSError } from '../../utils';
|
2017-05-25 16:00:53 +00:00
|
|
|
|
2017-05-26 14:18:09 +00:00
|
|
|
class Banner extends React.Component {
|
2017-05-25 16:00:53 +00:00
|
|
|
|
2017-05-26 14:18:09 +00:00
|
|
|
static propTypes = {
|
|
|
|
...View.propTypes,
|
2017-05-26 16:55:22 +00:00
|
|
|
// TODO ehesp: cant init this outside of the component; statics isn't defined
|
|
|
|
...(() => {
|
|
|
|
const eventProps = {};
|
|
|
|
Object.keys(statics.EventTypes).forEach((key) => {
|
|
|
|
eventProps[key] = PropTypes.func;
|
|
|
|
});
|
|
|
|
return eventProps;
|
|
|
|
}),
|
2017-05-26 14:18:09 +00:00
|
|
|
size: PropTypes.string,
|
|
|
|
unitId: PropTypes.string,
|
2017-05-26 16:55:22 +00:00
|
|
|
testing: PropTypes.bool,
|
2017-05-26 14:18:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
size: 'SMART_BANNER',
|
|
|
|
unitId: 'ca-app-pub-3940256099942544/6300978111', // Testing
|
2017-05-26 16:55:22 +00:00
|
|
|
testing: true,
|
2017-05-26 14:18:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
/**
|
|
|
|
* Handle a single banner event and pass to
|
|
|
|
* any props watching it
|
|
|
|
* @param nativeEvent
|
|
|
|
*/
|
2017-05-26 14:18:09 +00:00
|
|
|
onBannerEvent = ({ nativeEvent }) => {
|
|
|
|
if (this.props[nativeEvent.type]) {
|
|
|
|
if (nativeEvent.type === 'onAdFailedToLoad') {
|
2017-05-26 16:55:22 +00:00
|
|
|
const { code, message } = nativeEvent.payload;
|
|
|
|
this.props[nativeEvent.type](nativeToJSError(code, message));
|
2017-05-26 14:18:09 +00:00
|
|
|
} else {
|
|
|
|
this.props[nativeEvent.type](nativeEvent.payload || {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeEvent.type === 'onSizeChange') this.updateSize(nativeEvent.payload);
|
|
|
|
};
|
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
/**
|
|
|
|
* Handle a native onSizeChange event
|
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
*/
|
2017-05-26 14:18:09 +00:00
|
|
|
updateSize = ({ width, height }) => {
|
|
|
|
this.setState({ width, height });
|
|
|
|
};
|
|
|
|
|
2017-05-26 16:55:22 +00:00
|
|
|
/**
|
|
|
|
* Render the native component
|
|
|
|
* @returns {XML}
|
|
|
|
*/
|
2017-05-26 14:18:09 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<RNFirebaseAdMobBanner
|
|
|
|
{...this.props}
|
|
|
|
style={[this.props.style, { ...this.state }]}
|
|
|
|
bannerEvent={this.onBannerEvent}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const RNFirebaseAdMobBanner = requireNativeComponent('RNFirebaseAdMobBanner', Banner);
|
|
|
|
|
|
|
|
export default Banner;
|