Skip to content

Commit 46be742

Browse files
authored
added cancel button for new row (#1690)
* added cancel button for new row * removed extra line on add new row * added "add" button for new row * changed order of button for new row
1 parent ddf7548 commit 46be742

File tree

2 files changed

+89
-47
lines changed

2 files changed

+89
-47
lines changed

src/dashboard/Data/Browser/Browser.react.js

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class Browser extends DashboardView {
122122
this.handleShowAcl = this.handleShowAcl.bind(this);
123123
this.onDialogToggle = this.onDialogToggle.bind(this);
124124
this.abortAddRow = this.abortAddRow.bind(this);
125+
this.saveNewRow = this.saveNewRow.bind(this);
125126
}
126127

127128
componentWillMount() {
@@ -303,6 +304,66 @@ class Browser extends DashboardView {
303304
}
304305
}
305306

307+
saveNewRow(){
308+
const obj = this.state.newObject;
309+
if (!obj) {
310+
return;
311+
}
312+
313+
obj.save(null, { useMasterKey: true }).then(
314+
objectSaved => {
315+
let msg = objectSaved.className + ' with id \'' + objectSaved.id + '\' created';
316+
this.showNote(msg, false);
317+
318+
const state = { data: this.state.data };
319+
const relation = this.state.relation;
320+
if (relation) {
321+
const parent = relation.parent;
322+
const parentRelation = parent.relation(relation.key);
323+
parentRelation.add(obj);
324+
const targetClassName = relation.targetClassName;
325+
parent.save(null, { useMasterKey: true }).then(
326+
() => {
327+
this.setState({
328+
newObject: null,
329+
data: [obj, ...this.state.data],
330+
relationCount: this.state.relationCount + 1,
331+
counts: {
332+
...this.state.counts,
333+
[targetClassName]: this.state.counts[targetClassName] + 1
334+
}
335+
});
336+
},
337+
error => {
338+
let msg = typeof error === "string" ? error : error.message;
339+
if (msg) {
340+
msg = msg[0].toUpperCase() + msg.substr(1);
341+
}
342+
obj.set(attr, prev);
343+
this.setState({ data: this.state.data });
344+
this.showNote(msg, true);
345+
}
346+
);
347+
} else {
348+
state.newObject = null;
349+
if (this.props.params.className === obj.className) {
350+
this.state.data.unshift(obj);
351+
}
352+
this.state.counts[obj.className] += 1;
353+
}
354+
355+
this.setState(state);
356+
},
357+
error => {
358+
let msg = typeof error === "string" ? error : error.message;
359+
if (msg) {
360+
msg = msg[0].toUpperCase() + msg.substr(1);
361+
}
362+
this.showNote(msg, true);
363+
}
364+
);
365+
}
366+
306367
addRowWithModal() {
307368
this.addRow();
308369
this.selectRow(undefined, true);
@@ -573,60 +634,22 @@ class Browser extends DashboardView {
573634
} else {
574635
obj.set(attr, value);
575636
}
637+
if(isNewObject){
638+
this.setState({
639+
isNewObject: obj
640+
});
641+
return;
642+
}
576643
obj.save(null, { useMasterKey: true }).then((objectSaved) => {
577-
const createdOrUpdated = isNewObject ? 'created' : 'updated';
578-
let msg = objectSaved.className + ' with id \'' + objectSaved.id + '\' ' + createdOrUpdated;
644+
let msg = objectSaved.className + ' with id \'' + objectSaved.id + '\' updated';
579645
this.showNote(msg, false);
580-
581646
const state = { data: this.state.data };
582-
583-
if (isNewObject) {
584-
const relation = this.state.relation;
585-
if (relation) {
586-
const parent = relation.parent;
587-
const parentRelation = parent.relation(relation.key);
588-
parentRelation.add(obj);
589-
const targetClassName = relation.targetClassName;
590-
parent.save(null, { useMasterKey: true }).then(() => {
591-
this.setState({
592-
newObject: null,
593-
data: [
594-
obj,
595-
...this.state.data,
596-
],
597-
relationCount: this.state.relationCount + 1,
598-
counts: {
599-
...this.state.counts,
600-
[targetClassName]: this.state.counts[targetClassName] + 1,
601-
},
602-
});
603-
}, (error) => {
604-
let msg = typeof error === 'string' ? error : error.message;
605-
if (msg) {
606-
msg = msg[0].toUpperCase() + msg.substr(1);
607-
}
608-
obj.set(attr, prev);
609-
this.setState({ data: this.state.data });
610-
this.showNote(msg, true);
611-
});
612-
} else {
613-
state.newObject = null;
614-
if (this.props.params.className === obj.className) {
615-
this.state.data.unshift(obj);
616-
}
617-
this.state.counts[obj.className] += 1;
618-
}
619-
}
620647
this.setState(state);
621648
}, (error) => {
622649
let msg = typeof error === 'string' ? error : error.message;
623650
if (msg) {
624651
msg = msg[0].toUpperCase() + msg.substr(1);
625652
}
626-
if (!isNewObject) {
627-
obj.set(attr, prev);
628-
this.setState({ data: this.state.data });
629-
}
630653

631654
this.showNote(msg, true);
632655
});
@@ -1048,6 +1071,7 @@ class Browser extends DashboardView {
10481071
onCloneSelectedRows={this.showCloneSelectedRowsDialog}
10491072
onEditSelectedRow={this.showEditRowDialog}
10501073
onEditPermissions={this.onDialogToggle}
1074+
onSaveNewRow={this.saveNewRow}
10511075
onAbortAddRow={this.abortAddRow}
10521076

10531077
columns={columns}
@@ -1067,6 +1091,7 @@ class Browser extends DashboardView {
10671091
setRelation={this.setRelation}
10681092
onAddColumn={this.showAddColumn}
10691093
onAddRow={this.addRow}
1094+
onAbortAddRow={this.abortAddRow}
10701095
onAddRowWithModal={this.addRowWithModal}
10711096
onAddClass={this.showCreateClass}
10721097
showNote={this.showNote} />

src/dashboard/Data/Browser/BrowserTable.react.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export default class BrowserTable extends React.Component {
119119
if (this.props.newObject && this.state.offset <= 0) {
120120
const currentCol = this.props.current && this.props.current.row === -1 ? this.props.current.col : undefined;
121121
newRow = (
122-
<div style={{ marginBottom: 30, borderBottom: '1px solid #169CEE' }}>
122+
<div style={{ borderBottom: '1px solid #169CEE' }}>
123123
<BrowserRow
124124
key={-1}
125125
className={this.props.className}
@@ -140,7 +140,24 @@ export default class BrowserTable extends React.Component {
140140
setRelation={this.props.setRelation}
141141
setCopyableValue={this.props.setCopyableValue}
142142
setContextMenu={this.props.setContextMenu}
143-
onEditSelectedRow={this.props.onEditSelectedRow} />
143+
onEditSelectedRow={this.props.onEditSelectedRow}
144+
/>
145+
<Button
146+
value="Add"
147+
width="55px"
148+
primary={true}
149+
onClick={() => {
150+
this.props.onSaveNewRow();
151+
this.props.setEditing(false);
152+
}}
153+
additionalStyles={{ fontSize: '12px', height: '20px', lineHeight: '20px', margin: '5px', padding: '0'}}
154+
/>
155+
<Button
156+
value="Cancel"
157+
width="55px"
158+
onClick={this.props.onAbortAddRow}
159+
additionalStyles={{ fontSize: '12px', height: '20px', lineHeight: '20px', margin: '5px', padding: '0'}}
160+
/>
144161
</div>
145162
);
146163
}

0 commit comments

Comments
 (0)