diff --git a/package-lock.json b/package-lock.json index cb8d2f2..6231e76 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@angular/platform-browser": "^18.2.3", "@angular/platform-browser-dynamic": "^18.2.3", "@angular/router": "^18.2.3", + "angular-in-memory-web-api": "^0.18.0", "rxjs": "~7.8.0", "tslib": "^2.3.0", "zone.js": "~0.14.3" @@ -4533,6 +4534,19 @@ "ajv": "^8.8.2" } }, + "node_modules/angular-in-memory-web-api": { + "version": "0.18.0", + "resolved": "https://registry.npmjs.org/angular-in-memory-web-api/-/angular-in-memory-web-api-0.18.0.tgz", + "integrity": "sha512-Eqkr9+x3d7K4dmn6Qs3ZVAfqBDPZN0N7Qel5i8eU/pe5r44J/pfxlNW+1LC2Sb2PdENEdvFzC8wx8qly5+kQyQ==", + "dependencies": { + "tslib": "^2.3.0" + }, + "peerDependencies": { + "@angular/common": "^18.0.0", + "@angular/core": "^18.0.0", + "rxjs": "^6.5.3 || ^7.4.0" + } + }, "node_modules/ansi-colors": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.3.tgz", diff --git a/package.json b/package.json index d231758..d2db32a 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@angular/platform-browser": "^18.2.3", "@angular/platform-browser-dynamic": "^18.2.3", "@angular/router": "^18.2.3", + "angular-in-memory-web-api": "^0.18.0", "rxjs": "~7.8.0", "tslib": "^2.3.0", "zone.js": "~0.14.3" diff --git a/src/app/app.component.ts b/src/app/app.component.ts index a9fd26b..ffc14c4 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,18 +1,72 @@ -import { Component } from '@angular/core'; -import {Child1Component} from "./child/child-1/child-1.component"; -import {Child2Component} from "./child/child-2/child-2.component"; -import {ParentComponent} from "./child/parent/parent.component"; +import {Component, OnInit} from '@angular/core'; import {HotelsComponent} from "./hotel/hotels.component"; import {IdkComponent} from "./idek/idk.component"; +import {filter, from, map, reduce} from "rxjs"; +import {RouterOutlet} from "@angular/router"; @Component({ selector: 'app-root', standalone: true, - imports: [Child1Component, Child2Component, ParentComponent, HotelsComponent, IdkComponent], + imports: [HotelsComponent, IdkComponent, RouterOutlet], template: ` - - + + + ` }) -export class AppComponent { +export class AppComponent implements OnInit { + ngOnInit() { + const users = [ + { + name: 'John', + age: 15, + }, + { + name: 'Alice', + age: 16, + }, + { + name: 'Bob', + age: 45, + }, + { + name: 'Eve', + age: 29, + }, + { + name: 'Charlie', + age: 52, + }, + { + name: 'Dave', + age: 41, + }, + { + name: 'Mallory', + age: 37, + }, + { + name: 'Trent', + age: 48, + }, + { + name: 'Peggy', + age: 26, + }, + { + name: 'Victor', + age: 39, + } + ]; + + from(users).pipe( + filter(user => user.age >= 18), + reduce((acc, user) => { + acc.age += user.age + acc.count++; + return acc; + }, {age: 0, count: 0}), + map(data => data.age / data.count), + ).subscribe((data) => {console.log("avg age: ", data)}); + } } diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 82a7b0c..6701c4c 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -1,14 +1,22 @@ -import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; +import {ApplicationConfig, importProvidersFrom, provideZoneChangeDetection} from '@angular/core'; import { provideRouter } from '@angular/router'; import { routes } from './app.routes'; import {registerLocaleData} from "@angular/common"; import localeDe from "@angular/common/locales/de" import localeCn from "@angular/common/locales/en" import localeJap from "@angular/common/locales/en" +import {provideHttpClient} from "@angular/common/http"; +import {InMemoryWebApiModule} from "angular-in-memory-web-api"; +import {HotelDataService} from "./service/HotelData.service"; registerLocaleData(localeDe, 'de-DE') registerLocaleData(localeCn, 'cn-CN') registerLocaleData(localeJap, 'ja-JP') export const appConfig: ApplicationConfig = { - providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes)] + providers: [ + provideZoneChangeDetection({ eventCoalescing: true }), + provideRouter(routes), + provideHttpClient(), + importProvidersFrom(InMemoryWebApiModule.forRoot(HotelDataService)) + ] }; diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index dc39edb..8d67ad2 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,3 +1,16 @@ import { Routes } from '@angular/router'; +import {HotelsComponent} from "./hotel/hotels.component"; +import {HotelComponent} from "./hotel/hotel.component"; -export const routes: Routes = []; +export const routes: Routes = [ + { + path: '/hotels', + component: HotelsComponent, + title: 'Hotels', + }, + { + path: '/hotels/{id}', + component: HotelComponent, + title: 'Hotel', + } +]; diff --git a/src/app/child/child-1/child-1.component.html b/src/app/child/child-1/child-1.component.html deleted file mode 100644 index e69de29..0000000 diff --git a/src/app/child/child-1/child-1.component.ts b/src/app/child/child-1/child-1.component.ts deleted file mode 100644 index 34d85e0..0000000 --- a/src/app/child/child-1/child-1.component.ts +++ /dev/null @@ -1,23 +0,0 @@ -import {Component, EventEmitter, Input, Output} from "@angular/core"; - -@Component({ - selector: 'app-child-1', - standalone: true, - template: ` -

{{balance}}€

- -

No mo money

- ` -}) -export class Child1Component { - @Input() - public balance: number = 1000; - - @Output() - public balanceChange: EventEmitter = new EventEmitter(); - - public minusFifty() { - this.balance -= 50; - this.balanceChange.emit(this.balance); - } -} diff --git a/src/app/child/child-2/child-2.component.html b/src/app/child/child-2/child-2.component.html deleted file mode 100644 index b490223..0000000 --- a/src/app/child/child-2/child-2.component.html +++ /dev/null @@ -1,2 +0,0 @@ -

{{balance}}€

- diff --git a/src/app/child/child-2/child-2.component.ts b/src/app/child/child-2/child-2.component.ts deleted file mode 100644 index 3968785..0000000 --- a/src/app/child/child-2/child-2.component.ts +++ /dev/null @@ -1,23 +0,0 @@ -import {Component, EventEmitter, Input, Output} from "@angular/core"; - -@Component({ - selector: 'app-child-2', - standalone: true, - template: ` -

{{balance}}€

- -

No mo money

- ` -}) -export class Child2Component { - @Input() - public balance: number = 1000; - - @Output() - public balanceChange: EventEmitter = new EventEmitter(); - - public minusFifty() { - this.balance -= 50; - this.balanceChange.emit(this.balance); - } -} diff --git a/src/app/child/parent/parent.component.html b/src/app/child/parent/parent.component.html deleted file mode 100644 index bb7749f..0000000 --- a/src/app/child/parent/parent.component.html +++ /dev/null @@ -1,6 +0,0 @@ -

{{balance}}€

- - - - - diff --git a/src/app/child/parent/parent.component.ts b/src/app/child/parent/parent.component.ts deleted file mode 100644 index 294a8c4..0000000 --- a/src/app/child/parent/parent.component.ts +++ /dev/null @@ -1,24 +0,0 @@ -import {Component} from "@angular/core"; -import {Child1Component} from "../child-1/child-1.component"; -import {Child2Component} from "../child-2/child-2.component"; - -@Component({ - selector: 'app-parent', - standalone: true, - imports: [ - Child1Component, - Child2Component - ], - templateUrl: 'parent.component.html' -}) -export class ParentComponent { - public balance: number = 1000; - - public gibMoney(): void { - this.balance += 100; - } - - public setBalance(balance: number): void { - this.balance = balance; - } -} diff --git a/src/app/currency/currency.component.ts b/src/app/currency/currency.component.ts new file mode 100644 index 0000000..c9b743f --- /dev/null +++ b/src/app/currency/currency.component.ts @@ -0,0 +1,57 @@ +import {Component, EventEmitter, Output} from "@angular/core"; +import {FormsModule, ReactiveFormsModule} from "@angular/forms"; +import {Lang} from "../idek/lang"; + + +@Component({ + standalone: true, + selector: "app-currency", + imports: [ + ReactiveFormsModule, + FormsModule + ], + template: ` + + ` +}) +export class CurrencyComponent { + + @Output() + public currency: EventEmitter = new EventEmitter(); + + public currencies: Lang[] = [ + { + name: 'de', + code: 'de-DE', + currency: 'EUR' + }, + { + name: 'en', + code: 'en-US', + currency: 'USD' + }, + { + name: 'jap', + code: 'ja-JP', + currency: 'JPY' + }, + { + name: 'cn', + code: 'cn-CN', + currency: 'CNY' + } + ] + + public setCurrency(currencyInput: string): void { + for (const currency of this.currencies) { + if (currency.name === currencyInput) { + this.currency.emit(currency); + break; + } + } + } +} diff --git a/src/app/hotel/hotels.component.ts b/src/app/hotel/hotels.component.ts index e1d3f20..4542389 100644 --- a/src/app/hotel/hotels.component.ts +++ b/src/app/hotel/hotels.component.ts @@ -4,81 +4,40 @@ import {Hotel} from "./hotel"; import {FormsModule} from "@angular/forms"; import {Lang} from "../idek/lang"; import {HotelService} from "../service/hotel.service"; +import {Observable} from "rxjs"; +import {AsyncPipe} from "@angular/common"; +import {CurrencyComponent} from "../currency/currency.component"; @Component({ standalone: true, template: ` - +
- @for (hotel of matchingHotels; track hotel.hotelId) { + @for (hotel of (matchingHotels | async); track hotel.hotelId) {
} @empty {

no matching results for {{search}}

} `, - imports: [FormsModule, HotelComponent], + imports: [FormsModule, HotelComponent, AsyncPipe, CurrencyComponent], providers: [HotelService], selector: 'app-hotels' }) export class HotelsComponent { - public search: string = ''; public currency: Lang = {name: 'de', code: 'de-DE', currency: 'EUR'}; - public currencies: Lang[] = [ - { - name: 'de', - code: 'de-DE', - currency: 'EUR' - }, - { - name: 'en', - code: 'en-US', - currency: 'USD' - }, - { - name: 'jap', - code: 'ja-JP', - currency: 'JPY' - }, - { - name: 'cn', - code: 'cn-CN', - currency: 'CNY' - } - ] + public search: string = ''; - public matchingHotels: Hotel[] = []; + private hotelService: HotelService= inject(HotelService); - private hotelService: HotelService = inject(HotelService); - - constructor() { - this.matchingHotels = this.hotelService.getHotels; - } - - public setCurrency(currencyInput: string): void { - for (const currency of this.currencies) { - if (currency.name === currencyInput) { - this.currency = currency; - break; - } - } - } + public matchingHotels: Observable = this.hotelService.getHotels(); public searchEvent(input: string) { this.search = input.toLowerCase(); - this.matchingHotels = [] - this.hotelService.getHotels .forEach((hotel: Hotel) => { - if (hotel.hotelName.toLowerCase().includes(this.search)) { - this.matchingHotels.push(hotel); - } - }) + //this.matchingHotels.pipe(filter((hotel: Hotel) => hotel.hotelName.toLowerCase().includes(input.toLowerCase()))); } } diff --git a/src/app/idek/idk.component.ts b/src/app/idek/idk.component.ts index b73cd34..624215c 100644 --- a/src/app/idek/idk.component.ts +++ b/src/app/idek/idk.component.ts @@ -1,13 +1,12 @@ import {Component} from "@angular/core"; -import {CurrencyPipe, UpperCasePipe} from "@angular/common"; -import {TestPipe} from "./test.pipe"; +import {CurrencyPipe} from "@angular/common"; import {FormsModule} from "@angular/forms"; import {Lang} from "./lang"; @Component({ selector: 'app-idk', standalone: true, - imports: [UpperCasePipe, TestPipe, CurrencyPipe, FormsModule], + imports: [CurrencyPipe, FormsModule], template: `