Skip to content

Commit 8fb14a5

Browse files
author
Rob Tjalma
authored
Merge pull request #69 from com-pas/handle-saveto
Improve Check if Create or Update needs to be done
2 parents 8a587d9 + 232b6d2 commit 8fb14a5

File tree

8 files changed

+127
-45
lines changed

8 files changed

+127
-45
lines changed

__snapshots__/compas-save-to.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# `compas-save-to`
22

3+
## `still determining if document exists in CoMPAS`
4+
5+
#### `looks like the latest snapshot`
6+
7+
```html
8+
<mwc-list>
9+
<mwc-list-item
10+
aria-disabled="false"
11+
mwc-list-item=""
12+
tabindex="0"
13+
>
14+
[compas.loading]
15+
</mwc-list-item>
16+
</mwc-list>
17+
18+
```
19+
320
## `new document in compas`
421

522
#### `looks like the latest snapshot`

src/compas-editors/CompasVersions.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,20 @@ export default class CompasVersionsPlugin extends LitElement {
1717
@property()
1818
scls!: Element[];
1919

20-
firstUpdated() {
20+
firstUpdated(): void {
2121
if (!this.docId) {
2222
this.scls = [];
2323
} else {
2424
this.fetchData()
2525
}
2626
}
2727

28-
fetchData() {
28+
fetchData(): void {
2929
const type = getTypeFromDocName(this.docName);
3030
CompasSclDataService().listVersions(type, this.docId)
3131
.then(xmlResponse => {
3232
this.scls = Array.from(xmlResponse.querySelectorAll('Item') ?? [])
33-
})
34-
.catch(createLogEvent);
33+
});
3534
}
3635

3736
confirmRestoreCompas(version: string): void {

src/compas-services/foundation.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ import {get} from "lit-translate";
33
import {OpenSCD} from "../open-scd.js";
44
import {newLogEvent} from "../foundation.js";
55

6+
export const NOT_FOUND_ERROR = 'NotFoundError';
7+
export const APPLICATION_ERROR = 'ApplicationError';
8+
export const SERVER_ERROR = 'ServerError';
9+
10+
export function getOpenScdElement(): OpenSCD {
11+
return <OpenSCD>document.querySelector('open-scd');
12+
}
13+
614
export function handleResponse(response: Response): Promise<string> {
715
if (!response.ok) {
8-
let type = 'ApplicationError';
16+
let type = APPLICATION_ERROR;
917
if (response.status === 404) {
10-
type = 'NotFoundError';
18+
type = NOT_FOUND_ERROR;
1119
} else if (response.status >= 500)
1220
{
13-
type = 'ServerError';
21+
type = SERVER_ERROR;
1422
}
1523
return Promise.reject({type: type, status: response.status, message: response.statusText});
1624
}
@@ -22,7 +30,7 @@ export function parseXml(textContent: string): Promise<Document> {
2230
}
2331

2432
export function handleError(error: Error): Promise<never> {
25-
return Promise.reject({type: 'ServerError', message: error.message});
33+
return Promise.reject({type: SERVER_ERROR, message: error.message});
2634
}
2735

2836
export function createLogEvent(reason: any): void {
@@ -31,8 +39,7 @@ export function createLogEvent(reason: any): void {
3139
message += " (" + reason.status + ")";
3240
}
3341

34-
const openScd = <OpenSCD>document.querySelector('open-scd');
35-
openScd.dispatchEvent(
42+
getOpenScdElement().dispatchEvent(
3643
newLogEvent({
3744
kind: 'error',
3845
title: get('compas.error.server'),

src/compas/CompasSaveTo.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {newLogEvent, newPendingStateEvent, newWizardEvent, Wizard, WizardInput}
88
import {CompasChangeSetRadiogroup} from "./CompasChangeSet.js";
99
import {CompasScltypeRadiogroup} from "./CompasScltypeRadiogroup.js";
1010
import {CompasSclDataService} from "../compas-services/CompasSclDataService.js";
11-
import {createLogEvent} from "../compas-services/foundation.js";
11+
import {createLogEvent, NOT_FOUND_ERROR} from "../compas-services/foundation.js";
1212
import {getOpenScdElement, getTypeFromDocName, stripExtensionFromName, updateDocumentInOpenSCD} from "./foundation.js";
1313
import './CompasChangeSet.js';
1414
import './CompasScltypeRadiogroup.js';
@@ -20,6 +20,30 @@ export class CompasSaveTo extends LitElement {
2020
@property({type: String})
2121
docId!: string;
2222

23+
@property({type: Boolean})
24+
existInCompas?: boolean;
25+
26+
firstUpdated(): void {
27+
this.checkExistInCompas();
28+
}
29+
30+
checkExistInCompas(): void {
31+
if (this.docId) {
32+
const docType = getTypeFromDocName(this.docName);
33+
// Use the versions call to check if any exist, because then the document also exists
34+
// And it safes bandwidth not to retrieve the whole document.
35+
CompasSclDataService().listVersions(docType, this.docId)
36+
.then(() => this.existInCompas = true)
37+
.catch(reason => {
38+
if (reason.type && reason.type === NOT_FOUND_ERROR) {
39+
this.existInCompas = false;
40+
}
41+
});
42+
} else {
43+
this.existInCompas = false;
44+
}
45+
}
46+
2347
getNameField() : TextFieldBase {
2448
return <TextFieldBase>this.shadowRoot!.querySelector('mwc-textfield[id="name"]');
2549
}
@@ -39,7 +63,7 @@ export class CompasSaveTo extends LitElement {
3963
}
4064

4165
valid(): boolean {
42-
if (!this.docId) {
66+
if (!this.existInCompas) {
4367
return this.getNameField().checkValidity()
4468
&& this.getSclTypeRadioGroup().valid();
4569
}
@@ -108,7 +132,7 @@ export class CompasSaveTo extends LitElement {
108132
}
109133

110134
async saveToCompas(wizard: Element, docId: string, docName: string, doc: XMLDocument): Promise<void> {
111-
if (!docId) {
135+
if (!this.existInCompas) {
112136
await this.addSclToCompas(wizard, doc);
113137
} else {
114138
await this.updateSclInCompas(wizard, docId, docName, doc);
@@ -126,7 +150,14 @@ export class CompasSaveTo extends LitElement {
126150
}
127151

128152
render(): TemplateResult {
129-
if (!this.docId) {
153+
if (this.existInCompas === undefined) {
154+
return html `
155+
<mwc-list>
156+
<mwc-list-item>${translate("compas.loading")}</mwc-list-item>
157+
</mwc-list>`
158+
}
159+
160+
if (!this.existInCompas) {
130161
return html`
131162
<mwc-textfield dialogInitialFocus id="name" label="${translate('scl.name')}"
132163
value="${this.docName}" required>
@@ -137,8 +168,7 @@ export class CompasSaveTo extends LitElement {
137168
${this.renderCommentTextField()}
138169
`;
139170
}
140-
141-
return html `
171+
return html`
142172
<compas-changeset-radiogroup></compas-changeset-radiogroup>
143173
144174
${this.renderCommentTextField()}

src/compas/CompasScl.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ export class CompasScl extends LitElement {
1717
@property()
1818
scls!: Element[];
1919

20-
firstUpdated() {
20+
firstUpdated(): void {
2121
this.fetchData();
2222
}
2323

24-
fetchData() {
24+
fetchData(): void {
2525
CompasSclDataService().listScls(this.type)
2626
.then(xmlResponse => {
2727
this.scls = Array.from(xmlResponse.querySelectorAll('Item') ?? [])
28-
})
29-
.catch(createLogEvent)
28+
});
3029
}
3130

32-
openScl(id?: string) {
31+
openScl(id?: string): void {
3332
getOpenScdElement().dispatchEvent(newPendingStateEvent(this.getSclDocument(id)));
3433
}
3534

src/compas/CompasScltypeList.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@ import {SingleSelectedEvent} from "@material/mwc-list/mwc-list-foundation";
55
import {newWizardEvent, Wizard, WizardActor} from '../foundation.js';
66

77
import {CompasSclDataService, SDS_NAMESPACE} from "../compas-services/CompasSclDataService.js";
8-
import {createLogEvent} from "../compas-services/foundation.js";
98
import {listSclsWizard} from "./CompasScl.js";
109

1110
@customElement('compas-scltype-list')
1211
export class CompasScltypeList extends LitElement {
1312
@property()
1413
sclTypes!: Element[];
1514

16-
firstUpdated() {
15+
firstUpdated(): void {
1716
this.fetchData();
1817
}
1918

20-
fetchData() {
19+
fetchData(): void {
2120
CompasSclDataService().listSclTypesAndOrder()
22-
.then(types => this.sclTypes = types)
23-
.catch(createLogEvent);
21+
.then(types => this.sclTypes = types);
2422
}
2523

2624
listScls(type: string): void {

src/compas/CompasScltypeRadiogroup.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {ListItemBase} from "@material/mwc-list/mwc-list-item-base";
33
import {translate} from "lit-translate";
44

55
import {CompasSclDataService, SDS_NAMESPACE} from "../compas-services/CompasSclDataService.js";
6-
import {createLogEvent} from "../compas-services/foundation.js";
76

87
@customElement('compas-scltype-radiogroup')
98
export class CompasScltypeRadiogroup extends LitElement {
@@ -13,14 +12,13 @@ export class CompasScltypeRadiogroup extends LitElement {
1312
@property({type: Document})
1413
sclTypes!:Element[];
1514

16-
firstUpdated() {
15+
firstUpdated(): void {
1716
this.fetchData();
1817
}
1918

20-
fetchData() {
19+
fetchData(): void {
2120
CompasSclDataService().listSclTypesAndOrder()
22-
.then(types => this.sclTypes = types)
23-
.catch(createLogEvent);
21+
.then(types => this.sclTypes = types);
2422
}
2523

2624
private getSelectedListItem() : ListItemBase | null {
Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
1-
import {expect, fixture, html} from '@open-wc/testing';
1+
import {expect, fixtureSync, html, waitUntil} from '@open-wc/testing';
2+
import sinon from "sinon";
23

34
import "../../../src/compas/CompasSaveTo.js";
45
import {CompasSaveTo} from "../../../src/compas/CompasSaveTo.js";
56

67
describe('compas-save-to', () => {
8+
let element: CompasSaveTo;
9+
const docName = 'station123.scd';
10+
const docId = '6a45ae97-5605-44f8-b4e6-25305bc6c036';
11+
12+
describe('still determining if document exists in CoMPAS', () => {
13+
beforeEach(async () => {
14+
element = fixtureSync(
15+
html`<compas-save-to .docName="${docName}" .docId="${docId}"></compas-save-to>`
16+
);
17+
18+
sinon.stub(element, 'checkExistInCompas').callsFake(() => {
19+
// Do nothing so that it seems like loading from compas.
20+
});
21+
22+
await element;
23+
});
24+
25+
it('looks like the latest snapshot', () => {
26+
expect(element).shadowDom
27+
.to.equalSnapshot();
28+
});
29+
});
30+
731
describe('new document in compas', () => {
8-
let element: CompasSaveTo;
9-
const docName = 'station123.scd';
1032
beforeEach(async () => {
11-
element = await fixture(
12-
html`
13-
<compas-save-to .docName="${docName}"></compas-save-to>`
33+
element = fixtureSync(
34+
html`<compas-save-to .docName="${docName}"></compas-save-to>`
1435
);
36+
37+
sinon.stub(element, 'checkExistInCompas').callsFake(() => {
38+
element.existInCompas = false;
39+
});
40+
41+
await element;
42+
await waitUntil(() => element.existInCompas !== undefined);
1543
});
1644

1745
it('looks like the latest snapshot', () => {
@@ -21,21 +49,27 @@ describe('compas-save-to', () => {
2149
});
2250

2351
describe('existing document in compas', () => {
24-
let element: CompasSaveTo;
25-
const docName = 'station123.scd';
26-
const docId = '6a45ae97-5605-44f8-b4e6-25305bc6c036';
2752
beforeEach(async () => {
28-
element = <CompasSaveTo>(
29-
await fixture(
30-
html`
31-
<compas-save-to .docName="${docName}" .docId="${docId}"></compas-save-to>`
32-
)
53+
element = fixtureSync(
54+
html`<compas-save-to .docName="${docName}" .docId="${docId}"></compas-save-to>`
3355
);
56+
57+
sinon.stub(element, 'checkExistInCompas').callsFake(() => {
58+
element.existInCompas = true;
59+
});
60+
61+
await element;
62+
await waitUntil(() => element.existInCompas !== undefined);
63+
3464
});
3565

3666
it('looks like the latest snapshot', () => {
3767
expect(element).shadowDom
3868
.to.equalSnapshot();
3969
});
4070
})
71+
72+
afterEach(() => {
73+
sinon.restore();
74+
});
4175
});

0 commit comments

Comments
 (0)