TODO: Add validations to create user form
This commit is contained in:
@@ -1 +1,62 @@
|
||||
<p>admin works!</p>
|
||||
<div class="p-4">
|
||||
<p-button outlined="true" label="Create User" (onClick)="openCreateUserDialog()"></p-button>
|
||||
|
||||
<p-table [value]="users" class="mt-4">
|
||||
<ng-template pTemplate="header">
|
||||
<tr>
|
||||
<th>Username</th>
|
||||
<th>Full Name</th>
|
||||
<th>Email</th>
|
||||
<th style="width: 100px;">Can Upload</th>
|
||||
<th style="width: 100px;">Can Download</th>
|
||||
<th style="width: 100px;">Can Edit Metadata</th>
|
||||
<th style="width: 100px;">Edit</th>
|
||||
<th style="width: 100px;">Delete</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
|
||||
<ng-template pTemplate="body" let-user>
|
||||
<tr>
|
||||
<td>{{ user.username }}</td>
|
||||
<td>
|
||||
<input *ngIf="user.isEditing" type="text" [(ngModel)]="user.name" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!user.isEditing">{{ user.name }}</span>
|
||||
</td>
|
||||
<td>
|
||||
<input *ngIf="user.isEditing" type="email" [(ngModel)]="user.email" class="p-inputtext w-full"/>
|
||||
<span *ngIf="!user.isEditing">{{ user.email }}</span>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canUpload" *ngIf="user.isEditing"></p-checkbox>
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canUpload" disabled *ngIf="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canDownload" *ngIf="user.isEditing"></p-checkbox>
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canDownload" disabled *ngIf="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canEditMetadata" *ngIf="user.isEditing"></p-checkbox>
|
||||
<p-checkbox [binary]="true" [(ngModel)]="user.permissions.canEditMetadata" disabled *ngIf="!user.isEditing"></p-checkbox>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<ng-container *ngIf="user.username !== 'admin'">
|
||||
<p-button *ngIf="!user.isEditing" icon="pi pi-pencil" outlined="true" severity="primary" (onClick)="toggleEdit(user)"></p-button>
|
||||
<p-button *ngIf="user.isEditing" icon="pi pi-check" outlined="true" severity="success" (onClick)="saveUser(user)"></p-button>
|
||||
<p-button *ngIf="user.isEditing" icon="pi pi-times" outlined="true" severity="danger" (onClick)="toggleEdit(user)"></p-button>
|
||||
</ng-container>
|
||||
<p-button *ngIf="user.username === 'admin'"
|
||||
icon="pi pi-pencil"
|
||||
outlined="true"
|
||||
severity="primary"
|
||||
[ngStyle]="{'visibility': 'hidden'}">
|
||||
</p-button>
|
||||
</td>
|
||||
<td class="text-center">
|
||||
<ng-container *ngIf="user.username !== 'admin'">
|
||||
<p-button icon="pi pi-trash" outlined="true" severity="danger" (onClick)="deleteUser(user)"></p-button>
|
||||
</ng-container>
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
</div>
|
||||
|
||||
@@ -1,11 +1,98 @@
|
||||
import { Component } from '@angular/core';
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Button} from 'primeng/button';
|
||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {CreateUserDialogComponent} from './create-user-dialog/create-user-dialog.component';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {NgIf, NgStyle} from '@angular/common';
|
||||
import {User, UserService} from '../../../../user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {Checkbox} from 'primeng/checkbox';
|
||||
|
||||
@Component({
|
||||
selector: 'app-admin',
|
||||
imports: [],
|
||||
imports: [FormsModule, Button, TableModule, NgIf, Checkbox, NgStyle],
|
||||
templateUrl: './admin.component.html',
|
||||
styleUrl: './admin.component.scss'
|
||||
styleUrl: './admin.component.scss',
|
||||
})
|
||||
export class AdminComponent {
|
||||
export class AdminComponent implements OnInit {
|
||||
ref: DynamicDialogRef | undefined;
|
||||
private dialogService = inject(DialogService);
|
||||
private userService = inject(UserService);
|
||||
private messageService = inject(MessageService);
|
||||
|
||||
users: any[] = [];
|
||||
|
||||
ngOnInit() {
|
||||
this.loadUsers();
|
||||
}
|
||||
|
||||
loadUsers() {
|
||||
this.userService.getUsers().subscribe({
|
||||
next: (data) => {
|
||||
this.users = data.map(user => ({
|
||||
...user,
|
||||
isEditing: false,
|
||||
}));
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to fetch users'});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
openCreateUserDialog() {
|
||||
this.ref = this.dialogService.open(CreateUserDialogComponent, {
|
||||
header: 'Create New User',
|
||||
modal: true,
|
||||
closable: true,
|
||||
style: {position: 'absolute', top: '15%'},
|
||||
});
|
||||
this.ref.onClose.subscribe((result) => {
|
||||
if (result) {
|
||||
this.loadUsers();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toggleEdit(user: any) {
|
||||
user.isEditing = !user.isEditing;
|
||||
if (!user.isEditing) {
|
||||
this.loadUsers();
|
||||
}
|
||||
}
|
||||
|
||||
saveUser(user: any) {
|
||||
this.userService.updateUser(user.id, {
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
permissions: user.permissions,
|
||||
}).subscribe({
|
||||
next: () => {
|
||||
user.isEditing = false;
|
||||
this.messageService.add({severity: 'success', summary: 'Success', detail: 'User updated successfully'});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update user'});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
deleteUser(user: User) {
|
||||
if (confirm(`Are you sure you want to delete ${user.username}?`)) {
|
||||
this.userService.deleteUser(user.id).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({severity: 'success', summary: 'Success', detail: `User ${user.username} deleted successfully`});
|
||||
this.loadUsers();
|
||||
},
|
||||
error: (err) => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: err.error?.message || `Failed to delete user ${user.username}`,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
<div class="space-y-6 p-6">
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Full Name:</label>
|
||||
<input type="text" pInputText [(ngModel)]="name" placeholder="Enter full name..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Email:</label>
|
||||
<input type="email" pInputText [(ngModel)]="email" placeholder="Enter email..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Username:</label>
|
||||
<input type="text" pInputText [(ngModel)]="username" placeholder="Enter username..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Password:</label>
|
||||
<input type="password" pInputText [(ngModel)]="password" placeholder="Enter password..." class="w-full md:w-80"/>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<label class="w-40 text-right font-medium">Accessible Libraries:</label>
|
||||
<p-multiselect
|
||||
[options]="libraries"
|
||||
[(ngModel)]="selectedLibraries"
|
||||
placeholder="Select Libraries"
|
||||
optionLabel="name"
|
||||
display="chip"
|
||||
class="w-full md:w-80"
|
||||
appendTo="body">
|
||||
</p-multiselect>
|
||||
</div>
|
||||
|
||||
<!-- Grouped Permissions -->
|
||||
<div>
|
||||
<p class="text-right w-40 font-medium">Permissions:</p>
|
||||
<div class="flex flex-col gap-2 pl-44">
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox [(ngModel)]="canUpload" [binary]="true"></p-checkbox>
|
||||
<label>Upload Books</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox [(ngModel)]="canDownload" [binary]="true"></p-checkbox>
|
||||
<label>Download Books</label>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<p-checkbox [(ngModel)]="canEditMetadata" [binary]="true"></p-checkbox>
|
||||
<label>Edit Metadata</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-end">
|
||||
<p-button
|
||||
label="Create User"
|
||||
[disabled]="!username || password.length < 6 || !name || !email"
|
||||
(onClick)="createUser()">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
import {Component, inject, OnInit} from '@angular/core';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
|
||||
import {Checkbox} from 'primeng/checkbox';
|
||||
import {MultiSelectModule} from 'primeng/multiselect';
|
||||
import {Library} from '../../../../../book/model/library.model';
|
||||
import {Button} from 'primeng/button';
|
||||
import {LibraryService} from '../../../../../book/service/library.service';
|
||||
import {UserService} from '../../../../../user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
|
||||
@Component({
|
||||
selector: 'app-create-user-dialog',
|
||||
standalone: true,
|
||||
imports: [
|
||||
InputText,
|
||||
ReactiveFormsModule,
|
||||
FormsModule,
|
||||
Checkbox,
|
||||
MultiSelectModule,
|
||||
Button
|
||||
],
|
||||
templateUrl: './create-user-dialog.component.html',
|
||||
styleUrl: './create-user-dialog.component.scss'
|
||||
})
|
||||
export class CreateUserDialogComponent implements OnInit {
|
||||
libraries: Library[] = [];
|
||||
selectedLibraries: Library[] = [];
|
||||
|
||||
username: string = '';
|
||||
password: string = '';
|
||||
name: string = '';
|
||||
email: string = '';
|
||||
canUpload: boolean = false;
|
||||
canDownload: boolean = false;
|
||||
canEditMetadata: boolean = false;
|
||||
|
||||
private libraryService = inject(LibraryService);
|
||||
private userService = inject(UserService);
|
||||
private messageService = inject(MessageService);
|
||||
private ref = inject(DynamicDialogRef);
|
||||
|
||||
ngOnInit() {
|
||||
this.libraries = this.libraryService.getLibrariesFromState();
|
||||
}
|
||||
|
||||
createUser() {
|
||||
if (!this.username || this.password.length < 6 || !this.name || !this.email || this.selectedLibraries.length === 0) {
|
||||
this.messageService.add({
|
||||
severity: 'warn',
|
||||
summary: 'Validation Error',
|
||||
detail: 'Please fill all required fields correctly.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const userData = {
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
name: this.name,
|
||||
email: this.email,
|
||||
permissionUpload: this.canUpload,
|
||||
permissionDownload: this.canDownload,
|
||||
permissionEditMetadata: this.canEditMetadata
|
||||
};
|
||||
|
||||
this.userService.createUser(userData).subscribe({
|
||||
next: response => {
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'User Created',
|
||||
detail: response.message
|
||||
});
|
||||
this.ref.close(true);
|
||||
},
|
||||
error: err => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Error',
|
||||
detail: 'Failed to create user: ' + err.error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user