'use strict'; import React, {Component} from 'react'; import PropTypes from 'prop-types'; import {withTranslation} from '../lib/i18n'; import {requiresAuthenticatedUser, Title, withPageHelpers} from '../lib/page'; import {withAsyncErrorHandler, withErrorHandling} from '../lib/error-handling'; import {Button, ButtonRow, Form, FormSendMethod, TableSelect, withForm, withFormErrorHandlers} from '../lib/form'; import {Table} from '../lib/table'; import axios from '../lib/axios'; import mailtrainConfig from 'mailtrainConfig'; import {getUrl} from "../lib/urls"; import {withComponentMixins} from "../lib/decorator-helpers"; @withComponentMixins([ withTranslation, withForm, withErrorHandling, withPageHelpers, requiresAuthenticatedUser ]) export default class Share extends Component { constructor(props) { super(props); this.initForm(); } static propTypes = { title: PropTypes.string, entity: PropTypes.object, entityTypeId: PropTypes.string } @withAsyncErrorHandler async deleteShare(userId) { const data = { entityTypeId: this.props.entityTypeId, entityId: this.props.entity.id, userId }; await axios.put(getUrl('rest/shares'), data); this.sharesTable.refresh(); this.usersTableSelect.refresh(); } clearShareFields() { this.populateFormValues({ entityTypeId: this.props.entityTypeId, entityId: this.props.entity.id, userId: null, role: null }); } componentDidMount() { this.clearShareFields(); } localValidateFormValues(state) { const t = this.props.t; if (!state.getIn(['userId', 'value'])) { state.setIn(['userId', 'error'], t('userMustNotBeEmpty')); } else { state.setIn(['userId', 'error'], null); } if (!state.getIn(['role', 'value'])) { state.setIn(['role', 'error'], t('roleMustBeSelected')); } else { state.setIn(['role', 'error'], null); } } @withFormErrorHandlers async submitHandler() { const t = this.props.t; this.disableForm(); this.setFormStatusMessage('info', t('saving')); const submitSuccessful = await this.validateAndSendFormValuesToURL(FormSendMethod.PUT, 'rest/shares'); if (submitSuccessful) { this.hideFormValidation(); this.clearShareFields(); this.enableForm(); this.clearFormStatusMessage(); this.sharesTable.refresh(); this.usersTableSelect.refresh(); } else { this.enableForm(); this.setFormStatusMessage('warning', t('thereAreErrorsInTheFormPleaseFixThemAnd-1')); } } render() { const t = this.props.t; const sharesColumns = []; sharesColumns.push({ data: 0, title: t('username') }); if (mailtrainConfig.isAuthMethodLocal) { sharesColumns.push({ data: 1, title: t('name') }); } sharesColumns.push({ data: 2, title: t('role') }); sharesColumns.push({ actions: data => { const actions = []; const autoGenerated = data[4]; if (!autoGenerated) { actions.push({ label: 'Delete', action: () => this.deleteShare(data[3]) }); } return actions; } }) let usersLabelIndex = 1; const usersColumns = [ { data: 0, title: "#" }, { data: 1, title: "Username" }, ]; if (mailtrainConfig.isAuthMethodLocal) { usersColumns.push({ data: 2, title: "Full Name" }); usersLabelIndex = 2; } const rolesColumns = [ { data: 1, title: "Name" }, { data: 2, title: "Description" }, ]; return (
{this.props.title}

{t('addUser')}

this.usersTableSelect = node} id="userId" label={t('user')} withHeader dropdown dataUrl={`rest/shares-unassigned-users-table/${this.props.entityTypeId}/${this.props.entity.id}`} columns={usersColumns} selectionLabelIndex={usersLabelIndex}/>