Last active
May 12, 2022 14:47
-
-
Save bobbyg603/f2653a00d0f6e8e863f13f27dfc770f5 to your computer and use it in GitHub Desktop.
RxJS in the Wild: Alerts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
import { Alert, AlertColor } from './alert'; | |
@Pipe({ | |
name: 'alertColorClassName' | |
}) | |
export class AlertColorClassNamePipe implements PipeTransform { | |
transform(alert: Alert): string { | |
if (alert?.color === AlertColor.Green) { | |
return 'alert-success'; | |
} | |
if (alert?.color === AlertColor.Yellow) { | |
return 'alert-warning'; | |
} | |
return 'alert-danger'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ng-container *ngIf="alerts$ | async as alerts"> | |
<ul *ngIf="alerts?.length"> | |
<li *ngFor="let alert of alerts"> | |
<div class="d-flex justify-content-between rounded cursor-pointer p-3 mt-2 w-100" | |
[ngClass]="alert | alertColorClassName" (click)="onRemoveAlertClick(alert.id)"> | |
<div></div> | |
<div> | |
{{alert.message}} | |
</div> | |
<div> | |
<button type="button" class="btn-close float-right" aria-label="Close"></button> | |
</div> | |
</div> | |
</li> | |
</ul> | |
</ng-container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$media-breakpoint-up-lg: 992px; | |
:host { | |
position: fixed; | |
z-index: 10000; | |
margin-left: auto; | |
margin-right: auto; | |
left: 0; | |
right: 0; | |
} | |
@media only screen and (max-width: $media-breakpoint-up-lg) { | |
:host { | |
width: 100%; | |
} | |
} | |
@media only screen and (min-width: $media-breakpoint-up-lg) { | |
:host { | |
width: 500px; | |
} | |
} | |
.cursor-pointer { | |
cursor: pointer; | |
} | |
ul { | |
margin-top: 15px; | |
list-style-type: none; | |
padding-left: 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Input, Output, EventEmitter } from '@angular/core'; | |
import { Observable } from 'rxjs'; | |
import { Alert } from "./alert"; | |
import { AlertService } from './alert.service'; | |
@Component({ | |
selector: 'app-alert', | |
templateUrl: './alert.component.html', | |
styleUrls: ['./alert.component.scss'] | |
}) | |
export class AlertComponent { | |
alerts$: Observable<Alert[]>; | |
constructor(private _alertService: AlertService) { | |
this.alerts$ = this._alertService.alerts$; | |
} | |
onRemoveAlertClick(id: string): void { | |
this._alertService.dismissAlert(id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment