Services: add toast
This commit is contained in:
parent
22cc1dcc01
commit
7ee8e7e3ed
9 changed files with 94 additions and 1 deletions
|
|
@ -1 +1,2 @@
|
|||
<app-toast-container aria-live="polite" aria-atomic="true"/>
|
||||
<router-outlet/>
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import { Component } from '@angular/core';
|
||||
import { RouterOutlet } from '@angular/router';
|
||||
import {ToastContainerComponent} from '../components/toast-container/toast-container/toast-container.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
imports: [RouterOutlet],
|
||||
imports: [RouterOutlet, ToastContainerComponent],
|
||||
templateUrl: './app.component.html',
|
||||
styleUrl: './app.component.css'
|
||||
})
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
:host {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
margin: 0.5em;
|
||||
z-index: 1200;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
@for (toast of toastService.toasts; track toast) {
|
||||
<ngb-toast
|
||||
[header]="toast.header || ''" [autohide]="true" [delay]="toast.delay || 5000"
|
||||
(hidden)="toastService.remove(toast)"
|
||||
[animation]="true" [class]="toast.htmlClass"
|
||||
>{{ toast.body }}</ngb-toast>
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ToastContainerComponent } from './toast-container.component';
|
||||
|
||||
describe('ToastContainerComponent', () => {
|
||||
let component: ToastContainerComponent;
|
||||
let fixture: ComponentFixture<ToastContainerComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [ToastContainerComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(ToastContainerComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
import {Component, inject} from '@angular/core';
|
||||
import {ToastService} from '../../../services/toast/toast.service';
|
||||
import {NgbToast} from '@ng-bootstrap/ng-bootstrap';
|
||||
|
||||
@Component({
|
||||
selector: 'app-toast-container',
|
||||
imports: [
|
||||
NgbToast
|
||||
],
|
||||
templateUrl: './toast-container.component.html',
|
||||
styleUrl: './toast-container.component.css'
|
||||
})
|
||||
export class ToastContainerComponent {
|
||||
toastService: ToastService = inject(ToastService);
|
||||
}
|
||||
6
src/interfaces/toast.ts
Normal file
6
src/interfaces/toast.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
export interface Toast {
|
||||
header?: string;
|
||||
body: string;
|
||||
delay?: number;
|
||||
htmlClass?: string;
|
||||
}
|
||||
16
src/services/toast/toast.service.spec.ts
Normal file
16
src/services/toast/toast.service.spec.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { ToastService } from './toast.service';
|
||||
|
||||
describe('ToastService', () => {
|
||||
let service: ToastService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
service = TestBed.inject(ToastService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
});
|
||||
17
src/services/toast/toast.service.ts
Normal file
17
src/services/toast/toast.service.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import {Toast} from '../../interfaces/toast';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ToastService {
|
||||
toasts: Toast[] = [];
|
||||
|
||||
show(toast: Toast) {
|
||||
this.toasts.push(toast);
|
||||
}
|
||||
|
||||
remove(toast: Toast) {
|
||||
this.toasts = this.toasts.filter(t => t != toast);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue