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 { Injectable } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { environment } from '../../../../environments/environment'; import { environment } from '../../../../environments/environment';
@ -15,7 +15,7 @@ export class UserService {
private actionUrl: string; private actionUrl: string;
private headers: HttpHeaders; 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/'; this.actionUrl = configurationService.server + 'user/';
} }
@ -62,4 +62,9 @@ export class UserService {
const url = this.actionUrl + 'deleteDOIToken'; const url = this.actionUrl + 'deleteDOIToken';
return this.http.delete(url, { headers: this.headers }); 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"> <div class="container-fluid user-listing">
<h3>{{'USERS.LISTING.TITLE' | translate}}</h3> <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> <app-user-criteria-component></app-user-criteria-component>
<mat-card class="mat-card"> <mat-card class="mat-card">
<mat-table [dataSource]="dataSource" matSort> <mat-table [dataSource]="dataSource" matSort>

View File

@ -15,6 +15,16 @@
background-color: #0c748914; background-color: #0c748914;
// background-color: #eef0fb; // 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 { ::ng-deep .mat-paginator-container {

View File

@ -1,7 +1,7 @@
import {of as observableOf, merge as observableMerge, Observable } from 'rxjs'; 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 { DataSource } from '@angular/cdk/table';
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core'; import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator'; 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 { DataTableRequest } from '../../../../core/model/data-table/data-table-request';
import { UserCriteriaComponent } from './criteria/user-criteria.component'; import { UserCriteriaComponent } from './criteria/user-criteria.component';
import { BreadcrumbItem } from '../../../misc/breadcrumb/definition/breadcrumb-item'; 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> { export class UsersDataSource extends DataSource<UserListingModel> {
@ -93,7 +95,7 @@ export class UsersDataSource extends DataSource<UserListingModel> {
templateUrl: './user-listing.component.html', templateUrl: './user-listing.component.html',
styleUrls: ['./user-listing.component.scss'] 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(MatPaginator, { static: true }) _paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort; @ViewChild(MatSort, { static: true }) sort: MatSort;
@ -108,7 +110,7 @@ export class UserListingComponent implements OnInit, AfterViewInit {
private languageService: TranslateService, private languageService: TranslateService,
public snackBar: MatSnackBar public snackBar: MatSnackBar
) { ) {
super();
} }
ngOnInit() { ngOnInit() {
@ -138,4 +140,33 @@ export class UserListingComponent implements OnInit, AfterViewInit {
const defaultCriteria = new UserCriteria(); const defaultCriteria = new UserCriteria();
return defaultCriteria; 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", "LABEL": "Label",
"ROLES": "Rollen", "ROLES": "Rollen",
"NAME": "Name", "NAME": "Name",
"PERMISSIONS": "Berechtigungen" "PERMISSIONS": "Berechtigungen",
"EXPORT": "Export users"
} }
}, },
"TYPES": { "TYPES": {

View File

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

View File

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

View File

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

View File

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

View File

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