/* * 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 { isWebUri } from 'valid-url'; import { translate } from 'sonar-ui-common/helpers/l10n'; import InputValidationField from 'sonar-ui-common/components/controls/InputValidationField'; import ValidationModal from 'sonar-ui-common/components/controls/ValidationModal'; interface Props { onClose: () => void; onDone: (data: Values) => Promise; webhook?: T.Webhook; } interface Values { name: string; secret: string; url: string; } export default class CreateWebhookForm extends React.PureComponent { handleCancelClick = (event: React.SyntheticEvent) => { event.preventDefault(); event.currentTarget.blur(); this.props.onClose(); }; handleValidate = (data: Values) => { const { name, secret, url } = data; const errors: { name?: string; secret?: string; url?: string } = {}; if (!name.trim()) { errors.name = translate('webhooks.name.required'); } if (!url.trim()) { errors.url = translate('webhooks.url.required'); } else if (!url.startsWith('http://') && !url.startsWith('https://')) { errors.url = translate('webhooks.url.bad_protocol'); } else if (!isWebUri(url)) { errors.url = translate('webhooks.url.bad_format'); } if (secret && secret.length > 200) { errors.secret = translate('webhooks.secret.bad_format'); } return errors; }; render() { const { webhook } = this.props; const isUpdate = !!webhook; const modalHeader = isUpdate ? translate('webhooks.update') : translate('webhooks.create'); const confirmButtonText = isUpdate ? translate('update_verb') : translate('create'); return ( {({ dirty, errors, handleBlur, handleChange, isSubmitting, touched, values }) => ( <> {translate('webhooks.name')} * } name="name" onBlur={handleBlur} onChange={handleChange} touched={touched.name} type="text" value={values.name} /> {translate('webhooks.url')} * } name="url" onBlur={handleBlur} onChange={handleChange} touched={touched.url} type="text" value={values.url} /> {translate('webhooks.secret')}} name="secret" onBlur={handleBlur} onChange={handleChange} touched={touched.secret} type="password" value={values.secret} /> )} ); } }