11
11
@inject ICryptoStatsSource CryptoStatsSource ;
12
12
@inject IMatDialogService MatDialogService
13
13
@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
30
15
31
16
<div class =" mat-layout-grid" >
32
17
<div class =" mat-layout-grid-inner" >
33
18
<div class =" mat-layout-grid-cell-span-2" ></div >
34
19
<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 >
36
21
<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 )
38
23
{
39
24
<MatProgressBar Indeterminate =" true" ></MatProgressBar >
40
25
}
41
26
else
42
27
{
43
- @if (AvailableCryptocurrenciesWithUsage .Count > 0 )
28
+ @if (_availableCryptocurrenciesWithUsage .Count > 0 )
44
29
{
45
- <MatTable Items =" @AvailableCryptocurrenciesWithUsage " Striped =" true" RowClass =" tester" PageSize =" 10"
30
+ <MatTable Items =" @_availableCryptocurrenciesWithUsage " Striped =" true" RowClass =" tester" PageSize =" 10"
46
31
DebounceMilliseconds =" 150" class =" mat-elevation-z5" >
47
32
48
33
<MatTableHeader >
89
74
set
90
75
{
91
76
_cryptocurrencyFilter = value ;
77
+ // when setting the cryptocurrency symbol filter, do filter the list of available cryptos
92
78
FilterCurrenciesBySymbol (value );
93
79
this .StateHasChanged ();
94
80
}
95
81
}
96
82
97
83
private void FilterCurrenciesBySymbol (string value )
98
84
{
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 );
101
88
}
102
89
103
90
private string _cryptocurrencyFilter ;
104
91
105
92
[Parameter ]
106
93
public int PortfolioId { get ; set ; }
107
94
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 ;
114
109
115
110
protected override void OnInitialized ()
116
111
{
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 );
119
117
}
120
118
121
119
protected override async Task OnInitializedAsync ()
122
120
{
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 );
126
125
}
127
126
128
127
private void UpdateAvailableCryptocurrencies (List < Cryptocurrency > availableCryptocurrencies )
129
128
{
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 (
132
133
c => new Tuple <Cryptocurrency , bool >(c , ! entriesSymbols .Contains (c .Symbol .ToLower ()))
133
134
).OrderBy (c => c .Item2 ).ThenBy (c => c .Item1 .Symbol .Length ).ToList ();
134
135
}
135
136
136
137
private void OnAddCurrencyClicked (Cryptocurrency cryptocurrency )
137
138
{
138
- Console . WriteLine ( " OnAddCurrencyClicked " );
139
+ // create a new portfolio entry
139
140
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 );
142
145
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 , " " , " " );
144
149
}
145
150
146
151
private async void OnDeleteCurrencyClicked (Cryptocurrency cryptocurrency )
147
152
{
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
150
154
var result = await MatDialogService .ConfirmAsync ($" Do you really wish to delete {cryptocurrency .Symbol .ToUpper ()} entry including all of it's market entries?" );
151
155
if (result )
152
156
{
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
154
160
PortfolioEntryService .DeletePortfolioEntry (entry );
155
- PortfolioEntries .Remove (entry );
156
- UpdateAvailableCryptocurrencies (FilteredCryptocurrencies );
161
+ _portfolioEntries .Remove (entry );
162
+ // update the UI
163
+ UpdateAvailableCryptocurrencies (_filteredCryptocurrencies );
157
164
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 , " " , " " );
159
166
}
160
167
}
161
168
}
0 commit comments