Skip to content

Commit

Permalink
add unsubscribe in converter component
Browse files Browse the repository at this point in the history
  • Loading branch information
Kseniia Chepur committed Jan 14, 2025
1 parent e85f5a6 commit 47bd41a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { HeaderComponent } from "./components/header/header.component";
import { CurrencyConverterComponent } from "./components/currency-converter/currency-converter.component";

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, HeaderComponent, CurrencyConverterComponent],
imports: [HeaderComponent, CurrencyConverterComponent],
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
})
export class AppComponent {
title = 'currency-coverter_angular';
}
export class AppComponent {}

Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { Component, inject } from '@angular/core';
import {MatInputModule} from '@angular/material/input';
import {MatFormFieldModule} from '@angular/material/form-field';
import { FormsModule} from '@angular/forms';
import {MatSelectModule} from '@angular/material/select';
import { Component, DestroyRef, inject } from '@angular/core';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { FormsModule } from '@angular/forms';
import { MatSelectModule } from '@angular/material/select';
import { CommonModule } from '@angular/common';
import { Currencies } from '../../enums/currencies';
import { CurrencyConverterService } from '../../services/currency-converter.service';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';

@Component({
selector: 'app-currency-converter',
standalone: true,
imports: [CommonModule, FormsModule, MatFormFieldModule, MatInputModule, MatSelectModule],
imports: [
CommonModule,
FormsModule,
MatFormFieldModule,
MatInputModule,
MatSelectModule,
],
templateUrl: './currency-converter.component.html',
styleUrl: './currency-converter.component.scss'
styleUrl: './currency-converter.component.scss',
})
export class CurrencyConverterComponent {
private currencyConverterService = inject(CurrencyConverterService);
Expand All @@ -22,11 +29,16 @@ export class CurrencyConverterComponent {
fromAmount: number = 0;
toAmount: number = 0;

private destroyRef = inject(DestroyRef);

convertCurrency() {
if (this.fromCurrency === this.toCurrency) {
this.toAmount = this.fromAmount;
} else {
this.currencyConverterService.convertCurrency(this.fromCurrency, this.toCurrency, this.fromAmount).subscribe({
this.currencyConverterService
.convertCurrency(this.fromCurrency, this.toCurrency, this.fromAmount)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: (result) => {
this.toAmount = Math.round(result * 10000) / 10000;
},
Expand All @@ -39,7 +51,9 @@ export class CurrencyConverterComponent {
if (this.fromCurrency === this.toCurrency) {
this.fromAmount = this.toAmount;
} else {
this.currencyConverterService.convertCurrency(this.toCurrency, this.fromCurrency, this.toAmount).subscribe({
this.currencyConverterService
.convertCurrency(this.toCurrency, this.fromCurrency, this.toAmount)
.subscribe({
next: (result) => {
this.fromAmount = Math.round(result * 10000) / 10000;
},
Expand Down

0 comments on commit 47bd41a

Please sign in to comment.