-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GenericForm.js
63 lines (55 loc) · 1.62 KB
/
GenericForm.js
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
import React, { Component } from 'react'
import * as FormTypes from './forms'
import { completeTask, startProcessInstance, loadTaskVariables } from '../actions'
import { connect } from 'react-redux'
class GenericForm extends Component {
componentDidUpdate(prevProps, prevState) {
if (!this.state || !this.state.loading) {
this.loadExistingVariables()
}
}
render() {
const { formKey, processDefinitionKey, taskId } = this.props
const Form = FormTypes[processDefinitionKey][formKey]
if (taskId == null) {
return (
<div className="generic-form">
<Form onSubmit={(values, dispatch) => this.handleStartInstance(values, dispatch)} />
</div>
)
} else {
return (
<div className="generic-form">
<Form onSubmit={(values, dispatch) => this.handleComplete(values, dispatch)} />
</div>
)
}
}
loadExistingVariables() {
let { form, dispatch, taskId } = this.props
if (form) {
this.setState({ loading: true });
dispatch(loadTaskVariables(taskId, form.registeredFields))
}
}
handleComplete(values, dispatch) {
values = this.getBody(values)
return dispatch(completeTask(this.props.taskId, values))
}
handleStartInstance(values, dispatch) {
values = this.getBody(values)
return dispatch(startProcessInstance(this.props.processDefinitionKey, values))
}
getBody(values) {
let variables = {}
Object.keys(values).forEach((item) => {
variables[item] = {'value': values[item]}
});
return {
'variables': variables
}
}
}
export default connect(
state => ({})
)(GenericForm)