diff --git a/.github/workflows/firebase-hosting-merge.yml b/.github/workflows/firebase-hosting-merge.yml index 0579c36..9872577 100644 --- a/.github/workflows/firebase-hosting-merge.yml +++ b/.github/workflows/firebase-hosting-merge.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - run: npm run build + - run: npm install && npm run build - uses: FirebaseExtended/action-hosting-deploy@v0 with: repoToken: ${{ secrets.GITHUB_TOKEN }} diff --git a/src/app/display-recipe/display-recipe.component.ts b/src/app/display-recipe/display-recipe.component.ts index 9fa9447..ba9f254 100644 --- a/src/app/display-recipe/display-recipe.component.ts +++ b/src/app/display-recipe/display-recipe.component.ts @@ -9,20 +9,15 @@ import { RecipeGetterService } from '../recipe-getter.service'; templateUrl: './display-recipe.component.html', styleUrl: './display-recipe.component.scss' }) -export class DisplayRecipeComponent { +export class DisplayRecipeComponent implements OnInit{ data: any constructor(private recipeService: RecipeGetterService){} ngOnInit(): void { - this.recipeService.data$.subscribe(response => { - if (this.data != null){ - this.data = response.body; - } - else{ - this.data = '' - } - }); + this.recipeService.data$.subscribe( + resp => this.data = resp?.body + ) } } diff --git a/src/app/recipe-getter.service.ts b/src/app/recipe-getter.service.ts index 7b8b6a7..f34dfac 100644 --- a/src/app/recipe-getter.service.ts +++ b/src/app/recipe-getter.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; -import { HttpClient, HttpParams } from '@angular/common/http'; -import { Observable, BehaviorSubject } from 'rxjs'; +import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http'; +import { Observable, BehaviorSubject, throwError, catchError } from 'rxjs'; @Injectable({ @@ -12,6 +12,8 @@ export class RecipeGetterService { private dataSubject = new BehaviorSubject(null); data$ = this.dataSubject.asObservable(); + errorMessage : string = "
Error processing request. Please check your url.
" + constructor(private http: HttpClient) {} @@ -19,9 +21,26 @@ export class RecipeGetterService { const apiCallUrl = `${this.backendUrl}/getShortRecipe`; console.log(apiCallUrl) let params = new HttpParams().set('url', url); - const response = this.http.get(apiCallUrl, {params: params}).subscribe(response => { - this.dataSubject.next(response); + const response = this.http.get(apiCallUrl, {params: params}).subscribe({ + next: (data) => { + this.dataSubject.next(data) + }, + error: (error) => { + this.handleSpecificError(error) + } }); } + private handleSpecificError(error: HttpErrorResponse): void { + // Specific error handling + if (error.status === 404) { + console.error('Not Found:', error.message); + } else if (error.status === 500) { + console.error('Server Error:', error.message); + } else { + console.error('Unknown Error:', error.message); + } + this.dataSubject.next({'body':this.errorMessage}) + } + } diff --git a/src/app/url-input/url-input.component.html b/src/app/url-input/url-input.component.html index d824475..7c20b97 100644 --- a/src/app/url-input/url-input.component.html +++ b/src/app/url-input/url-input.component.html @@ -1,5 +1,11 @@ -
- - -
-
+ +
+ + + +
+ URL is required. + Invalid URL format. +
+
+ \ No newline at end of file diff --git a/src/app/url-input/url-input.component.scss b/src/app/url-input/url-input.component.scss index c2ff19d..fd906fe 100644 --- a/src/app/url-input/url-input.component.scss +++ b/src/app/url-input/url-input.component.scss @@ -1,3 +1,6 @@ +.error-message { + position: absolute +} .input-bar { width: 100%; max-width: 700px; @@ -23,13 +26,16 @@ position: absolute; top: 50%; right: 0px; - transform: translateY(-50%); text-emphasis-color: black; - color: #d23636; + color: rgba(3, 145, 25, 0.572); transition: color 0.3s; input:focus + & { color: #007bff; } } + .icon:disabled { + color: #d23636; + } + } \ No newline at end of file diff --git a/src/app/url-input/url-input.component.ts b/src/app/url-input/url-input.component.ts index 05d23c3..bbde15b 100644 --- a/src/app/url-input/url-input.component.ts +++ b/src/app/url-input/url-input.component.ts @@ -1,25 +1,34 @@ import { Component } from '@angular/core'; -import {FormsModule} from '@angular/forms'; +import { CommonModule } from '@angular/common'; + +import {FormBuilder, FormGroup, Validators, ReactiveFormsModule} from '@angular/forms'; import { RecipeGetterService } from '../recipe-getter.service'; @Component({ selector: 'app-url-input', standalone: true, - imports: [FormsModule], + imports: [ReactiveFormsModule, CommonModule], templateUrl: './url-input.component.html', styleUrl: './url-input.component.scss' }) export class UrlInputComponent { - url: string = ''; - + urlForm: FormGroup; + constructor( private recipeService: RecipeGetterService, - ) {} + private fb: FormBuilder + ) { + let pattern = '^(https?:\\/\\/)?([\\da-z.-]+)\\.([a-z.]{2,6})([\\/\\w .-]*)*\\/?$' + this.urlForm = this.fb.group({ + url: ['', [Validators.required, Validators.pattern(pattern)]] + }); + } getRecipe(){ - this.recipeService.getContent(this.url) + let url: string = this.urlForm.get('url')?.value + if(url != null){ + this.recipeService.getContent(url) + } + } - - - }