Files
maptology/web-angular/src/app/widgets/share-dialog.component.ts
T
handte d78cebfbc0
LocationBasedServices/maptology/master This commit looks good
Fix all ng linting errors.
2019-07-28 10:32:58 +02:00

64 lines
2.0 KiB
TypeScript

import { Component, Inject, ViewChild } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA, MatDialog, MatSnackBar } from '@angular/material';
import { Observable, Subscription } from 'rxjs';
import { NgForm } from '@angular/forms';
import { ApiClientService } from '../../api';
import { Snackbars } from '../utils/snackbars';
import { FormErrorEncoder } from '../utils/formcodes';
import { ResourceService } from '../services/resource.service';
@Component({
selector: 'app-share-dialog',
templateUrl: 'share-dialog.component.html',
})
export class ShareDialogComponent extends FormErrorEncoder {
@ViewChild('shareForm', { static: true }) shareForm: NgForm;
link: String;
private subscription: Subscription;
public static open(dialog: MatDialog, link?: string, w?: string): Observable<boolean> {
const ref = dialog.open(ShareDialogComponent, {
data: (link) ? link : '',
width: w
});
return ref.afterClosed();
}
constructor(
public snackbar: MatSnackBar,
public apiClientService: ApiClientService,
public dialog: MatDialogRef<any, boolean>,
@Inject(MAT_DIALOG_DATA) data: string,
public res: ResourceService
) {
super();
this.link = data;
}
isLoading(): boolean {
return this.subscription != null;
}
onClose(): void {
this.dialog.close(false);
}
onAccept(): void {
if (this.shareForm.invalid || this.subscription) { return; }
let token = this.link.trim();
const idx = token.lastIndexOf('/');
if (idx !== -1) { token = token.substring(idx + 1); }
this.subscription = this.apiClientService.addCurrentUserPrivilege(token).subscribe((result) => {
this.subscription = null;
this.dialog.close(true);
Snackbars.showMessage(this.snackbar, this.res.strings('share_dialog_success'), Snackbars.DURATION_SHORT);
}, (error) => {
this.subscription = null;
Snackbars.showError(this.snackbar, this.res.strings('share_dialog_error'));
});
}
}