Skip to content

Commit

Permalink
feat(message): add an optional date range to message application (#839)
Browse files Browse the repository at this point in the history
* feat(message): add an optional date range to message application

* wip

* wip

Co-authored-by: Pierre-Étienne Lord <[email protected]>
  • Loading branch information
pelord and Pierre-Étienne Lord authored Apr 22, 2021
1 parent 1b0268b commit 01d5eef
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
8 changes: 5 additions & 3 deletions packages/core/src/lib/message/shared/message.interface.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { TemplateRef } from '@angular/core';
import { MessageType } from './message.enum';
import { Notification } from 'angular2-notifications';

export interface Message {
title?: string;
text?: string;
html?: string | TemplateRef<any>;
icon?: string;
type?: MessageType;
options?: any;
options?: MessageOptions;
format?: 'text' | 'html';
}

export interface MessageOptions {
timeOut?: number;
export interface MessageOptions extends Notification {
template?: string;
from?: Date | string;
to?: Date | string;
}
71 changes: 43 additions & 28 deletions packages/core/src/lib/message/shared/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,53 @@ export class MessageService {
message(message: Message) {
this.messages$.next(this.messages$.value.concat([message]));

message.options = message.options || {};
message = this.handleTemplate(message);

let notification: Notification;
if (message.text) {
notification = this.notificationService.create(
message.title,
message.text,
(message.type as any) as NotificationType,
message.options
);
} else if (message.html) {
if (!message.icon) {
message.options.theClass = message.options.theClass
? message.options.theClass + ' noIcon'
: 'noIcon';
}
message.options = message.options || {} as MessageOptions;
const currentDate = new Date();

notification = this.notificationService.html(
message.html,
(message.type as any) as NotificationType,
message.options
);
} else {
return;
message.options.from = message.options.from ? message.options.from : new Date('1 jan 1900');
message.options.to = message.options.to ? message.options.to : new Date('1 jan 3000');
if (typeof message.options.from === 'string') {
message.options.from = new Date(Date.parse(message.options.from.replace(/-/g, ' ')));
}

if (message.icon !== undefined) {
this.addIcon(notification, message.icon);
if (typeof message.options.to === 'string') {
message.options.to = new Date(Date.parse(message.options.to.replace(/-/g, ' ')));
}
if (
currentDate > message.options.from && currentDate < message.options.to) {

message = this.handleTemplate(message);

let notification: Notification;
if (message.text) {
notification = this.notificationService.create(
message.title,
message.text,
(message.type as any) as NotificationType,
message.options
);
} else if (message.html) {
if (!message.icon) {
message.options.theClass = message.options.theClass
? message.options.theClass + ' noIcon'
: 'noIcon';
}

notification = this.notificationService.html(
message.html,
(message.type as any) as NotificationType,
message.options
);
} else {
return;
}

return notification;
if (message.icon !== undefined) {
this.addIcon(notification, message.icon);
}

return notification;
}
return;
}

success(text: string, title?: string, options: any = {}) {
Expand Down

0 comments on commit 01d5eef

Please sign in to comment.