updated create-form of loan and book
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
<form nz-form nzLayout="horizontal" [formGroup]="createBookForm" (ngSubmit)="submitForm()">
|
<form nz-form nzLayout="horizontal" [formGroup]="createBookForm">
|
||||||
<nz-form-item>
|
<nz-form-item>
|
||||||
<nz-form-label nzSpan="8" nzRequired>
|
<nz-form-label nzSpan="8" nzRequired>
|
||||||
Titre
|
Titre
|
||||||
@@ -25,7 +25,11 @@
|
|||||||
</nz-form-label>
|
</nz-form-label>
|
||||||
|
|
||||||
<nz-form-control nzSpan="40" nzErrorTip="Ce champ est requis">
|
<nz-form-control nzSpan="40" nzErrorTip="Ce champ est requis">
|
||||||
<input nz-input placeholder="Auteur" formControlName="author">
|
<nz-select formControlName="authorId" [nzPlaceHolder]="'Choisir un auteur'">
|
||||||
|
@for (author of authors(); track author.id) {
|
||||||
|
<nz-option [nzValue]="author.id" [nzLabel]="author.firstName + ' ' + author.name"></nz-option>
|
||||||
|
}
|
||||||
|
</nz-select>
|
||||||
</nz-form-control>
|
</nz-form-control>
|
||||||
</nz-form-item>
|
</nz-form-item>
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +1,40 @@
|
|||||||
import { Component } from '@angular/core';
|
import {Component, inject, input, OnInit, signal} from '@angular/core';
|
||||||
import {NzFormModule} from "ng-zorro-antd/form";
|
import {NzFormModule} from "ng-zorro-antd/form";
|
||||||
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
|
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
|
||||||
import {NzInputDirective} from "ng-zorro-antd/input";
|
import {NzInputDirective} from "ng-zorro-antd/input";
|
||||||
|
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
|
||||||
|
import {AuthorsService, GetAuthorDto, GetBookDto} from "../../services/api";
|
||||||
|
import {NzNotificationService} from "ng-zorro-antd/notification";
|
||||||
|
import {firstValueFrom} from "rxjs";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-create-book',
|
selector: 'app-create-book',
|
||||||
imports: [NzFormModule, ReactiveFormsModule, NzInputDirective],
|
imports: [NzFormModule, ReactiveFormsModule, NzInputDirective, NzOptionComponent, NzSelectComponent],
|
||||||
templateUrl: './create-book.html',
|
templateUrl: './create-book.html',
|
||||||
styleUrls: ['./create-book.css'],
|
styleUrls: ['./create-book.css'],
|
||||||
})
|
})
|
||||||
export class CreateBook {
|
export class CreateBook implements OnInit {
|
||||||
createBookForm = new FormGroup({
|
createBookForm = new FormGroup({
|
||||||
title: new FormControl<string>(null, [Validators.required]),
|
title: new FormControl<string>(null, [Validators.required]),
|
||||||
isbn: new FormControl<string>(null, [Validators.required]),
|
isbn: new FormControl<string>(null, [Validators.required]),
|
||||||
releaseYear: new FormControl<string>(null, [Validators.required]),
|
releaseYear: new FormControl<number>(null, [Validators.required]),
|
||||||
author: new FormControl<string>(null, [Validators.required])
|
authorId: new FormControl<number>(null, Validators.required)
|
||||||
})
|
})
|
||||||
|
|
||||||
submitForm() {
|
private authorsService = inject(AuthorsService);
|
||||||
// Pour annuler si le formulaire est invalide
|
private notificationService = inject(NzNotificationService);
|
||||||
if (this.createBookForm.invalid) return;
|
authors = signal<GetAuthorDto[]>([]);
|
||||||
|
|
||||||
// Pour obtenir la valeur du formulaire
|
async fetchAuthors() {
|
||||||
console.log(this.createBookForm.getRawValue())
|
try {
|
||||||
|
const authors = await firstValueFrom(this.authorsService.getAllAuthorsEndpoint());
|
||||||
|
this.authors.set(authors);
|
||||||
|
} catch (e) {
|
||||||
|
this.notificationService.error('Erreur', 'Impossible de récupérer les auteurs');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Pour vider le formulaire
|
async ngOnInit() {
|
||||||
this.createBookForm.reset()
|
await this.fetchAuthors();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,29 @@
|
|||||||
<form nz-form nzLayout="horizontal" [formGroup]="createLoanForm" (ngSubmit)="submitForm()">
|
<form nz-form nzLayout="horizontal" [formGroup]="createLoanForm">
|
||||||
<nz-form-item>
|
<nz-form-item>
|
||||||
<nz-form-label nzSpan="8" nzRequired>
|
<nz-form-label nzSpan="10" nzRequired>
|
||||||
Nom / Prénom
|
Utilisateur
|
||||||
</nz-form-label>
|
</nz-form-label>
|
||||||
|
|
||||||
<nz-form-control nzSpan="40" nzErrorTip="Ce champ est requis">
|
<nz-form-control nzSpan="40" nzErrorTip="Ce champ est requis">
|
||||||
<input nz-input placeholder="Non défini" formControlName="name">
|
<nz-select formControlName="userId" [nzPlaceHolder]="'Choisir un utilisateur'">
|
||||||
|
@for (user of users(); track user.id) {
|
||||||
|
<nz-option [nzValue]="user.id" [nzLabel]="user.firstName + ' ' + user.name"></nz-option>
|
||||||
|
}
|
||||||
|
</nz-select>
|
||||||
</nz-form-control>
|
</nz-form-control>
|
||||||
</nz-form-item>
|
</nz-form-item>
|
||||||
|
|
||||||
<nz-form-item>
|
<nz-form-item>
|
||||||
<nz-form-label nzSpan="8" nzRequired>
|
<nz-form-label nzSpan="10" nzRequired>
|
||||||
Livre
|
Livre
|
||||||
</nz-form-label>
|
</nz-form-label>
|
||||||
|
|
||||||
<nz-form-control nzSpan="40" nzErrorTip="Ce champ est requis">
|
<nz-form-control nzSpan="40" nzErrorTip="Ce champ est requis">
|
||||||
<input nz-input placeholder="Prénom de l'auteur" formControlName="book">
|
<nz-select formControlName="bookId" [nzPlaceHolder]="'Choisir un livre'">
|
||||||
|
@for (book of books(); track book.id) {
|
||||||
|
<nz-option [nzValue]="book.id" [nzLabel]="book.title"></nz-option>
|
||||||
|
}
|
||||||
|
</nz-select>
|
||||||
</nz-form-control>
|
</nz-form-control>
|
||||||
</nz-form-item>
|
</nz-form-item>
|
||||||
</form>
|
</form>
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
import { Component } from '@angular/core';
|
import {Component, inject, OnInit, signal} from '@angular/core';
|
||||||
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms";
|
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms";
|
||||||
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
|
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
|
||||||
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
|
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
|
||||||
import {NzInputDirective} from "ng-zorro-antd/input";
|
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
|
||||||
|
import {BooksService, GetBookDto, GetUserDto, UsersService} from "../../services/api";
|
||||||
|
import {NzNotificationService} from "ng-zorro-antd/notification";
|
||||||
|
import {firstValueFrom} from "rxjs";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-create-loan',
|
selector: 'app-create-loan',
|
||||||
@@ -15,25 +18,44 @@ import {NzInputDirective} from "ng-zorro-antd/input";
|
|||||||
NzFormLabelComponent,
|
NzFormLabelComponent,
|
||||||
NzRowDirective,
|
NzRowDirective,
|
||||||
ReactiveFormsModule,
|
ReactiveFormsModule,
|
||||||
NzInputDirective
|
NzOptionComponent,
|
||||||
|
NzSelectComponent
|
||||||
],
|
],
|
||||||
templateUrl: './create-loan.html',
|
templateUrl: './create-loan.html',
|
||||||
styleUrl: './create-loan.css',
|
styleUrl: './create-loan.css',
|
||||||
})
|
})
|
||||||
export class CreateLoan {
|
export class CreateLoan implements OnInit {
|
||||||
createLoanForm = new FormGroup({
|
createLoanForm = new FormGroup({
|
||||||
name: new FormControl<string>(null, [Validators.required]),
|
userId: new FormControl<number>(null, Validators.required),
|
||||||
book: new FormControl<string>(null, [Validators.required])
|
bookId: new FormControl<number>(null, Validators.required)
|
||||||
})
|
})
|
||||||
|
|
||||||
submitForm() {
|
private userService = inject(UsersService);
|
||||||
// Pour annuler si le formulaire est invalide
|
private bookService = inject(BooksService);
|
||||||
if (this.createLoanForm.invalid) return;
|
private notificationService = inject(NzNotificationService);
|
||||||
|
users = signal<GetUserDto[]>([]);
|
||||||
|
books = signal<GetBookDto[]>([]);
|
||||||
|
|
||||||
// Pour obtenir la valeur du formulaire
|
async fetchUsers() {
|
||||||
console.log(this.createLoanForm.getRawValue())
|
try {
|
||||||
|
const users = await firstValueFrom(this.userService.getAllUsersEndpoint());
|
||||||
|
this.users.set(users);
|
||||||
|
} catch (e) {
|
||||||
|
this.notificationService.error('Erreur', 'Impossible de récupérer les utilisateurs');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Pour vider le formulaire
|
async fetchBooks() {
|
||||||
this.createLoanForm.reset()
|
try {
|
||||||
|
const books = await firstValueFrom(this.bookService.getAllBooksEndpoint());
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user