UNPKG

react-native-cp-update-button

Version:

The goal of this component is to make the workflow of letting your users explicitly update the app as simple as CodePush.sync()

166 lines (136 loc) 6.49 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateAppButton = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactNative = require('react-native'); var _reactNativeCodePush = require('react-native-code-push'); var _downloadNewVersion = require('./lib/downloadNewVersion'); var _downloadNewVersion2 = _interopRequireDefault(_downloadNewVersion); var _utils = require('./lib/utils'); var _makeCancelable = require('./lib/makeCancelable'); var _makeCancelable2 = _interopRequireDefault(_makeCancelable); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var UpdateAppButton = exports.UpdateAppButton = function (_Component) { _inherits(UpdateAppButton, _Component); function UpdateAppButton(_ref) { var checkForUpdate = _ref.checkForUpdate; _classCallCheck(this, UpdateAppButton); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(UpdateAppButton).call(this)); _this.state = { newVersion: null }; _this.handleUpdatePress = _this.handleUpdatePress.bind(_this); _this._handleNewVersion = _this._handleNewVersion.bind(_this); _this.install = _this.install.bind(_this); if (checkForUpdate.onResume) { _this._onAppStateChange = function (newState) { return newState === 'active' && _this._checkForUpdate(); }; _reactNative.AppState.addEventListener('change', _this._onAppStateChange); } if (checkForUpdate.onInterval) { _this._interval = setTimeout(function () { return _this._checkForUpdate(); }, checkForUpdate.checkEvery); } return _this; } _createClass(UpdateAppButton, [{ key: 'componentDidMount', value: function componentDidMount() { if (this.props.checkForUpdate.onMount) this._checkForUpdate(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this._cancelablePromise) this._cancelablePromise.cancel(); if (this._onAppStateChange) _reactNative.AppState.removeEventListener('change', this._onAppStateChange); if (this._interval) clearInterval(this._interval); } }, { key: '_checkForUpdate', value: function _checkForUpdate() { var cancelablePromise = (0, _makeCancelable2.default)((0, _downloadNewVersion2.default)()); cancelablePromise.promise.then(this._handleNewVersion).catch(function (err) { return console.log('[react-native-cp-update-button] download caught an error', err); }); this._cancelablePromise = cancelablePromise; } }, { key: '_handleNewVersion', value: function _handleNewVersion(newVersion) { if (!newVersion) return; if (this.props.animate) _reactNative.LayoutAnimation.configureNext(_reactNative.LayoutAnimation.Presets.easeInEaseOut); this.setState({ newVersion: newVersion }); } }, { key: 'handleUpdatePress', value: function handleUpdatePress() { if (this.props.updateOnPress) return this.install(); var description = this.state.newVersion.description; _reactNative.Alert.alert(this.props.promptTitle, // title description || this.props.promptMessage, // mesage (body) // buttons [{ text: this.props.confirmButtonText, onPress: this.install }, { text: this.props.cancelButtonText, onPress: function onPress() { return console.log('[react-native-cp-update-button] cancel update pressed'); }, style: 'cancel' }]); } }, { key: 'install', value: function install() { this.state.newVersion.install(_reactNativeCodePush.InstallMode.IMMEDIATE); } }, { key: 'render', value: function render() { return _react2.default.createElement(this.props.component, { newVersion: this.state.newVersion, shownUpdatePrompt: this.handleUpdatePress }); } }]); return UpdateAppButton; }(_react.Component); ; UpdateAppButton.defaultProps = { animate: true, updateOnPress: false, checkForUpdate: { onMount: true, // will check on mount of the component onResume: false, // will check when the app resumes onInterval: false, // will check every interval in milliseconds (checkEvery) checkEvery: 5 * 60 * 1000 // the length of the interval that ^ will use if true }, promptTitle: 'New Update Available', promptMessage: 'A new update is now available. Do you want to update now? Note: Updating will restart the app and any changes not saved will be lost.', cancelButtonText: 'Cancel', confirmButtonText: 'Update Now' }; UpdateAppButton.propTypes = { animate: _react.PropTypes.bool, updateOnPress: _react.PropTypes.bool, component: _react.PropTypes.func.isRequired, checkForUpdate: _react.PropTypes.shape({ onMount: _react2.default.PropTypes.bool, onResume: _react2.default.PropTypes.bool, onInterval: _react2.default.PropTypes.bool, checkEvery: _react2.default.PropTypes.number }), promptTitle: _react.PropTypes.string, promptMessage: _react.PropTypes.string, cancelButtonText: _react.PropTypes.string, confirmButtonText: _react.PropTypes.string }; exports.default = UpdateAppButton;