Merge pull request #4 from HarmandI/feature/ajouter

Feature/ajouter
This commit is contained in:
HarmandI 2022-03-22 09:52:58 +01:00 committed by GitHub
commit e8c45f87e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 180 additions and 126 deletions

View File

@ -1,5 +1,5 @@
<div class="form-plant"> <div class="form-plant">
<form (ngSubmit)="isAdd? addPlant() : updatePlant()" [formGroup]="plantForm"> <form (ngSubmit)="onSubmit()" [formGroup]="plantForm">
<div class="form-floating"> <div class="form-floating">
<input <input
type="text" type="text"
@ -9,10 +9,8 @@
name="name" name="name"
formControlName="nameFc" formControlName="nameFc"
[ngClass]="{ [ngClass]="{
'is-valid': 'is-valid': plantForm.controls['nameFc'].valid,
plantForm.controls['nameFc'].valid, 'is-invalid': !plantForm.controls['nameFc'].valid
'is-invalid':
!plantForm.controls['nameFc'].valid
}" }"
/> />
<label for="floatingInputName">Nom</label> <label for="floatingInputName">Nom</label>
@ -26,61 +24,55 @@
name="price" name="price"
formControlName="priceFc" formControlName="priceFc"
[ngClass]="{ [ngClass]="{
'is-valid': 'is-valid': plantForm.controls['priceFc'].valid,
plantForm.controls['priceFc'].valid, 'is-invalid': !plantForm.controls['priceFc'].valid
'is-invalid':
!plantForm.controls['priceFc'].valid
}" }"
/> />
<label for="floatingInputPrice">Prix</label> <label for="floatingInputPrice">Prix</label>
</div> </div>
<div class="form-floating"> <div class="form-floating">
<input <input
type="text" type="number"
class="form-control" class="form-control"
id="floatingInputQuantity" id="floatingInputQuantity"
placeholder="" placeholder=""
name="quantity" name="quantity"
formControlName="quantityFc" formControlName="quantityFc"
[ngClass]="{ [ngClass]="{
'is-valid': 'is-valid': plantForm.controls['quantityFc'].valid,
plantForm.controls['quantityFc'].valid, 'is-invalid': !plantForm.controls['quantityFc'].valid
'is-invalid':
!plantForm.controls['quantityFc'].valid
}" }"
/> />
<label for="floatingInputQuantity">Quantité</label> <label for="floatingInputQuantity">Quantité</label>
</div> </div>
<div class="form-floating"> <div class="form-floating select">
<input <select
type="text"
class="form-control" class="form-control"
id="floatingCategory" id="floatingInputCategory"
placeholder="" placeholder=""
name="category" name="category"
formControlName="categoryFc" formControlName="categoryFc"
[ngClass]="{ >
'is-valid': <option value="plantes fleuries">Plantes fleuries</option>
plantForm.controls['categoryFc'].valid, <option value="orchides">Orchidées</option>
'is-invalid': <option value="cactus et plantes grasses">Cactus et plantes grasses</option>
!plantForm.controls['categoryFc'].valid <option value="bonsas">Bonsas</option>
}" <option value="plantes vertes">Plantes vertes</option>
/> <option value="palmier dintrieur">Palmier d'intérieur</option>
<label for="floatingInputCategory">Catégorie</label> </select>
<label value="category">Sélectionnez catégorie</label>
</div> </div>
<div class="form-floating"> <div class="form-floating">
<input <input
type="text" type="number"
class="form-control" class="form-control"
id="floatingRating" id="floatingRating"
placeholder="" placeholder=""
name="rating" name="rating"
formControlName="ratingFc" formControlName="ratingFc"
[ngClass]="{ [ngClass]="{
'is-valid': 'is-valid': plantForm.controls['ratingFc'].valid,
plantForm.controls['ratingFc'].valid, 'is-invalid': !plantForm.controls['ratingFc'].valid
'is-invalid':
!plantForm.controls['ratingFc'].valid
}" }"
/> />
<label for="floatingRating">Note</label> <label for="floatingRating">Note</label>
@ -99,10 +91,14 @@
</select> </select>
<label value="disponible">Sélectionnez disponibilité</label> <label value="disponible">Sélectionnez disponibilité</label>
</div> </div>
<button <button
class="w-100 btn btn-lg btn-outline-success" class="w-100 btn btn-lg btn-outline-success"
type="submit" type="submit"
[disabled]="plantForm.invalid" [disabled]="plantForm.invalid"
>{{buttonLabel}}</button> >
{{ buttonLabel }}
</button>
</form> </form>
</div> </div>

View File

@ -10,24 +10,26 @@ import { AdminService } from '../../services/admin.service';
}) })
export class FormulaireComponent implements OnInit { export class FormulaireComponent implements OnInit {
plantForm!: FormGroup; plantForm!: FormGroup;
@Input() buttonLabel!: string;
@Input() plantInfos: any;
@Input() isAdd : boolean = true;
constructor(private formBuilder: FormBuilder) {} @Input() plantInfos!: Plant;
@Input()buttonLabel!:String;
constructor(private fb : FormBuilder, private adminService: AdminService) {
}
ngOnInit(): void { ngOnInit(): void {
this.plantForm = this.formBuilder.group( this.plantForm = this.fb.group({
{ nameFc: new FormControl(this.plantInfos.name, [Validators.required]),
nameFc: new FormControl(this.plantInfos.product_name, [Validators.required]), priceFc: new FormControl(this.plantInfos.price, [Validators.required]),
priceFc: new FormControl(this.plantInfos.product_price, [Validators.required]), quantityFc: new FormControl(this.plantInfos.quantity, [Validators.required]),
quantityFc: new FormControl(this.plantInfos.product_quantity, [Validators.required]), inStockFc: new FormControl(this.plantInfos.inStock, [Validators.required]),
inStockFc: new FormControl(this.plantInfos.product_instock, [Validators.required]), categoryFc: new FormControl(this.plantInfos.category, [Validators.required]),
categoryFc: new FormControl(this.plantInfos.product_breadcrumb_label, [Validators.required]), ratingFc: new FormControl(this.plantInfos.rating, [Validators.required]),
ratingFc: new FormControl(this.plantInfos.product_rating, [Validators.required]),
}); });
} }
addPlant(){} addPlant(){}
updatePlant(){} updatePlant(){}

View File

@ -5,8 +5,8 @@ export class Plant {
public name: string='', public name: string='',
public price: number =1, public price: number =1,
public quantity: number= 0, public quantity: number= 0,
public inStock: string[]=['disponible', 'patriellement disponible', 'non disponible'], public inStock: string[]=['disponible','partiellement disponible', 'non disponible'],
public category:Category, public category:string[]=['plantes fleuries','orchides','cactus et plantes grasses','bonsas','plantes vertes','palmier dintrieur'],
public urlPicture: string = "https//picsum.photos/id/18/200/300", public urlPicture: string = "https//picsum.photos/id/18/200/300",
public rating: number = 0, public rating: number = 0,
public id?: number public id?: number

View File

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

View File

@ -0,0 +1,5 @@
h1{
color: rgba(0, 0, 0, 0.658);
font-size: 28px;
margin-top: 20px;
}

View File

@ -1,15 +1,61 @@
import { Component, OnInit } from '@angular/core'; 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({ @Component({
selector: 'app-page-ajouter', selector: 'app-page-ajouter',
templateUrl: './page-ajouter.component.html', templateUrl: './page-ajouter.component.html',
styleUrls: ['./page-ajouter.component.scss'] styleUrls: ['./page-ajouter.component.scss'],
}) })
export class PageAjouterComponent implements OnInit { export class PageAjouterComponent implements OnInit {
public plantForm: FormGroup;
public isAdd: boolean = false;
constructor() { }
constructor(private fb: FormBuilder, private router: Router, private adminService: AdminService) {
this.plantForm = new FormGroup({});
this.isAdd = false;
}
ngOnInit(): void { ngOnInit(): void {
}
} }
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['inStockFC'];
const categoryValue = this.plantForm.value['categotyFC'];
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("coco",plant);
this.adminService.addPlant(plant)?.subscribe((resp)=>{
})
this.router.navigate(['admin']);
}
}

View File

@ -27,3 +27,4 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<button routerLink="../ajouter">Ajouter une plante </button>

View File

@ -5,7 +5,7 @@ import { environment } from 'src/environments/environment';
import { Plant } from '../models/plant'; import { Plant } from '../models/plant';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root',
}) })
export class AdminService { export class AdminService {
public collection$!: Observable<Plant[]>; public collection$!: Observable<Plant[]>;
@ -37,6 +37,9 @@ export class AdminService {
}) })
} }
addPlant(plant: Plant): Observable<any> | void {
return this.httpClient.post(`${this.apiUrl}`, 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(
tap(() => this.refreshCollection()) tap(() => this.refreshCollection())