diff --git a/src/app/admin-view/account-mgmt/account-mgmt.component.html b/src/app/admin-view/account-mgmt/account-mgmt.component.html index cba85bc..27dad79 100644 --- a/src/app/admin-view/account-mgmt/account-mgmt.component.html +++ b/src/app/admin-view/account-mgmt/account-mgmt.component.html @@ -3,7 +3,7 @@ Wyszukaj - + @@ -24,17 +24,9 @@
-
+ diff --git a/src/app/admin-view/account-mgmt/account-mgmt.component.ts b/src/app/admin-view/account-mgmt/account-mgmt.component.ts index 8d6be8c..45c43b8 100644 --- a/src/app/admin-view/account-mgmt/account-mgmt.component.ts +++ b/src/app/admin-view/account-mgmt/account-mgmt.component.ts @@ -4,10 +4,7 @@ import { MatDialog } from '@angular/material/dialog'; import { MatTableDataSource } from '@angular/material/table'; import { MatPaginator } from '@angular/material/paginator'; import { MatSnackBar } from '@angular/material/snack-bar'; -import { UserDeleteComponent } from './user-delete/user-delete.component'; import { UserEditComponent } from './user-edit/user-edit.component'; -import { catchError, throwError } from 'rxjs'; -import { UserResetComponent } from './user-reset/user-reset.component'; import { LocalStorageService } from 'src/app/services/local-storage.service'; import { Group } from 'src/app/types/group'; import User from 'src/app/types/user'; @@ -57,75 +54,9 @@ export class AccountMgmtComponent implements OnInit, AfterViewInit { this.users.filter = value.toLowerCase().trim() } - edit(item: any) { - this.dialog.open(UserEditComponent, {data: {user: item, groups: this.groups}}).afterClosed().subscribe(reply => { - if (reply) { - this.ac.accs.putAcc(item._id, reply).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.ngOnInit() - } else { - this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.") - } - }) - } - }) - } - - new() { - this.dialog.open(UserEditComponent, {data: {groups: this.groups}}).afterClosed().subscribe(reply => { - if (reply) { - this.ac.accs.postAcc(reply).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.ngOnInit() - } else { - this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.") - } - }) - } - }) - } - - delete(id: string) { - this.dialog.open(UserDeleteComponent).afterClosed().subscribe(reply => { - if (reply) { - this.ac.accs.deleteAcc(id).subscribe((res) => { - if (res.status == 200) { - this.sb.open("Użytkownik został usunięty.", undefined, {duration: 2500}) - this.ngOnInit() - } else { - this.sb.open("Wystąpił błąd. Skontaktuj się z obsługą programu.") - console.error(res); - } - }) - } - }) - } - - resetPass(id: string) { - this.dialog.open(UserResetComponent).afterClosed().subscribe((res) => { - if (res == true) { - this.ac.accs.resetPass(id).subscribe((patch)=>{ - if (patch.status == 200) { - this.sb.open("Hasło zostało zresetowane", undefined, {duration: 2500}) - } - }) - } - }) - } - - toggleLock(item: any) { - this.ac.accs.putAcc(item._id, {locked: !item.locked}).subscribe((res) => { - if (res.status == 200) { - item.locked = !item.locked - } + openUserCard(id?: string) { + this.dialog.open(UserEditComponent, {data: {id: id, type: id ? "edit" : "new", groups: this.groups}}).afterClosed().subscribe(r => { + if (r) this.ngOnInit() }) } diff --git a/src/app/admin-view/account-mgmt/user-edit/user-edit.component.html b/src/app/admin-view/account-mgmt/user-edit/user-edit.component.html index 0d278b4..baad3aa 100644 --- a/src/app/admin-view/account-mgmt/user-edit/user-edit.component.html +++ b/src/app/admin-view/account-mgmt/user-edit/user-edit.component.html @@ -1,39 +1,67 @@ - - - Imię - - - - Nazwisko - - - - Pokój - - - - Nazwa użytkownika - - - - Grupy - - @for (item of groups; track $index) { - {{item.name}} +

Karta użytkownika

+ + +
+ + Imię + + + + Nazwisko + + + + Pokój + + + + Grupy + + @for (item of groups; track $index) { + {{item.name}} + } + + + Data rejestracji:
{{regDate?.format('DD.MM.YYYY')}}
+
+
+ + Nazwa użytkownika + + + @if (data.type == "edit") { + + @if (locked) { + + } @else { + + } + @if (lockout) { + + } @else { + + } + + Uprawnienia + + Wiadomości + Jadłospis + Powiadomienia + Grupy + Konta + Klucze + Czystość + + } - - - - Uprawnienia - - Wiadomości - Jadłospis - Powiadomienia - Grupy - Konta - Klucze - Czystość - - - - \ No newline at end of file +
+ +
+ + @if (data.type == "edit") { + + } + + + + \ No newline at end of file diff --git a/src/app/admin-view/account-mgmt/user-edit/user-edit.component.scss b/src/app/admin-view/account-mgmt/user-edit/user-edit.component.scss index e269236..9290470 100644 --- a/src/app/admin-view/account-mgmt/user-edit/user-edit.component.scss +++ b/src/app/admin-view/account-mgmt/user-edit/user-edit.component.scss @@ -4,7 +4,29 @@ } form { + margin-top: 1ch !important; display: flex; - flex-direction: column; + grid-auto-flow: column; + flex-direction: row; + flex-wrap: wrap; align-items: center; + justify-content: center; + column-gap: 3ch; + div { + display: grid; + grid-template-columns: 1fr; + grid-template-rows: repeat(5, 1fr); + align-items: center; + button { + align-self: stretch; + justify-self: stretch; + height: auto; + margin-bottom: 1lh; + } + } +} + +mat-dialog-actions { + display: flex; + justify-content: flex-end; } \ No newline at end of file diff --git a/src/app/admin-view/account-mgmt/user-edit/user-edit.component.ts b/src/app/admin-view/account-mgmt/user-edit/user-edit.component.ts index 76bdc20..09d8454 100644 --- a/src/app/admin-view/account-mgmt/user-edit/user-edit.component.ts +++ b/src/app/admin-view/account-mgmt/user-edit/user-edit.component.ts @@ -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(""), + surname: new FormControl(""), + room: new FormControl(""), + uname: new FormControl(""), + groups: new FormControl>([]), + flags: new FormControl>([]), + }) groups: Group[] - constructor (public dialogRef: MatDialogRef, @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, + @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 = [] + 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 = [] - 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(data.user.uname), - groups: new FormControl>(data.user.groups), - flags: new FormControl>(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 + } }) } } diff --git a/src/app/admin-view/admin-comm.service.ts b/src/app/admin-view/admin-comm.service.ts index f81e9c2..bee8779 100644 --- a/src/app/admin-view/admin-comm.service.ts +++ b/src/app/admin-view/admin-comm.service.ts @@ -131,7 +131,7 @@ export class AdminCommService { return this.http.post(environment.apiEndpoint+`/admin/accs`, item, {withCredentials: true}) }, - putAcc: (id: string, update: object) => { + putAcc: (id: string, update: Partial) => { return this.http.put(environment.apiEndpoint+`/admin/accs/${id}`, update, {withCredentials: true}) }, @@ -141,6 +141,14 @@ export class AdminCommService { deleteAcc: (id: string) => { return this.http.delete(environment.apiEndpoint+`/admin/accs/${id}`, {withCredentials: true}) + }, + + getUser: (id: string) => { + return this.http.get & {lockout: boolean}>(environment.apiEndpoint+`/admin/accs/${id}`, {withCredentials: true}) + }, + + clearLockout: (id: string) => { + return this.http.delete(environment.apiEndpoint+`/admin/accs/${id}/lockout`, {withCredentials: true}) } } //#endregion diff --git a/src/app/types/user.ts b/src/app/types/user.ts index 6d10c36..00b5aa2 100644 --- a/src/app/types/user.ts +++ b/src/app/types/user.ts @@ -1,3 +1,5 @@ +import { Moment } from "moment"; + export default interface User { _id: string; uname: string; @@ -8,4 +10,5 @@ export default interface User { fname?: string; surname?: string; groups: string[]; + regDate: Moment; } \ No newline at end of file
{{element.uname}} AkcjeKarta użytkownika - - - - +