-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted Share modal to Ant (#3424)
- Loading branch information
Showing
5 changed files
with
190 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import Input from 'antd/lib/input'; | ||
import Icon from 'antd/lib/icon'; | ||
import Tooltip from 'antd/lib/tooltip'; | ||
|
||
export default class InputWithCopy extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { copied: null }; | ||
this.ref = React.createRef(); | ||
this.copyFeatureSupported = document.queryCommandSupported('copy'); | ||
this.resetCopyState = null; | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.resetCopyState) { | ||
clearTimeout(this.resetCopyState); | ||
} | ||
} | ||
|
||
copy = () => { | ||
// select text | ||
this.ref.current.select(); | ||
|
||
// copy | ||
try { | ||
const success = document.execCommand('copy'); | ||
if (!success) { | ||
throw new Error(); | ||
} | ||
this.setState({ copied: 'Copied!' }); | ||
} catch (err) { | ||
this.setState({ | ||
copied: 'Copy failed', | ||
}); | ||
} | ||
|
||
// reset tooltip | ||
this.resetCopyState = setTimeout(() => this.setState({ copied: null }), 2000); | ||
}; | ||
|
||
render() { | ||
const copyButton = ( | ||
<Tooltip title={this.state.copied || 'Copy'}> | ||
<Icon | ||
type="copy" | ||
style={{ cursor: 'pointer' }} | ||
onClick={this.copy} | ||
/> | ||
</Tooltip> | ||
); | ||
|
||
return ( | ||
<Input {...this.props} ref={this.ref} addonAfter={this.copyFeatureSupported && copyButton} /> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { replace } from 'lodash'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import Switch from 'antd/lib/switch'; | ||
import Modal from 'antd/lib/modal'; | ||
import Form from 'antd/lib/form'; | ||
import Tooltip from 'antd/lib/tooltip'; | ||
import { $http, toastr } from '@/services/ng'; | ||
import { wrap as wrapDialog, DialogPropType } from '@/components/DialogWrapper'; | ||
import InputWithCopy from '@/components/InputWithCopy'; | ||
|
||
const API_SHARE_URL = 'api/dashboards/{id}/share'; | ||
const HELP_URL = 'https://redash.io/help/user-guide/dashboards/sharing-dashboards?source=dialog'; | ||
|
||
class ShareDashboardDialog extends React.Component { | ||
static propTypes = { | ||
dashboard: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types | ||
dialog: DialogPropType.isRequired, | ||
}; | ||
|
||
formItemProps = { | ||
labelCol: { span: 8 }, | ||
wrapperCol: { span: 16 }, | ||
style: { marginBottom: 7 }, | ||
} | ||
|
||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
saving: false, | ||
}; | ||
this.apiUrl = replace(API_SHARE_URL, '{id}', props.dashboard.id); | ||
} | ||
|
||
static get headerContent() { | ||
return ( | ||
<React.Fragment> | ||
Share Dashboard | ||
<div className="modal-header-desc"> | ||
Allow public access to this dashboard with a secret address.{' '} | ||
<Tooltip title="Guide: Sharing and Embedding Dashboards"> | ||
{ /* eslint-disable-next-line react/jsx-no-target-blank */} | ||
<a href={HELP_URL} target="_blank" rel="noopener">Learn more</a> | ||
</Tooltip> | ||
</div> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
enable = () => { | ||
const { dashboard } = this.props; | ||
this.setState({ saving: true }); | ||
|
||
$http | ||
.post(this.apiUrl) | ||
.success((data) => { | ||
dashboard.publicAccessEnabled = true; | ||
dashboard.public_url = data.public_url; | ||
}) | ||
.error(() => { | ||
toastr.error('Failed to turn on sharing for this dashboard'); | ||
}) | ||
.finally(() => { | ||
this.setState({ saving: false }); | ||
}); | ||
} | ||
|
||
disable = () => { | ||
const { dashboard } = this.props; | ||
this.setState({ saving: true }); | ||
|
||
$http | ||
.delete(this.apiUrl) | ||
.success(() => { | ||
dashboard.publicAccessEnabled = false; | ||
delete dashboard.public_url; | ||
}) | ||
.error(() => { | ||
toastr.error('Failed to turn off sharing for this dashboard'); | ||
}) | ||
.finally(() => { | ||
this.setState({ saving: false }); | ||
}); | ||
} | ||
|
||
onChange = (checked) => { | ||
if (checked) { | ||
this.enable(); | ||
} else { | ||
this.disable(); | ||
} | ||
}; | ||
|
||
render() { | ||
const { dialog, dashboard } = this.props; | ||
|
||
return ( | ||
<Modal | ||
{...dialog.props} | ||
title={this.constructor.headerContent} | ||
footer={null} | ||
> | ||
<Form layout="horizontal"> | ||
<Form.Item label="Allow public access" {...this.formItemProps}> | ||
<Switch | ||
checked={dashboard.publicAccessEnabled} | ||
onChange={this.onChange} | ||
loading={this.state.saving} | ||
/> | ||
</Form.Item> | ||
{dashboard.public_url && ( | ||
<Form.Item label="Secret address" {...this.formItemProps}> | ||
<InputWithCopy value={dashboard.public_url} /> | ||
</Form.Item> | ||
)} | ||
</Form> | ||
</Modal> | ||
); | ||
} | ||
} | ||
|
||
export default wrapDialog(ShareDashboardDialog); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.