'use strict'; import React, {Component} from 'react'; import PropTypes from 'prop-types'; import {withTranslation} from '../lib/i18n'; import { LinkButton, requiresAuthenticatedUser, Title, withPageHelpers } from '../lib/page'; import { Button, ButtonRow, Form, FormSendMethod, InputField, TextArea, TreeTableSelect, withForm } from '../lib/form'; import axios from '../lib/axios'; import { withAsyncErrorHandler, withErrorHandling } from '../lib/error-handling'; import interoperableErrors from '../../../shared/interoperable-errors'; import {DeleteModalDialog} from "../lib/modals"; import mailtrainConfig from 'mailtrainConfig'; import {getGlobalNamespaceId} from "../../../shared/namespaces"; import {getUrl} from "../lib/urls"; import {withComponentMixins} from "../lib/decorator-helpers"; @withComponentMixins([ withTranslation, withForm, withErrorHandling, withPageHelpers, requiresAuthenticatedUser ]) export default class CUD extends Component { constructor(props) { super(props); this.state = {}; this.initForm(); } static propTypes = { action: PropTypes.string.isRequired, entity: PropTypes.object } isEditGlobal() { return this.props.entity && this.props.entity.id === getGlobalNamespaceId(); } removeNsIdSubtree(data) { for (let idx = 0; idx < data.length; idx++) { const entry = data[idx]; if (entry.key === this.props.entity.id) { data.splice(idx, 1); return true; } if (this.removeNsIdSubtree(entry.children)) { return true; } } } @withAsyncErrorHandler async loadTreeData() { if (!this.isEditGlobal()) { const response = await axios.get(getUrl('rest/namespaces-tree')); const data = response.data; for (const root of data) { root.expanded = true; } if (this.props.entity && !this.isEditGlobal()) { this.removeNsIdSubtree(data); } this.setState({ treeData: data }); } } componentDidMount() { if (this.props.entity) { this.getFormValuesFromEntity(this.props.entity); } else { this.populateFormValues({ name: '', description: '', namespace: mailtrainConfig.user.namespace }); } // noinspection JSIgnoredPromiseFromCall this.loadTreeData(); } localValidateFormValues(state) { const t = this.props.t; if (!state.getIn(['name', 'value']).trim()) { state.setIn(['name', 'error'], t('nameMustNotBeEmpty')); } else { state.setIn(['name', 'error'], null); } if (!this.isEditGlobal()) { if (!state.getIn(['namespace', 'value'])) { state.setIn(['namespace', 'error'], t('parentNamespaceMustBeSelected')); } else { state.setIn(['namespace', 'error'], null); } } } async submitHandler(submitAndLeave) { const t = this.props.t; let sendMethod, url; if (this.props.entity) { sendMethod = FormSendMethod.PUT; url = `rest/namespaces/${this.props.entity.id}` } else { sendMethod = FormSendMethod.POST; url = 'rest/namespaces' } try { this.disableForm(); this.setFormStatusMessage('info', t('saving')); const submitResult = await this.validateAndSendFormValuesToURL(sendMethod, url); if (submitResult) { if (this.props.entity) { if (submitAndLeave) { this.navigateToWithFlashMessage('/namespaces', 'success', t('Namespace updated')); } else { await this.getFormValuesFromURL(`rest/namespaces/${this.props.entity.id}`); await this.loadTreeData(); this.enableForm(); this.setFormStatusMessage('success', t('Namespace updated')); } } else { if (submitAndLeave) { this.navigateToWithFlashMessage('/namespaces', 'success', t('Namespace created')); } else { this.navigateToWithFlashMessage(`/namespaces/${submitResult}/edit`, 'success', t('Namespace created')); } } } else { this.enableForm(); this.setFormStatusMessage('warning', t('thereAreErrorsInTheFormPleaseFixThemAnd')); } } catch (error) { if (error instanceof interoperableErrors.LoopDetectedError) { this.setFormStatusMessage('danger', {t('yourUpdatesCannotBeSaved')}{' '} {t('thereHasBeenALoopDetectedInTheAssignment')} ); return; } if (error instanceof interoperableErrors.DependencyNotFoundError) { this.setFormStatusMessage('danger', {t('yourUpdatesCannotBeSaved')}{' '} {t('itSeemsThatTheParentNamespaceHasBeen')} ); return; } throw error; } } render() { const t = this.props.t; const isEdit = !!this.props.entity; const canDelete = isEdit && !this.isEditGlobal() && this.props.entity.permissions.includes('delete'); return (
{canDelete && } {isEdit ? t('editNamespace') : t('createNamespace')}