UNPKG

d2-ui

Version:
128 lines (106 loc) 5.56 kB
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; }; }(); 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; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { config } from 'd2/lib/d2'; import ListSelect from '../list-select/ListSelect.component'; import DropDown from '../form-fields/DropDown.component'; config.i18n.strings.add('please_select_a_program'); config.i18n.strings.add('no_tracked_entity_attributes'); config.i18n.strings.add('no_program_indicators'); config.i18n.strings.add('no_program_data_elements'); config.i18n.strings.add('reporting_rates'); var styles = { list: { width: '100%', outline: 'none', border: 'none', padding: '0rem 1rem' }, dropDownStyle: { marginLeft: '1rem', marginRight: '1rem' } }; var reportingRates = [{ id: 'REPORTING_RATE', displayName: 'Reporting rate' }, { id: 'REPORTING_RATE_ON_TIME', displayName: 'Reporting rate on time' }, { id: 'ACTUAL_REPORTS', displayName: 'Actual reports' }, { id: 'ACTUAL_REPORTS_ON_TIME', displayName: 'Actual reports on time' }, { id: 'EXPECTED_REPORTS', displayName: 'Expected reports' }]; var ReportingRatesSelector = function (_Component) { _inherits(ReportingRatesSelector, _Component); function ReportingRatesSelector(props, context) { _classCallCheck(this, ReportingRatesSelector); var _this = _possibleConstructorReturn(this, (ReportingRatesSelector.__proto__ || Object.getPrototypeOf(ReportingRatesSelector)).call(this, props, context)); _this.state = { selectedReportingRate: 'REPORTING_RATE', dataSets: [], isLoaded: false }; _this.onSelectReportingRate = function (event) { _this.setState({ selectedReportingRate: event.target.value }); }; _this.onDoubleClickDataSet = function (dataSetId) { var reportingRateFormula = 'R{' + dataSetId + '.' + _this.state.selectedReportingRate + '}'; _this.props.onSelect(reportingRateFormula); }; var i18n = _this.context.d2.i18n; _this.getTranslation = i18n.getTranslation.bind(i18n); return _this; } _createClass(ReportingRatesSelector, [{ key: 'componentDidMount', value: function componentDidMount() { var _this2 = this; this.context.d2.models.dataSet.list({ paging: false, fields: 'id,displayName' }).then(function (dataSetCollection) { return dataSetCollection.toArray(); }).then(function (dataSets) { var dataSetItems = dataSets.map(function (dataSet) { return { value: dataSet.id, label: dataSet.displayName }; }); _this2.setState({ dataSets: dataSetItems, isLoaded: true }); }); } }, { key: 'render', value: function render() { return React.createElement( 'div', null, React.createElement( 'div', { style: styles.dropDownStyle }, React.createElement(DropDown, { menuItems: reportingRates, value: this.state.selectedReportingRate, onChange: this.onSelectReportingRate }) ), this.state.isLoaded && React.createElement(ListSelect, { onItemDoubleClick: this.onDoubleClickDataSet, source: this.state.dataSets, listStyle: this.props.listStyle, size: 12 }) ); } }]); return ReportingRatesSelector; }(Component); ReportingRatesSelector.propTypes = { onSelect: PropTypes.func.isRequired, listStyle: PropTypes.object }; ReportingRatesSelector.defaultProps = { listStyle: styles.list }; ReportingRatesSelector.contextTypes = { d2: PropTypes.object }; export default ReportingRatesSelector;