diff --git a/src/app/helpers/plugin.ts b/src/app/helpers/plugin.ts index b9f00da..74d8cae 100644 --- a/src/app/helpers/plugin.ts +++ b/src/app/helpers/plugin.ts @@ -16,6 +16,27 @@ export class Plugin { } launchURL(url: string): void { + // Internal helper for notify errors + const notifyError = (error?: any) => { + let msg: string = django.gettext( + 'Error communicating with your service. Please, retry again.' + ); + if (typeof error === 'string') { + msg = error; + } else if (error.status === 403) { + // Session timeout + msg = django.gettext('Your session has expired. Please, login again'); + } + window.setTimeout(() => { + this.showAlert(django.gettext('Error'), msg, 5000); + if (error.status === 403) { + window.setTimeout(() => { + this.api.logout(); + }, 5000); + } + }); + }; + // If uds url... if (url.substring(0, 7) === 'udsa://') { const params = url.split('//')[1].split('/'); @@ -27,70 +48,64 @@ export class Plugin { 0, // Now UDS tries to check status new Observable((observer) => { - const notifyError = () => { - window.setTimeout(() => { - this.showAlert( - django.gettext('Error'), - django.gettext( - 'Error communicating with your service. Please, retry again.' - ), - 5000 - ); - }); - }; let readyTime = 0; const checker = () => { - this.api.status(params[0], params[1]).subscribe( - (data) => { - if (data.status === 'ready') { - if (!readyTime) { - readyTime = Date.now(); // Milisecodns - alert.componentInstance.data.title = django.gettext( - 'Service ready' - ); - alert.componentInstance.data.body = django.gettext( - 'Launching UDS Client, almost done.' - ); - } else { - // If Component took too long... - if (Date.now() - readyTime > this.delay * 5) { - // Wait 5 times the default delay - alert.componentInstance.data.title = - django.gettext('Service ready') + - ' - ' + - django.gettext('UDS Client not launching'); - alert.componentInstance.data.body = - '' + - django.gettext( - 'It seems that you don\'t have UDS Client installed. Please, install it from here: ' - ) + - '' + - '' + - django.gettext('UDS Client Download') + - ''; + if (alert.componentInstance) { + // Not closed dialog... + this.api.status(params[0], params[1]).subscribe( + (data) => { + if (data.status === 'ready') { + if (!readyTime) { + readyTime = Date.now(); // Milisecodns + alert.componentInstance.data.title = django.gettext( + 'Service ready' + ); + alert.componentInstance.data.body = django.gettext( + 'Launching UDS Client, almost done.' + ); + } else { + // If Component took too long... + if (Date.now() - readyTime > this.delay * 5) { + // Wait 5 times the default delay + alert.componentInstance.data.title = + django.gettext('Service ready') + + ' - ' + + django.gettext('UDS Client not launching'); + alert.componentInstance.data.body = + '' + + django.gettext( + 'It seems that you don\'t have UDS Client installed. Please, install it from here: ' + ) + + '' + + '' + + django.gettext('UDS Client Download') + + ''; + } } + window.setTimeout(checker, this.delay); // Recheck after delay seconds + } else if (data.status === 'accessed') { + alert.componentInstance.data.body = django.gettext( + 'Machine ready, waiting for UDS Client' + ); + observer.next(true); + observer.complete(); + } else if (data.status === 'running') { + window.setTimeout(checker, this.delay); // Recheck after delay seconds + } else { + observer.next(true); + observer.complete(); + notifyError(); } - window.setTimeout(checker, this.delay); // Recheck after delay seconds - } else if (data.status === 'accessed') { - alert.componentInstance.data.body = django.gettext( - 'Machine ready, waiting for UDS Client' - ); + }, + (error) => { observer.next(true); observer.complete(); - } else if (data.status === 'running') { - window.setTimeout(checker, this.delay); // Recheck after delay seconds - } else { - observer.next(true); - observer.complete(); - notifyError(); + notifyError(error); } - }, - (error) => { - observer.next(true); - observer.complete(); - notifyError(); - } - ); + ); + } }; window.setTimeout(checker); }) @@ -126,58 +141,49 @@ export class Plugin { 0, // Now UDS tries to check status before closing dialog... new Observable((observer) => { - const notifyError = (error: string = null) => { - window.setTimeout(() => { - this.showAlert( - django.gettext('Error'), - error || - django.gettext( - 'Error communicating with your service. Please, retry again.' - ), - 5000 - ); - }); - }; const checker = () => { - this.api.transportUrl(url).subscribe( - (data) => { - if (data.url) { - observer.next(true); - observer.complete(); // Notify window to close... - if (data.url.indexOf('o_s_w=') !== -1) { - window.location.href = data.url; - } else { - // If the url contains "o_n_w", will open the url on a new window ALWAYS - let name = 'global'; - if (data.url.indexOf('o_n_w=') !== -1) { - // Extract window name from o_n_w parameter if present - const onw = /.*o_n_w=([a-zA-Z0-9._-]*)/.exec(data.url); - if (onw) { - name = onw[1]; + if (alert.componentInstance) { + // Not closed dialog... + this.api.transportUrl(url).subscribe( + (data) => { + if (data.url) { + observer.next(true); + observer.complete(); // Notify window to close... + if (data.url.indexOf('o_s_w=') !== -1) { + window.location.href = data.url; + } else { + // If the url contains "o_n_w", will open the url on a new window ALWAYS + let name = 'global'; + if (data.url.indexOf('o_n_w=') !== -1) { + // Extract window name from o_n_w parameter if present + const onw = /.*o_n_w=([a-zA-Z0-9._-]*)/.exec(data.url); + if (onw) { + name = onw[1]; + } } + if (Plugin.transportsWindow[name]) { + Plugin.transportsWindow[name].close(); + } + Plugin.transportsWindow[name] = window.open( + data.url, + 'uds_trans_' + name + ); } - if (Plugin.transportsWindow[name]) { - Plugin.transportsWindow[name].close(); - } - Plugin.transportsWindow[name] = window.open( - data.url, - 'uds_trans_' + name - ); + } else if (!data.running) { + observer.next(true); + observer.complete(); + notifyError(data.error); + } else { + window.setTimeout(checker, this.delay); // Recheck after 5 seconds } - } else if (!data.running) { + }, + (error) => { observer.next(true); observer.complete(); - notifyError(data.error); - } else { - window.setTimeout(checker, this.delay); // Recheck after 5 seconds + notifyError(error); } - }, - (error) => { - observer.next(true); - observer.complete(); - notifyError(); - } - ); + ); + } }; window.setTimeout(checker); }) diff --git a/src/app/uds-api.service-type.ts b/src/app/uds-api.service-type.ts index 7be9d60..ae6b2df 100644 --- a/src/app/uds-api.service-type.ts +++ b/src/app/uds-api.service-type.ts @@ -33,6 +33,12 @@ export interface UDSApiServiceType { /* transport url */ transportUrl(url: string): Observable; + /* Go to admin dashboard */ + gotoAdmin(): void; + + /* Executes logout */ + logout(): void; + /** * Gets services information */