You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddEventForm.tsx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { ButtonPrimary, InputField, Modal } from 'design-system';
  21. import * as React from 'react';
  22. import { translate } from '../../../../helpers/l10n';
  23. import { useCreateEventMutation } from '../../../../queries/project-analyses';
  24. import { ParsedAnalysis } from '../../../../types/project-activity';
  25. interface Props {
  26. category?: string;
  27. addEventButtonText: string;
  28. analysis: ParsedAnalysis;
  29. onClose: () => void;
  30. }
  31. export default function AddEventForm(props: Readonly<Props>) {
  32. const { addEventButtonText, onClose, analysis, category } = props;
  33. const [name, setName] = React.useState('');
  34. const { mutate: createEvent } = useCreateEventMutation(onClose);
  35. const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  36. setName(event.target.value);
  37. };
  38. const handleSubmit = (e: React.MouseEvent<HTMLButtonElement>) => {
  39. e.preventDefault();
  40. const data: Parameters<typeof createEvent>[0] = { analysis: analysis.key, name };
  41. if (category !== undefined) {
  42. data.category = category;
  43. }
  44. createEvent(data);
  45. };
  46. return (
  47. <Modal
  48. headerTitle={translate(addEventButtonText)}
  49. onClose={onClose}
  50. body={
  51. <form id="add-event-form">
  52. <label htmlFor="name">{translate('name')}</label>
  53. <InputField
  54. id="name"
  55. className="sw-my-2"
  56. autoFocus
  57. onChange={handleNameChange}
  58. type="text"
  59. value={name}
  60. size="full"
  61. />
  62. </form>
  63. }
  64. primaryButton={
  65. <ButtonPrimary
  66. id="add-event-submit"
  67. form="add-event-form"
  68. type="submit"
  69. disabled={name === ''}
  70. onClick={handleSubmit}
  71. >
  72. {translate('save')}
  73. </ButtonPrimary>
  74. }
  75. secondaryButtonLabel={translate('cancel')}
  76. />
  77. );
  78. }