SharePublicDashboard.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import React, { useState, useEffect } from 'react';
  2. import { Button, Field, Switch } from '@grafana/ui';
  3. import { notifyApp } from 'app/core/actions';
  4. import { createErrorNotification, createSuccessNotification } from 'app/core/copy/appNotification';
  5. import { dispatch } from 'app/store/store';
  6. import {
  7. dashboardCanBePublic,
  8. getPublicDashboardConfig,
  9. savePublicDashboardConfig,
  10. PublicDashboardConfig,
  11. } from './SharePublicDashboardUtils';
  12. import { ShareModalTabProps } from './types';
  13. interface Props extends ShareModalTabProps {}
  14. // 1. write test for dashboardCanBePublic
  15. // 2. figure out how to disable the switch
  16. export const SharePublicDashboard = (props: Props) => {
  17. const [publicDashboardConfig, setPublicDashboardConfig] = useState<PublicDashboardConfig>({ isPublic: false });
  18. const dashboardUid = props.dashboard.uid;
  19. useEffect(() => {
  20. getPublicDashboardConfig(dashboardUid)
  21. .then((pdc: PublicDashboardConfig) => {
  22. setPublicDashboardConfig(pdc);
  23. })
  24. .catch(() => {
  25. dispatch(notifyApp(createErrorNotification('Failed to retrieve public dashboard config')));
  26. });
  27. }, [dashboardUid]);
  28. const onSavePublicConfig = () => {
  29. // verify dashboard can be public
  30. if (!dashboardCanBePublic(props.dashboard)) {
  31. dispatch(notifyApp(createErrorNotification('This dashboard cannot be made public')));
  32. return;
  33. }
  34. try {
  35. savePublicDashboardConfig(props.dashboard.uid, publicDashboardConfig);
  36. dispatch(notifyApp(createSuccessNotification('Dashboard sharing configuration saved')));
  37. } catch (err) {
  38. console.error('Error while making dashboard public', err);
  39. dispatch(notifyApp(createErrorNotification('Error making dashboard public')));
  40. }
  41. };
  42. return (
  43. <>
  44. <p className="share-modal-info-text">Public Dashboard Configuration</p>
  45. <Field label="Enabled" description="Configures whether current dashboard can be available publicly">
  46. <Switch
  47. id="share-current-time-range"
  48. disabled={!dashboardCanBePublic(props.dashboard)}
  49. value={publicDashboardConfig?.isPublic}
  50. onChange={() =>
  51. setPublicDashboardConfig((state) => {
  52. return { ...state, isPublic: !state.isPublic };
  53. })
  54. }
  55. />
  56. </Field>
  57. <Button onClick={onSavePublicConfig}>Save Sharing Configuration</Button>
  58. </>
  59. );
  60. };