-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathusers.component.ts
More file actions
93 lines (84 loc) · 2.56 KB
/
users.component.ts
File metadata and controls
93 lines (84 loc) · 2.56 KB
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
import { takeUntil } from 'rxjs/operators';
import { UsersService } from './../services/users.service';
import {
AfterViewInit,
Component,
OnInit,
ViewChild,
OnDestroy,
} from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { MatSort } from '@angular/material/sort';
import { MatDialog } from '@angular/material/dialog';
import { ModalComponent } from './../components/modal/modal.component';
import { Subject } from 'rxjs';
import Swal from 'sweetalert2';
@Component({
selector: 'app-users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.scss'],
})
export class UsersComponent implements AfterViewInit, OnInit, OnDestroy {
displayedColumns: string[] = ['id', 'role', 'username', 'actions'];
dataSource = new MatTableDataSource();
private destroy$ = new Subject<any>();
@ViewChild(MatSort) sort: MatSort;
constructor(private userSvc: UsersService, private dialog: MatDialog) {}
ngOnInit(): void {
this.userSvc.getAll().subscribe((users) => {
this.dataSource.data = users;
});
}
ngAfterViewInit(): void {
this.dataSource.sort = this.sort;
}
onDelete(userId: number): void {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
this.userSvc
.delete(userId)
.pipe(takeUntil(this.destroy$))
.subscribe((res) => {
console.log("Del ->", res)
// Update result after deleting the user.
this.userSvc.getAll().subscribe((users) => {
this.dataSource.data = users;
});
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success',
)
});
}
});
}
onOpenModal(user = {}): void {
console.log('User ->', user);
let dialogRef = this.dialog.open(ModalComponent, {
height: '400px',
width: '600px',
hasBackdrop: false,
data: { title: 'New user', user },
});
dialogRef.afterClosed().subscribe(result => {
console.log(`Dialog result: ${result}`, typeof result);
// Update result after adding new user.
this.userSvc.getAll().subscribe((users) => {
this.dataSource.data = users;
});
});
}
ngOnDestroy(): void {
this.destroy$.next({});
this.destroy$.complete();
}
}