Skip to content

Commit 572d30a

Browse files
committed
Added comments to EditPortfolio.razor and Index.razor
1 parent ad2db7f commit 572d30a

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

WebFrontend/Pages/EditPortfolio.razor

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,29 @@
55
@inject IPortfolioService PortfolioService
66
@inject IMatDialogService MatDialogService
77
@inject IMatToaster Toaster
8-
@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager
8+
@inject NavigationManager NavigationManager
99

1010

1111
<style>
12-
.demo-mat-card {
13-
max-width: 400px;
14-
margin-bottom: 2em;
15-
}
16-
1712
.demo-mat-card-content {
1813
padding: 1rem;
1914
}
20-
21-
.clear-margin {
22-
margin: 0px;
23-
}
2415
</style>
2516

2617
<div class="mat-layout-grid">
2718
<div class="mat-layout-grid-inner">
2819
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
2920
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-8">
30-
<MatH5><MatButton Outlined="true" Icon="keyboard_arrow_left" Style="margin-right: 1rem;" OnClick='() => { NavigationManager.NavigateTo($""); }'>Back</MatButton></MatH5>
21+
<MatH5>
22+
<MatButton Outlined="true" Icon="keyboard_arrow_left" Style="margin-right: 1rem;" OnClick='() => { NavigationManager.NavigateTo($""); }'>Back</MatButton>
23+
</MatH5>
3124
<MatCard>
3225
<MatCardContent class="demo-mat-card-content">
3326
<h2>Edit portfolio</h2>
3427
<PortfolioForm
3528
DefaultCurrency="@Currency.Usd"
3629
AvailableCurrencies="@(EnumUtils.GetEnumList<Currency>())"
37-
FormModel="FormModel"
30+
FormModel="_formModel"
3831
Edit="true"
3932
OnSubmitEventHandler="@OnCreateFormSubmitted">
4033
</PortfolioForm>
@@ -48,27 +41,38 @@
4841

4942
@code
5043
{
51-
[Parameter] public int PortfolioId { get; set; }
44+
// ID of the portfolio to be edited
45+
[Parameter]
46+
public int PortfolioId { get; set; }
47+
48+
// model of the form
49+
private readonly PortfolioForm.NewPortfolioModel _formModel = new(Currency.Usd);
5250

53-
public PortfolioForm.NewPortfolioModel FormModel = new(Currency.Usd);
54-
55-
public Portfolio ActivePortfolio;
51+
// currently edited portfolio
52+
private Portfolio _activePortfolio;
5653

5754
protected override void OnInitialized()
5855
{
59-
ActivePortfolio = PortfolioService.GetPortfolio(PortfolioId);
60-
FormModel.Name = ActivePortfolio.Name;
61-
FormModel.Description = ActivePortfolio.Description;
56+
// find the portfolio
57+
_activePortfolio = PortfolioService.GetPortfolio(PortfolioId);
58+
59+
// update the form model
60+
_formModel.Name = _activePortfolio.Name;
61+
_formModel.Description = _activePortfolio.Description;
6262
}
6363

6464
private void OnCreateFormSubmitted(PortfolioForm.NewPortfolioModel formModel)
6565
{
66-
PortfolioService.UpdatePortfolio(ActivePortfolio with {
66+
// update the portfolio
67+
PortfolioService.UpdatePortfolio(_activePortfolio with {
6768
Name = formModel.Name,
6869
Description = formModel.Description
6970
});
71+
// reset the form
7072
formModel.Reset();
7173
Toaster.Add("Portfolio successfully edited", MatToastType.Success, "", "");
72-
NavigationManager.NavigateTo($"/portfolios/{ActivePortfolio.Id}");
74+
75+
// navigate back to the portfolio detail
76+
NavigationManager.NavigateTo($"/portfolios/{_activePortfolio.Id}");
7377
}
7478
}

WebFrontend/Pages/Index.razor

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
@page "/"
2-
@using Model
32
@using Services
43
@using Utils
5-
@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager
4+
@using Model
5+
@inject NavigationManager NavigationManager
66
@inject IPortfolioService PortfolioService
77
@inject IPortfolioEntryService PortfolioEntryService
88
@inject IMatDialogService MatDialogService
@@ -26,33 +26,29 @@
2626
bottom: 1rem;
2727
right: 1rem;
2828
}
29-
30-
.mat-paper {
31-
display: flex;
32-
flex-direction: column;
33-
justify-content: center;
34-
align-items: center;
35-
padding: 1em;
36-
}
37-
3829
</style>
3930
<div class="mat-layout-grid mat-layout-grid-align-center">
4031
<div class="mat-layout-grid-inner center">
4132

4233
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
4334
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-8">
4435
<MatH5>Portfolios</MatH5>
45-
@if (PortfoliosWithEntries == null)
36+
@if (_portfoliosWithEntries == null)
4637
{
4738
<MatProgressBar Indeterminate="true"></MatProgressBar>
4839
}
49-
else if (PortfoliosWithEntries.Count < 1)
40+
else if (_portfoliosWithEntries.Count < 1)
5041
{
51-
<MatPaper Elevation="2"><span>No portfolios were found.</span><div><MatButton Style="margin-top:1em;" Label="Create a new portfolio" OnClick='() => { NavigationManager.NavigateTo($"newportfolio");}'></MatButton></div></MatPaper>
42+
<MatPaper Elevation="2">
43+
<span>No portfolios were found.</span>
44+
<div>
45+
<MatButton Style="margin-top:1em;" Label="Create a new portfolio" OnClick='() => { NavigationManager.NavigateTo($"newportfolio"); }'></MatButton>
46+
</div>
47+
</MatPaper>
5248
}
5349
else
5450
{
55-
@foreach (var portfolioWithEntries in PortfoliosWithEntries)
51+
@foreach (var portfolioWithEntries in _portfoliosWithEntries)
5652
{
5753
<MatCard class="demo-mat-card" OnCli>
5854
<MatCardContent>
@@ -96,12 +92,13 @@
9692
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
9793
</div>
9894
</div>
99-
<MatFAB Class="app-fab--absolute" Icon="@MatIconNames.Add" Label="Add a new portfolio" OnClick='() => { NavigationManager.NavigateTo($"newportfolio");}'></MatFAB>
95+
<MatFAB Class="app-fab--absolute" Icon="@MatIconNames.Add" Label="Add a new portfolio" OnClick='() => { NavigationManager.NavigateTo($"newportfolio"); }'></MatFAB>
10096

10197

10298
@code
10399
{
104-
protected List<Tuple<Portfolio, List<PortfolioEntry>>> PortfoliosWithEntries;
100+
// list of portfolios with entries mapped to them
101+
private List<Tuple<Portfolio, List<PortfolioEntry>>> _portfoliosWithEntries;
105102

106103
protected record PortfolioEntryRow(string symbol, decimal currentPrice, decimal relativeChange, decimal percentage);
107104

@@ -112,7 +109,7 @@
112109

113110
private void LoadPortfolios()
114111
{
115-
PortfoliosWithEntries = PortfolioService.GetPortfolios().Select(
112+
_portfoliosWithEntries = PortfolioService.GetPortfolios().Select(
116113
portfolio => new Tuple<Portfolio, List<PortfolioEntry>>(
117114
portfolio,
118115
PortfolioEntryService.GetPortfolioEntries(portfolio.Id)
@@ -127,11 +124,15 @@
127124

128125
private async void DeletePortfolio(Portfolio portfolio)
129126
{
127+
// let user confirm whether he wants to delete the portfolio
130128
var result = await MatDialogService.ConfirmAsync("Do you really wish to delete this portfolio including all of it's portfolio entries and market orders?");
131129
if (result)
132130
{
131+
// delete portfolio
133132
PortfolioService.DeletePortfolio(portfolio);
133+
// reload the portfolio list
134134
LoadPortfolios();
135+
// refresh the UI
135136
StateHasChanged();
136137
Toaster.Add($"Portfolio \"{portfolio.Name}\" sucessfully deleted", MatToastType.Info, "", "");
137138
}
@@ -141,7 +142,7 @@
141142
{
142143
NavigationManager.NavigateTo($"/newportfolioentry/{portfolio.Id}");
143144
}
144-
145+
145146
private void ViewPortfolio(Portfolio portfolio)
146147
{
147148
NavigationManager.NavigateTo($"/portfolios/{portfolio.Id}");

0 commit comments

Comments
 (0)