-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmodal.component.ts
More file actions
80 lines (66 loc) · 1.9 KB
/
modal.component.ts
File metadata and controls
80 lines (66 loc) · 1.9 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
import { UsersService } from './../../services/users.service';
import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { BaseFormUser } from '@shared/utils/base-form-user';
enum Action {
EDIT = 'edit',
NEW = 'new',
}
import Swal from 'sweetalert2';
@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.scss'],
})
export class ModalComponent implements OnInit {
actionTODO = Action.NEW;
showPasswordField = true;
hide = true;
constructor(
@Inject(MAT_DIALOG_DATA) public data: any,
public userForm: BaseFormUser,
private userSvc: UsersService
) {}
ngOnInit(): void {
this.userForm.baseForm.reset(); // Clear Form , left the inputs loaded
if (this.data?.user.hasOwnProperty('id')) {
this.actionTODO = Action.EDIT;
this.showPasswordField = false;
this.userForm.baseForm.get('password').setValidators(null);
this.userForm.baseForm.updateValueAndValidity();
this.data.title = 'Edit user';
this.pathFormData();
}
}
onSave(): void {
const formValue = this.userForm.baseForm.value;
if ( this.actionTODO === Action.NEW ) {
this.userSvc.new(formValue).subscribe((res) =>
this.swal(res['message'])
);
}else{
const userId = this.data?.user?.id;
this.userSvc.update(userId, formValue).subscribe( (res) =>
this.swal(res['message'])
);
}
}
swal(title: string){
Swal.fire({
position: 'top',
icon: 'success',
title: title,
showConfirmButton: false,
timer: 1500
})
}
checkField(field: string): boolean {
return this.userForm.isValidField(field);
}
private pathFormData(): void {
this.userForm.baseForm.patchValue({
username: this.data?.user?.username,
role: this.data?.user?.role,
});
}
}