1
1
@page " /chat"
2
2
@using System .Threading
3
- @using Stl .CommandR
4
3
@using Stl .Fusion .Authentication .Commands
5
4
@inherits MixedStateComponent <Chat .Model , Chat .LocalsModel >
6
5
@inject Session Session
7
- @inject ICommander Commander
8
6
@inject IChatService ChatService
7
+ @inject UICommandRunner CommandRunner
9
8
@inject NavigationManager Navigator
9
+ @inject ILogger <Chat > Log
10
10
11
11
@{
12
12
var locals = MutableState .Value ;
33
33
in another window to see everything is updated in sync in real time.
34
34
</Note >
35
35
36
- <WhenException Title =" Action failed:" Exception =" @locals.Error" />
37
- <WhenException Title =" Update error:" Exception =" @error" />
36
+ <WhenUpdateError Exception =" @error" />
38
37
39
38
<AuthorizeView >
40
39
<Authorized >
110
109
111
110
public bool IsNameModified { get ; set ; }
112
111
public string Message { get ; set ; } = " " ;
113
- public Exception ? Error { get ; set ; }
114
112
}
115
113
116
114
public class Model
148
146
149
147
private async Task SetName ()
150
148
{
151
- ResetError ();
152
149
var locals = MutableState .Value ;
150
+ var command = new EditUserCommand (Session , locals .Name ?? " " );
153
151
try {
154
- var command = new EditUserCommand (Session , locals .Name ?? " " );
155
- if (command .Name ! .Length < 4 )
156
- throw new ApplicationException (" The name should be at least 4 character long." );
157
- await Commander .Call (command );
152
+ await CommandRunner .Run (new LocalCommand (() => {
153
+ if (command .Name ! .Length < 4 )
154
+ throw new ApplicationException (" The name should be at least 4 character long." );
155
+ }), true );
156
+ await CommandRunner .Run (command , true );
158
157
}
159
158
catch (Exception e ) {
160
- SetError ( e );
159
+ Log . LogError ( e , " Error! " );
161
160
}
162
161
}
163
162
164
163
private async Task Post ()
165
164
{
166
- ResetError ();
167
165
try {
168
- if (State .ValueOrDefault ? .ChatUser ? .IsValid != true )
169
- throw new ApplicationException (" Please sign in first." );
170
- await ChatService .Post (new (MutableState .Value .Message , Session ));
166
+ await CommandRunner .Run (new IChatService .PostCommand (MutableState .Value .Message , Session ), true );
171
167
MutableState .Value .Message = " " ;
172
168
MutableState .Invalidate ();
173
169
}
174
170
catch (Exception e ) {
175
- SetError ( e );
171
+ Log . LogError ( e , " Error! " );
176
172
}
177
173
}
178
174
179
175
// Convenience shortcuts
180
176
181
- private void ResetError ()
182
- => SetError (null );
183
-
184
177
private void ResetName (string name )
185
178
{
186
179
MutableState .Value .Name = name ;
187
180
MutableState .Value .IsNameModified = false ;
188
181
MutableState .Invalidate ();
189
182
}
190
-
191
- private void SetError (Exception ? error )
192
- {
193
- MutableState .Value .Error = error ;
194
- MutableState .Invalidate ();
195
- }
196
- }
183
+ }
0 commit comments