@@ -16,14 +16,15 @@ export class BarreRechercheComponent implements OnInit, OnDestroy{
1616 messageErreur : string ;
1717 recetteSubscription : Subscription ;
1818 isDisabled : boolean ;
19+ recherche : string = "" ;
1920
2021 constructor ( private recettesService : RecettesService ,
2122 private formBuilder : FormBuilder ,
2223 private router : Router ) { }
2324
2425 ngOnInit ( ) : void {
2526 // Récupération des données stockées dans la bdd
26- this . recettesService . getRecettesFromBDD ( ) ;
27+ // this.recettesService.getRecettesFromBDD();
2728 this . initForm ( ) ;
2829 }
2930
@@ -37,32 +38,43 @@ export class BarreRechercheComponent implements OnInit, OnDestroy{
3738 // Méthode exécutée lors du clic sur le bouton de la barre de recherche
3839 onRecherche ( ) {
3940 // Recupération de la recherche
40- const recherche = this . rechercheForm . get ( 'recherche' ) . value ;
41+ this . recherche = this . rechercheForm . get ( 'recherche' ) . value ;
4142
4243 // Vérification que l'utilisateur a saisi quelque chose
43- if ( recherche !== '' ) {
44- console . log ( "Recherche : " + recherche ) ;
45- this . recettesService . setValRecherche ( recherche ) ;
44+ if ( this . recherche !== '' ) {
45+ console . log ( "Recherche : " + this . recherche ) ;
46+ this . recettesService . setValRecherche ( this . recherche ) ;
4647
48+ this . messageErreur = '' ;
4749 // Recherche dans la bdd ou requete via api si pas de résultat
48- this . recettesService . rechercher ( recherche ) ;
50+ this . recettesService . rechercher ( this . recherche ) . then (
51+ ( ) => {
52+ this . rediriger ( this . recettesService . tabNonVide ( ) ) ;
53+ console . log ( "redirection Ok" ) ;
54+ } , ( err ) => {
55+ console . log ( "Erreur : " + err ) ;
56+ }
57+ ) ;
58+
59+ // this.recettesService.getRecettesUpdateListener().toPromise().
60+
4961
50- this . messageErreur = '' ;
5162
5263 // Si au moins un resultat est obtenu (== tableau non vide)
53- if ( this . recettesService . tabNonVide ( ) ) {
54- // redirection vers les resultats de la recherche
55- this . router . navigate ( [ '/' ] ) . then (
56- ( ) => { this . router . navigate ( [ '/results' , recherche ] ) }
57- ) ;
58- }
59- // cas ou pas de résultat (== tableau vide)
60- else {
61- // redirection vers la page 'no-result'
62- this . router . navigate ( [ '/' ] ) . then (
63- ( ) => { this . router . navigate ( [ '/results' , 'no-result' ] ) }
64- ) ;
65- }
64+
65+ // if(this.recettesService.tabNonVide()){
66+ // // redirection vers les resultats de la recherche
67+ // this.router.navigate(['/']).then(
68+ // () => { this.router.navigate(['/results', recherche]) }
69+ // );
70+ // }
71+ // // cas ou pas de résultat (== tableau vide)
72+ // else {
73+ // // redirection vers la page 'no-result'
74+ // this.router.navigate(['/']).then(
75+ // () => { this.router.navigate(['/results', 'no-result']) }
76+ // );
77+ // }
6678
6779 // Réinitialisation de la barre de recherche
6880 this . initForm ( ) ;
@@ -77,6 +89,21 @@ export class BarreRechercheComponent implements OnInit, OnDestroy{
7789
7890 }
7991
92+ rediriger ( val : boolean ) {
93+ if ( val ) {
94+ // redirection vers les resultats de la recherche
95+ this . router . navigate ( [ '/' ] ) . then (
96+ ( ) => { this . router . navigate ( [ '/results' , this . recherche ] ) }
97+ ) ;
98+ }
99+ else {
100+ // redirection vers la page 'no-result'
101+ this . router . navigate ( [ '/' ] ) . then (
102+ ( ) => { this . router . navigate ( [ '/results' , 'no-result' ] ) }
103+ ) ;
104+ }
105+ }
106+
80107 // Bloquer l'accès à plusieurs requêtes pendant 5 secondes
81108 onClick ( ) {
82109 this . isDisabled = true ;
0 commit comments