import React, { useState, useEffect } from 'react'; import { Button, Field, Switch } from '@grafana/ui'; import { notifyApp } from 'app/core/actions'; import { createErrorNotification, createSuccessNotification } from 'app/core/copy/appNotification'; import { dispatch } from 'app/store/store'; import { dashboardCanBePublic, getPublicDashboardConfig, savePublicDashboardConfig, PublicDashboardConfig, } from './SharePublicDashboardUtils'; import { ShareModalTabProps } from './types'; interface Props extends ShareModalTabProps {} // 1. write test for dashboardCanBePublic // 2. figure out how to disable the switch export const SharePublicDashboard = (props: Props) => { const [publicDashboardConfig, setPublicDashboardConfig] = useState({ isPublic: false }); const dashboardUid = props.dashboard.uid; useEffect(() => { getPublicDashboardConfig(dashboardUid) .then((pdc: PublicDashboardConfig) => { setPublicDashboardConfig(pdc); }) .catch(() => { dispatch(notifyApp(createErrorNotification('Failed to retrieve public dashboard config'))); }); }, [dashboardUid]); const onSavePublicConfig = () => { // verify dashboard can be public if (!dashboardCanBePublic(props.dashboard)) { dispatch(notifyApp(createErrorNotification('This dashboard cannot be made public'))); return; } try { savePublicDashboardConfig(props.dashboard.uid, publicDashboardConfig); dispatch(notifyApp(createSuccessNotification('Dashboard sharing configuration saved'))); } catch (err) { console.error('Error while making dashboard public', err); dispatch(notifyApp(createErrorNotification('Error making dashboard public'))); } }; return ( <>

Public Dashboard Configuration

setPublicDashboardConfig((state) => { return { ...state, isPublic: !state.isPublic }; }) } /> ); };