-
Notifications
You must be signed in to change notification settings - Fork 12
/
CollectionForm.jsx
130 lines (116 loc) · 4.7 KB
/
CollectionForm.jsx
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
import React from 'react';
import alertifyjs from 'alertifyjs';
import { Divider, Button } from '@mui/material';
import { orderBy, map, merge, cloneDeep, get, isEmpty } from 'lodash';
import APIService from '../../services/APIService';
import { recordGAUpsertEvent } from '../../common/utils';
import { COLLECTION_TYPES, WHITE } from '../../common/constants'
import FormHeader from '../common/conceptContainerFormComponents/FormHeader';
import NameAndDescription from '../common/conceptContainerFormComponents/NameAndDescription';
import ConfigurationForm from '../common/conceptContainerFormComponents/ConfigurationForm';
import LanguageForm from '../common/conceptContainerFormComponents/LanguageForm';
import AdvanceSettings from '../common/conceptContainerFormComponents/AdvanceSettings';
import CONFIG from './CollectionFormConfigs';
const TYPES = orderBy(map(COLLECTION_TYPES, t => ({id: t, name: t})), 'name');
class CollectionForm extends React.Component {
constructor(props) {
super(props)
this.state = {
owner: props.owner
}
}
onChange = (changes, replace) => {
let newState;
if(replace)
newState = {[replace]: changes[replace]}
else
newState = merge({...this.state}, changes)
this.setState(newState)
}
onSubmit = event => {
event.preventDefault();
event.stopPropagation();
const { edit, collection } = this.props
const form = document.getElementsByTagName('form')[0];
form.reportValidity()
const isFormValid = form.checkValidity()
if(isFormValid) {
recordGAUpsertEvent('Collection', edit)
const payload = this.getPayload()
const service = APIService.new().overrideURL(this.state.owner.url + 'collections/')
if(edit)
service.appendToUrl(`${collection.id}/`).put(payload).then(response => this.handleSubmitResponse(response))
else
service.post(payload).then(response => this.handleSubmitResponse(response))
}
}
handleSubmitResponse(response) {
const { edit, reloadOnSuccess, onCancel, resourceType, onSuccess } = this.props
if(response.status === 201 || response.status === 200) { // success
const verb = edit ? 'updated' : 'created'
const successMsg = `Successfully ${verb} ${resourceType}`;
const message = reloadOnSuccess ? successMsg + '. Reloading..' : successMsg;
onCancel();
alertifyjs.success(message, 1, () => {
if(reloadOnSuccess)
window.location.reload()
if(onSuccess)
onSuccess(response.data)
})
} else { // error
const genericError = get(response, '__all__')
if(genericError) {
alertifyjs.error(genericError.join('<br />'))
} else {
this.setState(
{fieldErrors: response || {}},
() => alertifyjs.error('Please fill mandatory fields.')
)
}
}
}
getPayload = () => {
let fields = cloneDeep(this.state)
delete fields.owner;
delete fields.fieldErrors;
[
'autoid_concept_mnemonic', 'autoid_concept_external_id',
'autoid_mapping_mnemonic', 'autoid_mapping_external_id',
].forEach(field => {
if(fields[field] !== 'sequential')
delete fields[`${field}_start_from`]
})
const collectionType = fields.type || get(this.props.source, 'collection_type')
if(collectionType)
fields.collection_type = collectionType
return fields
}
render () {
const { edit, owner, collection } = this.props
return (
<form>
<div className='col-xs-12 no-side-padding' style={{marginBottom: '30px', overflowX: 'hidden'}}>
<FormHeader {...CONFIG} edit={edit} />
<div className='col-xs-12'>
<NameAndDescription {...CONFIG} edit={edit} owner={owner} onChange={this.onChange} repo={collection} />
<Divider style={{width: '100%'}} />
<LanguageForm {...CONFIG} edit={edit} owner={owner} onChange={this.onChange} repo={collection} />
<Divider style={{width: '100%'}} />
<ConfigurationForm {...CONFIG} edit={edit} owner={owner} types={TYPES} onChange={this.onChange} repo={collection} />
<Divider style={{width: '100%'}} />
{
((edit && !isEmpty(collection)) || !edit) &&
<AdvanceSettings {...CONFIG} edit={edit} owner={owner} onChange={this.onChange} repo={collection} />
}
</div>
<div className='col-xs-12' style={{position: 'fixed', background: WHITE, bottom: 0, zIndex: 1999}}>
<Button variant='contained' type='submit' onClick={this.onSubmit} style={{margin: '10px 0'}}>
{edit ? 'Update Collection' : 'Create Collection'}
</Button>
</div>
</div>
</form>
)
}
}
export default CollectionForm;