Skip to content

Commit 06ba0b9

Browse files
authored
feat: revert and clone funcitonality for versions (#3921)
1 parent 9279eb3 commit 06ba0b9

File tree

3 files changed

+122
-40
lines changed

3 files changed

+122
-40
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Libraries
2+
import React, {FC, useContext} from 'react'
3+
import {useHistory} from 'react-router-dom'
4+
import {useSelector} from 'react-redux'
5+
6+
// Contexts
7+
import {FlowContext} from 'src/flows/context/flow.current'
8+
9+
// Components
10+
import {ComponentColor, SquareButton, IconFont} from '@influxdata/clockface'
11+
12+
// Utility
13+
import {event} from 'src/cloud/utils/reporting'
14+
import {getOrg} from 'src/organizations/selectors'
15+
import {postNotebook} from 'src/client/notebooksRoutes'
16+
17+
// Constants
18+
import {PROJECT_NAME_PLURAL} from 'src/flows'
19+
import {serialize} from 'src/flows/context/flow.list'
20+
21+
const CloneVersionButton: FC = () => {
22+
const {flow} = useContext(FlowContext)
23+
const history = useHistory()
24+
const {id: orgID} = useSelector(getOrg)
25+
26+
const handleClone = async () => {
27+
event('clone_notebook_version')
28+
try {
29+
const _flow = serialize(flow)
30+
const response = await postNotebook(_flow)
31+
32+
if (response.status !== 200) {
33+
throw new Error(response.data.message)
34+
}
35+
36+
const clonedId = response.data.id
37+
history.push(
38+
`/orgs/${orgID}/${PROJECT_NAME_PLURAL.toLowerCase()}/${clonedId}`
39+
)
40+
} catch (error) {
41+
console.error({error})
42+
}
43+
}
44+
45+
if (!flow) {
46+
return null
47+
}
48+
49+
return (
50+
<SquareButton
51+
icon={IconFont.Duplicate_New}
52+
onClick={handleClone}
53+
color={ComponentColor.Primary}
54+
titleText="Clone"
55+
/>
56+
)
57+
}
58+
59+
export default CloneVersionButton
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Libraries
2+
import React, {FC, useContext} from 'react'
3+
import {useHistory} from 'react-router-dom'
4+
import {useSelector} from 'react-redux'
5+
6+
// Contexts
7+
import {FlowContext} from 'src/flows/context/flow.current'
8+
9+
// Components
10+
import {ComponentColor, SquareButton, IconFont} from '@influxdata/clockface'
11+
12+
// Utility
13+
import {event} from 'src/cloud/utils/reporting'
14+
import {getOrg} from 'src/organizations/selectors'
15+
import {patchNotebook} from 'src/client/notebooksRoutes'
16+
17+
// Constants
18+
import {PROJECT_NAME_PLURAL} from 'src/flows'
19+
import {serialize} from 'src/flows/context/flow.list'
20+
21+
const RevertVersionButton: FC = () => {
22+
const {flow} = useContext(FlowContext)
23+
const history = useHistory()
24+
const {id: orgID} = useSelector(getOrg)
25+
26+
const handleRevert = async () => {
27+
event('revert_notebook_version')
28+
try {
29+
const _flow = serialize(flow)
30+
const response = await patchNotebook(_flow)
31+
32+
if (response.status !== 200) {
33+
throw new Error(response.data.message)
34+
}
35+
36+
history.push(
37+
`/orgs/${orgID}/${PROJECT_NAME_PLURAL.toLowerCase()}/${flow.id}`
38+
)
39+
} catch (error) {
40+
console.error({error})
41+
}
42+
}
43+
44+
if (!flow) {
45+
return null
46+
}
47+
48+
return (
49+
<SquareButton
50+
icon={IconFont.Undo}
51+
onClick={handleRevert}
52+
color={ComponentColor.Danger}
53+
titleText="Revert"
54+
/>
55+
)
56+
}
57+
58+
export default RevertVersionButton

src/flows/components/header/VersionHeader.tsx renamed to src/flows/components/header/VersionHeader/index.tsx

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
// Libraries
22
import React, {FC, useContext} from 'react'
3-
// import {useHistory} from 'react-router-dom'
4-
// import {useSelector} from 'react-redux'
53

64
// Contexts
75
import {FlowContext} from 'src/flows/context/flow.current'
86
import {VersionPublishProvider} from 'src/flows/context/version.publish'
97
import PublishedVersions from 'src/flows/components/header/PublishedVersions'
108

119
// Components
12-
import {
13-
ComponentColor,
14-
Dropdown,
15-
Page,
16-
SquareButton,
17-
IconFont,
18-
ComponentStatus,
19-
} from '@influxdata/clockface'
10+
import {Dropdown, Page, IconFont, ComponentStatus} from '@influxdata/clockface'
2011

2112
import AutoRefreshButton from 'src/flows/components/header/AutoRefreshButton'
2213
import TimeZoneDropdown from 'src/shared/components/TimeZoneDropdown'
2314
import Submit from 'src/flows/components/header/Submit'
15+
import CloneVersionButton from 'src/flows/components/header/VersionHeader/CloneButton'
16+
import RevertVersionButton from 'src/flows/components/header/VersionHeader/RevertButton'
2417

2518
// Utility
26-
import {event} from 'src/cloud/utils/reporting'
27-
// import {getOrg} from 'src/organizations/selectors'
2819
import {getTimeRangeLabel} from 'src/shared/utils/duration'
2920

3021
// Constants
@@ -34,22 +25,6 @@ import {AppSettingContext} from 'src/shared/contexts/app'
3425
const VersionHeader: FC = () => {
3526
const {timeZone} = useContext(AppSettingContext)
3627
const {flow} = useContext(FlowContext)
37-
// const history = useHistory()
38-
// const {id: orgID} = useSelector(getOrg)
39-
40-
const handleClone = () => {
41-
event('clone_notebook_version')
42-
// const clonedId = await clone(flow.id)
43-
// TODO(ariel): tweak this so we don't use the flowlist
44-
// history.push(
45-
// `/orgs/${orgID}/${PROJECT_NAME_PLURAL.toLowerCase()}/${clonedId}`
46-
// )
47-
}
48-
49-
const handleRevert = () => {
50-
event('revert_notebook_version')
51-
console.warn('reverting') // TODO(ariel): finish this in the next PR
52-
}
5328

5429
if (!flow) {
5530
return null
@@ -78,18 +53,8 @@ const VersionHeader: FC = () => {
7853
>
7954
{timeRangeLabel}
8055
</Dropdown.Button>
81-
<SquareButton
82-
icon={IconFont.Duplicate_New}
83-
onClick={handleClone}
84-
color={ComponentColor.Primary}
85-
titleText="Clone"
86-
/>
87-
<SquareButton
88-
icon={IconFont.Undo}
89-
onClick={handleRevert}
90-
color={ComponentColor.Danger}
91-
titleText="Clone"
92-
/>
56+
<CloneVersionButton />
57+
<RevertVersionButton />
9358
</Page.ControlBarRight>
9459
</Page.ControlBar>
9560
<Page.ControlBar fullWidth>

0 commit comments

Comments
 (0)