ReportsSettingsPage.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from 'react';
  2. import { connect, MapStateToProps } from 'react-redux';
  3. import { useAsync } from 'react-use';
  4. import { NavModel } from '@grafana/data';
  5. import { config, getBackendSrv, locationService } from '@grafana/runtime';
  6. import { Button, Form } from '@grafana/ui';
  7. import { ErrorPage } from 'app/core/components/ErrorPage/ErrorPage';
  8. import Page from 'app/core/components/Page/Page';
  9. import { contextSrv } from 'app/core/core';
  10. import { getNavModel } from 'app/core/selectors/navModel';
  11. import { EnterpriseStoreState, ReportsSettings, AccessControlAction } from '../types';
  12. import { NoRendererInfoBox } from './RenderingWarnings';
  13. import ReportBranding from './ReportBranding';
  14. interface OwnProps {}
  15. interface ConnectedProps {
  16. navModel: NavModel;
  17. }
  18. export type Props = ConnectedProps & OwnProps;
  19. export const ReportsSettingsPage = ({ navModel }: Props) => {
  20. const {
  21. value: settings,
  22. loading,
  23. error,
  24. } = useAsync(async () => {
  25. return getBackendSrv().get('/api/reports/settings');
  26. });
  27. const submitForm = (settingsData: ReportsSettings) => {
  28. getBackendSrv()
  29. .post('/api/reports/settings', settingsData)
  30. .then(() => locationService.push('/reports'));
  31. };
  32. if (error) {
  33. return <ErrorPage navModel={navModel} />;
  34. }
  35. const canEditSettings = contextSrv.hasPermission(AccessControlAction.ReportingSettingsWrite);
  36. return (
  37. <Page navModel={navModel}>
  38. <Page.Contents isLoading={loading}>
  39. {!config.rendererAvailable ? (
  40. <NoRendererInfoBox variant="error" />
  41. ) : (
  42. <Form onSubmit={submitForm} validateOn="onBlur">
  43. {(formProps) => {
  44. return (
  45. <>
  46. <ReportBranding settings={settings} {...formProps} />
  47. <Button type="submit" size="md" variant="primary" disabled={!canEditSettings}>
  48. Save
  49. </Button>
  50. </>
  51. );
  52. }}
  53. </Form>
  54. )}
  55. </Page.Contents>
  56. </Page>
  57. );
  58. };
  59. const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, EnterpriseStoreState> = (state) => {
  60. return {
  61. navModel: getNavModel(state.navIndex, 'reports-settings'),
  62. };
  63. };
  64. export default connect(mapStateToProps)(ReportsSettingsPage);