diff --git a/apps/dh/api-dh/source/DataHub.WebApi/GraphQL/Query/NotificationQuery.cs b/apps/dh/api-dh/source/DataHub.WebApi/GraphQL/Query/NotificationQuery.cs new file mode 100644 index 0000000000..d8b4f56973 --- /dev/null +++ b/apps/dh/api-dh/source/DataHub.WebApi/GraphQL/Query/NotificationQuery.cs @@ -0,0 +1,26 @@ +// Copyright 2020 Energinet DataHub A/S +// +// Licensed under the Apache License, Version 2.0 (the "License2"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Energinet.DataHub.WebApi.Clients.Notifications; +using Energinet.DataHub.WebApi.Clients.Notifications.Dto; + +namespace Energinet.DataHub.WebApi.GraphQL.Query; + +public partial class Query +{ + public async Task> GetNotificationsAsync( + [Service] INotificationsClient client, + CancellationToken cancellationToken) => + await client.GetUnreadNotificationsAsync(cancellationToken); +} diff --git a/libs/dh/core/data-access-graphql/query/get-notifications.graphql b/libs/dh/core/data-access-graphql/query/get-notifications.graphql new file mode 100644 index 0000000000..c159eb7f57 --- /dev/null +++ b/libs/dh/core/data-access-graphql/query/get-notifications.graphql @@ -0,0 +1,8 @@ +query getNotifications { + notifications { + id + notificationType + occurredAt + relatedToId + } +} diff --git a/libs/dh/core/feature-notifications/src/lib/dh-notification-banner.component.ts b/libs/dh/core/feature-notifications/src/lib/dh-notification-banner.component.ts index 4e0525ac6e..9967c699ea 100644 --- a/libs/dh/core/feature-notifications/src/lib/dh-notification-banner.component.ts +++ b/libs/dh/core/feature-notifications/src/lib/dh-notification-banner.component.ts @@ -35,8 +35,12 @@ import { DhNotification } from './dh-notification'; `, template: ` -
{{ t(toastRef.data.type + '.headline') }}
-

{{ t(toastRef.data.type + '.message', { relatedToId: toastRef.data.relatedToId }) }}

+
{{ t(toastRef.data.notificationType + '.headline') }}
+

+ {{ + t(toastRef.data.notificationType + '.message', { relatedToId: toastRef.data.relatedToId }) + }} +

`, }) diff --git a/libs/dh/core/feature-notifications/src/lib/dh-notification.component.ts b/libs/dh/core/feature-notifications/src/lib/dh-notification.component.ts index 530ea9afef..d7561c735b 100644 --- a/libs/dh/core/feature-notifications/src/lib/dh-notification.component.ts +++ b/libs/dh/core/feature-notifications/src/lib/dh-notification.component.ts @@ -80,8 +80,8 @@ import { DhNotification } from './dh-notification'; `, template: ` -
- @if (!notification().read) { +
+ @if (!notification()) {
- {{ t(notification().type + '.headline') }} + {{ t(notification().notificationType + '.headline') }}

- {{ t(notification().type + '.message', { relatedToId: notification().relatedToId }) }} + {{ + t(notification().notificationType + '.message', { + relatedToId: notification().relatedToId, + }) + }}

diff --git a/libs/dh/core/feature-notifications/src/lib/dh-notification.ts b/libs/dh/core/feature-notifications/src/lib/dh-notification.ts index 8104d79783..aa15754d3f 100644 --- a/libs/dh/core/feature-notifications/src/lib/dh-notification.ts +++ b/libs/dh/core/feature-notifications/src/lib/dh-notification.ts @@ -14,10 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -export type DhNotification = { - id: number; - type: string; - occurredAt: Date; - read: boolean; - relatedToId?: string | null; -}; +import { GetNotificationsDocument } from '@energinet-datahub/dh/shared/domain/graphql'; +import type { ResultOf } from '@graphql-typed-document-node/core'; +export type DhNotifications = ResultOf['notifications']; +export type DhNotification = DhNotifications[0]; diff --git a/libs/dh/core/feature-notifications/src/lib/dh-notifications-center.component.ts b/libs/dh/core/feature-notifications/src/lib/dh-notifications-center.component.ts index 71d94c6302..49e41674fa 100644 --- a/libs/dh/core/feature-notifications/src/lib/dh-notifications-center.component.ts +++ b/libs/dh/core/feature-notifications/src/lib/dh-notifications-center.component.ts @@ -19,9 +19,10 @@ import { Component, computed, inject, signal } from '@angular/core'; import { NgClass } from '@angular/common'; import { TranslocoDirective } from '@ngneat/transloco'; -import { mutation, subscription } from '@energinet-datahub/dh/shared/util-apollo'; +import { mutation, query, subscription } from '@energinet-datahub/dh/shared/util-apollo'; import { DismissNotificationDocument, + GetNotificationsDocument, OnNotificationAddedDocument, } from '@energinet-datahub/dh/shared/domain/graphql'; @@ -139,37 +140,33 @@ export class DhNotificationsCenterComponent { private readonly bannerService = inject(DhNotificationBannerService); private readonly now = new Date(); private readonly dismissMutation = mutation(DismissNotificationDocument); + private readonly getNotificationsQuery = query(GetNotificationsDocument); isOpen = false; - notifications = signal([]); - notificationAdded = subscription(OnNotificationAddedDocument, { - onData: ({ notificationAdded }) => { - const isDistinctNotification = this.isDistinctNotification(notificationAdded.id); + notifications = computed(() => this.getNotificationsQuery.data()?.notifications ?? []); - if (!isDistinctNotification) { - return; - } + constructor() { + this.getNotificationsQuery.subscribeToMore({ + document: OnNotificationAddedDocument, + updateQuery: (pref, { subscriptionData }) => { + const incomingNotification = subscriptionData.data?.notificationAdded; + if (!incomingNotification) return pref; - const { id, occurredAt, relatedToId, notificationType: type } = notificationAdded; - const notification: DhNotification = { - id, - type, - occurredAt, - read: false, - relatedToId, - }; - - if (dayjs(occurredAt).isAfter(this.now)) { - this.bannerService.showBanner(notification); - } + if (dayjs(incomingNotification.occurredAt).isAfter(this.now)) { + this.bannerService.showBanner(incomingNotification); + } - this.notifications.update((notifications) => [notification, ...notifications]); - }, - }); + return { + ...pref, + notifications: [incomingNotification, ...pref.notifications], + }; + }, + }); + } notificationIcon = computed(() => - this.notifications().every((n) => n.read) ? 'notifications' : 'notificationsUnread' + this.notifications().every((n) => n) ? 'notifications' : 'notificationsUnread' ); notificationDot = computed(() => this.notificationIcon() === 'notificationsUnread'); @@ -187,7 +184,7 @@ export class DhNotificationsCenterComponent { onDismiss(notificationId: number): void { this.dismissMutation.mutate({ variables: { input: { notificationId } }, - onCompleted: () => this.onDismissSuccess(notificationId), + refetchQueries: [GetNotificationsDocument], onError: () => console.error('Failed to dismiss notification'), }); } @@ -195,10 +192,4 @@ export class DhNotificationsCenterComponent { private isDistinctNotification(incomingNotificationId: number): boolean { return !this.notifications().some((n) => n.id === incomingNotificationId); } - - private onDismissSuccess(notificationId: number): void { - this.notifications.update((notifications) => - notifications.filter((n) => n.id !== notificationId) - ); - } }