Skip to content

Commit

Permalink
Add the ability to clone a template
Browse files Browse the repository at this point in the history
  • Loading branch information
Palakp41 committed Mar 26, 2019
1 parent fadb66d commit 44b79f3
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
1. [12876](https://github.com/influxdata/influxdb/pull/12876): Add the ability to update token's status in Token list
1. [12821](https://github.com/influxdata/influxdb/pull/12821): Allow variables to be re-ordered within control bar on a dashboard.
1. [12888](https://github.com/influxdata/influxdb/pull/12888): Add the ability to delete a template
1. [12910](https://github.com/influxdata/influxdb/pull/12910): Add the ability to clone a template

### Bug Fixes

Expand Down
47 changes: 33 additions & 14 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
},
"dependencies": {
"@influxdata/clockface": "0.0.5",
"@influxdata/influx": "0.2.50",
"@influxdata/influx": "0.2.52",
"@influxdata/react-custom-scrollbars": "4.3.8",
"axios": "^0.18.0",
"babel-polyfill": "^6.26.0",
Expand Down
10 changes: 10 additions & 0 deletions ui/src/shared/copy/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,16 @@ export const deleteTemplateSuccess = (): Notification => ({
message: 'Template was deleted successfully',
})

export const cloneTemplateFailed = (error: string): Notification => ({
...defaultErrorNotification,
message: `Failed to clone template: ${error}`,
})

export const cloneTemplateSuccess = (): Notification => ({
...defaultSuccessNotification,
message: 'Template cloned successfully',
})

export const resourceSavedAsTemplate = (
resourceName: string
): Notification => ({
Expand Down
51 changes: 41 additions & 10 deletions ui/src/templates/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,27 @@ export enum ActionTypes {
SetTemplatesStatus = 'SET_TEMPLATES_STATUS',
SetExportTemplate = 'SET_EXPORT_TEMPLATE',
RemoveTemplateSummary = 'REMOVE_TEMPLATE_SUMMARY',
AddTemplateSummary = 'ADD_TEMPLATE_SUMMARY',
}

export type Actions =
| PopulateTemplateSummaries
| SetTemplatesStatus
| SetExportTemplate
| RemoveTemplateSummary
| AddTemplateSummary

export interface AddTemplateSummary {
type: ActionTypes.AddTemplateSummary
payload: {item: TemplateSummary}
}

export const addTemplateSummary = (
item: TemplateSummary
): AddTemplateSummary => ({
type: ActionTypes.AddTemplateSummary,
payload: {item},
})

export interface PopulateTemplateSummaries {
type: ActionTypes.PopulateTemplateSummaries
Expand Down Expand Up @@ -68,6 +82,16 @@ export const setExportTemplate = (
payload: {status, item, orgID},
})

interface RemoveTemplateSummary {
type: ActionTypes.RemoveTemplateSummary
payload: {templateID: string}
}

const removeTemplateSummary = (templateID: string): RemoveTemplateSummary => ({
type: ActionTypes.RemoveTemplateSummary,
payload: {templateID},
})

export const getTemplatesForOrg = (orgName: string) => async dispatch => {
dispatch(setTemplatesStatus(RemoteDataState.Loading))
const items = await client.templates.getAll(orgName)
Expand Down Expand Up @@ -104,16 +128,6 @@ export const clearExportTemplate = () => async dispatch => {
dispatch(setExportTemplate(RemoteDataState.NotStarted, null))
}

interface RemoveTemplateSummary {
type: ActionTypes.RemoveTemplateSummary
payload: {templateID: string}
}

const removeTemplateSummary = (templateID: string): RemoveTemplateSummary => ({
type: ActionTypes.RemoveTemplateSummary,
payload: {templateID},
})

export const deleteTemplate = (templateID: string) => async (
dispatch
): Promise<void> => {
Expand All @@ -126,3 +140,20 @@ export const deleteTemplate = (templateID: string) => async (
dispatch(notify(copy.deleteTemplateFailed(e)))
}
}

export const cloneTemplate = (templateID: string, orgID: string) => async (
dispatch
): Promise<void> => {
try {
const createdTemplate = await client.templates.clone(templateID, orgID)

const {id, meta, labels} = createdTemplate
const item = {id, meta, labels}

dispatch(addTemplateSummary(item))
dispatch(notify(copy.cloneTemplateSuccess()))
} catch (e) {
console.error(e)
dispatch(notify(copy.cloneTemplateFailed(e)))
}
}
19 changes: 18 additions & 1 deletion ui/src/templates/components/TemplateCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {withRouter, WithRouterProps} from 'react-router'
import {ResourceList, Context, IconFont} from 'src/clockface'

// Actions
import {deleteTemplate} from 'src/templates/actions'
import {deleteTemplate, cloneTemplate} from 'src/templates/actions'

// Types
import {TemplateSummary} from '@influxdata/influx'
Expand All @@ -23,6 +23,7 @@ interface OwnProps {

interface DispatchProps {
onDelete: typeof deleteTemplate
onClone: typeof cloneTemplate
}

type Props = DispatchProps & OwnProps
Expand Down Expand Up @@ -60,6 +61,12 @@ export class TemplateCard extends PureComponent<Props & WithRouterProps> {
} = this.props
return (
<Context>
<Context.Menu
icon={IconFont.Duplicate}
color={ComponentColor.Secondary}
>
<Context.Item label="Clone" action={this.handleClone} value={id} />
</Context.Menu>
<Context.Menu
icon={IconFont.Trash}
color={ComponentColor.Danger}
Expand All @@ -76,6 +83,15 @@ export class TemplateCard extends PureComponent<Props & WithRouterProps> {
)
}

private handleClone = () => {
const {
template: {id},
params: {orgID},
onClone,
} = this.props
onClone(id, orgID)
}

private handleNameClick = (e: MouseEvent<HTMLAnchorElement>) => {
e.preventDefault()
this.handleExport()
Expand All @@ -93,6 +109,7 @@ export class TemplateCard extends PureComponent<Props & WithRouterProps> {

const mdtp: DispatchProps = {
onDelete: deleteTemplate,
onClone: cloneTemplate,
}

export default connect<{}, DispatchProps, OwnProps>(
Expand Down
9 changes: 9 additions & 0 deletions ui/src/templates/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ const templatesReducer = (

return
}

case ActionTypes.AddTemplateSummary: {
const {item} = action.payload
const {items} = draftState

items.push(item)

return
}
}
})

Expand Down

0 comments on commit 44b79f3

Please sign in to comment.