NewDashboardsFolder.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { Button, Input, Form, Field } from '@grafana/ui';
  4. import Page from 'app/core/components/Page/Page';
  5. import { getNavModel } from 'app/core/selectors/navModel';
  6. import { StoreState } from 'app/types';
  7. import { validationSrv } from '../../manage-dashboards/services/ValidationSrv';
  8. import { createNewFolder } from '../state/actions';
  9. const mapStateToProps = (state: StoreState) => ({
  10. navModel: getNavModel(state.navIndex, 'manage-dashboards'),
  11. });
  12. const mapDispatchToProps = {
  13. createNewFolder,
  14. };
  15. const connector = connect(mapStateToProps, mapDispatchToProps);
  16. interface OwnProps {}
  17. interface FormModel {
  18. folderName: string;
  19. }
  20. const initialFormModel: FormModel = { folderName: '' };
  21. type Props = OwnProps & ConnectedProps<typeof connector>;
  22. export class NewDashboardsFolder extends PureComponent<Props> {
  23. onSubmit = (formData: FormModel) => {
  24. this.props.createNewFolder(formData.folderName);
  25. };
  26. validateFolderName = (folderName: string) => {
  27. return validationSrv
  28. .validateNewFolderName(folderName)
  29. .then(() => {
  30. return true;
  31. })
  32. .catch((e) => {
  33. return e.message;
  34. });
  35. };
  36. render() {
  37. return (
  38. <Page navModel={this.props.navModel}>
  39. <Page.Contents>
  40. <h3>New dashboard folder</h3>
  41. <Form defaultValues={initialFormModel} onSubmit={this.onSubmit}>
  42. {({ register, errors }) => (
  43. <>
  44. <Field
  45. label="Folder name"
  46. invalid={!!errors.folderName}
  47. error={errors.folderName && errors.folderName.message}
  48. >
  49. <Input
  50. id="folder-name-input"
  51. {...register('folderName', {
  52. required: 'Folder name is required.',
  53. validate: async (v) => await this.validateFolderName(v),
  54. })}
  55. />
  56. </Field>
  57. <Button type="submit">Create</Button>
  58. </>
  59. )}
  60. </Form>
  61. </Page.Contents>
  62. </Page>
  63. );
  64. }
  65. }
  66. export default connector(NewDashboardsFolder);