added notification service with alertify as the concrete implementation

This commit is contained in:
nova 2019-12-11 10:56:16 +05:30
parent 2e7f6cced6
commit 70f0ea8054
6 changed files with 55 additions and 9 deletions

View File

@ -35,6 +35,7 @@
"dompurify": "global:DOMPurify", "dompurify": "global:DOMPurify",
"fuse.js": "global:Fuse", "fuse.js": "global:Fuse",
"sjcl": "global:sjcl", "sjcl": "global:sjcl",
"sprintf": "global:sprintf" "sprintf-js": "global:sprintf2",
"alertifyjs": "global:alertify"
} }
} }

View File

@ -19,7 +19,9 @@ import { MessageCipherDTO } from "./dto/MessageCipherDTO";
import { SearchService } from "./service/SearchService"; import { SearchService } from "./service/SearchService";
import { FuseSearchService } from "./service/FuseSearchService"; import { FuseSearchService } from "./service/FuseSearchService";
import { ChatMessageDTO } from "./dto/ChatMessageDTO"; import { ChatMessageDTO } from "./dto/ChatMessageDTO";
import { NotificationService } from "./service/NotificationService";
import { AlertifyNotificationService } from "./service/AlertifyNotificationService";
import * as sprintf2 from "sprintf-js";
const usersListElement = document.getElementById('contacts-box'); const usersListElement = document.getElementById('contacts-box');
const userSearchButton = document.getElementById('user-search'); const userSearchButton = document.getElementById('user-search');
@ -83,3 +85,9 @@ Handlebars.registerHelper('avatar', function() {
const testList: ChatMessageDTO[] = []; const testList: ChatMessageDTO[] = [];
const cmSearchService: SearchService<ChatMessageDTO> = new FuseSearchService(["userName"]); const cmSearchService: SearchService<ChatMessageDTO> = new FuseSearchService(["userName"]);
// @ts-ignore
log.info(sprintf("test sprintf"))
log.info(sprintf2.sprintf("test sprintf"))
const ns: NotificationService = new AlertifyNotificationService();
ns.success("Welcome");
// ns.errorWithDelay("Hmm very long error notif", 10);

View File

@ -0,0 +1,27 @@
import { NotificationService } from "./NotificationService";
// @ts-ignore
import * as alertify from "alertifyjs";
export class AlertifyNotificationService implements NotificationService {
private readonly _alertify = alertify;
constructor() {
this._alertify.set('notifier', 'position', 'top-center');
}
success(message: string): void {
this._alertify.success(message)
}
error(message: string): void {
this._alertify.error(message);
}
errorWithDelay(message: string, delay: number): void {
const _delay = this._alertify.get('notifier', 'delay');
this._alertify.set('notifier', 'delay', delay);
this._alertify.error(message);
this._alertify.set('notifier', 'delay', _delay);
}
warning(message: string): void {
this._alertify.warning(message);
}
message(message: string): void {
this._alertify.message(message);
}
}

View File

@ -1,6 +1,6 @@
import { MessageCipherDTO } from "../dto/MessageCipherDTO"; import { MessageCipherDTO } from "../dto/MessageCipherDTO";
export interface EncryptionService { export interface EncryptionService {
encrypt(passphrase: string, plainText: string): any, encrypt(passphrase: string, plainText: string): any;
decrypt(passphrase: string, cipher: MessageCipherDTO): string decrypt(passphrase: string, cipher: MessageCipherDTO): string;
} }

View File

@ -0,0 +1,7 @@
export interface NotificationService {
success(message: string): void;
error(message: string): void;
errorWithDelay(message: string, delay: number): void;
warning(message: string): void;
message(message: string): void;
}

View File

@ -1,22 +1,25 @@
import { sprintf } from "sprintf-js"; import { sprintf } from "sprintf-js";
import { NotificationService } from "../service/NotificationService";
import { AlertifyNotificationService } from "../service/AlertifyNotificationService";
export function fetchHandler(response: any) { export function fetchHandler(response: any) {
const ns: NotificationService = new AlertifyNotificationService();
if (response.ok) { if (response.ok) {
return response.json().then((json: any) => { return response.json().then((json: any) => {
// the status was ok and there is a json body // the status was ok and there is a json body
// return Promise.resolve({ json: json, response: response }); // return Promise.resolve({ json: json, response: response });
// alertify.success('Message sent succesfully' + sprintf(" (http code %d)", response.status)); ns.success('Message sent succesfully' + sprintf(" (http code %d)", response.status));
}).catch((err: any) => { }).catch((err: any) => {
// the status was ok but there is no json body // the status was ok but there is no json body
// return Promise.resolve({ response: response }); // return Promise.resolve({ response: response });
// alertify.success('Message sent succesfully' + sprintf(" (http code %d)", response.status)); ns.success('Message sent succesfully' + sprintf(" (http code %d)", response.status));
}); });
} else { } else {
return response.json().catch((err: any) => { return response.json().catch((err: any) => {
// the status was not ok and there is no json body // the status was not ok and there is no json body
// throw new Error(response.statusText); // throw new Error(response.statusText);
// alertify.error('Some error occured. Please try again.'); ns.error('Some error occured. Please try again.');
}).then((json: any) => { }).then((json: any) => {
// the status was not ok but there is a json body // the status was not ok but there is a json body
// throw new Error(json.error.message); // example error message returned by a REST // throw new Error(json.error.message); // example error message returned by a REST
@ -26,7 +29,7 @@ export function fetchHandler(response: any) {
json.errors.forEach(function(data: any) { json.errors.forEach(function(data: any) {
errorMessage += sprintf("Field Name: %s \n Rejected value: %s \n Reason: %s \n", data.field_name, data.rejected_value, data.error_message); errorMessage += sprintf("Field Name: %s \n Rejected value: %s \n Reason: %s \n", data.field_name, data.rejected_value, data.error_message);
}); });
// alertify.error(sprintf('There were errors in your message - %s', errorMessage)); ns.errorWithDelay(sprintf('There were errors in your message - %s', errorMessage), 30);
// alertify.set('notifier', 'delay', delay); // alertify.set('notifier', 'delay', delay);
}); });
} }