/* * SonarQube * Copyright (C) 2009-2019 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; import { Link } from 'react-router'; import { FormattedMessage } from 'react-intl'; import { createProject } from '../../api/components'; import UpgradeOrganizationBox from '../create/components/UpgradeOrganizationBox'; import VisibilitySelector from '../../components/common/VisibilitySelector'; import Modal from '../../components/controls/Modal'; import { SubmitButton, ResetButtonLink } from '../../components/ui/buttons'; import { translate } from '../../helpers/l10n'; import { getProjectUrl } from '../../helpers/urls'; import { Alert } from '../../components/ui/Alert'; interface Props { onClose: () => void; onProjectCreated: () => void; onOrganizationUpgrade: () => void; organization: T.Organization; } interface State { createdProject?: { key: string; name: string }; key: string; loading: boolean; name: string; visibility?: T.Visibility; // add index declaration to be able to do `this.setState({ [name]: value });` [x: string]: any; } export default class CreateProjectForm extends React.PureComponent { closeButton?: HTMLElement | null; mounted = false; constructor(props: Props) { super(props); this.state = { key: '', loading: false, name: '', visibility: props.organization.projectVisibility }; } componentDidMount() { this.mounted = true; } componentDidUpdate() { // wrap with `setImmediate` because of https://github.com/reactjs/react-modal/issues/338 setImmediate(() => { if (this.closeButton) { this.closeButton.focus(); } }); } componentWillUnmount() { this.mounted = false; } handleInputChange = (event: React.SyntheticEvent) => { const { name, value } = event.currentTarget; this.setState({ [name]: value }); }; handleVisibilityChange = (visibility: T.Visibility) => { this.setState({ visibility }); }; handleFormSubmit = (event: React.SyntheticEvent) => { event.preventDefault(); const data = { name: this.state.name, organization: this.props.organization && this.props.organization.key, project: this.state.key, visibility: this.state.visibility }; this.setState({ loading: true }); createProject(data).then( response => { if (this.mounted) { this.setState({ createdProject: response.project, loading: false }); this.props.onProjectCreated(); } }, () => { if (this.mounted) { this.setState({ loading: false }); } } ); }; render() { const { organization } = this.props; const { createdProject } = this.state; return ( {createdProject ? (

{translate('qualifiers.create.TRK')}

{createdProject.name} ) }} />
(this.closeButton = node)} onClick={this.props.onClose}> {translate('close')}
) : (

{translate('qualifiers.create.TRK')}

{organization.actions && organization.actions.admin && !organization.canUpdateProjectsVisibilityToPrivate && (
)}
{this.state.loading && } {translate('create')} {translate('cancel')}
)}
); } }