@betha-plataforma/estrutura-componentes
Version:
Coleção de Web Components para compor a estrutura de uma aplicação front-end da Betha Sistemas.
153 lines (152 loc) • 5.5 kB
JavaScript
import { Component, Host, h, Prop, Watch, State } from '@stencil/core';
import { isValidAuthorizationConfig } from '../../global/api';
import { isNill } from '../../utils/functions';
import { PesquisaService } from './pesquisa.service';
export class Pesquisa {
constructor() {
this.pesquisaDisponivel = false;
}
watchAuthorization() {
this.carregarPesquisaAtual();
}
// connectedCallback() {
// return this.carregarPesquisaAtual();
// }
render() {
if (this.pesquisaDisponivel) {
return (h(Host, null,
h("slot", null,
h("iframe", { id: "bth-pesquisa--iframe", name: "bfc-iframe-pesquisa", src: this.getUrlPesquisa(), title: "Question\u00E1rio de Pesquisa de Satisfa\u00E7\u00E3o - Betha Sistemas", width: "100%", frameborder: "0", allowtransparency: "true", scrolling: "no", sandbox: "allow-scripts allow-forms allow-same-origin" }))));
}
}
getPesquisaApi() {
if (!isNill(this.pesquisaApi)) {
return this.pesquisaApi;
}
if ('___bth' in window) {
return window['___bth'].envs.suite['pesquisa'].v1.host;
}
return null;
}
getLicencasApi() {
if (!isNill(this.licencasApi)) {
return this.licencasApi;
}
if ('___bth' in window) {
return window['___bth'].envs.suite['licenses'].v1.host;
}
return null;
}
isConfiguracaoApiInconsistente() {
if (isNill(this.getPesquisaApi())) {
return true;
}
if (isNill(this.getLicencasApi())) {
return true;
}
return !isValidAuthorizationConfig(this.authorization);
}
async carregarPesquisaAtual() {
if (this.isConfiguracaoApiInconsistente()) {
console.warn('[bth-pesquisa] O endereço do serviço de pesquisas, de licenças e as credenciais de autenticação devem ser informados. Consulte a documentação do componente.');
return;
}
try {
this.pesquisaService = new PesquisaService(this.authorization, this.getPesquisaApi(), this.getLicencasApi());
this.pesquisaService.carregarIdPesquisa()
.then(idPesquisa => this.idPesquisa = idPesquisa.id)
.then(() => this.pesquisaService.idPesquisaEmAberto(this.idPesquisa))
.then(pesquisaDisponivel => this.pesquisaDisponivel = pesquisaDisponivel)
.then(() => {
if (this.pesquisaDisponivel) {
window.addEventListener('message', this.onIframeMessage);
}
});
}
catch (_a) {
console.warn('Ocorreu um erro ao carregar a pesquisa de satisfação.');
}
}
getUrlPesquisa() {
return `${this.getPesquisaApi()}/index.jsp?id=${this.idPesquisa}`;
}
onIframeMessage(event) {
if (event.data.id !== 'pesquisa') {
return;
}
if (event.data.action === 'survey.submitted.done') {
document.querySelector('bth-pesquisa').remove();
}
}
static get is() { return "bth-pesquisa"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() { return {
"$": ["pesquisa.css"]
}; }
static get styleUrls() { return {
"$": ["pesquisa.css"]
}; }
static get properties() { return {
"authorization": {
"type": "unknown",
"mutable": false,
"complexType": {
"original": "AuthorizationConfig",
"resolved": "AuthorizationConfig",
"references": {
"AuthorizationConfig": {
"location": "import",
"path": "../../global/interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Configura\u00E7\u00E3o de autoriza\u00E7\u00E3o. \u00C9 necess\u00E1ria para o componente poder autenticar com os servi\u00E7os."
}
},
"pesquisaApi": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "URL para a api de pesquisas. Por padr\u00E3o ir\u00E1 obter do env.js."
},
"attribute": "pesquisa-api",
"reflect": false
},
"licencasApi": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "URL para a api de licen\u00E7as. Por padr\u00E3o ir\u00E1 obter do env.js."
},
"attribute": "licencas-api",
"reflect": false
}
}; }
static get states() { return {
"pesquisaDisponivel": {}
}; }
static get watchers() { return [{
"propName": "authorization",
"methodName": "watchAuthorization"
}]; }
}