ImportDashboardOverview.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { dateTimeFormat } from '@grafana/data';
  4. import { locationService, reportInteraction } from '@grafana/runtime';
  5. import { Form, Legend } from '@grafana/ui';
  6. import { StoreState } from 'app/types';
  7. import { clearLoadedDashboard, importDashboard } from '../state/actions';
  8. import { DashboardSource, ImportDashboardDTO } from '../state/reducers';
  9. import { ImportDashboardForm } from './ImportDashboardForm';
  10. const IMPORT_FINISHED_EVENT_NAME = 'dashboard_import_imported';
  11. const mapStateToProps = (state: StoreState) => {
  12. const searchObj = locationService.getSearchObject();
  13. return {
  14. dashboard: state.importDashboard.dashboard,
  15. meta: state.importDashboard.meta,
  16. source: state.importDashboard.source,
  17. inputs: state.importDashboard.inputs,
  18. folder: searchObj.folderId ? { id: Number(searchObj.folderId) } : { id: 0 },
  19. };
  20. };
  21. const mapDispatchToProps = {
  22. clearLoadedDashboard,
  23. importDashboard,
  24. };
  25. const connector = connect(mapStateToProps, mapDispatchToProps);
  26. type Props = ConnectedProps<typeof connector>;
  27. interface State {
  28. uidReset: boolean;
  29. }
  30. class ImportDashboardOverviewUnConnected extends PureComponent<Props, State> {
  31. state: State = {
  32. uidReset: false,
  33. };
  34. onSubmit = (form: ImportDashboardDTO) => {
  35. reportInteraction(IMPORT_FINISHED_EVENT_NAME);
  36. this.props.importDashboard(form);
  37. };
  38. onCancel = () => {
  39. this.props.clearLoadedDashboard();
  40. };
  41. onUidReset = () => {
  42. this.setState({ uidReset: true });
  43. };
  44. render() {
  45. const { dashboard, inputs, meta, source, folder } = this.props;
  46. const { uidReset } = this.state;
  47. return (
  48. <>
  49. {source === DashboardSource.Gcom && (
  50. <div style={{ marginBottom: '24px' }}>
  51. <div>
  52. <Legend>
  53. Importing dashboard from{' '}
  54. <a
  55. href={`https://grafana.com/dashboards/${dashboard.gnetId}`}
  56. className="external-link"
  57. target="_blank"
  58. rel="noreferrer"
  59. >
  60. Grafana.com
  61. </a>
  62. </Legend>
  63. </div>
  64. <table className="filter-table form-inline">
  65. <tbody>
  66. <tr>
  67. <td>Published by</td>
  68. <td>{meta.orgName}</td>
  69. </tr>
  70. <tr>
  71. <td>Updated on</td>
  72. <td>{dateTimeFormat(meta.updatedAt)}</td>
  73. </tr>
  74. </tbody>
  75. </table>
  76. </div>
  77. )}
  78. <Form
  79. onSubmit={this.onSubmit}
  80. defaultValues={{ ...dashboard, constants: [], dataSources: [], elements: [], folder: folder }}
  81. validateOnMount
  82. validateFieldsOnMount={['title', 'uid']}
  83. validateOn="onChange"
  84. >
  85. {({ register, errors, control, watch, getValues }) => (
  86. <ImportDashboardForm
  87. register={register}
  88. errors={errors}
  89. control={control}
  90. getValues={getValues}
  91. uidReset={uidReset}
  92. inputs={inputs}
  93. onCancel={this.onCancel}
  94. onUidReset={this.onUidReset}
  95. onSubmit={this.onSubmit}
  96. watch={watch}
  97. initialFolderId={folder.id}
  98. />
  99. )}
  100. </Form>
  101. </>
  102. );
  103. }
  104. }
  105. export const ImportDashboardOverview = connector(ImportDashboardOverviewUnConnected);
  106. ImportDashboardOverview.displayName = 'ImportDashboardOverview';