From 70f0ea8054ccad3d1efedd59b87fc9407d9dbe0d Mon Sep 17 00:00:00 2001 From: nova Date: Wed, 11 Dec 2019 10:56:16 +0530 Subject: [PATCH] added notification service with alertify as the concrete implementation --- chatto/src/main/javascript/package.json | 3 ++- chatto/src/main/javascript/ts/src/main.ts | 12 +++++++-- .../service/AlertifyNotificationService.ts | 27 +++++++++++++++++++ .../ts/src/service/EncryptionService.ts | 4 +-- .../ts/src/service/NotificationService.ts | 7 +++++ .../javascript/ts/src/view/FetchHandler.ts | 11 +++++--- 6 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts create mode 100644 chatto/src/main/javascript/ts/src/service/NotificationService.ts diff --git a/chatto/src/main/javascript/package.json b/chatto/src/main/javascript/package.json index 3740640..e79825e 100644 --- a/chatto/src/main/javascript/package.json +++ b/chatto/src/main/javascript/package.json @@ -35,6 +35,7 @@ "dompurify": "global:DOMPurify", "fuse.js": "global:Fuse", "sjcl": "global:sjcl", - "sprintf": "global:sprintf" + "sprintf-js": "global:sprintf2", + "alertifyjs": "global:alertify" } } \ No newline at end of file diff --git a/chatto/src/main/javascript/ts/src/main.ts b/chatto/src/main/javascript/ts/src/main.ts index 7dac815..76aaac6 100644 --- a/chatto/src/main/javascript/ts/src/main.ts +++ b/chatto/src/main/javascript/ts/src/main.ts @@ -19,7 +19,9 @@ import { MessageCipherDTO } from "./dto/MessageCipherDTO"; import { SearchService } from "./service/SearchService"; import { FuseSearchService } from "./service/FuseSearchService"; 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 userSearchButton = document.getElementById('user-search'); @@ -82,4 +84,10 @@ Handlebars.registerHelper('avatar', function() { }); const testList: ChatMessageDTO[] = []; -const cmSearchService: SearchService = new FuseSearchService(["userName"]); \ No newline at end of file +const cmSearchService: SearchService = 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); \ No newline at end of file diff --git a/chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts b/chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts new file mode 100644 index 0000000..fbc2d00 --- /dev/null +++ b/chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts @@ -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); + } +} \ No newline at end of file diff --git a/chatto/src/main/javascript/ts/src/service/EncryptionService.ts b/chatto/src/main/javascript/ts/src/service/EncryptionService.ts index 3867b68..7337c28 100644 --- a/chatto/src/main/javascript/ts/src/service/EncryptionService.ts +++ b/chatto/src/main/javascript/ts/src/service/EncryptionService.ts @@ -1,6 +1,6 @@ import { MessageCipherDTO } from "../dto/MessageCipherDTO"; export interface EncryptionService { - encrypt(passphrase: string, plainText: string): any, - decrypt(passphrase: string, cipher: MessageCipherDTO): string + encrypt(passphrase: string, plainText: string): any; + decrypt(passphrase: string, cipher: MessageCipherDTO): string; } \ No newline at end of file diff --git a/chatto/src/main/javascript/ts/src/service/NotificationService.ts b/chatto/src/main/javascript/ts/src/service/NotificationService.ts new file mode 100644 index 0000000..9d9b208 --- /dev/null +++ b/chatto/src/main/javascript/ts/src/service/NotificationService.ts @@ -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; +} \ No newline at end of file diff --git a/chatto/src/main/javascript/ts/src/view/FetchHandler.ts b/chatto/src/main/javascript/ts/src/view/FetchHandler.ts index 0f1c42a..9ceca62 100644 --- a/chatto/src/main/javascript/ts/src/view/FetchHandler.ts +++ b/chatto/src/main/javascript/ts/src/view/FetchHandler.ts @@ -1,22 +1,25 @@ import { sprintf } from "sprintf-js"; +import { NotificationService } from "../service/NotificationService"; +import { AlertifyNotificationService } from "../service/AlertifyNotificationService"; export function fetchHandler(response: any) { + const ns: NotificationService = new AlertifyNotificationService(); if (response.ok) { return response.json().then((json: any) => { // the status was ok and there is a json body // 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) => { // the status was ok but there is no json body // 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 { return response.json().catch((err: any) => { // the status was not ok and there is no json body // throw new Error(response.statusText); - // alertify.error('Some error occured. Please try again.'); + ns.error('Some error occured. Please try again.'); }).then((json: any) => { // the status was not ok but there is a json body // 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) { 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); }); }