92 lines
3.3 KiB
TypeScript
92 lines
3.3 KiB
TypeScript
import {Component, inject, input, OnChanges, OnInit, signal} from '@angular/core';
|
|
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms";
|
|
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
|
|
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
|
|
import {BooksService, GetBookDto, GetLoanDto, GetUserDto, UsersService} from "../../services/api";
|
|
import {NzDatePickerComponent} from "ng-zorro-antd/date-picker";
|
|
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
|
|
import {NzNotificationService} from "ng-zorro-antd/notification";
|
|
import {firstValueFrom} from "rxjs";
|
|
|
|
@Component({
|
|
selector: 'app-update-loan',
|
|
imports: [
|
|
FormsModule,
|
|
NzColDirective,
|
|
NzFormControlComponent,
|
|
NzFormDirective,
|
|
NzFormItemComponent,
|
|
NzFormLabelComponent,
|
|
NzRowDirective,
|
|
ReactiveFormsModule,
|
|
NzDatePickerComponent,
|
|
NzOptionComponent,
|
|
NzSelectComponent
|
|
],
|
|
templateUrl: './update-loan.html',
|
|
styleUrl: './update-loan.css',
|
|
})
|
|
export class UpdateLoan implements OnInit, OnChanges {
|
|
updateLoanForm = new FormGroup({
|
|
userId: new FormControl<number>(null, Validators.required),
|
|
bookId: new FormControl<number>(null, Validators.required),
|
|
plannedReturningDate: new FormControl(null, Validators.required),
|
|
effectiveReturningDate: new FormControl(null),
|
|
});
|
|
|
|
private userService = inject(UsersService);
|
|
private bookService = inject(BooksService);
|
|
private notificationService = inject(NzNotificationService);
|
|
users = signal<GetUserDto[]>([]);
|
|
books = signal<GetBookDto[]>([]);
|
|
loan = input.required<GetLoanDto>()
|
|
|
|
ngOnChanges() {
|
|
if (this.loan) {
|
|
if (this.loan().effectiveReturningDate != null) {
|
|
this.updateLoanForm.patchValue({
|
|
userId: this.loan().userId,
|
|
bookId: this.loan().bookId,
|
|
plannedReturningDate: new Date(this.loan().plannedReturningDate),
|
|
effectiveReturningDate: new Date(this.loan().effectiveReturningDate)
|
|
});
|
|
|
|
} else {
|
|
this.updateLoanForm.patchValue({
|
|
userId: this.loan().userId,
|
|
bookId: this.loan().bookId,
|
|
plannedReturningDate: new Date(this.loan().plannedReturningDate),
|
|
effectiveReturningDate: null
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
async fetchUsers() {
|
|
try {
|
|
const users = await firstValueFrom(this.userService.getAllUserEndpoint());
|
|
this.users.set(users);
|
|
} catch (e) {
|
|
this.notificationService.error('Erreur', 'Impossible de récupérer les utilisateurs');
|
|
}
|
|
}
|
|
|
|
async fetchBooks() {
|
|
try {
|
|
const books = await firstValueFrom(this.bookService.getAllBookEndpoint());
|
|
this.books.set(books);
|
|
} catch (e) {
|
|
this.notificationService.error('Erreur', 'Impossible de récupérer les livres');
|
|
}
|
|
}
|
|
|
|
async ngOnInit() {
|
|
await this.fetchUsers();
|
|
await this.fetchBooks();
|
|
}
|
|
|
|
filter(input: string, option: any) {
|
|
return option.nzLabel.toLowerCase().includes(input.toLowerCase());
|
|
}
|
|
}
|