Skip to content

Commit d646c3a

Browse files
committed
feat: Refactor investment data handling to use signals for reactive updates in components
1 parent 743d4a6 commit d646c3a

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<app-header></app-header>
22
<app-user-input (calculate)="onCalculate($event)"></app-user-input>
3-
<app-investment-results [annualData]="annualData"></app-investment-results>
3+
<app-investment-results [annualData]="annualData()"></app-investment-results>

04-essentials-practice/src/app/app.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component, signal } from '@angular/core';
22
import { HeaderComponent } from './header/header.component';
33
import { UserInputComponent } from "./user-input/user-input.component";
44
import { investmentData, UserInput } from './user-input/user-input.interface';
@@ -17,13 +17,13 @@ import { InvestmentResultsComponent } from "./investment-results/investment-resu
1717
})
1818

1919
export class AppComponent {
20-
annualData?: investmentData[]
20+
annualData = signal<investmentData[] | undefined>(undefined);
2121

2222
constructor(
2323
protected userInputService: UserInputService
2424
) { }
2525

2626
onCalculate(data: UserInput) {
27-
this.annualData = this.userInputService.calculateInvestmentResults(data);
27+
this.annualData.set(this.userInputService.calculateInvestmentResults(data));
2828
}
2929
}

04-essentials-practice/src/app/investment-results/investment-results.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if (!annualData) {
1+
@if (!annualData()) {
22
<p class="center">Please enter some values and press "Calculate".</p>
33
} @else {
44
<table>
@@ -12,7 +12,7 @@
1212
</tr>
1313
</thead>
1414
<tbody>
15-
<tr *ngFor="let data of annualData">
15+
<tr *ngFor="let data of annualData()">
1616
<td>{{ data.year }}</td>
1717
<td>{{ data.valueEndOfYear | currency:'MYR':true }}</td>
1818
<td>{{ data.interest | currency:'MYR':true }}</td>

04-essentials-practice/src/app/investment-results/investment-results.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input } from '@angular/core';
1+
import { Component, Input, input } from '@angular/core';
22
import { investmentData } from '../user-input/user-input.interface';
33
import { CommonModule } from '@angular/common';
44

@@ -12,5 +12,5 @@ import { CommonModule } from '@angular/common';
1212
styleUrl: './investment-results.component.css'
1313
})
1414
export class InvestmentResultsComponent {
15-
@Input() annualData?: investmentData[];
15+
annualData = input<investmentData[] | undefined>(undefined)
1616
}
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Component, EventEmitter, Output } from '@angular/core';
1+
import { Component, output, signal } from '@angular/core';
22
import { FormsModule } from '@angular/forms';
3-
import { UserInputService } from './user-input.service';
43
import { UserInput } from './user-input.interface';
54

65
@Component({
@@ -13,18 +12,23 @@ import { UserInput } from './user-input.interface';
1312
styleUrl: './user-input.component.css'
1413
})
1514
export class UserInputComponent {
16-
initialInvestment : string = '';
17-
annualInvestment : string = '';
18-
expectedReturn : string = '';
19-
duration : string = '';
20-
@Output() calculate = new EventEmitter<UserInput>();
15+
initialInvestment = signal<string>('');
16+
annualInvestment = signal<string>('');
17+
expectedReturn = signal<string>('');
18+
duration = signal<string>('');
19+
calculate = output<UserInput>();
2120

2221
onSubmit() {
2322
this.calculate.emit({
24-
initialInvestment: +this.initialInvestment,
25-
annualInvestment: +this.annualInvestment,
26-
expectedReturn: +this.expectedReturn,
27-
duration: +this.duration
23+
initialInvestment: +this.initialInvestment(),
24+
annualInvestment: +this.annualInvestment(),
25+
expectedReturn: +this.expectedReturn(),
26+
duration: +this.duration()
2827
})
28+
29+
this.initialInvestment.set('');
30+
this.annualInvestment.set('');
31+
this.expectedReturn.set('');
32+
this.duration.set('');
2933
}
3034
}

0 commit comments

Comments
 (0)