1+ @using System .Text .Json
2+ @using Avolutions .Baf .Blazor .Account .Extensions
3+ @using Avolutions .Baf .Blazor .Jobs .Abstractions
4+ @using Avolutions .Baf .Blazor .Jobs .Resources
5+ @using Avolutions .Baf .Core .Jobs .Abstractions
6+ @using Avolutions .Baf .Core .Resources
7+ @using Microsoft .AspNetCore .Components .Authorization
8+ @using Microsoft .Extensions .Localization
9+ @using MudBlazor
10+
11+ @inject IJobService JobService
12+ @inject AuthenticationStateProvider Auth
13+
14+ @inject IStringLocalizer <ExecuteJobDialogResources > L
15+ @inject IStringLocalizer <SharedResources > LShared
16+
17+ <MudDialog >
18+ <DialogContent >
19+ @if (_componentType is null )
20+ {
21+ <MudText Typo =" Typo.body1" >
22+ @L ["Content ", Job .Name ]
23+ </MudText >
24+ }
25+ else
26+ {
27+ <MudText Typo =" Typo.body1" >
28+ @L ["ContentWithParameters ", Job .Name ]
29+ </MudText >
30+ <MudElement HtmlTag =" div" Class =" pt-4" >
31+ <DynamicComponent Type =" _componentType" Parameters =" _childParams" />
32+ </MudElement >
33+ }
34+ </DialogContent >
35+ <DialogActions >
36+ <MudButton Color =" Color.Default" OnClick =" @Cancel" >@LShared ["Button.Cancel"]</MudButton >
37+ <MudButton Color =" Color.Primary" Variant =" Variant.Filled" OnClick =" @Execute" >@LShared ["Button.Execute"]</MudButton >
38+ </DialogActions >
39+ </MudDialog >
40+
41+ @code {
42+ [CascadingParameter ] IMudDialogInstance MudDialog { get ; set ; } = null ! ;
43+ [Parameter ] public IJob Job { get ; set ; } = null ! ;
44+
45+ private object _model = null ! ;
46+ private Type ? _componentType ;
47+ private Dictionary <string , object? > _childParams = null ! ;
48+
49+ protected override void OnInitialized ()
50+ {
51+ _model = JsonSerializer .Deserialize (" {}" , Job .ParamType )
52+ ?? throw new InvalidOperationException ($" Cannot create parameter instance for {Job .Key }" );
53+
54+ _componentType = (Job as IJobWithParameterComponent )? .ParameterComponentType ;
55+
56+ _childParams = new Dictionary <string , object ?>
57+ {
58+ [" Model" ] = _model
59+ };
60+ }
61+
62+ private async Task Execute ()
63+ {
64+ var json = JsonSerializer .Serialize (_model , Job .ParamType );
65+ var typed = JsonSerializer .Deserialize (json , Job .ParamType )! ;
66+
67+ var methodInfo = typeof (IJobService ).GetMethod (nameof (IJobService .EnqueueAsync ))! ;
68+ var generic = methodInfo .MakeGenericMethod (Job .ParamType );
69+ var userId = await Auth .GetUserIdAsync ();
70+ var task = (Task )generic .Invoke (JobService , [Job .Key , typed , userId , CancellationToken .None ])! ;
71+ await task ;
72+
73+ MudDialog .Close (DialogResult .Ok (true ));
74+ }
75+
76+ private void Cancel () => MudDialog .Cancel ();
77+ }
0 commit comments