Added notifications - (WIP, boilerplate)
parent
1c6b7815fe
commit
5e08ca004a
@ -0,0 +1,13 @@
|
||||
const utils = require('./utils');
|
||||
const logger = require('./logger');
|
||||
const db_api = require('./db');
|
||||
|
||||
exports.sendNotification = async () => {
|
||||
// TODO: hook into third party service
|
||||
|
||||
const notification = {}
|
||||
|
||||
await db_api.insertRecordIntoTable('notifications', notification);
|
||||
|
||||
return notification;
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
/* istanbul ignore file */
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
export type DeleteNotificationRequest = {
|
||||
uid: string;
|
||||
};
|
@ -0,0 +1,9 @@
|
||||
/* istanbul ignore file */
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
import type { Notification } from './Notification';
|
||||
|
||||
export type GetNotificationsResponse = {
|
||||
notifications?: Array<Notification>;
|
||||
};
|
@ -0,0 +1,14 @@
|
||||
/* istanbul ignore file */
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
import type { NotificationAction } from './NotificationAction';
|
||||
|
||||
export type Notification = {
|
||||
type: string;
|
||||
text: string;
|
||||
uid: string;
|
||||
action?: NotificationAction;
|
||||
read: boolean;
|
||||
data?: any;
|
||||
};
|
@ -0,0 +1,8 @@
|
||||
/* istanbul ignore file */
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
export type NotificationAction = {
|
||||
type: string;
|
||||
icon: string;
|
||||
};
|
@ -0,0 +1,7 @@
|
||||
/* istanbul ignore file */
|
||||
/* tslint:disable */
|
||||
/* eslint-disable */
|
||||
|
||||
export type SetNotificationsToReadRequest = {
|
||||
uids: Array<string>;
|
||||
};
|
@ -0,0 +1,9 @@
|
||||
.notification-divider {
|
||||
margin-bottom: 10px;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.notification-title {
|
||||
margin-bottom: 6px;
|
||||
text-align: center
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<h4 *ngIf="notifications !== null && notifications.length === 0 && read_notifications.length === 0" style="text-align: center; margin: 10px;">No notifications available</h4>
|
||||
<div style="margin: 10px;" *ngIf="notifications?.length > 0">
|
||||
<h4 class="notification-title">New notifications</h4>
|
||||
<div *ngFor="let notification of appService.notifications; let i = index;">
|
||||
<mat-divider class="notification-divider"></mat-divider>
|
||||
<div style="display: inline-block;">
|
||||
<button (click)="deleteNotification(notification.id, i, false)" mat-icon-button><mat-icon>close</mat-icon></button>
|
||||
</div>
|
||||
<div style="display: inline-block">
|
||||
{{notification.title}}
|
||||
</div>
|
||||
<div style="margin-left: 10px; float: right;" *ngIf="notification.action">
|
||||
<button (click)="notificationAction(notification)" [color]="notification.action.warn ? 'warn' : 'primary'" mat-raised-button>{{notification.action.title}}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="margin: 10px;" *ngIf="read_notifications?.length > 0">
|
||||
<h4 class="notification-title">Old notifications</h4>
|
||||
<div *ngFor="let notification of appService.read_notifications; let i = index">
|
||||
<mat-divider class="notification-divider"></mat-divider>
|
||||
<div style="display: inline-block;">
|
||||
<button (click)="deleteNotification(notification.id, i, true)" mat-icon-button><mat-icon>close</mat-icon></button>
|
||||
</div>
|
||||
<div style="display: inline-block">
|
||||
{{notification.title}}
|
||||
</div>
|
||||
<div style="margin-left: 10px; float: right" *ngIf="notification.action">
|
||||
<button (click)="notificationAction(notification)" [color]="notification.action.warn ? 'warn' : 'primary'" mat-raised-button>{{notification.action.title}}</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
@ -0,0 +1,25 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { NotificationsComponent } from './notifications.component';
|
||||
|
||||
describe('NotificationsComponent', () => {
|
||||
let component: NotificationsComponent;
|
||||
let fixture: ComponentFixture<NotificationsComponent>;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [ NotificationsComponent ]
|
||||
})
|
||||
.compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(NotificationsComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
@ -0,0 +1,76 @@
|
||||
import { Component, ElementRef, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
||||
import { MatMenu, MatMenuTrigger } from '@angular/material/menu';
|
||||
import { Router } from '@angular/router';
|
||||
import { PostsService } from 'app/posts.services';
|
||||
import { Notification } from 'api-types';
|
||||
|
||||
// TODO: fill this out
|
||||
const NOTIFICATION_ACTION_TO_STRING = {}
|
||||
|
||||
@Component({
|
||||
selector: 'app-notifications',
|
||||
templateUrl: './notifications.component.html',
|
||||
styleUrls: ['./notifications.component.css']
|
||||
})
|
||||
export class NotificationsComponent implements OnInit {
|
||||
|
||||
notifications = null;
|
||||
read_notifications = null;
|
||||
|
||||
@Input() menu: MatMenuTrigger;
|
||||
@Output() notificationCount = new EventEmitter<number>();
|
||||
|
||||
constructor(public postsService: PostsService, private router: Router, private elRef: ElementRef) { }
|
||||
|
||||
ngOnInit(): void {
|
||||
// wait for init
|
||||
if (this.postsService.initialized) {
|
||||
this.getNotifications();
|
||||
} else {
|
||||
this.postsService.service_initialized.subscribe(init => {
|
||||
if (init) {
|
||||
this.getNotifications();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getNotifications(): void {
|
||||
this.postsService.getNotifications().subscribe(res => {
|
||||
this.notifications = res['notifications'].filter(notification => notification.read == false);
|
||||
this.read_notifications = res['notifications'].filter(notification => notification.read == true);
|
||||
this.notificationCount.emit(this.notifications.length);
|
||||
});
|
||||
}
|
||||
|
||||
notificationAction(notification: Notification): void {
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
deleteNotification(uid: string, index: number): void {
|
||||
this.postsService.deleteNotification(uid).subscribe(res => {
|
||||
console.log(res);
|
||||
// TODO: remove from array
|
||||
this.notificationCount.emit(this.notifications.length);
|
||||
});
|
||||
}
|
||||
|
||||
deleteAllNotifications(): void {
|
||||
this.postsService.deleteAllNotifications().subscribe(res => {
|
||||
console.log(res);
|
||||
this.notifications = [];
|
||||
this.read_notifications = [];
|
||||
this.getNotifications();
|
||||
});
|
||||
this.notificationCount.emit(0);
|
||||
}
|
||||
|
||||
setNotificationsToRead(): void {
|
||||
const uids = this.notifications.map(notification => notification.uid);
|
||||
this.postsService.setNotificationsToRead(uids).subscribe(res => {
|
||||
console.log(res);
|
||||
});
|
||||
this.notificationCount.emit(0);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue