-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathSaveSnippetPopup.razor.cs
More file actions
86 lines (70 loc) · 2.42 KB
/
SaveSnippetPopup.razor.cs
File metadata and controls
86 lines (70 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
namespace TryMudBlazor.Client.Components
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MudBlazor;
using Try.Core;
using TryMudBlazor.Client.Models;
using TryMudBlazor.Client.Services;
public partial class SaveSnippetPopup
{
[Inject]
public ISnackbar Snackbar { get; set; }
[Inject]
protected IJsApiService JsApiService { get; set; }
[Inject]
public IJSInProcessRuntime JsRuntime { get; set; }
[Inject]
public SnippetsService SnippetsService { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Parameter]
public bool Visible { get; set; }
[Parameter]
public EventCallback<bool> VisibleChanged { get; set; }
[Parameter]
public IEnumerable<CodeFile> CodeFiles { get; set; } = Enumerable.Empty<CodeFile>();
[Parameter]
public Action UpdateActiveCodeFileContentAction { get; set; }
private bool Loading { get; set; }
private string SnippetLink { get; set; }
private async Task CopyLinkToClipboard()
{
await JsApiService.CopyToClipboardAsync(SnippetLink);
}
private async Task SaveAsync()
{
Loading = true;
try
{
this.UpdateActiveCodeFileContentAction?.Invoke();
var snippetId = await this.SnippetsService.SaveSnippetAsync(this.CodeFiles);
var urlBuilder = new UriBuilder(this.NavigationManager.BaseUri) { Path = $"snippet/{snippetId}" };
this.SnippetLink = urlBuilder.Uri.ToString();
this.JsRuntime.InvokeVoid(Try.ChangeDisplayUrl, SnippetLink);
}
catch (InvalidOperationException ex)
{
Snackbar.Add(ex.Message, Severity.Error);
}
catch (Exception)
{
Snackbar.Add("Error while saving snippet. Please try again later.", Severity.Error);
}
finally
{
Loading = false;
}
}
private async Task OnClose()
{
Loading = false;
SnippetLink = string.Empty;
await VisibleChanged.InvokeAsync(false);
}
}
}