Skip to content

Commit 0dfc027

Browse files
action configuration conversion
1 parent 6d564d8 commit 0dfc027

File tree

8 files changed

+198
-110
lines changed

8 files changed

+198
-110
lines changed

src/cloud/components/Automations/EventInfo.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react'
2+
import { BoostType } from '../../lib/automations'
23

34
interface EventSelectProps {
45
name: string
5-
typeDef: JsonTypeDef
6+
typeDef: BoostType
67
}
78

89
const EventInfo = ({ typeDef }: EventSelectProps) => {

src/cloud/components/Automations/FilterBuilder.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import FormSelect, {
1010
import FormRow from '../../../design/components/molecules/Form/templates/FormRow'
1111
import FormRowItem from '../../../design/components/molecules/Form/templates/FormRowItem'
1212
import { SerializedPipe } from '../../interfaces/db/automations'
13-
import { JsonTypeDef } from '../../lib/automations/events'
13+
import { BoostType } from '../../lib/automations'
1414
import { flattenObj } from '../../lib/utils/object'
1515

1616
interface FilterBuilderProps {
17-
typeDef: JsonTypeDef
17+
typeDef: BoostType
1818
filter: SerializedPipe['filter']
1919
onChange: (filter: SerializedPipe['filter']) => void
2020
}

src/cloud/components/Automations/PipeBuilder.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mdiPlus } from '@mdi/js'
2-
import React, { useMemo } from 'react'
2+
import React, { useCallback, useMemo } from 'react'
33
import Button from '../../../design/components/atoms/Button'
44
import Form from '../../../design/components/molecules/Form'
55
import FormInput from '../../../design/components/molecules/Form/atoms/FormInput'
@@ -8,10 +8,10 @@ import FormRow from '../../../design/components/molecules/Form/templates/FormRow
88
import FormRowItem from '../../../design/components/molecules/Form/templates/FormRowItem'
99
import styled from '../../../design/lib/styled'
1010
import { SerializedPipe } from '../../interfaces/db/automations'
11+
import { OpNode, StructNode } from '../../lib/automations/ast'
1112
import supportedEvents from '../../lib/automations/events'
1213
import CreateDocActionConfigurator from './actions/CreateDocActionConfigurator'
1314
import UpdateDocActionConfigurator from './actions/UpdateDocActionConfigurator'
14-
import EventInfo from './EventInfo'
1515
import FilterBuilder from './FilterBuilder'
1616

1717
const SUPPORTED_EVENT_NAMES = Object.keys(supportedEvents).map((key) => {
@@ -45,6 +45,13 @@ const PipeBuilder = ({ pipe, onChange }: PipeBuilderProps) => {
4545
)
4646
}, [pipe.configuration])
4747

48+
const updateConfig = useCallback(
49+
(input: SerializedPipe['configuration']['input']) => {
50+
onChange({ ...pipe, configuration: { ...pipe.configuration, input } })
51+
},
52+
[pipe, onChange]
53+
)
54+
4855
return (
4956
<Container>
5057
<FormRow>
@@ -100,14 +107,7 @@ const PipeBuilder = ({ pipe, onChange }: PipeBuilderProps) => {
100107
onChange={({ value }) =>
101108
onChange({
102109
...pipe,
103-
configuration: {
104-
type: 'operation',
105-
identifier: value,
106-
input: {
107-
type: 'constructor',
108-
info: { type: 'struct', refs: {} },
109-
},
110-
},
110+
configuration: OpNode(value, StructNode({})),
111111
})
112112
}
113113
/>
@@ -116,15 +116,15 @@ const PipeBuilder = ({ pipe, onChange }: PipeBuilderProps) => {
116116
<FormRow>
117117
{action.value === 'boost.doc.create' && (
118118
<CreateDocActionConfigurator
119-
configuration={pipe.configuration}
120-
onChange={(configuration) => onChange({ ...pipe, configuration })}
119+
configuration={pipe.configuration.input}
120+
onChange={updateConfig}
121121
eventType={currentEvent}
122122
/>
123123
)}
124124
{action.value === 'boost.doc.update' && (
125125
<UpdateDocActionConfigurator
126-
configuration={pipe.configuration}
127-
onChange={(configuration) => onChange({ ...pipe, configuration })}
126+
configuration={pipe.configuration.input}
127+
onChange={updateConfig}
128128
eventType={currentEvent}
129129
/>
130130
)}

src/cloud/components/Automations/actions/CreateDocActionConfigurator.tsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import FormEmoji from '../../../../design/components/molecules/Form/atoms/FormEm
44
import FormInput from '../../../../design/components/molecules/Form/atoms/FormInput'
55
import FormTextarea from '../../../../design/components/molecules/Form/atoms/FormTextArea'
66
import FormRow from '../../../../design/components/molecules/Form/templates/FormRow'
7-
import { LiteralNode, StructNode } from '../../../lib/automations/ast'
7+
import { BoostAST } from '../../../lib/automations'
8+
import {
9+
LiteralNode,
10+
RecordNode,
11+
StructNode,
12+
} from '../../../lib/automations/ast'
813
import { flattenObj } from '../../../lib/utils/object'
914
import { ActionConfiguratorProps } from './'
1015
import ActionConfigurationInput from './ActionConfigurationInput'
1116
import FolderSelect from './FolderSelect'
12-
import PropertySelect from './PropertySelect'
17+
import PropertySelect, { SupportedType } from './PropertySelect'
1318

14-
// TODO: flatten type (bring from backend) ? do top level? 'declarations' || 'imports'
15-
// TODO: sort by type
1619
const CreateDocActionConfigurator = ({
1720
configuration,
1821
onChange,
@@ -115,14 +118,30 @@ const CreateDocActionConfigurator = ({
115118
</FormRow>
116119
<FormRow row={{ title: 'Props' }}></FormRow>
117120
<PropertySelect
118-
value={constructorTree.props || {}}
121+
value={
122+
constructorTree.props != null &&
123+
constructorTree.props.type === 'constructor' &&
124+
constructorTree.props.info.type === 'record'
125+
? constructorTree.props.info.refs.filter(isSupportedType)
126+
: []
127+
}
119128
onChange={(props) =>
120-
onChange(StructNode({ ...constructorTree, props }))
129+
onChange(StructNode({ ...constructorTree, props: RecordNode(props) }))
121130
}
122131
eventDataOptions={eventDataOptions}
123132
/>
124133
</div>
125134
)
126135
}
127136

137+
function isSupportedType(x: {
138+
key: BoostAST
139+
val: BoostAST
140+
}): x is SupportedType {
141+
return (
142+
x.key.type === 'literal' &&
143+
(x.val.type === 'operation' || x.val.type === 'literal')
144+
)
145+
}
146+
128147
export default CreateDocActionConfigurator

src/cloud/components/Automations/actions/PropertySelect.tsx

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,15 @@ import ActionConfigurationInput from './ActionConfigurationInput'
1111
import { useModal } from '../../../../design/lib/stores/modal'
1212
import { PropData } from '../../../interfaces/db/props'
1313
import { BoostAST } from '../../../lib/automations'
14-
import {
15-
LiteralNode,
16-
OpNode,
17-
RecordNode,
18-
StructNode,
19-
} from '../../../lib/automations/ast'
14+
import { LiteralNode, OpNode, StructNode } from '../../../lib/automations/ast'
2015

2116
export interface PropertySelectProps {
22-
value: BoostAST
23-
onChange: (props: BoostAST) => void
17+
value: SupportedType[]
18+
onChange: (props: SupportedType[]) => void
2419
eventDataOptions: Record<string, any>
2520
}
2621

27-
type SupportedType = {
22+
export type SupportedType = {
2823
key: Extract<BoostAST, { type: 'literal' }>
2924
val: Extract<BoostAST, { type: 'operation' } | { type: 'literal' }>
3025
}
@@ -37,29 +32,17 @@ const PropertySelect = ({
3732
const { openContextModal } = useModal()
3833

3934
const props = useMemo(() => {
40-
if (value.type !== 'constructor' || value.info.type !== 'record') {
41-
return []
42-
}
43-
return value.info.refs.filter(isSupported).map((ref) => {
35+
return value.map((ref) => {
4436
return { key: ref.key.value, val: ref.val }
4537
})
4638
}, [value])
4739

4840
const addProp = useCallback(
4941
(key: string, val: SupportedType['val']) => {
50-
if (value.type !== 'constructor' || value.info.type !== 'record') {
51-
onChange(RecordNode([{ key: LiteralNode('string', key), val }]))
52-
return
53-
}
54-
5542
onChange(
56-
RecordNode(
57-
value.info.refs
58-
.filter(
59-
(ref) => ref.key.type === 'literal' && key !== ref.key.value
60-
)
61-
.concat([{ key: LiteralNode('string', key), val }])
62-
)
43+
value
44+
.filter((ref) => key !== ref.key.value)
45+
.concat([{ key: LiteralNode('string', key), val }])
6346
)
6447
},
6548
[value, onChange]
@@ -68,8 +51,6 @@ const PropertySelect = ({
6851
return (
6952
<>
7053
{props.map(({ key, val }, i) => {
71-
// if literal get type + data from value
72-
// else extrat type from subtype || type
7354
const [propType, subType] = getPropTypeFromAst(val)
7455
const iconPath = getIconPathOfPropType(subType || propType)
7556
return (
@@ -189,15 +170,6 @@ function getDataTypeForPropType(type: PropData['type']): string | undefined {
189170
}
190171
}
191172

192-
function isSupported(x: any): x is SupportedType {
193-
return (
194-
x.key.type === 'literal' &&
195-
x.key.def.def === 'string' &&
196-
((x.val.type === 'literal' && x.val.def.def === 'propData') ||
197-
(x.val.type === 'operation' && x.val.identifier === 'boost.props.make'))
198-
)
199-
}
200-
201173
function getPropTypeFromAst(x: SupportedType['val']) {
202174
if (x.type === 'literal') {
203175
return [x.value.subType, x.value.subType]

0 commit comments

Comments
 (0)