/* * SonarQube * Copyright (C) 2009-2017 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 Modal from 'react-modal'; import { Link } from 'react-router'; import { FormattedMessage } from 'react-intl'; import { Organization } from '../../app/types'; import UpgradeOrganizationBox from '../../components/common/UpgradeOrganizationBox'; import VisibilitySelector from '../../components/common/VisibilitySelector'; import { createProject } from '../../api/components'; import { translate } from '../../helpers/l10n'; import { getProjectUrl } from '../../helpers/urls'; interface Props { onClose: () => void; onProjectCreated: () => void; organization: Organization; } interface State { advanced: boolean; branch: string; createdProject?: { key: string; name: string }; key: string; loading: boolean; name: string; visibility: string; // 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: boolean; constructor(props: Props) { super(props); this.state = { advanced: false, branch: '', 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; } handleCancelClick = (event: React.SyntheticEvent) => { event.preventDefault(); this.props.onClose(); }; handleAdvancedClick = (event: React.SyntheticEvent) => { event.preventDefault(); event.currentTarget.blur(); this.setState(state => ({ advanced: !state.advanced })); }; handleInputChange = (event: React.SyntheticEvent) => { const { name, value } = event.currentTarget; this.setState({ [name]: value }); }; handleVisibilityChange = (visibility: string) => { this.setState({ visibility }); }; handleFormSubmit = (event: React.SyntheticEvent) => { event.preventDefault(); const data = { name: this.state.name, branch: this.state.branch, 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} ) }} />
) : (

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

{!organization.canUpdateProjectsVisibilityToPrivate && (
)}
{this.state.advanced ? (
) : ( )}
)}
); } }