fix: Redesigned user cards
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
import { Component, Inject } from '@angular/core';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
||||
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
|
||||
import { FormControl, FormGroup } from '@angular/forms';
|
||||
import { LocalStorageService } from 'src/app/services/local-storage.service';
|
||||
import { Group } from 'src/app/types/group';
|
||||
import { AdminCommService } from '../../admin-comm.service';
|
||||
import { UserDeleteComponent } from '../user-delete/user-delete.component';
|
||||
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||
import { UserResetComponent } from '../user-reset/user-reset.component';
|
||||
import { catchError, throwError } from 'rxjs';
|
||||
import { Moment } from 'moment';
|
||||
import * as moment from 'moment';
|
||||
|
||||
@Component({
|
||||
selector: 'app-user-edit',
|
||||
@@ -10,43 +17,105 @@ import { Group } from 'src/app/types/group';
|
||||
styleUrls: ['./user-edit.component.scss']
|
||||
})
|
||||
export class UserEditComponent {
|
||||
form: FormGroup
|
||||
lockout = false;
|
||||
locked = false;
|
||||
loading = false;
|
||||
form: FormGroup = new FormGroup({
|
||||
fname: new FormControl<string>(""),
|
||||
surname: new FormControl<string>(""),
|
||||
room: new FormControl<string>(""),
|
||||
uname: new FormControl<string>(""),
|
||||
groups: new FormControl<Array<string>>([]),
|
||||
flags: new FormControl<Array<number>>([]),
|
||||
})
|
||||
groups: Group[]
|
||||
constructor (public dialogRef: MatDialogRef<UserEditComponent>, @Inject(MAT_DIALOG_DATA) public data: any, readonly ls: LocalStorageService) {
|
||||
if (data.user == null) {
|
||||
data.user = {
|
||||
fname: "",
|
||||
surname: "",
|
||||
room: "",
|
||||
uname: "",
|
||||
groups: [],
|
||||
admin: 0
|
||||
id?: string
|
||||
regDate?: Moment;
|
||||
constructor (
|
||||
public dialogRef: MatDialogRef<UserEditComponent>,
|
||||
@Inject(MAT_DIALOG_DATA) public data: ({type: "edit", id: string} | {type: "new"}) & {groups: Group[]},
|
||||
readonly ls: LocalStorageService,
|
||||
readonly acu: AdminCommService,
|
||||
private dialog: MatDialog,
|
||||
private sb: MatSnackBar
|
||||
) {
|
||||
this.groups = data.groups
|
||||
if (data.type == "edit") {
|
||||
this.id = data.id
|
||||
this.acu.accs.getUser(data.id).subscribe((r) => {
|
||||
this.regDate = moment(r.regDate)
|
||||
var flags: Array<number> = []
|
||||
if (r.admin) {
|
||||
if ((r.admin & 1) == 1) flags.push(1)
|
||||
if ((r.admin & 2) == 2) flags.push(2)
|
||||
if ((r.admin & 4) == 4) flags.push(4)
|
||||
if ((r.admin & 8) == 8) flags.push(8)
|
||||
if ((r.admin & 16) == 16) flags.push(16)
|
||||
if ((r.admin & 32) == 32) flags.push(32)
|
||||
if ((r.admin & 64) == 64) flags.push(64)
|
||||
if ((r.admin & 128) == 128) flags.push(128)
|
||||
}
|
||||
this.locked = r.locked ? true : false
|
||||
this.lockout = r.lockout
|
||||
this.form.get("fname")?.setValue(r.fname)
|
||||
this.form.get("surname")?.setValue(r.surname)
|
||||
this.form.get("room")?.setValue(r.room)
|
||||
this.form.get("uname")?.setValue(r.uname)
|
||||
this.form.get("groups")?.setValue(r.groups)
|
||||
this.form.get("flags")?.setValue(flags)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
protected submit() {
|
||||
this.loading = true
|
||||
if (this.data.type == "edit") {
|
||||
this.acu.accs.putAcc(this.id!, this.getForm()).pipe(catchError((err)=>{
|
||||
this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.")
|
||||
return throwError(()=> new Error(err.message))
|
||||
})).subscribe((data)=> {
|
||||
if (data.status == 200) {
|
||||
this.sb.open("Użytkownik został zmodyfikowany.", undefined, {duration: 2500})
|
||||
this.dialogRef.close(true)
|
||||
} else {
|
||||
this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.")
|
||||
this.loading = false
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this.acu.accs.postAcc(this.getForm()).pipe(catchError((err)=>{
|
||||
this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.")
|
||||
return throwError(()=> new Error(err.message))
|
||||
})).subscribe((data)=> {
|
||||
if (data.status == 201) {
|
||||
this.sb.open("Użytkownik został utworzony.", undefined, {duration: 2500})
|
||||
this.dialogRef.close(true)
|
||||
} else {
|
||||
this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.")
|
||||
this.loading = false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
protected disableLockout() {
|
||||
this.loading = true
|
||||
this.acu.accs.clearLockout(this.id!).pipe(catchError((err)=>{
|
||||
this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.")
|
||||
return throwError(()=> new Error(err.message))
|
||||
})).subscribe((s) => {
|
||||
if (s.status == 200) {
|
||||
this.loading = false
|
||||
this.lockout = false
|
||||
} else {
|
||||
this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.")
|
||||
this.loading = false
|
||||
}
|
||||
}
|
||||
this.groups = data.groups ? data.groups : []
|
||||
var flags: Array<number> = []
|
||||
if (data.user.admin) {
|
||||
if ((data.user.admin & 1) == 1) flags.push(1)
|
||||
if ((data.user.admin & 2) == 2) flags.push(2)
|
||||
if ((data.user.admin & 4) == 4) flags.push(4)
|
||||
if ((data.user.admin & 8) == 8) flags.push(8)
|
||||
if ((data.user.admin & 16) == 16) flags.push(16)
|
||||
if ((data.user.admin & 32) == 32) flags.push(32)
|
||||
if ((data.user.admin & 64) == 64) flags.push(64)
|
||||
if ((data.user.admin & 128) == 128) flags.push(128)
|
||||
}
|
||||
this.form = new FormGroup({
|
||||
fname: new FormControl(data.user.fname),
|
||||
surname: new FormControl(data.user.surname),
|
||||
room: new FormControl(data.user.room),
|
||||
uname: new FormControl<string>(data.user.uname),
|
||||
groups: new FormControl<Array<string>>(data.user.groups),
|
||||
flags: new FormControl<Array<number>>(flags),
|
||||
})
|
||||
}
|
||||
|
||||
protected editUser() {
|
||||
this.dialogRef.close({
|
||||
protected getForm() {
|
||||
return {
|
||||
fname: this.form.get('fname')?.value,
|
||||
surname: this.form.get('surname')?.value,
|
||||
room: this.form.get('room')?.value,
|
||||
@@ -60,6 +129,44 @@ export class UserEditComponent {
|
||||
return undefined
|
||||
}
|
||||
})()
|
||||
}
|
||||
}
|
||||
|
||||
protected delete() {
|
||||
this.dialog.open(UserDeleteComponent).afterClosed().subscribe(reply => {
|
||||
if (reply) {
|
||||
this.acu.accs.deleteAcc(this.id!).subscribe((res) => {
|
||||
if (res.status == 200) {
|
||||
this.sb.open("Użytkownik został usunięty.", undefined, {duration: 2500})
|
||||
this.dialogRef.close()
|
||||
} else {
|
||||
this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.")
|
||||
console.error(res);
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
protected resetPass() {
|
||||
this.loading = true
|
||||
this.dialog.open(UserResetComponent).afterClosed().subscribe((res) => {
|
||||
if (res == true) {
|
||||
this.acu.accs.resetPass(this.id!).subscribe((patch)=>{
|
||||
if (patch.status == 200) {
|
||||
this.sb.open("Hasło zostało zresetowane", undefined, {duration: 2500})
|
||||
this.loading = false
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
protected toggleLock(state: boolean) {
|
||||
this.acu.accs.putAcc(this.id!, {locked: state}).subscribe((res) => {
|
||||
if (res.status == 200) {
|
||||
this.locked = state
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user