Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to clone a template #12910

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add the ability to clone a template
  • Loading branch information
Palakp41 committed Mar 26, 2019
commit aa79a26a7122770a12c6b13ba8fc7f1e387e50e7
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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. [12901](https://github.com/influxdata/influxdb/pull/12901): Save user preference for variable control bar visibility and default to visible
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
53 changes: 43 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,22 @@ 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)
Palakp41 marked this conversation as resolved.
Show resolved Hide resolved

dispatch(
addTemplateSummary({
...createdTemplate,
labels: createdTemplate.labels || [],
})
)
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

draftState.items = [...items, item]

return
}
}
})

Expand Down