|
| 1 | +import { Component, OnInit, signal } from '@angular/core'; |
| 2 | +import { CommonModule } from '@angular/common'; |
| 3 | +import { FormsModule } from '@angular/forms'; |
| 4 | +import { ActivatedRoute, Router, RouterLink } from '@angular/router'; |
| 5 | +import { AdminService } from '../../services/admin.service'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'app-user-edit', |
| 9 | + imports: [CommonModule, FormsModule, RouterLink], |
| 10 | + template: ` |
| 11 | + <div class="min-h-screen bg-gray-50 py-12 px-4 sm:px-6 lg:px-8"> |
| 12 | + <div class="max-w-md w-full mx-auto space-y-8"> |
| 13 | + <div> |
| 14 | + <h2 class="mt-6 text-center text-3xl font-extrabold text-gray-900">Edit User</h2> |
| 15 | + </div> |
| 16 | +
|
| 17 | + @if (loading()) { |
| 18 | + <div class="text-center py-12" data-testid="loading"> |
| 19 | + <p class="text-gray-500">Loading user...</p> |
| 20 | + </div> |
| 21 | + } @else { |
| 22 | + <form class="mt-8 space-y-6" (ngSubmit)="onSubmit()" #editForm="ngForm"> |
| 23 | + @if (errorMessage()) { |
| 24 | + <div |
| 25 | + class="bg-red-50 border border-red-400 text-red-700 px-4 py-3 rounded" |
| 26 | + data-testid="error-message" |
| 27 | + > |
| 28 | + {{ errorMessage() }} |
| 29 | + </div> |
| 30 | + } |
| 31 | +
|
| 32 | + <div class="space-y-4"> |
| 33 | + <div> |
| 34 | + <label for="email" class="block text-sm font-medium text-gray-700">Email</label> |
| 35 | + <input |
| 36 | + id="email" |
| 37 | + name="email" |
| 38 | + type="email" |
| 39 | + required |
| 40 | + [(ngModel)]="email" |
| 41 | + class="mt-1 appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" |
| 42 | + data-testid="email-input" |
| 43 | + /> |
| 44 | + </div> |
| 45 | +
|
| 46 | + <div> |
| 47 | + <label for="password" class="block text-sm font-medium text-gray-700" |
| 48 | + >Password (leave blank to keep current)</label |
| 49 | + > |
| 50 | + <input |
| 51 | + id="password" |
| 52 | + name="password" |
| 53 | + type="password" |
| 54 | + minlength="6" |
| 55 | + [(ngModel)]="password" |
| 56 | + class="mt-1 appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" |
| 57 | + data-testid="password-input" |
| 58 | + /> |
| 59 | + </div> |
| 60 | +
|
| 61 | + <div> |
| 62 | + <label for="username" class="block text-sm font-medium text-gray-700" |
| 63 | + >Username (optional)</label |
| 64 | + > |
| 65 | + <input |
| 66 | + id="username" |
| 67 | + name="username" |
| 68 | + type="text" |
| 69 | + [(ngModel)]="username" |
| 70 | + class="mt-1 appearance-none relative block w-full px-3 py-2 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-md focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" |
| 71 | + data-testid="username-input" |
| 72 | + /> |
| 73 | + </div> |
| 74 | +
|
| 75 | + <div class="flex items-center"> |
| 76 | + <input |
| 77 | + id="is_admin" |
| 78 | + name="is_admin" |
| 79 | + type="checkbox" |
| 80 | + [(ngModel)]="isAdmin" |
| 81 | + class="h-4 w-4 text-indigo-600 focus:ring-indigo-500 border-gray-300 rounded" |
| 82 | + data-testid="admin-checkbox" |
| 83 | + /> |
| 84 | + <label for="is_admin" class="ml-2 block text-sm text-gray-900"> Admin User </label> |
| 85 | + </div> |
| 86 | + </div> |
| 87 | +
|
| 88 | + <div class="flex space-x-4"> |
| 89 | + <button |
| 90 | + type="submit" |
| 91 | + [disabled]="saving() || !editForm.valid" |
| 92 | + class="flex-1 py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 disabled:opacity-50" |
| 93 | + data-testid="submit-button" |
| 94 | + > |
| 95 | + {{ saving() ? 'Saving...' : 'Update User' }} |
| 96 | + </button> |
| 97 | + <a |
| 98 | + routerLink="/admin/users" |
| 99 | + class="flex-1 py-2 px-4 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 text-center" |
| 100 | + data-testid="cancel-button" |
| 101 | + > |
| 102 | + Cancel |
| 103 | + </a> |
| 104 | + </div> |
| 105 | + </form> |
| 106 | + } |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + `, |
| 110 | + styles: ``, |
| 111 | +}) |
| 112 | +export class UserEdit implements OnInit { |
| 113 | + userId = ''; |
| 114 | + email = ''; |
| 115 | + password = ''; |
| 116 | + username = ''; |
| 117 | + isAdmin = false; |
| 118 | + loading = signal(true); |
| 119 | + saving = signal(false); |
| 120 | + errorMessage = signal(''); |
| 121 | + |
| 122 | + constructor( |
| 123 | + private adminService: AdminService, |
| 124 | + private route: ActivatedRoute, |
| 125 | + private router: Router |
| 126 | + ) {} |
| 127 | + |
| 128 | + async ngOnInit() { |
| 129 | + this.userId = this.route.snapshot.paramMap.get('id') || ''; |
| 130 | + await this.loadUser(); |
| 131 | + } |
| 132 | + |
| 133 | + async loadUser() { |
| 134 | + this.loading.set(true); |
| 135 | + this.errorMessage.set(''); |
| 136 | + |
| 137 | + try { |
| 138 | + const users = await this.adminService.listUsers(); |
| 139 | + const user = users.find((u) => u.id === this.userId); |
| 140 | + |
| 141 | + if (!user) { |
| 142 | + this.errorMessage.set('User not found'); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + this.email = user.email; |
| 147 | + // Note: We can't load username/is_admin from listUsers, would need a separate endpoint |
| 148 | + // For now, user can set these values |
| 149 | + } catch (error: any) { |
| 150 | + this.errorMessage.set(error.message || 'Failed to load user'); |
| 151 | + } finally { |
| 152 | + this.loading.set(false); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + async onSubmit() { |
| 157 | + this.saving.set(true); |
| 158 | + this.errorMessage.set(''); |
| 159 | + |
| 160 | + try { |
| 161 | + const updateData: any = { email: this.email }; |
| 162 | + |
| 163 | + if (this.password) { |
| 164 | + updateData.password = this.password; |
| 165 | + } |
| 166 | + |
| 167 | + if (this.username) { |
| 168 | + updateData.username = this.username; |
| 169 | + } |
| 170 | + |
| 171 | + updateData.is_admin = this.isAdmin; |
| 172 | + |
| 173 | + await this.adminService.updateUser(this.userId, updateData); |
| 174 | + this.router.navigate(['/admin/users']); |
| 175 | + } catch (error: any) { |
| 176 | + this.errorMessage.set(error.message || 'Failed to update user'); |
| 177 | + this.saving.set(false); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments