UNPKG

@bigfishtv/cockpit

Version:

187 lines (165 loc) 6.77 kB
var _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; }; var _class, _temp, _initialiseProps; 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 { createValue, Fieldset } from '@bigfishtv/react-forms'; import _set from 'lodash/set'; import Modal from './Modal'; import Button from '../button/Button'; import Field from '../form/Field'; import CheckboxGroup from '../input/CheckboxGroup'; import DateTimeInput from '../input/DateTimeInput'; import SwitchInput from '../input/SwitchInput'; import { formatDate, getTimeRange, inTimeRange } from '../../utils/timeUtils'; var SectionsPublishModal = (_temp = _class = function (_Component) { _inherits(SectionsPublishModal, _Component); function SectionsPublishModal(props) { _classCallCheck(this, SectionsPublishModal); var _this = _possibleConstructorReturn(this, _Component.call(this, props)); _initialiseProps.call(_this); var value = { publish_date: props.formValue.value.publish_date, expiry_date: props.formValue.value.expiry_date, enabled: props.formValue.value.enabled }; _this.state = { paths: [props.formValue.keyPath.join('.')], formValue: createValue({ value: value, onChange: _this.onChange }) }; var relativeKeyPath = props.formValue.keyPath.slice(0, -2); _this.sectionOptions = props.sections.map(function (_ref) { var section = _ref.section, sectionType = _ref.sectionType, index = _ref.index; var enabled = section.enabled, publish_date = section.publish_date, expiry_date = section.expiry_date; console.log(publish_date, expiry_date, inTimeRange(publish_date, expiry_date)); var textColor = ''; var subtitle = ''; if (!enabled) textColor = 'text-error';else if (publish_date || expiry_date) textColor = inTimeRange(publish_date, expiry_date) ? 'text-success' : 'text-warning'; if (publish_date && !expiry_date) subtitle = 'From ' + formatDate(publish_date);else if (!publish_date && expiry_date) subtitle = 'Until ' + formatDate(expiry_date);else if (publish_date && expiry_date) subtitle = 'From ' + getTimeRange(section.publish_date, section.expiry_date); return { value: [].concat(relativeKeyPath, [sectionType.property, index]).join('.'), text: React.createElement( 'span', { className: textColor }, section.title || section.name || section.label || sectionType.title + ' Section', subtitle && React.createElement( 'small', null, React.createElement( 'strong', null, React.createElement('br', null), subtitle ) ) ) }; }); return _this; } SectionsPublishModal.prototype.render = function render() { var _this2 = this; var _props = this.props, title = _props.title, multiple = _props.multiple; return React.createElement( Modal, { size: 'large', title: title, onClose: function onClose() { return _this2.props.closeModal(); }, ModalActions: function ModalActions(props) { return React.createElement( 'div', null, React.createElement(Button, { text: 'Save', style: 'primary', onClick: _this2.handleSubmit }), React.createElement(Button, { text: 'Cancel', onClick: _this2.handleClose }) ); } }, React.createElement( 'div', { className: 'flex' }, multiple && React.createElement( 'div', { className: 'flex-1-1-auto', style: { overflow: 'auto', maxHeight: 465 } }, React.createElement( 'div', { className: 'form-field' }, React.createElement( 'label', null, 'Update the following sections:' ), React.createElement(CheckboxGroup, { options: this.sectionOptions, value: this.state.paths, onChange: function onChange(paths) { return _this2.setState({ paths: paths }); } }) ) ), React.createElement( 'div', { className: 'flex-1-0-auto', style: multiple ? { width: 120 } : {} }, React.createElement( Fieldset, { formValue: this.state.formValue }, React.createElement(Field, { select: 'enabled', label: 'Published', Input: SwitchInput }), React.createElement( Field, { select: 'publish_date' }, React.createElement(DateTimeInput, { maxDate: this.state.formValue.value.expiry_date }) ), React.createElement( Field, { select: 'expiry_date' }, React.createElement(DateTimeInput, { minDate: this.state.formValue.value.publish_date }) ) ) ) ) ); }; return SectionsPublishModal; }(Component), _class.defaultProps = { title: 'Update Section(s) Publish Settings', multiple: true }, _initialiseProps = function _initialiseProps() { var _this3 = this; this.onChange = function (formValue) { return _this3.setState({ formValue: formValue }); }; this.handleSubmit = function () { var value = _this3.state.formValue.value; var rootFormValue = _this3.props.formValue.root; var newRootValue = rootFormValue.value; for (var _iterator = _this3.state.paths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref2; if (_isArray) { if (_i >= _iterator.length) break; _ref2 = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref2 = _i.value; } var path = _ref2; var item = rootFormValue.select(path).value || {}; _set(newRootValue, path, _extends({}, item, value)); } rootFormValue.update(newRootValue); _this3.handleClose(); }; this.handleClose = function () { _this3.props.closeModal(); }; }, _temp); export { SectionsPublishModal as default };