Skip to content

Commit

Permalink
Added an alert service [#328]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce authored and BRUCELLA2 committed Nov 14, 2020
1 parent 50a8d12 commit 86a0dd3
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 5 deletions.
39 changes: 35 additions & 4 deletions comixed-web/src/app/app.module.ts
Expand Up @@ -21,16 +21,47 @@ import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
import { HttpInterceptor } from '@app/interceptors/http.interceptor';
import { HomeComponent } from './pages/home/home.component';
import { NavigationBarComponent } from './components/navigation-bar/navigation-bar.component';
import {MatToolbarModule} from "@angular/material/toolbar";
import {MatIconModule} from "@angular/material/icon";
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatIconModule } from '@angular/material/icon';
import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';
import { AppEffects } from './app.effects';
import { StoreRouterConnectingModule } from '@ngrx/router-store';
import {
TranslateCompiler,
TranslateLoader,
TranslateModule
} from '@ngx-translate/core';
import { HttpLoaderFactory } from '@app/app.translate';
import { TranslateMessageFormatCompiler } from 'ngx-translate-messageformat-compiler';

@NgModule({
declarations: [AppComponent, HomeComponent, NavigationBarComponent],
imports: [BrowserModule, AppRoutingModule, BrowserAnimationsModule, MatToolbarModule, MatIconModule],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatToolbarModule,
MatIconModule,
StoreModule.forRoot({}, {}),
EffectsModule.forRoot([AppEffects]),
StoreRouterConnectingModule.forRoot(),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
},
compiler: {
provide: TranslateCompiler,
useClass: TranslateMessageFormatCompiler
}
})
],
providers: [
[{ provide: HTTP_INTERCEPTORS, useClass: HttpInterceptor, multi: true }]
],
Expand Down
26 changes: 26 additions & 0 deletions comixed-web/src/app/app.translate.ts
@@ -0,0 +1,26 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import { MultiTranslateHttpLoader } from 'ngx-translate-multi-http-loader';
import { HttpClient } from '@angular/common/http';

export function HttpLoaderFactory(http: HttpClient) {
return new MultiTranslateHttpLoader(http, [
{ prefix: './assets/i18n/', suffix: '/core.json' }
]);
}
4 changes: 3 additions & 1 deletion comixed-web/src/app/core/core.module.ts
Expand Up @@ -18,10 +18,12 @@

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatSnackBarModule } from '@angular/material/snack-bar';

@NgModule({
declarations: [],
imports: [CommonModule],
imports: [CommonModule, MatSnackBarModule],
exports: [MatSnackBarModule],
providers: []
})
export class CoreModule {}
1 change: 1 addition & 0 deletions comixed-web/src/app/core/index.ts
Expand Up @@ -17,3 +17,4 @@
*/

export { TokenService } from '@app/core/services/token.service';
export { AlertService } from '@app/core/services/alert.service';
90 changes: 90 additions & 0 deletions comixed-web/src/app/core/services/alert.service.spec.ts
@@ -0,0 +1,90 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import { TestBed } from '@angular/core/testing';
import {
ALERT_HORZ_POSITION,
ALERT_VERT_POSITION,
AlertService,
ERROR_MESSAGE_DURATION,
INFO_MESSAGE_DURATION
} from './alert.service';
import { LoggerModule } from '@angular-ru/logger';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { TranslateModule } from '@ngx-translate/core';

describe('AlertService', () => {
const TEST_MESSAGE = 'This is the alert message';

let service: AlertService;
let snackbar: MatSnackBar;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
LoggerModule.forRoot(),
TranslateModule.forRoot(),
MatSnackBarModule
]
});

service = TestBed.inject(AlertService);
snackbar = TestBed.inject(MatSnackBar);
spyOn(snackbar, 'open');
});

it('should be created', () => {
expect(service).toBeTruthy();
});

describe('showing an information alert', () => {
beforeEach(() => {
service.info(TEST_MESSAGE);
});

it('opens the message popup', () => {
expect(snackbar.open).toHaveBeenCalledWith(
TEST_MESSAGE,
jasmine.any(String),
jasmine.objectContaining({
duration: INFO_MESSAGE_DURATION,
horizontalPosition: ALERT_HORZ_POSITION,
verticalPosition: ALERT_VERT_POSITION
})
);
});
});

describe('showing an error alert', () => {
beforeEach(() => {
service.error(TEST_MESSAGE);
});

it('opens the message popup', () => {
expect(snackbar.open).toHaveBeenCalledWith(
TEST_MESSAGE,
jasmine.any(String),
jasmine.objectContaining({
duration: ERROR_MESSAGE_DURATION,
horizontalPosition: ALERT_HORZ_POSITION,
verticalPosition: ALERT_VERT_POSITION
})
);
});
});
});
69 changes: 69 additions & 0 deletions comixed-web/src/app/core/services/alert.service.ts
@@ -0,0 +1,69 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import { Injectable } from '@angular/core';
import { LoggerService } from '@angular-ru/logger';
import { MatSnackBar } from '@angular/material/snack-bar';
import { TranslateService } from '@ngx-translate/core';

export const INFO_MESSAGE_DURATION = 500;
export const ERROR_MESSAGE_DURATION = 0;
export const ALERT_HORZ_POSITION = 'end';
export const ALERT_VERT_POSITION = 'top';

/**
* AlertService shows message toasters to the user.
*/
@Injectable({
providedIn: 'root'
})
export class AlertService {
constructor(
private logger: LoggerService,
private translateService: TranslateService,
private snackbar: MatSnackBar
) {}

/** Shows an information alert. */
info(message: string): void {
this.logger.trace('Showing info message:', message);
this.snackbar.open(
message,
this.translateService.instant('alert.info-action'),
{
duration: INFO_MESSAGE_DURATION,
horizontalPosition: ALERT_HORZ_POSITION,
verticalPosition: ALERT_VERT_POSITION
}
);
}

/** Shows an error alert. */
error(message: string): void {
this.logger.trace('Showing error message:', message);
this.snackbar.open(
message,
this.translateService.instant('alert.error-action'),
{
duration: ERROR_MESSAGE_DURATION,
horizontalPosition: ALERT_HORZ_POSITION,
verticalPosition: ALERT_VERT_POSITION
}
);
}
}
6 changes: 6 additions & 0 deletions comixed-web/src/assets/i18n/en/core.json
@@ -0,0 +1,6 @@
{
"alert": {
"info-action": "INFO",
"error-action": "ERROR"
}
}
6 changes: 6 additions & 0 deletions comixed-web/src/assets/i18n/es/core.json
@@ -0,0 +1,6 @@
{
"alert": {
"info-action": "INFO",
"error-action": "ERROR"
}
}
6 changes: 6 additions & 0 deletions comixed-web/src/assets/i18n/fr/core.json
@@ -0,0 +1,6 @@
{
"alert": {
"info-action": "INFO",
"error-action": "ERROR"
}
}
6 changes: 6 additions & 0 deletions comixed-web/src/assets/i18n/pt/core.json
@@ -0,0 +1,6 @@
{
"alert": {
"info-action": "INFO",
"error-action": "ERROR"
}
}

0 comments on commit 86a0dd3

Please sign in to comment.