Skip to content

Commit c00fcae

Browse files
committed
Solution for lab #15
1 parent b917594 commit c00fcae

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

src/app/currency-renderer.pipe.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { Pipe, PipeTransform } from '@angular/core';
22
import {CurrencyPipe, DecimalPipe} from '@angular/common';
3+
import {CurrencyService} from './currency-switcher/currency.service';
34

45
@Pipe({
5-
name: 'currencyRenderer'
6+
name: 'currencyRenderer',
7+
pure: false
68
})
79
export class CurrencyRendererPipe implements PipeTransform {
810

911
private format = '1.0-2';
1012

11-
transform(value: any, currency: string = 'USD', changeRate: number = 1.0): any {
13+
constructor(private service: CurrencyService) {}
14+
15+
transform(value: any, changeRate: number = 1.0): any {
16+
const currency = this.service.getCurrency();
1217
if (currency != 'EUR'){
1318
return new CurrencyPipe('en-US')
1419
.transform(value * changeRate, currency, 'symbol', this.format);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { CurrencyService } from './currency.service';
4+
5+
describe('CurrencyService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [CurrencyService]
9+
});
10+
});
11+
12+
it('should be created', inject([CurrencyService], (service: CurrencyService) => {
13+
expect(service).toBeTruthy();
14+
}));
15+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Injectable } from '@angular/core';
2+
import {Currency} from './currency';
3+
4+
@Injectable()
5+
export class CurrencyService {
6+
7+
currency: Currency = 'USD';
8+
9+
setCurrency(currency: Currency) {
10+
this.currency = currency;
11+
}
12+
13+
getCurrency(): Currency {
14+
return this.currency;
15+
}
16+
}

src/app/license-plate/license-plate.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ <h2>{{plate.title}} <img src="assets/sale.png" class="sale" *ngIf="plate.onSale"
22
<img src="{{plate.picture}}" class="img-fluid" >
33
<p>{{plate.description}}</p>
44
<div>
5-
<h2 class="float-left">{{plate.price | currencyRenderer:'EUR': 1.40}}</h2>
5+
<h2 class="float-left">{{plate.price | currencyRenderer: 1.40}}</h2>
66
<button class="btn btn-primary float-right" role="button" (click)="alert('Plate added to cart')">
77
{{buttonText}}
88
</button>

src/app/navigation/navigation.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
</li>
1818

1919
</ul>
20+
<app-currency-switcher></app-currency-switcher>
2021
<form class="form-inline my-2 my-lg-0">
2122
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
2223
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
2324
</form>
2425
</div>
26+
2527
</nav>
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-navigation',
55
templateUrl: './navigation.component.html',
66
styleUrls: ['./navigation.component.css']
77
})
8-
export class NavigationComponent implements OnInit {
8+
export class NavigationComponent {
99

1010
constructor() { }
1111

12-
ngOnInit() {
13-
}
14-
1512
}

0 commit comments

Comments
 (0)