From 070ae977accc4a3f07d321bb4f8b76bb55e4027a Mon Sep 17 00:00:00 2001 From: fabio Date: Mon, 8 Feb 2021 21:43:30 -0300 Subject: [PATCH 01/11] =?UTF-8?q?Adiciona=20abertura=20autom=C3=A1tica=20d?= =?UTF-8?q?e=20link=20na=20notifica=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notificacoes/notificacoes.interfaces.ts | 2 ++ .../notificacoes/notificacoes.service.ts | 2 +- src/components/notificacoes/notificacoes.tsx | 22 ++++++++++++++++--- src/global/api.ts | 8 +++++-- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/components/notificacoes/notificacoes.interfaces.ts b/src/components/notificacoes/notificacoes.interfaces.ts index e07e4e9..e163e4c 100644 --- a/src/components/notificacoes/notificacoes.interfaces.ts +++ b/src/components/notificacoes/notificacoes.interfaces.ts @@ -9,6 +9,7 @@ export interface NotificacaoLink { label?: string; target: string; href: string; + autoOpen: boolean; } export interface NotificacaoLeituraEvent { @@ -25,6 +26,7 @@ export interface NotificacaoComLinkEvent { // Esta interface representa a estrutura da notificação que vem da API export interface Notificacao { id: string; + systemId?: number; sequence: number; text: string; source: string; diff --git a/src/components/notificacoes/notificacoes.service.ts b/src/components/notificacoes/notificacoes.service.ts index 15ca953..9b67708 100644 --- a/src/components/notificacoes/notificacoes.service.ts +++ b/src/components/notificacoes/notificacoes.service.ts @@ -34,7 +34,7 @@ export class NotificacoesService { } async setRead(notificationId: string): Promise { - return this.api.request('PUT', `api/messages/${notificationId}/read`); + return this.api.request('PUT', `api/messages/${notificationId}/read`, true, true); } async setUnread(notificationId: string): Promise { diff --git a/src/components/notificacoes/notificacoes.tsx b/src/components/notificacoes/notificacoes.tsx index 98eb446..4348914 100644 --- a/src/components/notificacoes/notificacoes.tsx +++ b/src/components/notificacoes/notificacoes.tsx @@ -100,9 +100,7 @@ export class Notificacoes implements ComponentInterface { this.notificacoesService .setRead(event.detail.id) - .catch(() => { - this.marcarNotificacaoComoNaoLida(notificacao); - }); + .catch(() => this.marcarNotificacaoComoNaoLida(notificacao)); this.marcarNotificacaoComoLida(notificacao); } @@ -278,6 +276,24 @@ export class Notificacoes implements ComponentInterface { } if (!isNill(message.link)) { + + if (message.link.autoOpen) { + var auth = this.authorization.getAuthorization(); + + var timeout = !message.systemId || (auth && message.systemId === auth.systemId) + ? 0 + : 3000; + + setTimeout(() => { + this.notificacoesService.setRead(message.id) + .then((res) => { + if (res.status === 200) { + window.open(message.link.href, '_blank') + } + }); + }, timeout); + } + this.novaNotificacaoComLink.emit({ texto: message.text, link: message.link }); } } diff --git a/src/global/api.ts b/src/global/api.ts index e12c319..efdf03c 100644 --- a/src/global/api.ts +++ b/src/global/api.ts @@ -25,12 +25,16 @@ export class Api { private baseUrl: string ) { } - async request(method: string, path: string, retryUnauthorizedAccess: boolean = true): Promise { + async request(method: string, path: string, retryUnauthorizedAccess: boolean = true, returnResponse: boolean = false): Promise { const response = await fetch(`${this.baseUrl}/${path}`, { method, headers: this.getHeaders() }); if (response.status === UNAUTHORIZED_STATUS_CODE && retryUnauthorizedAccess && this.handleUnauthorizedAccess !== undefined) { await this.handleUnauthorizedAccess(); - return await this.request(method, path, false); + return await this.request(method, path, false, returnResponse); + } + + if (returnResponse) { + return response } if (response.status !== OK_STATUS_CODE) { From db60ef5a5e491a4b94677f33438f87cab7d12afe Mon Sep 17 00:00:00 2001 From: fabio Date: Mon, 8 Feb 2021 21:56:46 -0300 Subject: [PATCH 02/11] fix: ajustes de lint --- src/components/notificacoes/notificacoes.tsx | 4 ++-- src/global/api.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/notificacoes/notificacoes.tsx b/src/components/notificacoes/notificacoes.tsx index 4348914..5835386 100644 --- a/src/components/notificacoes/notificacoes.tsx +++ b/src/components/notificacoes/notificacoes.tsx @@ -280,7 +280,7 @@ export class Notificacoes implements ComponentInterface { if (message.link.autoOpen) { var auth = this.authorization.getAuthorization(); - var timeout = !message.systemId || (auth && message.systemId === auth.systemId) + var timeout = message.systemId == null || (auth && message.systemId === auth.systemId) ? 0 : 3000; @@ -288,7 +288,7 @@ export class Notificacoes implements ComponentInterface { this.notificacoesService.setRead(message.id) .then((res) => { if (res.status === 200) { - window.open(message.link.href, '_blank') + window.open(message.link.href, '_blank'); } }); }, timeout); diff --git a/src/global/api.ts b/src/global/api.ts index efdf03c..67cc999 100644 --- a/src/global/api.ts +++ b/src/global/api.ts @@ -34,7 +34,7 @@ export class Api { } if (returnResponse) { - return response + return response; } if (response.status !== OK_STATUS_CODE) { From af0d5be7548dcab353cacf9e56296e203b903229 Mon Sep 17 00:00:00 2001 From: fabio Date: Tue, 9 Feb 2021 10:46:15 -0300 Subject: [PATCH 03/11] Ajusta metodo getDataHoraDescrita --- src/utils/date.ts | 62 ++++++++++++----------------------------------- 1 file changed, 15 insertions(+), 47 deletions(-) diff --git a/src/utils/date.ts b/src/utils/date.ts index ded2ab0..52f232a 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -13,37 +13,25 @@ export function getDataHoraDescrita(dateTime: number | string): string { const data = new Date(dateTime); const dataAgora = new Date(); - let tempoFormatado = ''; - if (isHoje(data, dataAgora)) { - if (!isMesmaHora(data, dataAgora)) { - const horas = getDiferencaEmHoras(data, dataAgora); + const minutos = getDiferencaEmMinutos(data, dataAgora); - tempoFormatado = 'há ' + horas + ' hora'; - - if (horas > 1) { - tempoFormatado += 's'; - } - - return tempoFormatado; - } - - if (!isMesmoMinuto(data, dataAgora)) { - const minutos = getDiferencaEmMinutos(data, dataAgora); - - if (minutos > 0) { - tempoFormatado = 'há ' + minutos + ' minuto'; - - if (minutos > 1) { - tempoFormatado += 's'; - } + // Menos de 1 minuto + if (minutos < 1) { + return 'agora há pouco'; + } - return tempoFormatado; - } - } + // Memos de uma hora + if (minutos < 60) { + return 'há ' + minutos + ' minuto' + (minutos > 1 ? 's' : ''); + } - return 'agora há pouco'; + // Memos de 24 horas + if (minutos < 1440) { + const horas = Math.trunc(minutos / 60); + return 'há ' + horas + ' hora' + (horas > 1 ? 's' : ''); } + // Mais de 24 horas return formatarData(data) + ' às ' + data.toTimeString().substring(0, 5); } @@ -51,26 +39,6 @@ function formatarData(data: Date) { return data.toLocaleString('pt-BR').substring(0, 10); } -function isHoje(dataA: Date, dataB: Date) { - return getDateISOString(dataA) === getDateISOString(dataB); -} - -function getDateISOString(data: Date) { - return data.toISOString().substring(0, 10); -} - -function isMesmaHora(dataA: Date, dataB: Date) { - return getDiferencaEmMinutos(dataA, dataB) < 60; -} - function getDiferencaEmMinutos(dataA: Date, dataB: Date) { - return Math.floor((dataB.valueOf() - dataA.valueOf()) / (60 * 1000)); -} - -function getDiferencaEmHoras(dataA: Date, dataB: Date) { - return dataB.getHours() - dataA.getHours(); -} - -function isMesmoMinuto(dataA: Date, dataB: Date) { - return dataB.getMinutes() === dataA.getMinutes(); + return Math.floor((dataB.getTime() - dataA.getTime()) / (60 * 1000)); } From 07337392ca6c89b22afe8d964626bc84cc119fd8 Mon Sep 17 00:00:00 2001 From: fabio Date: Tue, 9 Feb 2021 17:27:58 -0300 Subject: [PATCH 04/11] atualiza versao para 1.1.0 --- package.json | 2 +- src/utils/date.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 1ff2160..faa9105 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@betha-plataforma/estrutura-componentes", - "version": "1.0.5", + "version": "1.1.0", "description": "Coleção de Web Components para compor a estrutura de uma aplicação front-end da Betha Sistemas.", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/src/utils/date.ts b/src/utils/date.ts index 52f232a..27c972b 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -20,12 +20,12 @@ export function getDataHoraDescrita(dateTime: number | string): string { return 'agora há pouco'; } - // Memos de uma hora + // Menos de uma hora if (minutos < 60) { return 'há ' + minutos + ' minuto' + (minutos > 1 ? 's' : ''); } - // Memos de 24 horas + // Menos de 24 horas if (minutos < 1440) { const horas = Math.trunc(minutos / 60); return 'há ' + horas + ' hora' + (horas > 1 ? 's' : ''); From 376a447019a26319c2ebad42f386d59d40476ed8 Mon Sep 17 00:00:00 2001 From: fabio Date: Wed, 10 Feb 2021 09:52:56 -0300 Subject: [PATCH 05/11] Adiciona identificador da mensagem na abertura do link --- src/components/notificacoes/notificacoes.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/components/notificacoes/notificacoes.tsx b/src/components/notificacoes/notificacoes.tsx index 5835386..cb3011b 100644 --- a/src/components/notificacoes/notificacoes.tsx +++ b/src/components/notificacoes/notificacoes.tsx @@ -288,7 +288,13 @@ export class Notificacoes implements ComponentInterface { this.notificacoesService.setRead(message.id) .then((res) => { if (res.status === 200) { - window.open(message.link.href, '_blank'); + var target = '_blank'; + + if (isNill(message.identifier) && message.identifier != '') { + target = message.identifier as string; + } + + window.open(message.link.href, target); } }); }, timeout); From e546c81b571427647d4ea2422e044102596c36a1 Mon Sep 17 00:00:00 2001 From: fabio Date: Wed, 10 Feb 2021 09:53:56 -0300 Subject: [PATCH 06/11] =?UTF-8?q?Ajusta=20valida=C3=A7=C3=A3o=20de=20statu?= =?UTF-8?q?s=20ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global/api.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/global/api.ts b/src/global/api.ts index 67cc999..323ff46 100644 --- a/src/global/api.ts +++ b/src/global/api.ts @@ -2,7 +2,6 @@ import { isNill } from '../utils/functions'; import { Authorization, AuthorizationConfig } from './interfaces'; const UNAUTHORIZED_STATUS_CODE = 401; -const OK_STATUS_CODE = 200; export const isValidAuthorizationConfig = (authorization: AuthorizationConfig) => { if (isNill(authorization)) { @@ -37,7 +36,7 @@ export class Api { return response; } - if (response.status !== OK_STATUS_CODE) { + if (!response.ok) { throw new Error(response.statusText); } From 5a1644e9be2690f713457ab2b7fdbd4a0b141a58 Mon Sep 17 00:00:00 2001 From: fabio Date: Wed, 10 Feb 2021 10:00:14 -0300 Subject: [PATCH 07/11] =?UTF-8?q?Ajusta=20valida=C3=A7=C3=A3o=20de=20statu?= =?UTF-8?q?s=20ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global/api.ts b/src/global/api.ts index 323ff46..f6a8a86 100644 --- a/src/global/api.ts +++ b/src/global/api.ts @@ -36,7 +36,7 @@ export class Api { return response; } - if (!response.ok) { + if (response.status < 200 || response.status >= 300) { throw new Error(response.statusText); } From 406e49353c8cc8af5ceaaad50df15b35b46a5683 Mon Sep 17 00:00:00 2001 From: fabio Date: Thu, 11 Feb 2021 16:37:46 -0300 Subject: [PATCH 08/11] Ajusta Api.request para retorno objeto response --- .../marca-produto/marca-produto.tsx | 5 ++- .../notificacoes/notificacoes.service.ts | 25 +++++++++------ src/components/novidades/novidades.service.ts | 2 +- src/global/api.ts | 31 +++++++++---------- src/global/test/api.spec.ts | 4 +-- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/components/marca-produto/marca-produto.tsx b/src/components/marca-produto/marca-produto.tsx index d7babc2..ec1b05a 100644 --- a/src/components/marca-produto/marca-produto.tsx +++ b/src/components/marca-produto/marca-produto.tsx @@ -94,12 +94,11 @@ export class MarcaProduto implements ComponentInterface { const UserAccountsApi = new Api(authorization, this.authorization.handleUnauthorizedAccess, `${this.getUserAccountsApi()}/api`); const promise = UserAccountsApi.request('GET', `access/${authorization.accessId}/systems`) + .then(res => res.json()) .then(produtos => { this.produtos = produtos.filter((produto: Produto) => produto.id !== authorization.systemId); }) - .catch(() => { - this.isApiIndisponivel = true; - }); + .catch(() => this.isApiIndisponivel = true); this.tracker.addPromise(promise); } diff --git a/src/components/notificacoes/notificacoes.service.ts b/src/components/notificacoes/notificacoes.service.ts index 9b67708..191ef9d 100644 --- a/src/components/notificacoes/notificacoes.service.ts +++ b/src/components/notificacoes/notificacoes.service.ts @@ -10,34 +10,41 @@ export class NotificacoesService { } async buscar(params: PaginationQueryParams = { offset: 0, limit: 20 }): Promise { - return this.api.request('GET', `api/messages/?limit=${params.limit}&offset=${params.offset}`); + return this.api.request('GET', `api/messages/?limit=${params.limit}&offset=${params.offset}`) + .then(res => res.json()); } async buscarNaoLidas(params: PaginationQueryParams = { offset: 0, limit: 20 }): Promise { - return this.api.request('GET', `api/messages/unreads/all?limit=${params.limit}&offset=${params.offset}`); + return this.api.request('GET', `api/messages/unreads/all?limit=${params.limit}&offset=${params.offset}`) + .then(res => res.json()); } async buscarLidas(params: PaginationQueryParams = { offset: 0, limit: 20 }): Promise { - return this.api.request('GET', `api/messages/reads?limit=${params.limit}&offset=${params.offset}`); + return this.api.request('GET', `api/messages/reads?limit=${params.limit}&offset=${params.offset}`) + .then(res => res.json()); } async buscarEmProgresso(params: PaginationQueryParams = { offset: 0, limit: 20 }): Promise { - return this.api.request('GET', `api/messages/in-progress?limit=${params.limit}&offset=${params.offset}`); + return this.api.request('GET', `api/messages/in-progress?limit=${params.limit}&offset=${params.offset}`) + .then(res => res.json()); } async clearInProgressUnread(): Promise { - return this.api.request('DELETE', 'api/messages/in-progress/unread'); + return this.api.request('DELETE', 'api/messages/in-progress/unread') + .then(res => res.json()); } async clearUnreads(): Promise { - return this.api.request('DELETE', 'api/messages/unread?keepInProgress=true'); + return this.api.request('DELETE', 'api/messages/unread?keepInProgress=true') + .then(res => res.json()); } - async setRead(notificationId: string): Promise { - return this.api.request('PUT', `api/messages/${notificationId}/read`, true, true); + async setRead(notificationId: string): Promise { + return this.api.request('PUT', `api/messages/${notificationId}/read`, true); } async setUnread(notificationId: string): Promise { - return this.api.request('PUT', `api/messages/${notificationId}/unread`); + return this.api.request('PUT', `api/messages/${notificationId}/unread`) + .then(res => res.json()); } } diff --git a/src/components/novidades/novidades.service.ts b/src/components/novidades/novidades.service.ts index c8b8dbd..31152e3 100644 --- a/src/components/novidades/novidades.service.ts +++ b/src/components/novidades/novidades.service.ts @@ -12,7 +12,7 @@ export class NovidadesService { } async buscar(): Promise { - return this.api.request('GET', 'api/novidades'); + return this.api.request('GET', 'api/novidades').then(res => res.json()); } marcarComoLida(novidadeId: string): void { diff --git a/src/global/api.ts b/src/global/api.ts index f6a8a86..feeafeb 100644 --- a/src/global/api.ts +++ b/src/global/api.ts @@ -24,23 +24,20 @@ export class Api { private baseUrl: string ) { } - async request(method: string, path: string, retryUnauthorizedAccess: boolean = true, returnResponse: boolean = false): Promise { - const response = await fetch(`${this.baseUrl}/${path}`, { method, headers: this.getHeaders() }); - - if (response.status === UNAUTHORIZED_STATUS_CODE && retryUnauthorizedAccess && this.handleUnauthorizedAccess !== undefined) { - await this.handleUnauthorizedAccess(); - return await this.request(method, path, false, returnResponse); - } - - if (returnResponse) { - return response; - } - - if (response.status < 200 || response.status >= 300) { - throw new Error(response.statusText); - } - - return await response.json().catch(() => null); + async request(method: string, path: string, retryUnauthorizedAccess: boolean = true): Promise { + return await fetch(`${this.baseUrl}/${path}`, { method, headers: this.getHeaders() }) + .then(async response => { + if (response.status === UNAUTHORIZED_STATUS_CODE && retryUnauthorizedAccess && this.handleUnauthorizedAccess !== undefined) { + await this.handleUnauthorizedAccess(); + return await this.request(method, path, false); + } + + if (response.status < 200 || response.status >= 300) { + throw new Error(response.statusText); + } + + return Promise.resolve(response); + }); } private getHeaders(): Headers { diff --git a/src/global/test/api.spec.ts b/src/global/test/api.spec.ts index 553df29..d73eb33 100644 --- a/src/global/test/api.spec.ts +++ b/src/global/test/api.spec.ts @@ -55,7 +55,7 @@ describe('Api', () => { const baseUrl = 'https://site.com'; const apiEndpoint = 'api/v1/tests'; const siteApi = new Api({ accessToken }, () => Promise.resolve(), baseUrl); - await siteApi.request('GET', apiEndpoint); + await siteApi .request('GET', apiEndpoint) .then(res => res.json()); expect(fetchMock).toBeCalledTimes(1); const expectedFetchOptions = { @@ -81,7 +81,7 @@ describe('Api', () => { }); const siteApi = new Api({ accessToken }, handleUnauthorizedAccess, baseUrl); - const response = await siteApi.request('GET', apiEndpoint); + const response = await siteApi.request('GET', apiEndpoint).then(res => res.json()); expect(response).not.toBeNull(); expect(fetchMock).toBeCalledTimes(2); From 137b3d21860510ea89947642bf6cb93140bb308e Mon Sep 17 00:00:00 2001 From: fabio Date: Thu, 11 Feb 2021 16:52:10 -0300 Subject: [PATCH 09/11] =?UTF-8?q?fix:=20remove=20convers=C3=A3o=20para=20j?= =?UTF-8?q?son=20das=20request=20DELETE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/notificacoes/notificacoes.service.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/components/notificacoes/notificacoes.service.ts b/src/components/notificacoes/notificacoes.service.ts index 191ef9d..8c5cf61 100644 --- a/src/components/notificacoes/notificacoes.service.ts +++ b/src/components/notificacoes/notificacoes.service.ts @@ -30,13 +30,11 @@ export class NotificacoesService { } async clearInProgressUnread(): Promise { - return this.api.request('DELETE', 'api/messages/in-progress/unread') - .then(res => res.json()); + return this.api.request('DELETE', 'api/messages/in-progress/unread'); } async clearUnreads(): Promise { - return this.api.request('DELETE', 'api/messages/unread?keepInProgress=true') - .then(res => res.json()); + return this.api.request('DELETE', 'api/messages/unread?keepInProgress=true'); } async setRead(notificationId: string): Promise { From 55d1f2d6b42e4ad9493c1f8938ba9b2319e7bab4 Mon Sep 17 00:00:00 2001 From: fabio Date: Fri, 12 Feb 2021 11:04:53 -0300 Subject: [PATCH 10/11] fix: remove abertura duplicada de link ao marcar item como lido --- .../notificacoes/notificacao-item/notificacao-item.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/notificacoes/notificacao-item/notificacao-item.tsx b/src/components/notificacoes/notificacao-item/notificacao-item.tsx index c3ad8b8..24a6fab 100644 --- a/src/components/notificacoes/notificacao-item/notificacao-item.tsx +++ b/src/components/notificacoes/notificacao-item/notificacao-item.tsx @@ -97,6 +97,7 @@ export class NotificacaoItem implements ComponentInterface { private onClick = (event: UIEvent) => { event.preventDefault(); + event.stopPropagation(); const payload: NotificacaoLeituraEvent = { id: this.identificador From b63d71904b359cbd9ce4a09b91d8bc2d489cb376 Mon Sep 17 00:00:00 2001 From: fabio Date: Fri, 12 Feb 2021 11:05:58 -0300 Subject: [PATCH 11/11] fix: ajusta retorno do metodo setUnread --- src/components/notificacoes/notificacoes.service.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/components/notificacoes/notificacoes.service.ts b/src/components/notificacoes/notificacoes.service.ts index 8c5cf61..1806e9f 100644 --- a/src/components/notificacoes/notificacoes.service.ts +++ b/src/components/notificacoes/notificacoes.service.ts @@ -38,11 +38,10 @@ export class NotificacoesService { } async setRead(notificationId: string): Promise { - return this.api.request('PUT', `api/messages/${notificationId}/read`, true); + return this.api.request('PUT', `api/messages/${notificationId}/read`); } async setUnread(notificationId: string): Promise { - return this.api.request('PUT', `api/messages/${notificationId}/unread`) - .then(res => res.json()); + return this.api.request('PUT', `api/messages/${notificationId}/unread`); } }