Adds button to export all user emails (as admin)

This commit is contained in:
apapachristou 2020-10-07 16:35:23 +03:00
parent f4f3099b50
commit 7439281c50
10 changed files with 84 additions and 29 deletions

View File

@ -1,4 +1,4 @@
import { HttpHeaders } from '@angular/common/http';
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from '../../../../environments/environment';
@ -15,7 +15,7 @@ export class UserService {
private actionUrl: string;
private headers: HttpHeaders;
constructor(private http: BaseHttpService, private configurationService: ConfigurationService) {
constructor(private http: BaseHttpService, private httpClient: HttpClient, private configurationService: ConfigurationService) {
this.actionUrl = configurationService.server + 'user/';
}
@ -62,4 +62,9 @@ export class UserService {
const url = this.actionUrl + 'deleteDOIToken';
return this.http.delete(url, { headers: this.headers });
}
downloadCSV(): Observable<HttpResponse<Blob>> {
let headerCsv: HttpHeaders = this.headers.set('Content-Type', 'application/csv')
return this.httpClient.get(this.actionUrl + 'getCsv/', { responseType: 'blob', observe: 'response', headers: headerCsv });
}
}

View File

@ -2,6 +2,9 @@
<div class="container-fluid user-listing">
<h3>{{'USERS.LISTING.TITLE' | translate}}</h3>
<div class="d-flex mb-3"><button mat-raised-button type="button" class="export-btn ml-auto" (click)="exportUsers()">
<mat-icon class="mr-2 export-icon">save_alt</mat-icon>{{'USERS.LISTING.EXPORT' | translate}}
</button></div>
<app-user-criteria-component></app-user-criteria-component>
<mat-card class="mat-card">
<mat-table [dataSource]="dataSource" matSort>

View File

@ -15,6 +15,16 @@
background-color: #0c748914;
// background-color: #eef0fb;
}
.export-btn {
background: #129D99 0% 0% no-repeat padding-box;
border-radius: 30px;
color: #ffffff;
}
.export-icon {
font-size: 20px;
}
}
::ng-deep .mat-paginator-container {

View File

@ -1,7 +1,7 @@
import {of as observableOf, merge as observableMerge, Observable } from 'rxjs';
import {map, catchError, switchMap, startWith} from 'rxjs/operators';
import {map, catchError, switchMap, startWith, takeUntil} from 'rxjs/operators';
import { DataSource } from '@angular/cdk/table';
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
@ -15,6 +15,8 @@ import { SnackBarNotificationComponent } from '../../../../library/notification/
import { DataTableRequest } from '../../../../core/model/data-table/data-table-request';
import { UserCriteriaComponent } from './criteria/user-criteria.component';
import { BreadcrumbItem } from '../../../misc/breadcrumb/definition/breadcrumb-item';
import { BaseComponent } from '@common/base/base.component';
import * as FileSaver from 'file-saver';
export class UsersDataSource extends DataSource<UserListingModel> {
@ -93,7 +95,7 @@ export class UsersDataSource extends DataSource<UserListingModel> {
templateUrl: './user-listing.component.html',
styleUrls: ['./user-listing.component.scss']
})
export class UserListingComponent implements OnInit, AfterViewInit {
export class UserListingComponent extends BaseComponent implements OnInit, AfterViewInit {
@ViewChild(MatPaginator, { static: true }) _paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort;
@ -108,7 +110,7 @@ export class UserListingComponent implements OnInit, AfterViewInit {
private languageService: TranslateService,
public snackBar: MatSnackBar
) {
super();
}
ngOnInit() {
@ -138,4 +140,33 @@ export class UserListingComponent implements OnInit, AfterViewInit {
const defaultCriteria = new UserCriteria();
return defaultCriteria;
}
// Export user mails
exportUsers(){
this.userService.downloadCSV()
.pipe(takeUntil(this._destroyed))
.subscribe(response => {
const blob = new Blob([response.body], { type: 'application/csv' });
const filename = this.getFilenameFromContentDispositionHeader(response.headers.get('Content-Disposition'));
FileSaver.saveAs(blob, filename);
});
}
getFilenameFromContentDispositionHeader(header: string): string {
const regex: RegExp = new RegExp(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/g);
const matches = header.match(regex);
let filename: string;
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
if (match.includes('filename="')) {
filename = match.substring(10, match.length - 1);
break;
} else if (match.includes('filename=')) {
filename = match.substring(9);
break;
}
}
return filename;
}
}

View File

@ -1068,7 +1068,8 @@
"LABEL": "Label",
"ROLES": "Rollen",
"NAME": "Name",
"PERMISSIONS": "Berechtigungen"
"PERMISSIONS": "Berechtigungen",
"EXPORT": "Export users"
}
},
"TYPES": {

View File

@ -1068,7 +1068,8 @@
"LABEL": "Label",
"ROLES": "Roles",
"NAME": "Name",
"PERMISSIONS": "Permissions"
"PERMISSIONS": "Permissions",
"EXPORT": "Export users"
}
},
"TYPES": {

View File

@ -1068,7 +1068,8 @@
"LABEL": "Etiqueta",
"ROLES": "Función",
"NAME": "Nombre",
"PERMISSIONS": "Permisos"
"PERMISSIONS": "Permisos",
"EXPORT": "Export users"
}
},
"TYPES": {

View File

@ -1068,7 +1068,8 @@
"LABEL": "Ετικέτα",
"ROLES": "Ρόλος",
"NAME": "Όνομα",
"PERMISSIONS": "ʼδειες"
"PERMISSIONS": "ʼδειες",
"EXPORT": "Export users"
}
},
"TYPES": {

View File

@ -1068,7 +1068,8 @@
"LABEL": "Označenie",
"ROLES": "Role",
"NAME": "Názov",
"PERMISSIONS": "Povolenia"
"PERMISSIONS": "Povolenia",
"EXPORT": "Export users"
}
},
"TYPES": {

View File

@ -1069,7 +1069,8 @@
"LABEL": "Etiket",
"ROLES": "Görevler",
"NAME": "İsim",
"PERMISSIONS": "İzinler"
"PERMISSIONS": "İzinler",
"EXPORT": "Export users"
}
},
"TYPES": {