UNPKG

react-meteo-chart

Version:

pre alpha of meteo chart

320 lines (278 loc) 12.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _LevelsChart = _interopRequireDefault(require("../Levels/LevelsChart")); var _reactApollo = require("react-apollo"); var _moment = _interopRequireDefault(require("moment")); var _apolloBoost = _interopRequireDefault(require("apollo-boost")); var _reactLoading = _interopRequireDefault(require("react-loading")); var _graphqlTag = _interopRequireDefault(require("graphql-tag")); require("./style.css"); var _reactSelect = _interopRequireDefault(require("react-select")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _templateObject2() { var data = _taggedTemplateLiteral(["\n query($token: String!) {\n me(token: $token) {\n lastLogin\n }\n }\n"]); _templateObject2 = function _templateObject2() { return data; }; return data; } function _templateObject() { var data = _taggedTemplateLiteral(["\n query(\n $token: String!\n $stationId: String!\n $since: DateTime\n $to: DateTime\n $typeName: String!\n $maxPoints: Int\n ) {\n levelsPlotData(\n token: $token\n stationId: $stationId\n typeName: $typeName\n since: $since\n to: $to\n maxPoints: $maxPoints\n )\n }\n"]); _templateObject = function _templateObject() { return data; }; return data; } function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); } var LEVELS_DATAPLOT_QUERY = (0, _graphqlTag["default"])(_templateObject()); var ME = (0, _graphqlTag["default"])(_templateObject2()); var selecterStyle = { control: function control(base, state) { return _objectSpread({}, base, { backgroundColor: "ccccff", opacity: 1, height: "12px", width: "100px", border: state.isFocused ? 0 : 0, boxShadow: state.isFocused ? 0 : 0, "&:hover": { border: state.isFocused ? 0 : 0 } }); }, dropdownIndicator: function dropdownIndicator(e) { return _objectSpread({}, e, { color: "#646464", transform: "rotate(180deg)" }); }, menu: function menu(e) { return _objectSpread({}, e, { color: "#00000", width: "100px", backgroundColor: "#ffFffF" }); } }; var zooms = [[1, "d", "День"], [3, "d", "3 Дня"], [7, "d", "Неделя"], [14, "d", "2 Недели"]]; var selectValues = [{ label: "День", value: 0 }, { label: "3 Дня", value: 1 }, { label: "Неделя", value: 2 }, { label: "2 Недели", value: 3 }]; var types = ["LW", "TW"]; var LevelsChartGen = /*#__PURE__*/ function (_React$Component) { _inherits(LevelsChartGen, _React$Component); function LevelsChartGen(props) { var _this; _classCallCheck(this, LevelsChartGen); _this = _possibleConstructorReturn(this, _getPrototypeOf(LevelsChartGen).call(this, props)); _this.state = { chunks: [], isLoadingChunks: [], zoom: 0, since: _this.props.dateFrom ? (0, _moment["default"])(_this.props.dateFrom).utc().format("YYYY-MM-DDTHH:mm:ss") : null, to: (0, _moment["default"])(_this.props.dateTo).utc().format("YYYY-MM-DDTHH:mm:ss"), stationId: _this.props.stationId, loadMessage: _this.props.loadMessage !== undefined ? _this.props.loadMessage : true }; _this.deep = true; _this.apollo = new _apolloBoost["default"]({ uri: _this.props.client }); return _this; } _createClass(LevelsChartGen, [{ key: "componentDidUpdate", value: function componentDidUpdate() { if (this.state.to !== (0, _moment["default"])(this.props.dateTo).utc().format("YYYY-MM-DDTHH:mm:ss") && this.props.dateTo) { this.setState({ to: (0, _moment["default"])(this.props.dateTo).utc().format("YYYY-MM-DDTHH:mm:ss") }); this.deep = true; } if (this.state.since !== (0, _moment["default"])(this.props.dateFrom).utc().format("YYYY-MM-DDTHH:mm:ss") && this.props.dateFrom) { this.setState({ since: (0, _moment["default"])(this.props.dateFrom).utc().format("YYYY-MM-DDTHH:mm:ss") }); this.deep = true; } if (this.props.stationId !== this.state.stationId) { this.setState({ stationId: this.props.stationId }); this.deep = true; } } }, { key: "render", value: function render() { var _this2 = this; var variables = { token: this.props.token, stationId: this.state.stationId, since: this.state.since ? this.state.since : (0, _moment["default"])(this.props.dateTo).utc().add(-zooms[this.state.zoom][0], zooms[this.state.zoom][1]).format("YYYY-MM-DDTHH:mm:ss"), to: (0, _moment["default"])(this.props.dateTo).utc().format("YYYY-MM-DDTHH:mm:ss"), typeList: [], maxPoints: this.props.maxPoints }; if (this.props.doneLoading) { this.props.doneLoading(this.state.isLoadingChunks.filter(function (d) { return d == false; }).length == 0 && this.state.isLoadingChunks.length > 0); } return _react["default"].createElement(_reactApollo.ApolloProvider, { client: this.apollo }, _react["default"].createElement("div", { className: "levelsGraph" }, this.deep && _react["default"].createElement(_reactApollo.Query, { query: ME, variables: { token: variables.token }, partialRefetch: true }, function (_ref) { var loading = _ref.loading, error = _ref.error, plotdata = _ref.data, refetch = _ref.refetch, client = _ref.client; if (loading) return _react["default"].createElement("div", null); if (error) return setTimeout(function () { return refetch(); }, 5000); _this2.deep = false; types.forEach(function (t, indx) { var req = client.query({ query: LEVELS_DATAPLOT_QUERY, variables: { token: variables.token, stationId: variables.stationId, typeName: t, since: variables.since, to: variables.to, maxPoints: _this2.props.maxPoints } }); setTimeout(function () { return _this2.setState(function (state) { var chunk = false; var isLoadingChunks = state.isLoadingChunks[indx] = chunk; return isLoadingChunks; }); }), req.then(function (data) { if (_this2.props.doneLoading) _this2.props.doneLoading(_this2.state.isLoadingChunks.filter(function (d) { return d == false; }).length > 0); setTimeout(function () { return _this2.setState(function (state) { var chunk = []; chunk = data.data.levelsPlotData.map(function (s) { return { x: s[0] * 1000, y: s[1] !== null ? s[1] : "" }; }); var chunks = state.chunks[indx] = chunk; return chunks; }); }); setTimeout(function () { return _this2.setState(function (state) { var chunk = true; var isLoadingChunks = state.isLoadingChunks[indx] = chunk; return isLoadingChunks; }); }); }); }); return null; }), this.state.isLoadingChunks.filter(function (d) { return d == false; }).length > 0 && this.state.loadMessage && _react["default"].createElement("div", { style: { display: "flex", justifyContent: "center", alignContent: "center", alignItems: "center", flexDirection: "column", fontSize: "17px", height: "100%" } }, _react["default"].createElement(_reactLoading["default"], { type: "balls", color: "#333333", height: 25, width: 50 }), " ", _react["default"].createElement("p", null, " \u0417\u0430\u0433\u0440\u0443\u0437\u043A\u0430 \u0433\u0440\u0430\u0444\u0438\u043A\u043E\u0432 ")), _react["default"].createElement("div", null, _react["default"].createElement(_LevelsChart["default"], { opacity: this.props.opacity, superArr: this.state.chunks, plotHeight: this.props.height, dateFrom: variables.since, dateTo: variables.to, isAdmin: this.props.isAdmin }), _react["default"].createElement("div", { style: { marginTop: "-40px", width: "100px" } }, !this.props.isAdmin && _react["default"].createElement(_reactSelect["default"], { theme: function theme(_theme) { return _objectSpread({}, _theme, { borderRadius: 1, spacing: { baseUnit: 2, controlHeight: 30, menuGutter: 2 }, colors: _objectSpread({}, _theme.colors, { primary: "#cee0eb", neutral0: "#000000" }) }); }, isSearchable: false // opacity={10} , onChange: function onChange(option) { return _this2.state.zoom !== option.value ? (_this2.setState({ zoom: option.value }), _this2.deep = true) : ""; }, placeholder: "", value: selectValues[this.state.zoom], menuPlacement: "top", menuPosition: "fixed", styles: selecterStyle, options: selectValues })))), " "); } }]); return LevelsChartGen; }(_react["default"].Component); var _default = LevelsChartGen; exports["default"] = _default;