diff options
Diffstat (limited to 'public/projects/angular-small-apps/apps/recipes/src/app/components/search-results/search-results.component.ts')
| -rw-r--r-- | public/projects/angular-small-apps/apps/recipes/src/app/components/search-results/search-results.component.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/public/projects/angular-small-apps/apps/recipes/src/app/components/search-results/search-results.component.ts b/public/projects/angular-small-apps/apps/recipes/src/app/components/search-results/search-results.component.ts new file mode 100644 index 0000000..4a77732 --- /dev/null +++ b/public/projects/angular-small-apps/apps/recipes/src/app/components/search-results/search-results.component.ts @@ -0,0 +1,44 @@ +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute, Router } from '@angular/router'; +import { Recipes } from 'src/app/shared/recipes'; +import { SearchService } from 'src/app/shared/services/search.service'; + +@Component({ + templateUrl: './search-results.component.html', + styleUrls: ['./search-results.component.scss'], +}) +export class SearchResultsComponent implements OnInit { + recipes: Recipes[] = []; + query: string = ''; + by: string = ''; + + constructor( + private search: SearchService, + private router: Router, + private route: ActivatedRoute + ) { + this.getRecipes(); + this.route.params.subscribe((param) => { + this.query = param['query']; + this.by = param['by']; + }); + } + + ngOnInit(): void { + if (this.recipes.length === 0) { + const currentURL = this.router.url.replace('%20', ' '); + const byParam = currentURL.match(/(?:by=)(\w+)/i); + const queryParam = currentURL.match(/(?:query=)([a-zA-Z\s]*)/i); + this.query = (queryParam && queryParam[1]) || ''; + this.by = (byParam && byParam[1]) || ''; + + this.search.findResults(this.query, this.by); + } + } + + getRecipes(): void { + this.search.getResults().subscribe((recipes) => { + this.recipes = recipes; + }); + } +} |
