Browse Source

added notification service with alertify as the concrete implementation

master
nova 4 years ago
parent
commit
70f0ea8054
  1. 3
      chatto/src/main/javascript/package.json
  2. 12
      chatto/src/main/javascript/ts/src/main.ts
  3. 27
      chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts
  4. 4
      chatto/src/main/javascript/ts/src/service/EncryptionService.ts
  5. 7
      chatto/src/main/javascript/ts/src/service/NotificationService.ts
  6. 11
      chatto/src/main/javascript/ts/src/view/FetchHandler.ts

3
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"
}
}

12
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<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);

27
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);
}
}

4
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;
}

7
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;
}

11
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);
});
}

Loading…
Cancel
Save