-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathWorkflowBuilder.tsx
More file actions
143 lines (132 loc) · 3.92 KB
/
WorkflowBuilder.tsx
File metadata and controls
143 lines (132 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { mdiClose, mdiPlus } from '@mdi/js'
import React, { useCallback, useState } from 'react'
import { remove } from 'ramda'
import Button from '../../../design/components/atoms/Button'
import { getTeamIndexPageData } from '../../api/pages/teams'
import PipeBuilder from '../../components/Automations/PipeBuilder'
import {
SerializedPipe,
SerializedWorkflow,
} from '../../interfaces/db/automations'
import Flexbox from '../../../design/components/atoms/Flexbox'
import Form from '../../../design/components/molecules/Form'
import FormRow from '../../../design/components/molecules/Form/templates/FormRow'
import FormRowItem from '../../../design/components/molecules/Form/templates/FormRowItem'
import FormInput from '../../../design/components/molecules/Form/atoms/FormInput'
import styled from '../../../design/lib/styled'
export type NewWorkflow = Omit<
SerializedWorkflow,
'id' | 'createdAt' | 'updatedAt'
> & { id?: number }
interface WorkflowBuilderProps {
initialWorkflow: SerializedWorkflow | NewWorkflow
onSubmit: (workflow: SerializedWorkflow | NewWorkflow) => void
working?: boolean
}
const WorkflowBuilder = ({
initialWorkflow,
onSubmit,
working = false,
}: WorkflowBuilderProps) => {
const [workflow, setWorkflow] = useState(initialWorkflow)
const setPipe = useCallback((pipe: SerializedPipe, index: number) => {
return setWorkflow((workflow) => {
return {
...workflow,
pipes: workflow.pipes.map((item, i) => (i === index ? pipe : item)),
}
})
}, [])
const addPipe = useCallback(() => {
setWorkflow((workflow) => {
return {
...workflow,
pipes: workflow.pipes.concat([
{ ...defaultPipe, name: `New Pipeline ${workflow.pipes.length + 1}` },
]),
}
})
}, [])
const removePipe = useCallback((index: number) => {
setWorkflow((workflow) => {
return {
...workflow,
pipes: remove(index, 1, workflow.pipes),
}
})
}, [])
return (
<Container>
<Form>
<FormRow row={{ title: 'Name' }}>
<FormRowItem>
<FormInput
value={workflow.name}
onChange={(ev) =>
setWorkflow({ ...workflow, name: ev.target.value })
}
/>
</FormRowItem>
</FormRow>
<FormRow row={{ title: 'Description' }}>
<FormRowItem>
<FormInput
value={workflow.description}
onChange={(ev) =>
setWorkflow({ ...workflow, description: ev.target.value })
}
/>
</FormRowItem>
</FormRow>
</Form>
<Flexbox inline={true} className='workflow__pipe' alignItems='flex-start'>
{workflow.pipes.map((pipe, i) => {
return (
<Flexbox key={i} alignItems='flex-start'>
<PipeBuilder pipe={pipe} onChange={(pipe) => setPipe(pipe, i)} />
<Button
variant='transparent'
onClick={() => removePipe(i)}
iconPath={mdiClose}
></Button>
</Flexbox>
)
})}
<Button onClick={addPipe} iconPath={mdiPlus}>
Add Pipeline
</Button>
</Flexbox>
<div>
<Button onClick={() => onSubmit(workflow)} disabled={working}>
Save
</Button>
</div>
</Container>
)
}
WorkflowBuilder.getInitialProps = getTeamIndexPageData
export default WorkflowBuilder
const Container = styled.div`
padding: ${({ theme }) => theme.sizes.spaces.df}px;
& .workflow__pipe {
padding: ${({ theme }) => theme.sizes.spaces.df}px;
& > * {
flex: 0 0 auto;
}
}
`
const defaultPipe: SerializedPipe = {
name: 'New Pipeline',
event: 'github.issues.opened',
configuration: {
type: 'operation',
identifier: 'boost.docs.create',
input: {
type: 'constructor',
info: {
type: 'struct',
refs: {},
},
},
},
}