Skip to content

Commit bca8069

Browse files
committed
Add possibility to create new object
1 parent cda0319 commit bca8069

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

src/api/catalogApi.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,21 @@ class CatalogApi {
5454
}
5555

5656

57-
static saveCatalog(catalogClass, id, catalog) {
57+
static updateObject(catalogClass, id, catalog) {
5858
return axios({
5959
method: 'put',
6060
url: `${server}/form/object/${catalogClass}/${id}`,
6161
data: catalog
6262
});
6363
}
64+
65+
static saveObject(catalogClass, catalog) {
66+
return axios({
67+
method: 'post',
68+
url: `${server}/form/object/${catalogClass}`,
69+
data: catalog
70+
});
71+
}
6472
}
6573

6674
export default CatalogApi;

src/components/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class App extends Component {
3636
<Route exact path={"/"} component={CatalogListPage}/>
3737
<Route exact path={"/catalog/:name"} component={CatalogPage}/>
3838
<Route exact path={"/catalog/:name/object/:id"} component={ManageCatalogPage}/>
39+
<Route exact path={"/catalog/:name/object"} component={ManageCatalogPage}/>
3940

4041
</Content>
4142
<Footer style={{ textAlign: 'center' }}>

src/components/catalog/CatalogPage.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import {Breadcrumb, Table, Button} from 'antd';
3+
import { Breadcrumb, Table, Button, notification } from 'antd';
44
import { NavLink } from 'react-router-dom';
55
import CatalogApi from '../../api/catalogApi';
66

@@ -65,11 +65,23 @@ class CatalogPage extends React.Component {
6565
this.setState({catalog: response.data});
6666
return CatalogApi.getCatalogExtent(name);
6767
})
68+
6869
.then((response) => {
6970
this.setState({extent: response.data.children});
71+
})
72+
73+
.catch((error) => {
74+
notification.error({
75+
message: 'An error has occurred: ' + error.summary
76+
});
7077
});
7178
}
7279

80+
add = () => {
81+
const path = this.props.location.pathname;
82+
this.props.history.replace(`${path}/object`);
83+
};
84+
7385
edit = (record) => {
7486
const path = this.props.location.pathname;
7587
this.props.history.replace(`${path}/object/${record._id}`);
@@ -93,7 +105,7 @@ class CatalogPage extends React.Component {
93105
<Button type="primary"
94106
style={{float: 'right', marginBottom: 8}}
95107
size="small"
96-
onClick={this.handleAdd}
108+
onClick={this.add}
97109
className="clearfix"
98110
>
99111
Add

src/components/catalog/ManageCatalogPage.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export class ManageCatalogPage extends React.Component {
1515
catalogPath: `/catalog/${this.props.match.params.name}`,
1616
collections: {},
1717
isTouched: false,
18-
form: { name: '' },
18+
form: {
19+
name: ''
20+
},
1921
errors: {},
2022
saving: false,
2123
loading: true
@@ -35,11 +37,26 @@ export class ManageCatalogPage extends React.Component {
3537
}
3638
});
3739

38-
return CatalogApi.getFormObjectById(this.props.match.params.name, this.props.match.params.id);
40+
const id = this.props.match.params.id;
41+
const name = this.props.match.params.name;
42+
43+
if (id) {
44+
return CatalogApi.getFormObjectById(name, id);
45+
}
46+
47+
return { data: { _class: this.state.catalog.class } };
3948
})
4049

41-
.then((response) => {
42-
this.setState({form: response.data, loading: false});
50+
.then(({data}) => {
51+
this.setState({form: data, loading: false});
52+
})
53+
54+
.catch(({response}) => {
55+
const summary = (response && response.data && response.data.summary);
56+
57+
notification.error({
58+
message: 'An error has occurred: ' + summary
59+
});
4360
});
4461
}
4562

@@ -83,7 +100,17 @@ export class ManageCatalogPage extends React.Component {
83100

84101
this.setState({ saving: true });
85102

86-
CatalogApi.saveCatalog(this.props.match.params.name, this.props.match.params.id, this.state.form)
103+
let promise;
104+
const name = this.props.match.params.name;
105+
const id = this.props.match.params.id;
106+
107+
if (id) {
108+
promise = CatalogApi.updateObject(name, id, this.state.form);
109+
} else {
110+
promise = CatalogApi.saveObject(name, this.state.form);
111+
}
112+
113+
promise
87114
.then(() => {
88115
notification.success({
89116
message: 'Saved successfully!'
@@ -145,7 +172,7 @@ export class ManageCatalogPage extends React.Component {
145172

146173
{!this.state.loading &&
147174
<div>
148-
{!this.props.match.params.id && <h1>'{this.state.catalog.name}: creating new object'</h1>}
175+
{!this.props.match.params.id && <h1>{this.state.catalog.name}: creating new object</h1>}
149176

150177
{this.props.match.params.id &&
151178
(this.state.catalog.objpermissions.indexOf('U') !== -1) &&

0 commit comments

Comments
 (0)