-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathWriteReview.razor
135 lines (109 loc) · 4.42 KB
/
WriteReview.razor
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
@using Azure.Storage.Blobs
@using Azure.Storage.Blobs.Models
@using Azure.Storage.Queues
@using System.Diagnostics.CodeAnalysis;
@using System.Security.Claims;
@inject IJSRuntime JS
@inject ReviewService reviewService
@inject BlobServiceClient blobService
@inject QueueServiceClient queueService
@inject IConfiguration configuration
@inject AuthenticationStateProvider authenticationStateProvider
<div class="modal fade" id="reviewModal" tabindex="-1" role="dialog" aria-hidden="true" aria-modal="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="modalTitleLabel">What did you think about @Product?.Name?</h4>
<button type="button" class="close" data-dismiss="modal" aria-lable="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<textarea class="form-control" rows="10"
placeholder="Leave us a review, we'll ❤️ it and we'll ❤️ you!"
@bind="reviewText" />
@if (showUpload)
{
<InputFile id="hi" OnChange="@LoadFiles" />
}
@if (uploadSuccessful)
{
<p class="bg-success">Uploaded</p>
}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" @onclick="@SaveReview">Save</button>
</div>
</div>
</div>
</div>
@code {
[Parameter]
public Product Product { get; set; }
string reviewText = string.Empty;
string newBlobName = string.Empty;
string loggedInUser = "matt"; // the name matt will eventually be replaced by the logged in user
string containerName = "reviewimages";
bool loadedPhoto = false;
bool showUpload = true;
bool uploadSuccessful = false;
private async Task LoadFiles(InputFileChangeEventArgs e)
{
try
{
var authState = await authenticationStateProvider.GetAuthenticationStateAsync();
if (!authState.User.Identity.IsAuthenticated)
return;
loggedInUser = authState.User.Identity.Name!;
string userId = authState.User.FindFirstValue(ClaimTypes.NameIdentifier)!;
uploadSuccessful = false;
// immediately save to blob storage
var containerClient = blobService.GetBlobContainerClient(containerName);
await containerClient.CreateIfNotExistsAsync(PublicAccessType.Blob);
// just assuming everything is a jpg :)
var imageName = $"{Guid.NewGuid().ToString()}.jpg";
newBlobName = $"{loggedInUser.Replace(" ", "").ToLower()}/{imageName}";
await containerClient.UploadBlobAsync(
newBlobName, e.File.OpenReadStream(maxAllowedSize: 2048000)
);
// create a new message for the queue
var queueClient = queueService.GetQueueClient("review-images");
await queueClient.CreateIfNotExistsAsync();
await queueClient.SendMessageAsync($"{userId} {imageName}");
loadedPhoto = true;
// this is a way for me to know the upload worked
uploadSuccessful = true;
}
catch (Exception ex)
{
Console.WriteLine(ex);
uploadSuccessful = false;
}
finally
{
StateHasChanged();
}
}
async Task SaveReview()
{
var cdn = configuration["Cdn:Endpoint"];
List<string>? photoUrls = new();
if (loadedPhoto)
{
photoUrls.Add($"{cdn}/{containerName}/{newBlobName}");
}
await reviewService.AddReview(reviewText, photoUrls, Product.Id);
// we'll just assume everything worked out great and close the modal
reviewText = string.Empty;
newBlobName = string.Empty;
loadedPhoto = false;
showUpload = false;
uploadSuccessful = false;
// hacky work around to get the upload to clear file name
StateHasChanged();
showUpload = true;
StateHasChanged();
await JS.InvokeVoidAsync("hideModal", "reviewModal");
}
}