-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter01 Network requests, concurrency.
- Loading branch information
Nikola Mitrovic
authored and
Nikola Mitrovic
committed
Jun 23, 2024
1 parent
a65dd08
commit abeb2e2
Showing
38 changed files
with
2,344 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import { ApplicationConfig } from '@angular/core'; | ||
import { provideRouter } from '@angular/router'; | ||
import { provideHttpClient } from '@angular/common/http'; | ||
|
||
import { routes } from './app.routes'; | ||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async'; | ||
|
||
export const appConfig: ApplicationConfig = { | ||
providers: [provideRouter(routes)] | ||
providers: [ | ||
provideRouter(routes), | ||
provideHttpClient(), provideAnimationsAsync(), provideAnimationsAsync() | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
import { Routes } from '@angular/router'; | ||
import { RecipeDetailsComponent } from './components/recipe-details/recipe-details.component'; | ||
import { RecipesListComponent } from './components/recipes-list/recipes-list.component'; | ||
|
||
export const routes: Routes = []; | ||
export const routes: Routes = [ | ||
{ path: '', component: RecipesListComponent }, | ||
{ path: 'recipes/:id', component: RecipeDetailsComponent }, | ||
]; |
38 changes: 38 additions & 0 deletions
38
Chapter01/network-requests/src/app/components/recipe-details/recipe-details.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@if (recipe) { | ||
<mat-card class="card"> | ||
<mat-card-content> | ||
<div class="card-content"> | ||
<div class="card-info"> | ||
<div> | ||
<h5> | ||
{{ recipe.name }} | ||
</h5> | ||
<p>{{ recipe.description }}</p> | ||
</div> | ||
<mat-chip-set class="ingredients"> | ||
@for (ingredient of recipe.ingredients; track ingredient) { | ||
<mat-chip color="accent" class="ingredient">{{ ingredient }}</mat-chip> | ||
} | ||
</mat-chip-set> | ||
|
||
@if (details) { | ||
<div class="instructions"> | ||
<h5>Details</h5> | ||
<p>Prep time: {{ details.prepTime }}</p> | ||
<p>Cuisine: {{ details.cuisine }}</p> | ||
<p>Diet: {{ details.diet }}</p> | ||
<div> | ||
<p>Calories: {{ details.nutrition.calories }}</p> | ||
<p>Carbs: {{ details.nutrition.carbs }}</p> | ||
<p>Fat: {{ details.nutrition.fat }}</p> | ||
<p>Protein: {{ details.nutrition.protein }}</p> | ||
</div> | ||
</div> | ||
} | ||
</div> | ||
<img mat-card-image class="card-image" [src]="details?.url" [alt]="recipe.name"> | ||
</div> | ||
</mat-card-content> | ||
</mat-card> | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
Chapter01/network-requests/src/app/components/recipe-details/recipe-details.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
.card { | ||
cursor: pointer; | ||
} | ||
|
||
.card-image { | ||
width: 100%; | ||
height: 100%; | ||
object-fit: contain; | ||
} | ||
|
||
.mat-mdc-card-content { | ||
padding: 0; | ||
|
||
&:first-child, &:last-child { | ||
padding: 0; | ||
} | ||
} | ||
|
||
.card-content { | ||
display: flex; | ||
justify-content: space-between; | ||
|
||
img { | ||
width: 50%; | ||
} | ||
} | ||
|
||
.card-info { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
padding: 20px; | ||
} | ||
|
||
.ingredient:nth-child(2n + 1) { | ||
background-color: #F9E49B; | ||
} | ||
|
||
.ingredient:nth-child(2n + 2) { | ||
background-color: #F0A594; | ||
} | ||
|
||
// .ingredient:nth-child(4n + 3) { | ||
// background-color: #7FD8C0; | ||
// } | ||
|
||
// .ingredient:nth-child(4n + 4) { | ||
// background-color: #F6D294; | ||
// } | ||
|
||
// .ingredient:nth-child(4n) { | ||
// background-color: #BDE6FF; | ||
// } |
23 changes: 23 additions & 0 deletions
23
...ter01/network-requests/src/app/components/recipe-details/recipe-details.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { RecipeDetailsComponent } from './recipe-details.component'; | ||
|
||
describe('RecipeDetailsComponent', () => { | ||
let component: RecipeDetailsComponent; | ||
let fixture: ComponentFixture<RecipeDetailsComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [RecipeDetailsComponent] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(RecipeDetailsComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
Chapter01/network-requests/src/app/components/recipe-details/recipe-details.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { MatCardModule } from '@angular/material/card'; | ||
import { MatChipsModule } from '@angular/material/chips'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { RecipesService } from '../../services/recipes.service'; | ||
import { Recipe, RecipeDetails } from '../../types/recipes.type'; | ||
|
||
@Component({ | ||
selector: 'app-recipe-details', | ||
standalone: true, | ||
imports: [MatCardModule, MatChipsModule], | ||
templateUrl: './recipe-details.component.html', | ||
styleUrl: './recipe-details.component.scss' | ||
}) | ||
export class RecipeDetailsComponent implements OnInit { | ||
private id = ''; | ||
public recipe: Recipe | null = null; | ||
public details: RecipeDetails | null = null; | ||
|
||
constructor(private route: ActivatedRoute, private recipesService: RecipesService) { } | ||
|
||
ngOnInit(): void { | ||
this.id = this.route.snapshot.paramMap.get('id') || ''; | ||
this.recipesService.getRecipeDetails$(+this.id).subscribe({ | ||
next: ({ recipe, details }) => { | ||
this.recipe = recipe; | ||
this.details = details; | ||
}, | ||
error: (error) => { | ||
console.error(error); | ||
} | ||
}); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
Chapter01/network-requests/src/app/components/recipe-item/recipe-item.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<mat-card class="card"> | ||
<mat-card-content> | ||
<div class="card-content"> | ||
<div class="card-info"> | ||
<div> | ||
<h5> | ||
{{ recipe()?.name }} | ||
</h5> | ||
<p>{{ recipe()?.description }}</p> | ||
</div> | ||
<mat-chip-set class="ingredients"> | ||
@for (ingredient of recipe()?.ingredients; track ingredient) { | ||
<mat-chip color="accent" class="ingredient">{{ ingredient }}</mat-chip> | ||
} | ||
</mat-chip-set> | ||
</div> | ||
@if (image()) { | ||
<img mat-card-image class="card-image" [src]="image()" [alt]="recipe()?.name"> | ||
} @else { | ||
<img mat-card-image class="card-image" src="/assets/images/image-placeholder.png"> | ||
} | ||
</div> | ||
</mat-card-content> | ||
</mat-card> | ||
|
53 changes: 53 additions & 0 deletions
53
Chapter01/network-requests/src/app/components/recipe-item/recipe-item.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
.card { | ||
cursor: pointer; | ||
} | ||
|
||
.card-image { | ||
width: 100%; | ||
height: 100%; | ||
object-fit: contain; | ||
} | ||
|
||
.mat-mdc-card-content { | ||
padding: 0; | ||
|
||
&:first-child, &:last-child { | ||
padding: 0; | ||
} | ||
} | ||
|
||
.card-content { | ||
display: flex; | ||
justify-content: space-between; | ||
|
||
img { | ||
width: 50%; | ||
} | ||
} | ||
|
||
.card-info { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
padding: 20px; | ||
} | ||
|
||
.ingredient:nth-child(2n + 1) { | ||
background-color: #F9E49B; | ||
} | ||
|
||
.ingredient:nth-child(2n + 2) { | ||
background-color: #F0A594; | ||
} | ||
|
||
// .ingredient:nth-child(4n + 3) { | ||
// background-color: #7FD8C0; | ||
// } | ||
|
||
// .ingredient:nth-child(4n + 4) { | ||
// background-color: #F6D294; | ||
// } | ||
|
||
// .ingredient:nth-child(4n) { | ||
// background-color: #BDE6FF; | ||
// } |
23 changes: 23 additions & 0 deletions
23
Chapter01/network-requests/src/app/components/recipe-item/recipe-item.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { RecipeItemComponent } from './recipe-item.component'; | ||
|
||
describe('RecipeItemComponent', () => { | ||
let component: RecipeItemComponent; | ||
let fixture: ComponentFixture<RecipeItemComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [RecipeItemComponent] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(RecipeItemComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
Chapter01/network-requests/src/app/components/recipe-item/recipe-item.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Component, input } from '@angular/core'; | ||
import { MatCardModule } from '@angular/material/card'; | ||
import { MatChipsModule } from '@angular/material/chips'; | ||
import { Recipe } from '../../types/recipes.type'; | ||
|
||
@Component({ | ||
selector: 'app-recipe-item', | ||
standalone: true, | ||
imports: [MatCardModule, MatChipsModule], | ||
templateUrl: './recipe-item.component.html', | ||
styleUrl: './recipe-item.component.scss' | ||
}) | ||
export class RecipeItemComponent { | ||
recipe = input<Recipe>(); | ||
image = input<string>(); | ||
} |
15 changes: 15 additions & 0 deletions
15
Chapter01/network-requests/src/app/components/recipes-list/recipes-list.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@if(recipes) { | ||
<div class="recipes-list"> | ||
@for (item of recipes; track item.id) { | ||
<div class="item" (click)="goToDetailsPage(item.id)"> | ||
<app-recipe-item [recipe]="item" [image]="findRecipeImage(item.id)"></app-recipe-item> | ||
</div> | ||
} | ||
</div> | ||
} | ||
@if(error) { | ||
<div class="error"> | ||
<p>{{ error }}</p> | ||
<p>Recipes list could not be downloaded</p> | ||
</div> | ||
} |
Oops, something went wrong.