Skip to content

Commit

Permalink
fix(core): implement review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
KarimovDev committed Sep 27, 2021
1 parent b0e6078 commit 0206071
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {TuiContextWithImplicit} from '@taiga-ui/cdk';
import {TuiNotification} from '@taiga-ui/core/enums';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {Observer} from 'rxjs';
import {TuiNotificationContentContext} from '../notification-content-context';
Expand All @@ -9,15 +7,15 @@ import {
} from '../notification-options';

export class NotificationAlert<O, I> {
readonly status?: TuiNotification;
readonly status = this.options.status;

readonly hasIcon?: boolean;
readonly hasIcon = this.options.hasIcon;

readonly autoClose?: boolean | number;
readonly autoClose = this.options.autoClose;

readonly hasCloseButton?: boolean;
readonly hasCloseButton = this.options.hasCloseButton;

readonly label?: PolymorpheusContent<TuiContextWithImplicit<TuiNotification>>;
readonly label = this.options.label;

readonly data!: I;

Expand All @@ -28,19 +26,13 @@ export class NotificationAlert<O, I> {
constructor(
observer: Observer<O>,
content: PolymorpheusContent<TuiNotificationContentContext<O, I>>,
options: TuiNotificationOptions | TuiNotificationOptionsWithData<I>,
private readonly options:
| TuiNotificationOptions
| TuiNotificationOptionsWithData<I>,
) {
const {label, status, hasIcon, autoClose, hasCloseButton} = options;

this.observer = observer;
this.content = content;

this.label = label;
this.status = status;
this.hasIcon = hasIcon;
this.autoClose = autoClose;
this.hasCloseButton = hasCloseButton;

if (options && this.withData(options)) {
this.data = options.data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const DEFAULT_ALERT_AUTOCLOSE_TIMEOUT = 3000;
})
export class TuiNotificationAlertComponent<O, I> implements OnInit {
@Input()
item?: NotificationAlert<O, I>;
item!: NotificationAlert<O, I>;

constructor(
@Inject(ElementRef) private readonly elementRef: ElementRef<HTMLElement>,
Expand All @@ -44,45 +44,37 @@ export class TuiNotificationAlertComponent<O, I> implements OnInit {
this.initAutoClose();
}

get safeItem(): NotificationAlert<O, I> {
if (!this.item) {
throw new Error('Notification was created as undefined');
}

return this.item;
}

get context(): TuiNotificationContentContext<O, I> {
return this.calculateContext(this.safeItem);
return this.calculateContext(this.item);
}

@tuiPure
get label(): PolymorpheusContent<TuiContextWithImplicit<TuiNotification>> {
return this.safeItem.label ?? this.options.label;
return this.item.label ?? this.options.label;
}

@tuiPure
get hasCloseButton(): boolean {
return this.safeItem.hasCloseButton ?? this.options.hasCloseButton;
return this.item.hasCloseButton ?? this.options.hasCloseButton;
}

@tuiPure
get status(): TuiNotification {
return this.safeItem.status ?? this.options.status;
return this.item.status ?? this.options.status;
}

@tuiPure
get hasIcon(): boolean {
return this.safeItem.hasIcon ?? this.options.hasIcon;
return this.item.hasIcon ?? this.options.hasIcon;
}

@tuiPure
get autoClose(): boolean | number {
return this.safeItem.autoClose ?? this.options.autoClose;
return this.item.autoClose ?? this.options.autoClose;
}

closeNotification() {
this.safeItem.observer.complete();
this.item.observer.complete();
}

@tuiPure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
<label
*ngIf="label"
polymorpheus-outlet
[content]="label"
[context]="context"
automation-id="tui-notification-alert__heading"
class="heading"
[content]="label"
[context]="context"
>
</label>
<div
polymorpheus-outlet
automation-id="tui-notification-alert__content"
class="content"
[content]="safeItem.content"
[content]="item.content"
[context]="context"
></div>
</tui-notification>
Expand All @@ -26,17 +26,17 @@
<label
*ngIf="label"
polymorpheus-outlet
[content]="label"
[context]="context"
automation-id="tui-notification-alert__heading"
class="heading"
[content]="label"
[context]="context"
>
</label>
<div
polymorpheus-outlet
automation-id="tui-notification-alert__content"
class="content"
[content]="safeItem.content"
[content]="item.content"
[context]="context"
></div>
</tui-notification>
Expand Down
13 changes: 11 additions & 2 deletions projects/core/modules/notifications/notifications.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Injectable} from '@angular/core';
import {Inject, Injectable} from '@angular/core';
import {tuiAssert} from '@taiga-ui/cdk';
import {NotificationTokenOptions, TUI_NOTIFICATION_OPTIONS} from '@taiga-ui/core';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {BehaviorSubject, Observable, Observer} from 'rxjs';
import {NotificationAlert} from './notification-alert/Notification-alert';
Expand All @@ -19,6 +20,11 @@ export class TuiNotificationsService {
/** @internal */
readonly items$ = new BehaviorSubject<ReadonlyArray<NotificationAlert<any, any>>>([]);

constructor(
@Inject(TUI_NOTIFICATION_OPTIONS)
public readonly options: NotificationTokenOptions,
) {}

show<O = void>(
content: PolymorpheusContent<TuiNotificationContentContext<O>>,
): Observable<O>;
Expand All @@ -37,7 +43,10 @@ export class TuiNotificationsService {
return new Observable((observer: Observer<O>) => {
tuiAssert.assert(!!this.items$.observers.length, NO_HOST);

const notification = new NotificationAlert(observer, content, options);
const notification = new NotificationAlert(observer, content, {
...this.options,
...options,
});

this.items$.next([...this.items$.value, notification]);

Expand Down

0 comments on commit 0206071

Please sign in to comment.