80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
// npm run build produz um arquivo abrirNps.js que é copiado para a pasta public
|
|
|
|
import { respostaComuns } from "p-respostas"
|
|
import type { tipo_proxima_avaliacao } from "./tipos_nps"
|
|
|
|
// exibe o iframe em tela cheia
|
|
|
|
export const abrirNps =
|
|
(emDesenvolvimento: boolean) =>
|
|
async (parametros: tipo_proxima_avaliacao["parametros"]) => {
|
|
const base_site = emDesenvolvimento
|
|
? "http://localhost:5040/nps"
|
|
: "https://carro-de-boi.idz.one/nps"
|
|
|
|
const base_api = `${base_site}/api`
|
|
|
|
const { sistema, codigo_organizacao, codigo_usuario } = parametros
|
|
|
|
const nome_local_storage_proxima = `nps_proxima_avaliacao_${sistema}_${codigo_usuario}_${codigo_organizacao}_0`
|
|
|
|
const proxima_avaliacao = localStorage.getItem(nome_local_storage_proxima)
|
|
|
|
if (!proxima_avaliacao) {
|
|
const url_proxima_avaliacao = new URL(
|
|
`${base_api}/${sistema}/proxima_avaliacao`,
|
|
)
|
|
|
|
for (const [chave, valor] of Object.entries(parametros)) {
|
|
url_proxima_avaliacao.searchParams.append(chave, valor)
|
|
}
|
|
|
|
const response = await fetch(url_proxima_avaliacao.href)
|
|
.then(
|
|
(resposta) =>
|
|
resposta.json() as Promise<tipo_proxima_avaliacao["retorno"]>,
|
|
)
|
|
.catch((error) => respostaComuns.erro(error.message))
|
|
|
|
const proxima_avaliacao = response.valor
|
|
|
|
proxima_avaliacao &&
|
|
localStorage.setItem(nome_local_storage_proxima, proxima_avaliacao)
|
|
}
|
|
|
|
const abrir_modal =
|
|
proxima_avaliacao &&
|
|
new Date().toISOString().slice(0, 10) >= proxima_avaliacao
|
|
|
|
if (!abrir_modal) {
|
|
return
|
|
}
|
|
|
|
localStorage.removeItem(nome_local_storage_proxima)
|
|
|
|
const urlIfrma = new URL(base_site)
|
|
|
|
for (const [chave, valor] of Object.entries(parametros)) {
|
|
urlIfrma.searchParams.append(chave, valor)
|
|
}
|
|
|
|
const iframe = document.createElement("iframe")
|
|
iframe.src = urlIfrma.href
|
|
iframe.style.position = "fixed"
|
|
iframe.style.top = "0"
|
|
iframe.style.left = "0"
|
|
iframe.style.width = "100%"
|
|
iframe.style.height = "100%"
|
|
iframe.style.border = "none"
|
|
iframe.style.zIndex = "999999"
|
|
document.body.appendChild(iframe)
|
|
|
|
// receber mensagem do iframe
|
|
window.addEventListener("message", (event) => {
|
|
if (event.data === "fechar") {
|
|
document.body.removeChild(iframe)
|
|
}
|
|
})
|
|
}
|
|
|
|
export type { tipo_proxima_avaliacao }
|