@@ -8,7 +8,7 @@ import {newLogEvent, newPendingStateEvent, newWizardEvent, Wizard, WizardInput}
88import { CompasChangeSetRadiogroup } from "./CompasChangeSet.js" ;
99import { CompasScltypeRadiogroup } from "./CompasScltypeRadiogroup.js" ;
1010import { 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" ;
1212import { getOpenScdElement , getTypeFromDocName , stripExtensionFromName , updateDocumentInOpenSCD } from "./foundation.js" ;
1313import './CompasChangeSet.js' ;
1414import './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 ( ) }
0 commit comments