Skip to content

Commit 7e82ed5

Browse files
committed
Added comments to PortfolioEntryManagement.razor
1 parent a59df29 commit 7e82ed5

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

WebFrontend/Pages/PortfolioEntryManagement.razor

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,23 @@
1111
@inject ICryptoStatsSource CryptoStatsSource;
1212
@inject IMatDialogService MatDialogService
1313
@inject IMatToaster Toaster
14-
@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager
15-
16-
<style>
17-
.demo-mat-card {
18-
max-width: 400px;
19-
margin-bottom: 2em;
20-
}
21-
22-
.demo-mat-card-content {
23-
padding: 1rem;
24-
}
25-
26-
.clear-margin {
27-
margin: 0px;
28-
}
29-
</style>
14+
@inject NavigationManager NavigationManager
3015

3116
<div class="mat-layout-grid">
3217
<div class="mat-layout-grid-inner">
3318
<div class="mat-layout-grid-cell-span-2"></div>
3419
<div class="mat-layout-grid-cell-span-8">
35-
<MatH5><MatButton Outlined="true" Icon="keyboard_arrow_left" Style="margin-right: 1rem;" OnClick='() => { NavigationManager.NavigateTo($"/portfolios/{Portfolio.Id}"); }'>Back to portfolio</MatButton>Manage entries of <b>@Portfolio.Name</b></MatH5>
20+
<MatH5><MatButton Outlined="true" Icon="keyboard_arrow_left" Style="margin-right: 1rem;" OnClick='() => { NavigationManager.NavigateTo($"/portfolios/{_portfolio.Id}"); }'>Back to portfolio</MatButton>Manage entries of <b>@_portfolio.Name</b></MatH5>
3621
<MatTextField Label="Filter by symbol" Style="margin-bottom: 2rem;" Icon="filter_list" FullWidth="true" @bind-Value="@CryptocurrencyFilter"></MatTextField>
37-
@if (AvailableCryptocurrenciesWithUsage == null)
22+
@if (_availableCryptocurrenciesWithUsage == null)
3823
{
3924
<MatProgressBar Indeterminate="true"></MatProgressBar>
4025
}
4126
else
4227
{
43-
@if (AvailableCryptocurrenciesWithUsage.Count > 0)
28+
@if (_availableCryptocurrenciesWithUsage.Count > 0)
4429
{
45-
<MatTable Items="@AvailableCryptocurrenciesWithUsage" Striped="true" RowClass="tester" PageSize="10"
30+
<MatTable Items="@_availableCryptocurrenciesWithUsage" Striped="true" RowClass="tester" PageSize="10"
4631
DebounceMilliseconds="150" class="mat-elevation-z5">
4732

4833
<MatTableHeader>
@@ -89,73 +74,95 @@
8974
set
9075
{
9176
_cryptocurrencyFilter = value;
77+
// when setting the cryptocurrency symbol filter, do filter the list of available cryptos
9278
FilterCurrenciesBySymbol(value);
9379
this.StateHasChanged();
9480
}
9581
}
9682

9783
private void FilterCurrenciesBySymbol(string value)
9884
{
99-
FilteredCryptocurrencies = AvailableCryptocurrencies.FindAll(c => c.Symbol.Contains(value));
100-
UpdateAvailableCryptocurrencies(FilteredCryptocurrencies);
85+
// filter by symbol
86+
_filteredCryptocurrencies = _availableCryptocurrencies.FindAll(c => c.Symbol.Contains(value));
87+
UpdateAvailableCryptocurrencies(_filteredCryptocurrencies);
10188
}
10289

10390
private string _cryptocurrencyFilter;
10491

10592
[Parameter]
10693
public int PortfolioId { get; set; }
10794

108-
protected Portfolio Portfolio;
109-
protected List<PortfolioEntry> PortfolioEntries;
110-
111-
protected List<Cryptocurrency> AvailableCryptocurrencies;
112-
protected List<Cryptocurrency> FilteredCryptocurrencies;
113-
protected List<Tuple<Cryptocurrency, bool>> AvailableCryptocurrenciesWithUsage;
95+
// the portfolio whose entries are being managed
96+
private Portfolio _portfolio;
97+
98+
// existing entries of the portfolio
99+
private List<PortfolioEntry> _portfolioEntries;
100+
101+
// list of available cryptocurrencies
102+
private List<Cryptocurrency> _availableCryptocurrencies;
103+
104+
// list of filtered cryptocurrencies
105+
private List<Cryptocurrency> _filteredCryptocurrencies;
106+
107+
// cryptocurrencies mapped to a flag indicating whether it is present in the portfolio or not
108+
private List<Tuple<Cryptocurrency, bool>> _availableCryptocurrenciesWithUsage;
114109

115110
protected override void OnInitialized()
116111
{
117-
Portfolio = PortfolioService.GetPortfolio(PortfolioId);
118-
PortfolioEntries = PortfolioEntryService.GetPortfolioEntries(PortfolioId);
112+
// load the portfolio using the specified ID
113+
_portfolio = PortfolioService.GetPortfolio(PortfolioId);
114+
115+
// load all entries of the portfolio
116+
_portfolioEntries = PortfolioEntryService.GetPortfolioEntries(PortfolioId);
119117
}
120118

121119
protected override async Task OnInitializedAsync()
122120
{
123-
AvailableCryptocurrencies = await CryptoStatsSource.GetAvailableCryptocurrencies();
124-
FilteredCryptocurrencies = AvailableCryptocurrencies;
125-
UpdateAvailableCryptocurrencies(AvailableCryptocurrencies);
121+
// find all available cryptocurrencies
122+
_availableCryptocurrencies = await CryptoStatsSource.GetAvailableCryptocurrencies();
123+
_filteredCryptocurrencies = _availableCryptocurrencies;
124+
UpdateAvailableCryptocurrencies(_availableCryptocurrencies);
126125
}
127126

128127
private void UpdateAvailableCryptocurrencies(List<Cryptocurrency> availableCryptocurrencies)
129128
{
130-
var entriesSymbols = PortfolioEntries.Select(e => e.Symbol.ToLower());
131-
AvailableCryptocurrenciesWithUsage = availableCryptocurrencies.Select(
129+
var entriesSymbols = _portfolioEntries.Select(e => e.Symbol.ToLower());
130+
// map available cryptocurrencies to a flag indicating whether they are used in the given portfolio
131+
// order by the flag and then by symbol length
132+
_availableCryptocurrenciesWithUsage = availableCryptocurrencies.Select(
132133
c => new Tuple<Cryptocurrency, bool>(c, !entriesSymbols.Contains(c.Symbol.ToLower()))
133134
).OrderBy(c => c.Item2).ThenBy(c => c.Item1.Symbol.Length).ToList();
134135
}
135136

136137
private void OnAddCurrencyClicked(Cryptocurrency cryptocurrency)
137138
{
138-
Console.WriteLine("OnAddCurrencyClicked");
139+
// create a new portfolio entry
139140
var entry = PortfolioEntryService.CreatePortfolioEntry(cryptocurrency.Symbol, PortfolioId);
140-
PortfolioEntries.Add(entry);
141-
UpdateAvailableCryptocurrencies(FilteredCryptocurrencies);
141+
_portfolioEntries.Add(entry);
142+
143+
// update the UI
144+
UpdateAvailableCryptocurrencies(_filteredCryptocurrencies);
142145
StateHasChanged();
143-
Toaster.Add($"{cryptocurrency.Symbol.ToUpper()} entry successfully added to {Portfolio.Name}.", MatToastType.Success, "", "");
146+
147+
// notify the user
148+
Toaster.Add($"{cryptocurrency.Symbol.ToUpper()} entry successfully added to {_portfolio.Name}.", MatToastType.Success, "", "");
144149
}
145150

146151
private async void OnDeleteCurrencyClicked(Cryptocurrency cryptocurrency)
147152
{
148-
Console.WriteLine("OnDeleteCurrencyClicked");
149-
// TODO display confirmation dialog only when entry orders exist
153+
// let user confirm whether he wants to delete the entry
150154
var result = await MatDialogService.ConfirmAsync($"Do you really wish to delete {cryptocurrency.Symbol.ToUpper()} entry including all of it's market entries?");
151155
if (result)
152156
{
153-
var entry = PortfolioEntries.Find(entry => entry.Symbol == cryptocurrency.Symbol);
157+
// find the entry
158+
var entry = _portfolioEntries.Find(entry => entry.Symbol == cryptocurrency.Symbol);
159+
// delete the entry
154160
PortfolioEntryService.DeletePortfolioEntry(entry);
155-
PortfolioEntries.Remove(entry);
156-
UpdateAvailableCryptocurrencies(FilteredCryptocurrencies);
161+
_portfolioEntries.Remove(entry);
162+
// update the UI
163+
UpdateAvailableCryptocurrencies(_filteredCryptocurrencies);
157164
StateHasChanged();
158-
Toaster.Add($"{cryptocurrency.Symbol.ToUpper()} entry successfully deleted from {Portfolio.Name}.", MatToastType.Success, "", "");
165+
Toaster.Add($"{cryptocurrency.Symbol.ToUpper()} entry successfully deleted from {_portfolio.Name}.", MatToastType.Success, "", "");
159166
}
160167
}
161168
}

0 commit comments

Comments
 (0)