1414
1515import { CommonModule } from '@angular/common' ;
1616import { HttpErrorResponse } from '@angular/common/http' ;
17- import { Component , effect , ViewChild } from '@angular/core' ;
17+ import { Component , effect } from '@angular/core' ;
1818import { MatSnackBar } from '@angular/material/snack-bar' ;
19- import { MatSort } from '@angular/material/sort' ;
20- import { MatTableDataSource } from '@angular/material/table' ;
2119import { SelectedRegistrarModule } from '../app.module' ;
2220import { MaterialModule } from '../material.module' ;
2321import { RegistrarService } from '../registrar/registrar.service' ;
2422import { SnackBarModule } from '../snackbar.module' ;
2523import { UserEditComponent } from './userEdit.component' ;
26- import { roleToDescription , User , UsersService } from './users.service' ;
27-
28- export const columns = [
29- {
30- columnDef : 'emailAddress' ,
31- header : 'User email' ,
32- cell : ( record : User ) => `${ record . emailAddress || '' } ` ,
33- } ,
34- {
35- columnDef : 'role' ,
36- header : 'User role' ,
37- cell : ( record : User ) => `${ roleToDescription ( record . role ) } ` ,
38- } ,
39- ] ;
24+ import { User , UsersService } from './users.service' ;
25+ import { UserDataService } from '../shared/services/userData.service' ;
26+ import { FormsModule } from '@angular/forms' ;
27+ import { UsersListComponent } from './usersList.component' ;
28+ import { MatSelectChange } from '@angular/material/select' ;
4029
4130@Component ( {
4231 selector : 'app-users' ,
4332 templateUrl : './users.component.html' ,
4433 styleUrls : [ './users.component.scss' ] ,
4534 standalone : true ,
4635 imports : [
36+ FormsModule ,
4737 MaterialModule ,
4838 SnackBarModule ,
4939 CommonModule ,
5040 SelectedRegistrarModule ,
41+ UsersListComponent ,
5142 UserEditComponent ,
5243 ] ,
5344 providers : [ UsersService ] ,
5445} )
5546export class UsersComponent {
56- dataSource : MatTableDataSource < User > ;
57- columns = columns ;
58- displayedColumns = this . columns . map ( ( c ) => c . columnDef ) ;
5947 isLoading = false ;
60-
61- @ViewChild ( MatSort ) sort ! : MatSort ;
48+ selectingExistingUser = false ;
49+ selectedRegistrarId = '' ;
50+ usersSelection : User [ ] = [ ] ;
51+ selectedExistingUser : User | undefined ;
6252
6353 constructor (
6454 protected registrarService : RegistrarService ,
6555 protected usersService : UsersService ,
56+ private userDataService : UserDataService ,
6657 private _snackBar : MatSnackBar
6758 ) {
68- this . dataSource = new MatTableDataSource < User > ( usersService . users ( ) ) ;
69-
7059 effect ( ( ) => {
7160 if ( registrarService . registrarId ( ) ) {
7261 this . loadUsers ( ) ;
7362 }
7463 } ) ;
75- effect ( ( ) => {
76- this . dataSource . data = usersService . users ( ) ;
77- } ) ;
7864 }
7965
80- ngAfterViewInit ( ) {
81- this . dataSource . sort = this . sort ;
66+ addExistingUser ( ) {
67+ this . selectingExistingUser = true ;
68+ this . selectedRegistrarId = '' ;
69+ this . usersSelection = [ ] ;
70+ this . selectedExistingUser = undefined ;
71+ }
72+
73+ existingUserSelected ( user : User ) {
74+ this . selectedExistingUser = user ;
8275 }
8376
8477 loadUsers ( ) {
@@ -96,7 +89,7 @@ export class UsersComponent {
9689
9790 createNewUser ( ) {
9891 this . isLoading = true ;
99- this . usersService . createNewUser ( ) . subscribe ( {
92+ this . usersService . createOrAddNewUser ( null ) . subscribe ( {
10093 error : ( err : HttpErrorResponse ) => {
10194 this . _snackBar . open ( err . error || err . message ) ;
10295 this . isLoading = false ;
@@ -107,7 +100,39 @@ export class UsersComponent {
107100 } ) ;
108101 }
109102
110- openDetails ( emailAddress : string ) {
111- this . usersService . currentlyOpenUserEmail . set ( emailAddress ) ;
103+ openDetails ( user : User ) {
104+ this . usersService . currentlyOpenUserEmail . set ( user . emailAddress ) ;
105+ }
106+
107+ onRegistrarSelectionChange ( e : MatSelectChange ) {
108+ if ( e . value ) {
109+ this . usersService . fetchUsersForRegistrar ( e . value ) . subscribe ( {
110+ error : ( err ) => {
111+ this . _snackBar . open ( err . error || err . message ) ;
112+ } ,
113+ next : ( users ) => {
114+ this . usersSelection = users ;
115+ } ,
116+ } ) ;
117+ }
118+ }
119+
120+ submitExistingUser ( ) {
121+ this . isLoading = true ;
122+ if ( this . selectedExistingUser ) {
123+ this . usersService
124+ . createOrAddNewUser ( this . selectedExistingUser )
125+ . subscribe ( {
126+ error : ( err ) => {
127+ this . _snackBar . open ( err . error || err . message ) ;
128+ this . isLoading = false ;
129+ } ,
130+ complete : ( ) => {
131+ this . isLoading = false ;
132+ this . selectingExistingUser = false ;
133+ this . loadUsers ( ) ;
134+ } ,
135+ } ) ;
136+ }
112137 }
113138}
0 commit comments