better toasts, better dashboard, first implementation of consumer status

This commit is contained in:
Jonas Winkler
2020-11-07 12:05:15 +01:00
parent 572e40ca27
commit 036f11acaa
28 changed files with 450 additions and 134 deletions

View File

@@ -28,7 +28,7 @@ export class AuthInterceptor implements HttpInterceptor {
catchError((error: HttpErrorResponse) => {
if (error.status == 401 && this.authService.isAuthenticated()) {
this.authService.logout()
this.toastService.showToast(Toast.makeError("Your session has expired. Please log in again."))
this.toastService.showError("Your session has expired. Please log in again.")
}
return throwError(error)
})

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ConsumerStatusService } from './consumer-status.service';
describe('ConsumerStatusService', () => {
let service: ConsumerStatusService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ConsumerStatusService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,71 @@
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
export interface FileStatus {
filename?: string
current_progress?: number
max_progress?: number
status?: string
message?: string
document_id?: number
}
@Injectable({
providedIn: 'root'
})
export class ConsumerStatusService {
constructor() { }
private statusWebSocked: WebSocket
consumerStatus: FileStatus[] = []
private documentConsumptionFinishedSubject = new Subject<FileStatus>()
private documentConsumptionFailedSubject = new Subject<FileStatus>()
connect() {
this.disconnect()
this.statusWebSocked = new WebSocket("ws://localhost:8000/ws/status/");
this.statusWebSocked.onmessage = (ev) => {
let statusUpdate: FileStatus = JSON.parse(ev['data'])
let index = this.consumerStatus.findIndex(fs => fs.filename == statusUpdate.filename)
if (index > -1) {
this.consumerStatus[index] = statusUpdate
} else {
this.consumerStatus.push(statusUpdate)
}
if (statusUpdate.status == "SUCCESS") {
this.documentConsumptionFinishedSubject.next(statusUpdate)
}
if (statusUpdate.status == "FAILED") {
this.documentConsumptionFailedSubject.next(statusUpdate)
}
}
}
disconnect() {
if (this.statusWebSocked) {
this.statusWebSocked.close()
this.statusWebSocked = null
}
}
dismiss(status: FileStatus) {
let index = this.consumerStatus.findIndex(s => s.filename == status.filename)
if (index > -1) {
this.consumerStatus.splice(index, 1)
}
}
onDocumentConsumptionFinished() {
return this.documentConsumptionFinishedSubject
}
onDocumentConsumptionFailed() {
return this.documentConsumptionFailedSubject
}
}

View File

@@ -1,30 +1,17 @@
import { Injectable } from '@angular/core';
import { Subject, zip } from 'rxjs';
export class Toast {
static make(title: string, content: string, classname?: string, delay?: number): Toast {
let t = new Toast()
t.title = title
t.content = content
t.classname = classname
if (delay) {
t.delay = delay
}
return t
}
static makeError(content: string) {
return Toast.make("Error", content, null, 10000)
}
export interface Toast {
title: string
classname: string
content: string
delay: number = 5000
delay?: number
action?: any
actionName?: string
}
@@ -44,6 +31,14 @@ export class ToastService {
this.toastsSubject.next(this.toasts)
}
showInfo(message: string) {
this.showToast({title: "Information", content: message, delay: 5000})
}
showError(message: string) {
this.showToast({title: "Error", content: message, delay: 10000})
}
closeToast(toast: Toast) {
let index = this.toasts.findIndex(t => t == toast)
if (index > -1) {