This commit is contained in:
HarmandI 2022-03-22 11:47:05 +01:00
parent 1b18f648a0
commit be9e510a94
5 changed files with 22 additions and 18 deletions

View File

@ -1,5 +1,5 @@
<div class="form-plant"> <div class="form-plant">
<form [formGroup]="plantForm"> <form [formGroup]="plantForm"(ngSubmit)="onSubmit()">
<div class="form-floating"> <div class="form-floating">
<input <input
type="text" type="text"

View File

@ -1,4 +1,4 @@
import { Component, Input, OnInit } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Plant } from '../../models/plant'; import { Plant } from '../../models/plant';
import { AdminService } from '../../services/admin.service'; import { AdminService } from '../../services/admin.service';
@ -13,6 +13,7 @@ export class FormulaireComponent implements OnInit {
@Input() plantInfos!: Plant; @Input() plantInfos!: Plant;
@Input()buttonLabel!:String; @Input()buttonLabel!:String;
@Output()submitted= new EventEmitter<Plant>();
constructor(private fb : FormBuilder, private adminService: AdminService) { constructor(private fb : FormBuilder, private adminService: AdminService) {
} }
@ -28,10 +29,12 @@ export class FormulaireComponent implements OnInit {
}); });
} }
public onSubmit(): void {
this.submitted.emit(this.plantForm.value);
addPlant(){} }
}
updatePlant(){}
function newEventEmitter() {
throw new Error('Function not implemented.');
} }

View File

@ -1,2 +1,2 @@
<h1>Ajouter une plante</h1> <h1>Ajouter une plante</h1>
<app-formulaire [buttonLabel]="'Ajouter une plante'"></app-formulaire> <app-formulaire (submitted)="onSubmitted($event)" [buttonLabel]="'Ajouter une plante'"></app-formulaire>

View File

@ -21,6 +21,7 @@ import { AdminService } from '../../services/admin.service';
export class PageAjouterComponent implements OnInit { export class PageAjouterComponent implements OnInit {
public plantForm: FormGroup; public plantForm: FormGroup;
public isAdd: boolean = false; public isAdd: boolean = false;
public newplant = new Plant();
constructor(private fb: FormBuilder, private router: Router, private adminService: AdminService) { constructor(private fb: FormBuilder, private router: Router, private adminService: AdminService) {
@ -32,7 +33,7 @@ export class PageAjouterComponent implements OnInit {
} }
public onSubmit(): void { public onSubmitted(submittedPlant: Plant): void {
const nameValue = this.plantForm.value['nameFC']; const nameValue = this.plantForm.value['nameFC'];
const priceValue = this.plantForm.value['priceFc']; const priceValue = this.plantForm.value['priceFc'];
const quantityValue = this.plantForm.value['quantityFc']; const quantityValue = this.plantForm.value['quantityFc'];
@ -43,18 +44,18 @@ export class PageAjouterComponent implements OnInit {
const idValue = this.plantForm.value['']; const idValue = this.plantForm.value[''];
const plant : Plant = { const plant : Plant = {
name: nameValue, product_name: nameValue,
price: priceValue, product_price: priceValue,
quantity: quantityValue, product_qty: quantityValue,
inStock: [inStockValue], product_instock: [inStockValue],
category: [categoryValue], product_breadcrumb_label: [categoryValue],
urlPicture: 'https//picsum.photos/id/18/200/300', product_url_picture: 'https//picsum.photos/id/18/200/300',
rating: ratingValue, product_rating: ratingValue,
id: idValue, id: idValue,
}; };
console.log("coco",plant); console.log("coco",plant);
this.adminService.addPlant(plant)?.subscribe((resp)=>{ this.adminService.addPlant(submittedPlant)?.subscribe((resp)=>{
}) })
this.router.navigate(['admin']); this.router.navigate(['admin']);
} }

View File

@ -38,7 +38,7 @@ export class AdminService {
} }
addPlant(plant: Plant): Observable<any> | void { addPlant(plant: Plant): Observable<any> | void {
return this.httpClient.post(`${this.apiUrl}`, plant); return this.httpClient.post(`${this.apiUrl}/list_products`, plant);
} }
onClickDelete(id: number): Observable<any> { onClickDelete(id: number): Observable<any> {
return this.httpClient.delete<any>(`${this.apiUrl}/list_products/${id}`).pipe( return this.httpClient.delete<any>(`${this.apiUrl}/list_products/${id}`).pipe(