// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import React from 'react'; import PropTypes from 'prop-types'; import {FormattedMessage, FormattedHTMLMessage} from 'react-intl'; import {Modal} from 'react-bootstrap'; import {Constants} from 'utils/constants'; import {localizeMessage} from 'utils/utils.jsx'; import {t} from 'utils/i18n'; const INT32_MAX = 2147483647; export default class EditPostTimeLimitModal extends React.Component { static propTypes = { config: PropTypes.object.isRequired, show: PropTypes.bool, onClose: PropTypes.func.isRequired, actions: PropTypes.shape({ updateConfig: PropTypes.func.isRequired, getConfig: PropTypes.func.isRequired, }).isRequired, }; constructor(props) { super(props); this.state = { postEditTimeLimit: parseInt(props.config.ServiceSettings.PostEditTimeLimit, 10), saving: false, errorMessage: '', }; } UNSAFE_componentWillMount() { // eslint-disable-line camelcase this.props.actions.getConfig(); } save = async () => { this.setState({saving: true, errorMessage: ''}); const val = parseInt(this.state.postEditTimeLimit, 10); if (val !== Constants.UNSET_POST_EDIT_TIME_LIMIT) { if (val.toString() === 'NaN' || val <= 0 || val > INT32_MAX) { this.setState({errorMessage: localizeMessage('edit_post.time_limit_modal.invalid_time_limit', 'Invalid time limit'), saving: false}); return false; } } const newConfig = JSON.parse(JSON.stringify(this.props.config)); newConfig.ServiceSettings.PostEditTimeLimit = val; const {error: err} = await this.props.actions.updateConfig(newConfig); if (err) { this.setState({errorMessage: err, saving: false}); } else { this.setState({saving: false}); this.props.onClose(); } return true; } handleOptionChange = (e) => { const {value} = e.target; if (value === Constants.ALLOW_EDIT_POST_ALWAYS) { this.setState({postEditTimeLimit: Constants.UNSET_POST_EDIT_TIME_LIMIT}); } else { this.setState({postEditTimeLimit: ''}); } } handleSecondsChange = (e) => { const {value} = e.target; this.setState({postEditTimeLimit: value}); } render = () => { return (

{this.state.errorMessage}
); }; }