2022-02-18 16:47:23 +01:00
2022-03-07 16:17:44 +01:00
import { Component , Input , OnInit } from '@angular/core' ;
2022-03-02 12:43:16 +01:00
import { FormArray , FormControl , FormGroup , Validators } from '@angular/forms' ;
2022-02-18 16:47:23 +01:00
import { Router } from '@angular/router' ;
2022-03-07 16:17:44 +01:00
import { Observable , Subscription } from 'rxjs' ;
2022-03-08 14:37:39 +01:00
import { EMPTY_RESTAU , Restaurant } from 'src/app/pages/models/restaurant' ;
2022-02-18 16:47:23 +01:00
import { ApiBackService } from 'src/app/services/api-back.service' ;
@Component ( {
selector : 'app-add-restau' ,
templateUrl : './add-restau.component.html' ,
styleUrls : [ './add-restau.component.scss' ]
} )
export class AddRestauComponent implements OnInit {
public signupForm : FormGroup ;
2022-03-04 15:21:24 +01:00
public errorMessage? : string ;
2022-03-08 14:37:39 +01:00
public listCategories : any [ ] ;
public idRestau : Subscription | undefined ;
public formArrayCheckbox : FormArray ;
public idRestauAModifier? : number = 0 ; // On instancie a 0 ou undefined = on est en CREATION de Restau
2022-03-10 13:14:32 +01:00
public successMessage? : string ;
2022-02-18 16:47:23 +01:00
2022-03-04 15:21:24 +01:00
constructor ( private router : Router , private apiBackService : ApiBackService ) {
2022-02-18 16:47:23 +01:00
this . signupForm = new FormGroup ( { } ) ;
2022-03-08 14:37:39 +01:00
this . listCategories = [ ] ;
this . formArrayCheckbox = new FormArray ( [ ] ) ;
2022-02-18 16:47:23 +01:00
}
ngOnInit ( ) : void {
2022-03-07 16:17:44 +01:00
2022-03-08 14:37:39 +01:00
this . apiBackService . getCategories ( ) . subscribe ( listCateg = > this . listCategories = listCateg ) ;
this . initForm ( EMPTY_RESTAU ) ;
this . formArrayCheckbox = this . signupForm . get ( 'typerestausFc' ) as FormArray ;
2022-03-07 16:17:44 +01:00
2022-03-08 14:37:39 +01:00
// lorsqu'on recoit l'evenement click sur le bouton modif dans le composant "update-del-restau"
// on rentre dans cette souscription
this . idRestau = this . apiBackService . restauAModif . subscribe ( restau = > {
//On enregistre un identifiant de restaurant, preuve qu'on est bien en MODIFICATION
this . idRestauAModifier = restau . id ;
2022-03-10 13:18:57 +01:00
this . cleanCheckbox ( ) ;
2022-03-08 14:37:39 +01:00
if ( restau . typerestaus != undefined && restau . typerestaus != null ) {
for ( let index = 0 ; index < restau . typerestaus . length ; index ++ ) {
// juste l'affichage
const idCheckbox = document . getElementById ( ` categ ${ restau . typerestaus [ index ] . id } ` ) as HTMLInputElement
idCheckbox . checked = true ;
// pour reformer un formArray avec les données récupérées
this . formArrayCheckbox . push ( new FormControl ( { id : ` ${ restau . typerestaus [ index ] . id } ` } ) ) ;
}
}
this . initForm ( restau ) ;
} )
2022-02-18 16:47:23 +01:00
}
2022-03-08 14:37:39 +01:00
public initForm ( restau : Restaurant ) {
2022-03-02 12:43:16 +01:00
2022-03-08 14:37:39 +01:00
this . signupForm = new FormGroup ( {
nomFc : new FormControl ( restau . nom , [ Validators . required ] ) ,
prixFc : new FormControl ( restau . prix ) ,
longitudeFc : new FormControl ( restau . longitude , [ Validators . required , ] ) , // chercher une meilleure regex
latitudeFc : new FormControl ( restau . latitude , [ Validators . required ] ) ,
adresseFc : new FormControl ( restau . adresse , [ Validators . required ] ) ,
telephoneFc : new FormControl ( restau . telephone ) ,
2022-03-10 09:40:46 +01:00
websiteFc : new FormControl ( restau . website , [ Validators . pattern ( "/^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/" ) ] ) , // voir une autre regex
2022-03-08 14:37:39 +01:00
surPlaceFc : new FormControl ( restau . surPlace ) ,
aEmporterFc : new FormControl ( restau . aEmporter ) ,
accesPMRFc : new FormControl ( restau . accesPMR ) ,
typerestausFc : new FormArray ( [ ] )
} )
2022-03-02 12:43:16 +01:00
}
2022-03-08 14:37:39 +01:00
2022-02-18 16:47:23 +01:00
public onSubmit ( ) : void {
console . log ( "value : " , this . signupForm . value ) ;
2022-03-08 14:37:39 +01:00
// console.log("form : ", this.signupForm);
2022-02-18 16:47:23 +01:00
const nomFc = this . signupForm . value [ 'nomFc' ] ;
const prixFc = this . signupForm . value [ 'prixFc' ] ;
const longitudeFc = this . signupForm . value [ 'longitudeFc' ] ;
const latitudeFc = this . signupForm . value [ 'latitudeFc' ] ;
const adresseFc = this . signupForm . value [ 'adresseFc' ] ;
const telephoneFc = this . signupForm . value [ 'telephoneFc' ] ;
const websiteFc = this . signupForm . value [ 'websiteFc' ] ;
const surPlaceFc = this . signupForm . value [ 'surPlaceFc' ] ;
const aEmporterFc = this . signupForm . value [ 'aEmporterFc' ] ;
const accesPMRFc = this . signupForm . value [ 'accesPMRFc' ] ;
2022-03-08 14:37:39 +01:00
const typerestausFc = this . formArrayCheckbox . value ;
2022-03-04 15:21:24 +01:00
2022-03-08 14:37:39 +01:00
// console.log(this.formArray);
2022-03-04 15:21:24 +01:00
2022-02-18 16:47:23 +01:00
const restaurant : Restaurant = {
2022-03-08 14:37:39 +01:00
id : this.idRestauAModifier ,
2022-02-18 16:47:23 +01:00
latitude : latitudeFc ,
longitude : longitudeFc ,
2022-03-04 15:21:24 +01:00
nom : nomFc ,
2022-02-18 16:47:23 +01:00
prix : prixFc ,
2022-03-04 15:21:24 +01:00
adresse : adresseFc ,
telephone : telephoneFc ,
website : websiteFc ,
surPlace : surPlaceFc ,
aEmporter : aEmporterFc ,
accesPMR : accesPMRFc ,
typerestaus : typerestausFc
2022-02-18 16:47:23 +01:00
}
2022-03-02 12:43:16 +01:00
2022-03-04 15:21:24 +01:00
if ( restaurant . latitude !== '' &&
restaurant . longitude !== '' &&
restaurant . nom !== '' &&
restaurant . adresse !== '' ) {
2022-03-08 14:37:39 +01:00
// si l'identifiant du restaurant est rensigné alors on MODIFIE
if ( this . idRestauAModifier != 0 &&
this . idRestauAModifier != null &&
this . idRestauAModifier != undefined ) {
this . apiBackService . modifRestaurant ( restaurant ) . subscribe (
2022-03-10 13:18:57 +01:00
resp = > {
this . idRestauAModifier = 0
2022-03-08 14:37:39 +01:00
// Une fois le retour du subscribe qui est la preuve du bon enregistrement alors
// On reinitialise identifiant restau a 0 pour se remettre en mode CREATION
2022-03-10 13:21:42 +01:00
this . successMessage = "Restaurant modifié !"
2022-03-10 13:18:57 +01:00
this . initForm ( EMPTY_RESTAU ) ;
this . cleanCheckbox ( ) ;
2022-03-08 14:37:39 +01:00
2022-03-10 13:18:57 +01:00
//this.router.navigate(['restaurants'])
2022-03-08 14:37:39 +01:00
}
)
} else { // sinon on crée un restau
this . apiBackService . addRestaurant ( restaurant ) . subscribe (
2022-03-10 13:18:57 +01:00
resp = > {
this . initForm ( EMPTY_RESTAU )
2022-03-10 13:14:32 +01:00
this . successMessage = "Restaurant ajouté !"
2022-03-10 13:20:12 +01:00
this . cleanCheckbox ( )
2022-03-10 13:18:57 +01:00
//this.router.navigate(['restaurants'])
}
2022-03-08 14:37:39 +01:00
) ;
2022-03-10 13:14:32 +01:00
this . initForm ( EMPTY_RESTAU ) ;
2022-03-08 14:37:39 +01:00
}
2022-03-04 15:21:24 +01:00
} else {
2022-02-18 16:47:23 +01:00
this . errorMessage = "Renseigner les champs obligatoires **" ;
}
2022-03-04 15:21:24 +01:00
2022-02-18 16:47:23 +01:00
}
2022-03-08 14:37:39 +01:00
onCheckChange ( event : any ) {
// this.formArrayCheckbox = this.signupForm.get('typerestausFc') as FormArray;
// console.log(this.formArrayCheckbox);
2022-03-04 15:21:24 +01:00
if ( event . target . checked ) {
2022-03-08 14:37:39 +01:00
this . formArrayCheckbox . push ( new FormControl ( { id : event.target.value } ) ) ;
} else {
2022-03-04 15:21:24 +01:00
let i : number = 0 ;
2022-03-08 14:37:39 +01:00
this . formArrayCheckbox . controls . forEach ( ( ctrl ) = > {
2022-03-04 15:21:24 +01:00
if ( ctrl . value [ 'id' ] == event . target . value ) {
2022-03-08 14:37:39 +01:00
this . formArrayCheckbox . removeAt ( i ) ;
2022-03-04 15:21:24 +01:00
return ;
}
2022-03-08 14:37:39 +01:00
2022-03-04 15:21:24 +01:00
i ++ ;
} ) ;
}
}
2022-03-10 13:18:57 +01:00
cleanCheckbox ( ) {
// on vide les checkbox si on en a coché avant de cliquer sur une modif
this . formArrayCheckbox . controls = [ ] ;
// on vide (pour l'affichage également)
let inputCategories = document . querySelectorAll ( '#checkboxes li input' ) ;
for ( let i = 0 ; i < inputCategories . length ; i ++ ) {
let input = inputCategories [ i ] as HTMLInputElement
input . checked = false ;
}
}
2022-03-04 15:21:24 +01:00
2022-03-07 16:17:44 +01:00
ngOnDestroy() {
if ( this . idRestau ) {
this . idRestau . unsubscribe ( ) ;
this . idRestau = undefined ;
}
}
2022-02-18 16:47:23 +01:00
}