ajouter 1

This commit is contained in:
HarmandI 2022-03-21 18:25:59 +01:00
parent 8b00bb2ba3
commit 1406011efc
7 changed files with 81 additions and 48 deletions

View File

@ -8,12 +8,8 @@
name="name"
formControlName="nameFc"
[ngClass]="{
'is-valid':
plantForm.controls['nameFc'].touched &&
plantForm.controls['nameFc'].valid,
'is-invalid':
plantForm.controls['nameFc'].touched &&
!plantForm.controls['nameFc'].valid
'is-valid': plantForm.controls['nameFc'].valid,
'is-invalid': !plantForm.controls['nameFc'].valid
}"
/>
<label for="floatingInputName">Nom</label>
@ -27,12 +23,8 @@
name="price"
formControlName="priceFc"
[ngClass]="{
'is-valid':
plantForm.controls['priceFc'].touched &&
plantForm.controls['priceFc'].valid,
'is-invalid':
plantForm.controls['priceFc'].touched &&
!plantForm.controls['priceFc'].valid
'is-valid': plantForm.controls['priceFc'].valid,
'is-invalid': !plantForm.controls['priceFc'].valid
}"
/>
<label for="floatingInputPrice">Prix</label>
@ -46,12 +38,8 @@
name="quantity"
formControlName="quantityFc"
[ngClass]="{
'is-valid':
plantForm.controls['quantityFc'].touched &&
plantForm.controls['quantityFc'].valid,
'is-invalid':
plantForm.controls['quantityFc'].touched &&
!plantForm.controls['quantityFc'].valid
'is-valid': plantForm.controls['quantityFc'].valid,
'is-invalid': !plantForm.controls['quantityFc'].valid
}"
/>
<label for="floatingInputQuantity">Quantité</label>
@ -65,12 +53,8 @@
name="category"
formControlName="categoryFc"
[ngClass]="{
'is-valid':
plantForm.controls['categoryFc'].touched &&
plantForm.controls['categoryFc'].valid,
'is-invalid':
plantForm.controls['categoryFc'].touched &&
!plantForm.controls['categoryFc'].valid
'is-valid': plantForm.controls['categoryFc'].valid,
'is-invalid': !plantForm.controls['categoryFc'].valid
}"
/>
<label for="floatingInputCategory">Catégorie</label>
@ -84,12 +68,8 @@
name="rating"
formControlName="ratingFc"
[ngClass]="{
'is-valid':
plantForm.controls['ratingFc'].touched &&
plantForm.controls['ratingFc'].valid,
'is-invalid':
plantForm.controls['ratingFc'].touched &&
!plantForm.controls['ratingFc'].valid
'is-valid': plantForm.controls['ratingFc'].valid,
'is-invalid': !plantForm.controls['ratingFc'].valid
}"
/>
<label for="floatingRating">Note</label>
@ -114,13 +94,6 @@
type="submit"
[disabled]="plantForm.invalid"
>
Créer une plante
</button>
<button
class="w-100 btn btn-lg btn-outline-success"
type="submit"
[disabled]="plantForm.invalid"
>
Modifier une plante
{{ buttonLabel }}
</button>
</form>

View File

@ -1,5 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
@Component({
selector: 'app-formulaire',
@ -8,7 +8,10 @@ import { FormGroup } from '@angular/forms';
})
export class FormulaireComponent implements OnInit {
plantForm!: FormGroup;
constructor() {}
@Input()buttonLabel!:String;
constructor(private fb : FormBuilder) {
this.plantForm = this.fb.group({});
}
ngOnInit(): void {}

View File

@ -5,7 +5,7 @@ export class Plant {
public name: string='',
public price: number =1,
public quantity: number= 0,
public instock: boolean= true,
public inStock: string[]=['disponible','partiellement disponible', 'non disponible'],
public category:Category,
public urlPicture: string = "https//picsum.photos/id/18/200/300",
public rating: number = 0,

View File

@ -1 +1,2 @@
<p>page-ajouter works!</p>
<h2>Ajouter une plante</h2>
<app-formulaire [buttonLabel]="'Ajouter une plante'"></app-formulaire>

View File

@ -1,15 +1,65 @@
import { Component, OnInit } from '@angular/core';
import {
FormBuilder,
FormControl,
FormGroup,
Validators,
} from '@angular/forms';
import { Router } from '@angular/router';
import { FormulaireComponent } from '../../components/formulaire/formulaire.component';
import { Plant } from '../../models/plant';
import { AdminService } from '../../services/admin.service';
@Component({
selector: 'app-page-ajouter',
templateUrl: './page-ajouter.component.html',
styleUrls: ['./page-ajouter.component.scss']
styleUrls: ['./page-ajouter.component.scss'],
})
export class PageAjouterComponent implements OnInit {
public plantForm: FormGroup;
constructor() { }
ngOnInit(): void {
constructor(private fb: FormBuilder, private router: Router, private adminService: AdminService) {
this.plantForm = new FormGroup({});
}
ngOnInit(): void {
this.plantForm = this.fb.group({
nameFC: new FormControl('', [Validators.required]),
priceFc: new FormControl('', [Validators.required]),
quantityFc: new FormControl('', [Validators.required]),
categoryFc: new FormControl('', [Validators.required]),
ratingFc: new FormControl('', [Validators.required]),
inStockFc: new FormControl('', []),
});
}
public onSubmit(): void {
const nameValue = this.plantForm.value['nameFC'];
const priceValue = this.plantForm.value['priceFc'];
const quantityValue = this.plantForm.value['quantityFc'];
const inStockValue = this.plantForm.value['categoryFc'];
const categoryValue = this.plantForm.value[''];
const urlPicture: string = 'https//picsum.photos/id/18/200/300';
const ratingValue = this.plantForm.value['ratingFc'];
const idValue = this.plantForm.value[''];
const plant : Plant = {
name: nameValue,
price: priceValue,
quantity: quantityValue,
inStock: [inStockValue],
category: categoryValue,
urlPicture: 'https//picsum.photos/id/18/200/300',
rating: ratingValue,
id: idValue,
};
console.log(plant);
this.adminService.addPlant(plant)?.subscribe((resp)=>{
})
this.router.navigate(['admin']);
}
}

View File

@ -27,3 +27,4 @@
</tr>
</tbody>
</table>
<button>Ajouter une plante</button>

View File

@ -2,9 +2,10 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { Plant } from '../models/plant';
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class AdminService {
apiUrl: string;
@ -16,4 +17,8 @@ export class AdminService {
getData(): Observable<any[]> {
return this.httpClient.get<any[]>(`${this.apiUrl}/list_products`);
}
addPlant(plant: Plant): Observable<any> | void {
return this.httpClient.post(`${this.apiUrl}`, plant);
}
}