|
| 1 | +// Copyright 2024 The Nomulus Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { CommonModule } from '@angular/common'; |
| 16 | +import { Component } from '@angular/core'; |
| 17 | +import { MatSnackBar } from '@angular/material/snack-bar'; |
| 18 | +import { SelectedRegistrarModule } from '../app.module'; |
| 19 | +import { MaterialModule } from '../material.module'; |
| 20 | +import { RegistrarService } from '../registrar/registrar.service'; |
| 21 | +import { SnackBarModule } from '../snackbar.module'; |
| 22 | +import { User, UsersService, roleToDescription } from './users.service'; |
| 23 | + |
| 24 | +@Component({ |
| 25 | + selector: 'app-user-edit', |
| 26 | + templateUrl: './userEdit.component.html', |
| 27 | + styleUrls: ['./userEdit.component.scss'], |
| 28 | + standalone: true, |
| 29 | + imports: [ |
| 30 | + MaterialModule, |
| 31 | + SnackBarModule, |
| 32 | + CommonModule, |
| 33 | + SelectedRegistrarModule, |
| 34 | + ], |
| 35 | + providers: [], |
| 36 | +}) |
| 37 | +export class UserEditComponent { |
| 38 | + inEdit = false; |
| 39 | + isPasswordVisible = false; |
| 40 | + isNewUser = false; |
| 41 | + isLoading = false; |
| 42 | + userDetails: User; |
| 43 | + |
| 44 | + constructor( |
| 45 | + protected registrarService: RegistrarService, |
| 46 | + protected usersService: UsersService, |
| 47 | + private _snackBar: MatSnackBar |
| 48 | + ) { |
| 49 | + this.userDetails = this.usersService |
| 50 | + .users() |
| 51 | + .filter( |
| 52 | + (u) => u.emailAddress === this.usersService.currentlyOpenUserEmail() |
| 53 | + )[0]; |
| 54 | + if (this.usersService.isNewUser) { |
| 55 | + this.isNewUser = true; |
| 56 | + this.usersService.isNewUser = false; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + roleToDescription(role: string) { |
| 61 | + return roleToDescription(role); |
| 62 | + } |
| 63 | + |
| 64 | + deleteUser() { |
| 65 | + this.isLoading = true; |
| 66 | + this.usersService.deleteUser(this.userDetails.emailAddress).subscribe({ |
| 67 | + error: (err) => { |
| 68 | + this._snackBar.open(err.error || err.message); |
| 69 | + this.isLoading = false; |
| 70 | + }, |
| 71 | + complete: () => { |
| 72 | + this.isLoading = false; |
| 73 | + this.goBack(); |
| 74 | + }, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + goBack() { |
| 79 | + this.usersService.currentlyOpenUserEmail.set(''); |
| 80 | + } |
| 81 | +} |
0 commit comments