Skip to content

Commit d64cf4c

Browse files
author
Dennis Labordus
authored
Merge pull request #192 from com-pas/fix-save-with-error
Close Save Dialog only if service call is successful
2 parents a489729 + 7cb9d62 commit d64cf4c

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

src/compas/CompasLabelsField.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ export class CompasLabelsFieldElement extends LitElement {
7777
this.requestUpdate('labels');
7878
}
7979

80+
public updateLabelsInPrivateElement(privateElement: Element): void {
81+
// We will just add or replace the complete Labels Element, so if it exists
82+
// first remove it and always add the new one.
83+
if (this.originalLabelsElement) {
84+
privateElement?.removeChild(this.originalLabelsElement);
85+
}
86+
privateElement?.append(this.newLabelsElement);
87+
this.originalLabelsElement = this.newLabelsElement;
88+
}
89+
8090
render(): TemplateResult {
8191
const labels = this.labels;
8292
return html`

src/compas/CompasSave.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,14 @@ export default class CompasSaveElement extends CompasExistsIn(LitElement) {
7676
private updateLabels() {
7777
const sclElement = this.doc.documentElement;
7878
const privateElement = getPrivate(sclElement, COMPAS_SCL_PRIVATE_TYPE);
79-
// We will just add or replace the complete Labels Element, so if it exists
80-
// first remove it and always add the new one.
81-
if (this.labelsField.originalLabelsElement) {
82-
privateElement?.removeChild(this.labelsField.originalLabelsElement);
83-
}
84-
privateElement?.append(this.labelsField.newLabelsElement);
79+
this.labelsField.updateLabelsInPrivateElement(privateElement!);
8580
}
8681

87-
private async addSclToCompas(doc: XMLDocument): Promise<void> {
82+
private async addSclToCompas(doc: XMLDocument): Promise<boolean> {
8883
const name = stripExtensionFromName(this.nameField.value);
8984
const comment = this.commentField.value;
9085
const docType = this.sclTypeRadioGroup.getSelectedValue() ?? '';
86+
let success = false;
9187

9288
await CompasSclDataService()
9389
.addSclDocument(docType, { sclName: name, comment: comment, doc: doc })
@@ -101,18 +97,22 @@ export default class CompasSaveElement extends CompasExistsIn(LitElement) {
10197
title: get('compas.save.addSuccess'),
10298
})
10399
);
100+
success = true;
104101
})
105102
.catch(reason => createLogEvent(this, reason));
103+
104+
return success;
106105
}
107106

108107
private async updateSclInCompas(
109108
docId: string,
110109
docName: string,
111110
doc: XMLDocument
112-
): Promise<void> {
111+
): Promise<boolean> {
113112
const changeSet = this.changeSetRadiogroup.getSelectedValue();
114113
const comment = this.commentField.value;
115114
const docType = getTypeFromDocName(docName);
115+
let success = false;
116116

117117
await CompasSclDataService()
118118
.updateSclDocument(docType, docId, {
@@ -130,16 +130,19 @@ export default class CompasSaveElement extends CompasExistsIn(LitElement) {
130130
title: get('compas.save.updateSuccess'),
131131
})
132132
);
133+
success = true;
133134
})
134135
.catch(reason => createLogEvent(this, reason));
136+
137+
return success;
135138
}
136139

137-
async saveToCompas(): Promise<void> {
140+
async saveToCompas(): Promise<boolean> {
138141
this.updateLabels();
139142
if (!this.docId || !this.existInCompas) {
140-
await this.addSclToCompas(this.doc);
143+
return this.addSclToCompas(this.doc);
141144
} else {
142-
await this.updateSclInCompas(this.docId, this.docName, this.doc);
145+
return this.updateSclInCompas(this.docId, this.docName, this.doc);
143146
}
144147
}
145148

src/menu/CompasSave.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ export default class CompasSaveMenuPlugin extends LitElement {
3434
this.dialog.open = true;
3535
}
3636

37+
private async saveToCoMPAS(): Promise<void> {
38+
const success = await this.compasSaveElement.saveToCompas();
39+
if (success) {
40+
this.dialog.close();
41+
}
42+
}
43+
3744
render(): TemplateResult {
3845
return html`<mwc-dialog
3946
id="compas-save-dlg"
@@ -55,10 +62,7 @@ export default class CompasSaveMenuPlugin extends LitElement {
5562
label="${translate('save')}"
5663
@click=${() => {
5764
if (this.compasSaveElement.valid()) {
58-
this.dispatchEvent(
59-
newPendingStateEvent(this.compasSaveElement.saveToCompas())
60-
);
61-
this.dialog.close();
65+
this.dispatchEvent(newPendingStateEvent(this.saveToCoMPAS()));
6266
}
6367
}}
6468
></mwc-button>

src/menu/CompasSaveAs.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ export default class CompasSaveAsMenuPlugin extends LitElement {
3131
this.dialog.open = true;
3232
}
3333

34+
private async saveToCoMPAS(): Promise<void> {
35+
const success = await this.compasSaveElement.saveToCompas();
36+
if (success) {
37+
this.dialog.close();
38+
}
39+
}
40+
3441
render(): TemplateResult {
3542
return html`<mwc-dialog
3643
id="compas-save-as-dlg"
@@ -48,10 +55,7 @@ export default class CompasSaveAsMenuPlugin extends LitElement {
4855
label="${translate('save')}"
4956
@click=${() => {
5057
if (this.compasSaveElement.valid()) {
51-
this.dispatchEvent(
52-
newPendingStateEvent(this.compasSaveElement.saveToCompas())
53-
);
54-
this.dialog.close();
58+
this.dispatchEvent(newPendingStateEvent(this.saveToCoMPAS()));
5559
}
5660
}}
5761
></mwc-button>

test/unit/compas/CompasLabelsField.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ describe('compas-labels-field', () => {
3939
expect(labelElements.length).to.be.equal(1);
4040
});
4141

42+
it('when calling updateLabelsInPrivateElement then Private Element is updated', async () => {
43+
expect(privateElement.querySelectorAll('Label').length).to.be.equal(0);
44+
45+
await addLabel(element, 'Label1');
46+
element.updateLabelsInPrivateElement(privateElement);
47+
48+
expect(privateElement.querySelectorAll('Label').length).to.be.equal(1);
49+
});
50+
4251
it('looks like the latest snapshot', async () => {
4352
await expect(element).shadowDom.to.equalSnapshot();
4453
});
@@ -72,6 +81,15 @@ describe('compas-labels-field', () => {
7281
expect(labelElements.length).to.be.equal(2);
7382
});
7483

84+
it('when calling updateLabelsInPrivateElement then Private Element is updated', async () => {
85+
expect(privateElement.querySelectorAll('Label').length).to.be.equal(1);
86+
87+
await addLabel(element, 'Label2');
88+
element.updateLabelsInPrivateElement(privateElement);
89+
90+
expect(privateElement.querySelectorAll('Label').length).to.be.equal(2);
91+
});
92+
7593
it('when removing a label then label element removed', async () => {
7694
await removeLabel(element, 'Label1');
7795

0 commit comments

Comments
 (0)