Skip to content

Commit fa758a0

Browse files
committed
feat: add row selection
1 parent 976df82 commit fa758a0

7 files changed

+37
-6
lines changed

src/app/app.component.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
<mat-progress-bar *ngIf="loading$ | async" mode="query"></mat-progress-bar>
77

88
<div class="container">
9-
<app-currency-table class="table" [coins]="items$ | async"></app-currency-table>
9+
<app-currency-table class="table" [coins]="items$ | async" (selected)="onRowSelect($event)"></app-currency-table>
1010
<app-converter [currencies]="coinsList$ | async"
11+
[activeCurrency]="activeCoinId$ | async"
1112
[rate]="currentRate$ | async"
12-
(currencyChanged)="loadRate($event)" >
13+
(currencyChanged)="loadRate($event)">
1314
</app-converter>
1415
</div>
1516
</app-navigation>

src/app/app.component.ts

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Observable } from 'rxjs';
66
import { filter, switchMapTo } from 'rxjs/operators';
77
import { CoinItem } from './models';
88
import { CoinFlags, WsMessageTypes } from './models/web-scoket.types';
9+
import { ID } from '@datorama/akita';
910

1011
@Component({
1112
selector: 'app-root',
@@ -19,6 +20,7 @@ export class AppComponent implements OnInit {
1920
coinsList$: Observable<string[]>;
2021
loading$: Observable<boolean>;
2122
currentRate$: Observable<number>;
23+
activeCoinId$: Observable<ID>;
2224

2325
subscribed = false;
2426

@@ -39,6 +41,7 @@ export class AppComponent implements OnInit {
3941
this.loading$ = this.coinsQuery.selectLoading();
4042
this.coinsList$ = this.coinsQuery.coinNames$;
4143
this.currentRate$ = this.coinsQuery.currentRate$;
44+
this.activeCoinId$ = this.coinsQuery.selectActiveId();
4245

4346

4447
const eventMapper = (message: string): Partial<CoinItem> => {
@@ -78,4 +81,8 @@ export class AppComponent implements OnInit {
7881
loadRate(event: { from: string; to: string }) {
7982
this.coinsService.getRate(event.from, event.to).subscribe();
8083
}
84+
85+
onRowSelect(coinName: string) {
86+
this.coinsService.setSelected(coinName);
87+
}
8188
}

src/app/converter/converter.component.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
1+
import { Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges } from '@angular/core';
22
import { FormBuilder, FormGroup } from '@angular/forms';
33
import { distinctUntilChanged, filter, takeUntil } from 'rxjs/operators';
44
import { combineLatest, merge, Subject } from 'rxjs';
@@ -8,12 +8,14 @@ import { combineLatest, merge, Subject } from 'rxjs';
88
templateUrl: './converter.component.html',
99
styleUrls: ['./converter.component.scss']
1010
})
11-
export class ConverterComponent implements OnInit, OnDestroy {
11+
export class ConverterComponent implements OnInit, OnDestroy, OnChanges {
1212

1313
@Input() currencies: string[];
1414

1515
@Input() rate: number;
1616

17+
@Input() activeCurrency: string;
18+
1719
@Output() currencyChanged = new EventEmitter<{from: string, to: string}>();
1820

1921
form: FormGroup;
@@ -57,4 +59,10 @@ export class ConverterComponent implements OnInit, OnDestroy {
5759
this.destroy$.complete();
5860
}
5961

62+
ngOnChanges(changes: SimpleChanges): void {
63+
if (changes && changes.activeCurrency && changes.activeCurrency.currentValue) {
64+
this.form.get('fromCurrency').setValue(this.activeCurrency);
65+
}
66+
}
67+
6068
}

src/app/currency-table/currency-table.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
</ng-container>
2727

2828
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
29-
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
29+
<tr mat-row *matRowDef="let row; columns: displayedColumns;" (click)="selectRow(row)"></tr>
3030
</table>
3131
</div>

src/app/currency-table/currency-table.component.scss

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@
1515
display: flex;
1616
align-items: center;
1717
}
18+
19+
tr.mat-row:hover {
20+
background: #777;
21+
cursor: pointer
22+
}

src/app/currency-table/currency-table.component.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
22
import { CoinItem } from '../models';
33
import { CoinFlags } from '../models/web-scoket.types';
44

@@ -13,6 +13,12 @@ export class CurrencyTableComponent {
1313

1414
@Input() coins: CoinItem[];
1515

16+
@Output() selected = new EventEmitter<string>();
17+
1618
displayedColumns = ['image', 'name', 'price'];
1719
coinFlags = CoinFlags;
20+
21+
selectRow(row: CoinItem) {
22+
this.selected.emit(row.name);
23+
}
1824
}

src/app/state/coins.service.ts

+4
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ export class CoinsService {
3030
this.coinsStore.update({currentRate: price});
3131
}));
3232
}
33+
34+
setSelected(id: string) {
35+
this.coinsStore.setActive(id);
36+
}
3337
}

0 commit comments

Comments
 (0)