UNPKG

react-fetch-preloader

Version:

Simple React component for display preloader while fetching

3 lines (2 loc) 5.52 kB
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var React=_interopDefault(require("react"));function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _extends(){return(_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),n.forEach(function(t){_defineProperty(e,t,r[t])})}return e}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&_setPrototypeOf(e,t)}function _getPrototypeOf(e){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function _setPrototypeOf(e,t){return(_setPrototypeOf=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function _assertThisInitialized(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function _possibleConstructorReturn(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?_assertThisInitialized(e):t}var INTERVAL=50,ShowPreloader=function(e){function t(){var e,r;_classCallCheck(this,t);for(var n=arguments.length,o=new Array(n),i=0;i<n;i++)o[i]=arguments[i];return _defineProperty(_assertThisInitialized(_assertThisInitialized(r=_possibleConstructorReturn(this,(e=_getPrototypeOf(t)).call.apply(e,[this].concat(o))))),"state",{responseTime:0,isLoading:!1,showPreloader:!1,data:null,error:null}),_defineProperty(_assertThisInitialized(_assertThisInitialized(r)),"checkResponse",function(){var e=r.props.duration,t=r.state,n=t.responseTime,o=t.isLoading,i=t.showPreloader;r.setState(function(e){return{responseTime:e.responseTime+INTERVAL}},function(){n>=e&&o&&(i||r.setState({showPreloader:!0}),clearInterval(r.intervalId))})}),_defineProperty(_assertThisInitialized(_assertThisInitialized(r)),"makeResponse",function(e){var t=r.props.fetch;r.setState({responseTime:0,isLoading:!0}),t(e).then(function(e){return e.json()}).then(function(e){r.setState({isLoading:!1,data:e,showPreloader:!1}),clearInterval(r.intervalId)}).catch(function(e){r.setState({isLoading:!1,error:e,showPreloader:!1}),clearInterval(r.intervalId)})}),_defineProperty(_assertThisInitialized(_assertThisInitialized(r)),"renderContent",function(){var e=r.props,t=e.loaded,n=e.errored,o=r.state,i=o.data,a=o.isLoading,s=o.error;return s?React.createElement(n,{error:s}):!a&&i?React.createElement(t,{data:i}):null}),r}return _inherits(t,React.Component),_createClass(t,[{key:"componentDidMount",value:function(){var e=this.props,t=e.url,r=e.loaded;if(!t)throw new Error("url param is required!");if(!r)throw new Error("loaded param is required!");this.makeResponse(t),this.intervalId=setInterval(this.checkResponse,INTERVAL)}},{key:"componentDidUpdate",value:function(e){var t=this.props.url;t!==e.url&&(this.makeResponse(t),this.intervalId=setInterval(this.checkResponse,INTERVAL))}},{key:"componentWillUnmount",value:function(){clearInterval(this.intervalId)}},{key:"render",value:function(){var e=this.state.showPreloader,t=this.props.preloader;return e?React.createElement(t,null):this.renderContent()}}]),t}();_defineProperty(ShowPreloader,"defaultProps",{fetch:"undefined"!=typeof window?window.fetch:function(){},duration:500,errored:function(){return null},preloader:function(){return React.createElement("span",null,"Loading...")}});var withPreloader=function(e){return function(t){function r(){var e,t;_classCallCheck(this,r);for(var n=arguments.length,o=new Array(n),i=0;i<n;i++)o[i]=arguments[i];return _defineProperty(_assertThisInitialized(_assertThisInitialized(t=_possibleConstructorReturn(this,(e=_getPrototypeOf(r)).call.apply(e,[this].concat(o))))),"state",{}),_defineProperty(_assertThisInitialized(_assertThisInitialized(t)),"preload",function(e,r){if(!e)throw new Error("Id param is required!");if(!r)throw new Error("Url param is required!");t.setState(_defineProperty({},e,{url:r}))}),_defineProperty(_assertThisInitialized(_assertThisInitialized(t)),"renderPreloader",function(e,r){var n=_objectSpread({},t.props.preloadOptions,r);return t.state[e]&&React.createElement(ShowPreloader,_extends({url:t.state[e].url},n))}),t}return _inherits(r,React.Component),_createClass(r,[{key:"render",value:function(){return React.createElement(e,{preload:this.preload,renderPreloader:this.renderPreloader})}}]),r}()};exports.withPreloader=withPreloader,exports.ShowPreloader=ShowPreloader; //# sourceMappingURL=react-fetch-preloader.cjs.production.min.js.map