Add utility for proper naming of task clones

pull/11679/head
Deniz Kusefoglu 2019-02-04 17:06:53 -08:00
parent d8e32cff4b
commit 7d635e9e1c
3 changed files with 40 additions and 3 deletions

View File

@ -33,6 +33,7 @@ import {
TaskOptions,
TaskSchedule,
} from 'src/utils/taskOptionsToFluxScript'
import {incrementCloneName} from 'src/utils/naming'
export type Action =
| SetNewScript
@ -246,9 +247,11 @@ export const deleteTask = (task: Task) => async dispatch => {
}
}
export const cloneTask = (task: Task) => async dispatch => {
export const cloneTask = (task: Task, tasks: Tasks[]) => async dispatch => {
try {
await client.tasks.create(task.orgID, task.flux, task.name)
const allTaskNames = tasks.map(t => t.name)
const clonedName = incrementCloneName(allTaskNames, task.name)
await client.tasks.create(task.orgID, task.flux, clonedName)
dispatch(notify(taskCloneSuccess(task.name)))
dispatch(populateTasks())

View File

@ -145,7 +145,8 @@ class TasksPage extends PureComponent<Props, State> {
}
private handleClone = (task: Task) => {
this.props.cloneTask(task)
const {tasks} = this.props
this.props.cloneTask(task, tasks)
}
private handleCreateTask = () => {

33
ui/src/utils/naming.ts Normal file
View File

@ -0,0 +1,33 @@
export const incrementCloneName = (
namesList: string[],
cloneName: string
): string => {
const root = cloneName.replace(/\s\(clone\s(\d)+\)/g, '').replace(/\)/, '')
const filteredNames = namesList.filter(n => n.includes(root))
const highestNumberedClone = filteredNames.reduce((acc, name) => {
if (name.match(/\(clone(\s|\d)+\)/)) {
const strippedName = name
.replace(root, '')
.replace(/\(clone/, '')
.replace(/\)/, '')
const cloneNumber = Number(strippedName)
return cloneNumber >= acc ? cloneNumber : acc
}
return acc
}, 0)
if (highestNumberedClone) {
const newCloneNumber = highestNumberedClone + 1
return `${cloneName.replace(
/\(clone\s(\d)+\)/,
''
)} (clone ${newCloneNumber})`
}
return `${cloneName} (clone 1)`
}