|
| 1 | +import React, {PureComponent} from 'react' |
| 2 | +import {withRouter, RouteComponentProps} from 'react-router-dom' |
| 3 | +import {connect, ConnectedProps} from 'react-redux' |
| 4 | + |
| 5 | +// Components |
| 6 | +import ImportOverlay from 'src/shared/components/ImportOverlay' |
| 7 | + |
| 8 | +// Copy |
| 9 | +import {invalidJSON} from 'src/shared/copy/notifications' |
| 10 | + |
| 11 | +// Actions |
| 12 | +import {createTaskFromTemplate as createTaskFromTemplateAction} from 'src/tasks/actions/thunks' |
| 13 | +import {notify as notifyAction} from 'src/shared/actions/notifications' |
| 14 | + |
| 15 | +// Types |
| 16 | +import {ComponentStatus} from '@influxdata/clockface' |
| 17 | + |
| 18 | +// Utils |
| 19 | +import jsonlint from 'jsonlint-mod' |
| 20 | + |
| 21 | +interface State { |
| 22 | + status: ComponentStatus |
| 23 | +} |
| 24 | + |
| 25 | +type ReduxProps = ConnectedProps<typeof connector> |
| 26 | +type Props = RouteComponentProps<{orgID: string}> & ReduxProps |
| 27 | + |
| 28 | +class TaskImportOverlay extends PureComponent<Props> { |
| 29 | + public state: State = { |
| 30 | + status: ComponentStatus.Default, |
| 31 | + } |
| 32 | + |
| 33 | + public render() { |
| 34 | + return ( |
| 35 | + <ImportOverlay |
| 36 | + onDismissOverlay={this.onDismiss} |
| 37 | + resourceName="Task" |
| 38 | + onSubmit={this.handleImportTask} |
| 39 | + status={this.state.status} |
| 40 | + updateStatus={this.updateOverlayStatus} |
| 41 | + /> |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + private onDismiss = () => { |
| 46 | + const {history} = this.props |
| 47 | + |
| 48 | + history.goBack() |
| 49 | + } |
| 50 | + |
| 51 | + private updateOverlayStatus = (status: ComponentStatus) => |
| 52 | + this.setState(() => ({status})) |
| 53 | + |
| 54 | + private handleImportTask = (uploadContent: string) => { |
| 55 | + const {createTaskFromTemplate, notify} = this.props |
| 56 | + |
| 57 | + let template |
| 58 | + this.updateOverlayStatus(ComponentStatus.Default) |
| 59 | + try { |
| 60 | + template = jsonlint.parse(uploadContent) |
| 61 | + } catch (error) { |
| 62 | + this.updateOverlayStatus(ComponentStatus.Error) |
| 63 | + notify(invalidJSON(error.message)) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + createTaskFromTemplate(template) |
| 68 | + |
| 69 | + this.onDismiss() |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const mdtp = { |
| 74 | + createTaskFromTemplate: createTaskFromTemplateAction, |
| 75 | + notify: notifyAction, |
| 76 | +} |
| 77 | + |
| 78 | +const connector = connect(null, mdtp) |
| 79 | + |
| 80 | +export default connector(withRouter(TaskImportOverlay)) |
0 commit comments