UNPKG

5.19 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; }; }();
8
9var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
10
11var _react = require("react");
12
13var React = _interopRequireWildcard(_react);
14
15var _reactRouterDom = require("react-router-dom");
16
17var _reactRedux = require("react-redux");
18
19var _actions = require("./actions");
20
21var _Messages = require("x25/Messages");
22
23function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
24
25function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
26
27function _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; }
28
29function _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; }
30
31var mapStateToProps = function mapStateToProps(state, _ref) {
32 var selectors = _ref.settings.selectors,
33 id = _ref.id;
34 return {
35 data: selectors.getItem(state, id),
36 hasError: selectors.getItemHasError(state, id),
37 fetched: selectors.getItemIsFetched(state, id),
38 isFetching: selectors.getIsFetchingItemInfo(state, id),
39 shouldFetch: selectors.getShouldFetchItemInfo(state, id)
40 };
41},
42 mapDispatchToProps = function mapDispatchToProps(dispatch, _ref2) {
43 var id = _ref2.id,
44 settings = _ref2.settings;
45 return {
46 fetchItem: function fetchItem() {
47 dispatch((0, _actions.fetchItem)(_extends({}, settings, {
48 id: id
49 })));
50 }
51 };
52};
53
54var LoadDataItem = function (_React$Component) {
55 _inherits(LoadDataItem, _React$Component);
56
57 function LoadDataItem() {
58 _classCallCheck(this, LoadDataItem);
59
60 return _possibleConstructorReturn(this, (LoadDataItem.__proto__ || Object.getPrototypeOf(LoadDataItem)).apply(this, arguments));
61 }
62
63 _createClass(LoadDataItem, [{
64 key: "UNSAFE_componentWillMount",
65 value: function UNSAFE_componentWillMount() {
66 var _props = this.props,
67 shouldFetch = _props.shouldFetch,
68 fetchItem = _props.fetchItem;
69
70
71 if (shouldFetch) {
72 fetchItem();
73 }
74 }
75 }, {
76 key: "shouldComponentUpdate",
77 value: function shouldComponentUpdate(nextProps) {
78 return this.props.data !== nextProps.data || this.props.hasError !== nextProps.hasError || this.props.fetched !== nextProps.fetched || this.props.isFetching !== nextProps.isFetching || this.props.shouldFetch !== nextProps.shouldFetch || this.props.id !== nextProps.id;
79 }
80 }, {
81 key: "UNSAFE_componentWillReceiveProps",
82 value: function UNSAFE_componentWillReceiveProps(nextProps) {
83 var shouldFetch = nextProps.shouldFetch,
84 fetchItem = nextProps.fetchItem;
85
86
87 if (shouldFetch) {
88 fetchItem();
89 }
90 }
91 }, {
92 key: "render",
93 value: function render() {
94 var _props2 = this.props,
95 children = _props2.children,
96 data = _props2.data,
97 isFetching = _props2.isFetching,
98 hasError = _props2.hasError,
99 fetchItem = _props2.fetchItem;
100
101
102 if (isFetching) {
103 return React.createElement(_Messages.LoadingMessage, { message: "Preiau datele..." });
104 }
105
106 if (hasError) {
107 return React.createElement(_Messages.LargeErrorMessage, {
108 message: "Ceva nu a mers bine",
109 onRetry: fetchItem
110 });
111 }
112
113 if (data.size === 0) {
114 return null;
115 }
116
117 return React.createElement(
118 React.Fragment,
119 null,
120 children
121 );
122 }
123 }]);
124
125 return LoadDataItem;
126}(React.Component);
127
128exports.default = (0, _reactRouterDom.withRouter)((0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(LoadDataItem));
\No newline at end of file