Skip to content

Commit b946896

Browse files
committed
Feature: Added Umb-Notifications
1 parent 7b7b18e commit b946896

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { UmbNotificationColor } from '../notification/notification.context.js';
2+
import { EventMessageTypeModel } from '@umbraco-cms/backoffice/external/backend-api';
3+
4+
export function extractUmbColorNotification(type: EventMessageTypeModel): UmbNotificationColor {
5+
switch (type) {
6+
case EventMessageTypeModel.ERROR:
7+
return 'danger';
8+
case EventMessageTypeModel.WARNING:
9+
return 'warning';
10+
case EventMessageTypeModel.INFO:
11+
case EventMessageTypeModel.DEFAULT:
12+
return 'default';
13+
case EventMessageTypeModel.SUCCESS:
14+
return 'positive';
15+
default:
16+
return '';
17+
}
18+
}

src/packages/core/resources/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export * from './resource.controller.js';
22
export * from './tryExecute.function.js';
33
export * from './tryExecuteAndNotify.function.js';
4+
export * from './extractUmbColorNotification.function.js';
45
export * from './extractUmbColorVariable.function.js';
6+
export * from './isUmbNotification.function.js';
7+
export * from './isUmbNotifications.function.js';
58
export * from './apiTypeValidators.function.js';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { UmbNotificationsEventModel } from './resource.controller.js';
2+
import { EventMessageTypeModel } from '@umbraco-cms/backoffice/external/backend-api';
3+
4+
export function isUmbNotification(notification: unknown): notification is UmbNotificationsEventModel {
5+
if (typeof notification !== 'object' || notification === null) {
6+
return false;
7+
}
8+
const object = notification as UmbNotificationsEventModel;
9+
return (
10+
typeof object.category === 'string' &&
11+
typeof object.message === 'string' &&
12+
typeof object.type === 'string' &&
13+
Object.values(EventMessageTypeModel).includes(object.type)
14+
);
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { isUmbNotification } from './isUmbNotification.function.js';
2+
import type { UmbNotificationsEventModel } from './resource.controller.js';
3+
4+
export function isUmbNotifications(notifications: Array<unknown>): notifications is Array<UmbNotificationsEventModel> {
5+
return notifications.every(isUmbNotification);
6+
}

src/packages/core/resources/resource.controller.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { UMB_AUTH_CONTEXT } from '../auth/index.js';
33
import { isApiError, isCancelError, isCancelablePromise } from './apiTypeValidators.function.js';
4+
import { extractUmbColorNotification } from './extractUmbColorNotification.function.js';
5+
import { isUmbNotifications } from './isUmbNotifications.function.js';
46
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
57
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
68
import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api';
79
import { UMB_NOTIFICATION_CONTEXT, type UmbNotificationOptions } from '@umbraco-cms/backoffice/notification';
810
import type { UmbDataSourceResponse } from '@umbraco-cms/backoffice/repository';
11+
import type { EventMessageTypeModel } from '@umbraco-cms/backoffice/external/backend-api';
12+
13+
export interface UmbNotificationsEventModel {
14+
category: string;
15+
message: string;
16+
type: EventMessageTypeModel;
17+
}
918

1019
export class UmbResourceController extends UmbControllerBase {
1120
#promise: Promise<any>;
@@ -128,7 +137,9 @@ export class UmbResourceController extends UmbControllerBase {
128137
break;
129138
default:
130139
// Other errors
131-
if (this.#notificationContext) {
140+
if (error && error.body && Array.isArray(error.body) && isUmbNotifications(error.body)) {
141+
this.#peekUmbNotifications(error.body);
142+
} else if (this.#notificationContext) {
132143
this.#notificationContext.peek('danger', {
133144
data: {
134145
headline: error.body?.title ?? error.name ?? 'Server Error',
@@ -144,9 +155,21 @@ export class UmbResourceController extends UmbControllerBase {
144155
}
145156
}
146157

158+
if (Array.isArray(data) && isUmbNotifications(data)) {
159+
this.#peekUmbNotifications(data);
160+
}
161+
147162
return { data, error };
148163
}
149164

165+
#peekUmbNotifications(notifications: Array<UmbNotificationsEventModel>) {
166+
notifications.forEach((notification) => {
167+
this.#notificationContext?.peek(extractUmbColorNotification(notification.type), {
168+
data: { headline: notification.category, message: notification.message },
169+
});
170+
});
171+
}
172+
150173
/**
151174
* Cancel all resources that are currently being executed by this controller if they are cancelable.
152175
*

0 commit comments

Comments
 (0)