/* * 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. */ // @flow import React from 'react'; import classNames from 'classnames'; import Step from './Step'; import CloseIcon from '../../../components/icons-components/CloseIcon'; import { generateToken, revokeToken } from '../../../api/user-tokens'; import { translate } from '../../../helpers/l10n'; /*:: type Props = {| finished: boolean, open: boolean, onContinue: (token: string) => void, onOpen: () => void, stepNumber: number |}; */ /*:: type State = { existingToken:string, loading: boolean, selection: string, tokenName?: string, token?: string }; */ export default class TokenStep extends React.PureComponent { /*:: mounted: boolean; */ /*:: props: Props; */ state /*: State */ = { existingToken: '', loading: false, selection: 'generate' }; componentDidMount() { this.mounted = true; } componentWillUnmount() { this.mounted = false; } getToken = () => this.state.selection === 'generate' ? this.state.token : this.state.existingToken; handleTokenNameChange = (event /*: { target: HTMLInputElement } */) => { this.setState({ tokenName: event.target.value }); }; handleTokenGenerate = (event /*: Event */) => { event.preventDefault(); const { tokenName } = this.state; if (tokenName) { this.setState({ loading: true }); generateToken(tokenName).then( ({ token }) => { if (this.mounted) { this.setState({ loading: false, token }); } }, () => { if (this.mounted) { this.setState({ loading: false }); } } ); } }; handleTokenRevoke = (event /*: Event */) => { event.preventDefault(); const { tokenName } = this.state; if (tokenName) { this.setState({ loading: true }); revokeToken(tokenName).then( () => { if (this.mounted) { this.setState({ loading: false, token: undefined, tokenName: undefined }); } }, () => { if (this.mounted) { this.setState({ loading: false }); } } ); } }; handleContinueClick = (event /*: Event */) => { event.preventDefault(); const token = this.getToken(); if (token) { this.props.onContinue(token); } }; handleGenerateClick = (event /*: Event */) => { event.preventDefault(); this.setState({ selection: 'generate' }); }; handleUseExistingClick = (event /*: Event */) => { event.preventDefault(); this.setState({ selection: 'use-existing' }); }; handleExisingTokenChange = (event /*: { currentTarget: HTMLInputElement } */) => { this.setState({ existingToken: event.currentTarget.value }); }; renderGenerateOption = () => (