Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0b03a8d
task3-update-balance-navbar Issue Found
ddipole Aug 8, 2025
83499cc
Update navbar.component.ts
ddipole Aug 12, 2025
1db2d53
task3-update-balance-navbar Add bug in update balance
ddipole Aug 12, 2025
47289ff
Update navbar.component.ts
ddipole Aug 12, 2025
d836da8
Update navbar.component.ts
ddipole Aug 12, 2025
d93874f
Update navbar.component.ts
ddipole Aug 13, 2025
ec1a5f5
task3-update-balance-navbar Add New test case
ddipole Aug 18, 2025
5c8b67c
task3-update-balance-navbar Working Test cases with new navbar component
ddipole Aug 21, 2025
aa58eb5
Update navbar.component.html
ddipole Aug 21, 2025
af0eae2
Update send-money.component.spec.ts
ddipole Aug 21, 2025
29a7ada
Update navbar-balance.component.ts
ddipole Aug 21, 2025
c1fe07a
Update navbar-balance.component.ts
ddipole Aug 21, 2025
64aac3f
Update send-money.component.spec.ts
ddipole Aug 21, 2025
70ae0b9
task3-update-balance-navbar-bug-V1 Add Bug related navbar component
ddipole Aug 21, 2025
624b255
task3-update-balance-navbar-bug-V1 Add module import
ddipole Aug 26, 2025
46e123b
task3-update-balance-navbar-bug-V1 Add some new testcases related new
ddipole Aug 26, 2025
82da9dc
task3-update-balance-navbar-bug-V1 Fix bug 0 value
ddipole Aug 27, 2025
506ea2b
task3-update-balance-navbar-bug-V1 Changes for balance update
ddipole Aug 27, 2025
691c192
task3-update-balance-navbar-bug-V1 Add Question
ddipole Aug 28, 2025
9c426b1
task3-update-balance-navbar-bug-V1 Add new test cases for
ddipole Aug 28, 2025
368a469
Update send-money.component.spec.ts
ddipole Aug 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { balanceReducer } from './state/balance.reducer';
import { RewardPointsComponent } from './reward-points/reward-points.component';
import {AuthInterceptor} from "./services/auth.interceptor";
import { ApplyLoanComponent } from './apply-loan/apply-loan.component';

import { NavbarBalanceComponent } from './components/navbar/navbar-balance.component';

@NgModule({
declarations: [
Expand All @@ -45,6 +45,7 @@ import { ApplyLoanComponent } from './apply-loan/apply-loan.component';
ProfileComponent,
RewardPointsComponent,
ApplyLoanComponent,
NavbarBalanceComponent
],
imports: [
BrowserModule,
Expand Down
63 changes: 63 additions & 0 deletions frontend/src/app/components/navbar/navbar-balance.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Component } from "@angular/core";
import { MenuList, Account } from "src/app/dto/types";
import { menuList } from "src/app/menu";
import { AuthenticationService } from "src/app/services/authentication.service";
import { Store } from "@ngrx/store";
import { updateBalance } from "src/app/state/balance.actions";
import { DarkThemeSelectorService } from "src/app/services/themeToggle.service";
import { Observable } from "rxjs";
import { selectBalance, selectBalanceLoaded } from "src/app/state/balance.selectors";
import { skip, take, map, filter, withLatestFrom } from "rxjs/operators";

@Component({
selector: "app-navbar-balance",
template: `
<span
id="balance-display"
[ngStyle]="{ 'color': getBalanceColor((balance$ | async) || 0) }"
>
Balance: $ {{ numberFormat((balance$ | async) || 0) }}
</span>
`,
})
export class NavbarBalanceComponent {
public menuList: MenuList[] = menuList;
public isAuth = false;
public account?: Account;
public balance$!: Observable<number>;

constructor(
private authenticationService: AuthenticationService,
private store: Store,
protected darkThemeSelectorService: DarkThemeSelectorService,
) {
this.authenticationService.isAuthenticate().subscribe((status: boolean) => {
this.isAuth = status;
});

this.authenticationService.account().subscribe((account: Account) => {
this.account = account;
this.store.dispatch(updateBalance({ balance: Number(account.balance) || 0 }));
});

this.balance$ = this.store.select(selectBalance).pipe(
withLatestFrom(this.store.select(selectBalanceLoaded)),
filter(([_, loaded]: [number, boolean]) => loaded),
map(([value]) => value),
take(1)
);
}

numberFormat(value: any): string {
if (!value) return "0.00";
return value;
}

getBalanceColor(balance: any): string {
const value = Number(this.numberFormat(balance));
if (value === 0) return "pink";
if (value > 0 && value >= 500) return "orange";
if (value > 500) return "green";
return "black";
}
}
10 changes: 5 additions & 5 deletions frontend/src/app/components/navbar/navbar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class="text-primary-blue cursor-pointer text-xl leading-none px-3 py-1 border border-solid border-transparent rounded bg-transparent block lg:hidden outline-none focus:outline-none"
type="button"
(click)="toggleNavbar()"
aria-label="Open main menu"
>
<span class="sr-only">Open main menu</span>
<svg
Expand All @@ -46,11 +47,10 @@
class="lg:flex lg:flex-grow items-center"
>
<ul class="flex flex-col lg:flex-row list-none ml-auto">
<li *ngIf="balance !== 0" class="nav-item">
<div
class="px-3 py-2 flex items-center text-lg leading-snug text-red-500 font-bold"
>
Balance: $ {{ numberFormat(balance) }}
<!-- Balance moved to its own component -->
<li class="nav-item" *ngIf="isAuth">
<div class="px-3 py-2 flex items-center text-lg leading-snug text-red-500 font-bold">
<app-navbar-balance></app-navbar-balance>
</div>
</li>
<li *ngFor="let menu of menuList" class="nav-item">
Expand Down
31 changes: 14 additions & 17 deletions frontend/src/app/components/navbar/navbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { HttpErrorResponse } from "@angular/common/http";
import { Store } from "@ngrx/store";
import { updateBalance } from "src/app/state/balance.actions";
import { AppTheme, DarkThemeSelectorService } from "src/app/services/themeToggle.service";
import { take } from "rxjs/operators";

@Component({
selector: "app-navbar",
Expand All @@ -21,7 +22,7 @@ export class NavbarComponent {
public currentUrl?: string;
public isAuth: boolean = false;
public account?: Account;
public balance: Number = 0;
public balance!: number | string;
public isDarkMode: boolean = false;
public themeText = '';

Expand All @@ -38,14 +39,20 @@ export class NavbarComponent {
this.authenticationService.isAuthenticate().subscribe((status: boolean) => {
this.isAuth = status;
});
}
getBalance() {
this.store
.select('balance')
.pipe(take(1))
.subscribe((state: any) => {
const val = (typeof state === 'object' && state.data?.balaance !== undefined)
? state.balance
: state;

this.authenticationService.account().subscribe((account: Account) => {
this.account = account;
this.store.dispatch(updateBalance({ balance: Number(account.balance) }));
this.balance = Number(account.balance);
});
this.balance = String(val ?? '');
});
}
getBalance() {}


ngOnInit() {
// Always set to light theme
Expand Down Expand Up @@ -85,14 +92,4 @@ export class NavbarComponent {
},
});
}
numberFormat(value: any) {
if (!value) return "0.00";
if (typeof value === "string") {
return Number(value).toFixed(2);
}
if (typeof value === "number") {
return value.toFixed(2);
}
return "0.00";
}
}
9 changes: 7 additions & 2 deletions frontend/src/app/send-money/send-money.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { updateBalance } from 'src/app/state/balance.actions';
styleUrls: ['./send-money.component.scss']
})
export class SendMoneyComponent {
public resData?: { balance: number };
public isAuth: boolean = false;
public account?: Account;
public accountId!: number
Expand Down Expand Up @@ -92,6 +93,11 @@ export class SendMoneyComponent {
return "";
}

updateBalanceFromResponse() {
const balance = Number(this.account?.balance) - this.sendMoneyForm.get("transferAmount")!.value
this.store.dispatch(updateBalance({ balance: balance }));
}

onSubmit() {
if (this.sendMoneyForm.invalid) {
this.toastr.error("Please fill in all the required fields.");
Expand Down Expand Up @@ -124,8 +130,7 @@ export class SendMoneyComponent {
this.loader = false;
},
complete: () => {
const balance = Number(this.account?.balance) - this.sendMoneyForm.get("transferAmount")!.value
this.store.dispatch(updateBalance({ balance: balance }));
this.updateBalanceFromResponse();

this.loader = false;
this.toastr.success("Money Sent Successfully");
Expand Down
21 changes: 16 additions & 5 deletions frontend/src/app/state/balance.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// balance.reducer.ts
import { createReducer, on } from '@ngrx/store';
import { updateBalance } from './balance.actions';

export const initialState = {
balance: 0,
export interface BalanceState {
value: number;
loaded: boolean;
}

export const initialState: BalanceState = {
value: 0,
loaded: false
};

export const balanceReducer = createReducer(
initialState,
on(updateBalance, (state, { balance }) => ({ ...state, balance })),
);
initialState,
on(updateBalance, (state, { balance }) => ({
...state,
value: balance,
loaded: true
}))
);
15 changes: 15 additions & 0 deletions frontend/src/app/state/balance.selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// balance.selectors.ts
import { createSelector, createFeatureSelector } from '@ngrx/store';
import { BalanceState } from './balance.reducer';

export const selectBalanceState = createFeatureSelector<BalanceState>('balance');

export const selectBalance = createSelector(
selectBalanceState,
(state: BalanceState) => state.value
);

export const selectBalanceLoaded = createSelector(
selectBalanceState,
(state: BalanceState) => state.loaded
);
Loading