2017-05-31 15:33:08 +00:00
|
|
|
import React from 'react';
|
2017-06-20 11:19:26 +00:00
|
|
|
import { ViewPropTypes, requireNativeComponent } from 'react-native';
|
2017-05-31 15:33:08 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-08-21 05:58:40 +00:00
|
|
|
import EventTypes, { NativeExpressEventTypes } from './EventTypes';
|
2017-05-31 15:33:08 +00:00
|
|
|
import { nativeToJSError } from '../../utils';
|
|
|
|
|
|
|
|
import AdRequest from './AdRequest';
|
|
|
|
import VideoOptions from './VideoOptions';
|
|
|
|
|
2017-08-21 05:58:40 +00:00
|
|
|
const adMobPropTypes = {
|
|
|
|
...ViewPropTypes,
|
|
|
|
size: PropTypes.string.isRequired,
|
|
|
|
unitId: PropTypes.string.isRequired,
|
2018-01-25 18:25:39 +00:00
|
|
|
/* eslint-disable react/forbid-prop-types */
|
2017-08-21 05:58:40 +00:00
|
|
|
request: PropTypes.object,
|
|
|
|
video: PropTypes.object,
|
2018-01-25 18:25:39 +00:00
|
|
|
/* eslint-enable react/forbid-prop-types */
|
2017-10-20 18:39:45 +00:00
|
|
|
};
|
2018-01-25 18:25:39 +00:00
|
|
|
Object.keys(EventTypes).forEach(eventType => {
|
2017-08-21 05:58:40 +00:00
|
|
|
adMobPropTypes[eventType] = PropTypes.func;
|
|
|
|
});
|
2018-01-25 18:25:39 +00:00
|
|
|
Object.keys(NativeExpressEventTypes).forEach(eventType => {
|
2017-08-21 05:58:40 +00:00
|
|
|
adMobPropTypes[eventType] = PropTypes.func;
|
|
|
|
});
|
2017-05-31 15:33:08 +00:00
|
|
|
|
2017-10-20 18:39:45 +00:00
|
|
|
const nativeComponents = {};
|
|
|
|
|
|
|
|
function getNativeComponent(name) {
|
|
|
|
if (nativeComponents[name]) return nativeComponents[name];
|
|
|
|
const component = requireNativeComponent(name, AdMobComponent, {
|
|
|
|
nativeOnly: {
|
|
|
|
onBannerEvent: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
nativeComponents[name] = component;
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
2017-08-21 05:58:40 +00:00
|
|
|
class AdMobComponent extends React.Component {
|
|
|
|
static propTypes = adMobPropTypes;
|
2017-05-31 15:33:08 +00:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
request: new AdRequest().addTestDevice().build(),
|
|
|
|
video: new VideoOptions().build(),
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
};
|
|
|
|
|
2017-10-20 18:39:45 +00:00
|
|
|
this.nativeView = getNativeComponent(props.class);
|
2017-05-31 15:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a single banner event and pass to
|
|
|
|
* any props watching it
|
|
|
|
* @param nativeEvent
|
|
|
|
*/
|
|
|
|
onBannerEvent = ({ nativeEvent }) => {
|
|
|
|
if (this.props[nativeEvent.type]) {
|
|
|
|
if (nativeEvent.type === 'onAdFailedToLoad') {
|
|
|
|
const { code, message } = nativeEvent.payload;
|
|
|
|
this.props[nativeEvent.type](nativeToJSError(code, message));
|
|
|
|
} else {
|
|
|
|
this.props[nativeEvent.type](nativeEvent.payload || {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 18:25:39 +00:00
|
|
|
if (nativeEvent.type === 'onSizeChange')
|
|
|
|
this.updateSize(nativeEvent.payload);
|
2017-05-31 15:33:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the JS size of the loaded banner
|
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
*/
|
|
|
|
updateSize = ({ width, height }) => {
|
|
|
|
this.setState({ width, height });
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the native component
|
|
|
|
* @returns {XML}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<this.nativeView
|
|
|
|
{...this.props}
|
|
|
|
style={[this.props.style, { ...this.state }]}
|
2017-06-16 14:20:34 +00:00
|
|
|
onBannerEvent={this.onBannerEvent}
|
2017-05-31 15:33:08 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AdMobComponent;
|