/
admin.component.ts
142 lines (116 loc) · 4.64 KB
/
admin.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {Component, OnInit} from '@angular/core';
import {AppConfig} from '../../app-config/app-config';
import {AlertService} from '../../alerts/alert.service';
import {Alert} from '../../alerts/alert';
import FileSaver from 'file-saver';
import {BackupService} from '../backup.service';
import {ConfirmationDialogService} from '../../ui-helper/confirmation-dialog/confirmation-dialog.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import PouchDB from 'pouchdb-browser';
@Component({
selector: 'app-admin',
templateUrl: './admin.component.html',
styleUrls: ['./admin.component.scss']
})
export class AdminComponent implements OnInit {
appConfig = AppConfig.settings;
db;
alerts: Alert[];
constructor(private alertService: AlertService,
private backupService: BackupService,
private confirmationDialog: ConfirmationDialogService,
private snackBar: MatSnackBar) { }
ngOnInit() {
this.alerts = this.alertService.alerts;
this.db = new PouchDB(AppConfig.settings.database.name);
}
debugDatabase() {
console.log(this.db);
}
saveBackup() {
this.backupService.getJsonExport()
.then(bac => this.startDownload(bac, 'text/json', 'backup.json'));
}
saveCsvExport() {
this.backupService.getCsvExport()
.then(csv => this.startDownload(csv, 'text/csv', 'export.csv'));
}
private startDownload(data: string, type: string, name: string) {
const blob = new Blob([data], { type: type });
FileSaver.saveAs(blob, name);
}
private readFile(file): Promise<string> {
return new Promise(resolve => {
const fileReader = new FileReader();
fileReader.onload = () => {
resolve(fileReader.result as string);
};
fileReader.readAsText(file);
}
);
}
loadBackup(file) {
const pRestorePoint = this.backupService.getJsonExport();
const pLoadedData = this.readFile(file);
Promise.all([pLoadedData, pRestorePoint]).then(r => {
const newData = r[0];
const restorePoint = r[1];
const dialogRef = this.confirmationDialog
.openDialog('Overwrite complete database?', 'Are you sure you want to restore this backup? ' +
'This will delete all ' + restorePoint.split('\n').length + ' existing records in the database, ' +
'restoring ' + newData.split('\n').length + ' records from the loaded file.');
dialogRef.afterClosed()
.subscribe(confirmed => {
if (confirmed) {
this.backupService.clearDatabase();
this.backupService.importJson(newData, true);
const snackBarRef = this.snackBar.open('Backup restored', 'Undo', { duration: 8000 });
snackBarRef.onAction().subscribe(() => {
this.backupService.clearDatabase();
this.backupService.importJson(restorePoint, true);
});
}
});
});
}
loadCsv(file) {
const pRestorePoint = this.backupService.getJsonExport();
const pLoadedData = this.readFile(file);
Promise.all([pLoadedData, pRestorePoint]).then(r => {
const newData = r[0];
const restorePoint = r[1];
const dialogRef = this.confirmationDialog
.openDialog('Import new data?', 'Are you sure you want to import this file? ' +
'This will add or update ' + (newData.trim().split('\n').length - 1) + ' records from the loaded file. ' +
'Existing records with same "_id" in the database will be overwritten!');
dialogRef.afterClosed()
.subscribe(confirmed => {
if (confirmed) {
this.backupService.importCsv(newData, true);
const snackBarRef = this.snackBar.open('Import completed', 'Undo', {duration: 8000});
snackBarRef.onAction().subscribe(() => {
this.backupService.clearDatabase();
this.backupService.importJson(restorePoint, true);
});
}
});
});
}
clearDatabase() {
this.backupService.getJsonExport().then(restorePoint => {
const dialogRef = this.confirmationDialog
.openDialog('Empty complete database?', 'Are you sure you want to clear the database? ' +
'This will delete all ' + restorePoint.split('\n').length + ' existing records in the database!');
dialogRef.afterClosed()
.subscribe(confirmed => {
if (confirmed) {
this.backupService.clearDatabase();
const snackBarRef = this.snackBar.open('Import completed', 'Undo', {duration: 8000});
snackBarRef.onAction().subscribe(() => {
this.backupService.importJson(restorePoint, true);
});
}
});
});
}
}