-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathPipeBuilder.tsx
166 lines (149 loc) · 4.87 KB
/
PipeBuilder.tsx
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { mdiPlus } from '@mdi/js'
import React, { useCallback, useMemo } from 'react'
import Button from '../../../design/components/atoms/Button'
import Form from '../../../design/components/molecules/Form'
import FormInput from '../../../design/components/molecules/Form/atoms/FormInput'
import FormSelect from '../../../design/components/molecules/Form/atoms/FormSelect'
import FormRow from '../../../design/components/molecules/Form/templates/FormRow'
import FormRowItem from '../../../design/components/molecules/Form/templates/FormRowItem'
import styled from '../../../design/lib/styled'
import { SerializedPipe } from '../../interfaces/db/automations'
import { OpNode, StructNode } from '../../lib/automations/ast'
import supportedEvents from '../../lib/automations/events'
import CreateDocActionConfigurator from './actions/CreateDocActionConfigurator'
import UpdateDocActionConfigurator from './actions/UpdateDocActionConfigurator'
import FilterBuilder from './FilterBuilder'
const SUPPORTED_EVENT_NAMES = Object.keys(supportedEvents).map((key) => {
return { label: key, value: key }
})
const SUPPORTED_ACTION_OPTIONS = [
{ value: 'boost.docs.create', label: 'boost.docs.create' },
{ value: 'boost.docs.update', label: 'boost.docs.update' },
]
interface PipeBuilderProps {
pipe: SerializedPipe
onChange: (pipe: SerializedPipe) => void
}
const PipeBuilder = ({ pipe, onChange }: PipeBuilderProps) => {
const currentEvent = useMemo(() => {
return supportedEvents[pipe.event]
}, [pipe.event])
const action = useMemo(() => {
if (pipe.configuration.type !== 'operation') {
return SUPPORTED_ACTION_OPTIONS[0]
}
const identifier = pipe.configuration.identifier
return (
SUPPORTED_ACTION_OPTIONS.find(({ value }) => value === identifier) ||
SUPPORTED_ACTION_OPTIONS[0]
)
}, [pipe.configuration])
const updateConfig = useCallback(
(input: SerializedPipe['configuration']['input']) => {
onChange({ ...pipe, configuration: { ...pipe.configuration, input } })
},
[pipe, onChange]
)
return (
<Container>
<FormRow>
<FormRowItem>
<FormInput
value={pipe.name}
onChange={(ev) => onChange({ ...pipe, name: ev.target.value })}
/>
</FormRowItem>
</FormRow>
<div className='section'>
<h4>Event</h4>
<FormRow>
<FormRowItem>
<FormSelect
value={{ label: pipe.event, value: pipe.event }}
options={SUPPORTED_EVENT_NAMES}
onChange={({ value }) => onChange({ ...pipe, event: value })}
/>
</FormRowItem>
</FormRow>
<FormRow>
<div>Select Event</div>
</FormRow>
</div>
<div className='spacer'></div>
{pipe.filter != null ? (
<div className='section'>
<h4>Filter</h4>
<FilterBuilder
filter={pipe.filter}
typeDef={currentEvent}
onChange={(filter) => onChange({ ...pipe, filter })}
/>
</div>
) : (
<Button
onClick={() => onChange({ ...pipe, filter: {} })}
iconPath={mdiPlus}
></Button>
)}
<div className='spacer'></div>
<div className='section'>
<h4>Action</h4>
<FormRow>
<FormRowItem>
<FormSelect
options={SUPPORTED_ACTION_OPTIONS}
value={action}
onChange={({ value }) =>
onChange({
...pipe,
configuration: OpNode(value, StructNode({})),
})
}
/>
</FormRowItem>
</FormRow>
<FormRow>
{action.value === 'boost.docs.create' && (
<CreateDocActionConfigurator
configuration={pipe.configuration.input}
onChange={updateConfig}
eventType={currentEvent}
/>
)}
{action.value === 'boost.docs.update' && (
<UpdateDocActionConfigurator
configuration={pipe.configuration.input}
onChange={updateConfig}
eventType={currentEvent}
/>
)}
</FormRow>
</div>
</Container>
)
}
export default PipeBuilder
const Container = styled(Form)`
display: flex;
flex-direction: column;
& .section {
border: 1px solid ${({ theme }) => theme.colors.border.main};
background-color: ${({ theme }) => theme.colors.background.primary};
padding: ${({ theme }) => theme.sizes.spaces.sm}px;
& h4 {
margin: ${({ theme }) => theme.sizes.spaces.sm}px 0;
}
}
& .spacer {
position: relative;
height: ${({ theme }) => theme.sizes.spaces.xl}px;
margin: 0;
&:before {
content: '';
height: 100%;
width: 50%;
border-right: 2px solid ${({ theme }) => theme.colors.border.main};
position: absolute;
}
}
`