diff --git a/ShoppingAssistantWebClient.Web/Pages/Chat.razor b/ShoppingAssistantWebClient.Web/Pages/Chat.razor index 1c24cfe..09d53f3 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor @@ -85,7 +85,7 @@
- Send message + Send message
@@ -101,12 +101,6 @@ } }; - function myJavaScriptFunction(wishlistId) { - - UpdateMenu(wishlistId); - - } - document.getElementById('button_open').addEventListener('click', changetyle); @@ -123,7 +117,7 @@ { if (e.Code == "Enter" || e.Code == "NumpadEnter") { - AddNewMessage(inputValue); + AddNewMessage(); } } @@ -140,11 +134,4 @@ inputValue = item; } - private async Task UpdateSideMenu(string wishlistId) - { - - await JSRuntime.InvokeVoidAsync("myJavaScriptFunction", wishlistId); - - } - } \ No newline at end of file diff --git a/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs b/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs index 656b2c9..282567e 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs @@ -37,47 +37,12 @@ public partial class Chat : ComponentBase private string name = ""; protected override async Task OnInitializedAsync() { - var input = _searchServise.firstMassage; - - if (input!=null){ - await LoadMessages(); - - await AddNewMessage(input); - - string wishlistId = chatId; - var request = new GraphQLRequest - { - Query = @"mutation GenerateNameForPersonalWishlist($wishlistId: String!) { - generateNameForPersonalWishlist(wishlistId: $wishlistId) { - id - name - } - }", - Variables = new - { - wishlistId - - } - }; - - var response = await _apiClient.QueryAsync(request); - _searchServise.SetFirstMassage(null); - isLoading = false; - await UpdateSideMenu(wishlistId); - StateHasChanged(); - - }else{ - await LoadMessages(); - } - } private async Task LoadMessages() { - - try{ string wishlistId = chatId; @@ -100,29 +65,29 @@ public partial class Chat : ComponentBase name = responseData.personalWishlist.name; - isLoading = true; - int pageNumber = 1; - request = new GraphQLRequest - { - Query = @"query MessagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { - messagesPageFromPersonalWishlist( wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) - { - items { - id - text - role - createdById - } - } - }", - - Variables = new + isLoading = true; + int pageNumber = 1; + request = new GraphQLRequest { - wishlistId, - pageNumber, - pageSize = 200 - } - }; + Query = @"query MessagesPageFromPersonalWishlist($wishlistId: String!, $pageNumber: Int!, $pageSize: Int!) { + messagesPageFromPersonalWishlist( wishlistId: $wishlistId, pageNumber: $pageNumber, pageSize: $pageSize) + { + items { + id + text + role + createdById + } + } + }", + + Variables = new + { + wishlistId, + pageNumber, + pageSize = 200 + } + }; @@ -137,69 +102,53 @@ public partial class Chat : ComponentBase Console.WriteLine($"Error : {ex.Message}"); } } - private async Task AddNewMessage(string inputMessage) + private async Task AddNewMessage() { - - try{ - - messageCreateDto = new MessageCreateDto { Text = inputMessage };; + messageCreateDto = new MessageCreateDto { Text = inputValue };; Message = new Messages(); - Message.Text = inputMessage; + Message.Text = inputValue; Message.Role = "User"; Message.Id = ""; Message.CreatedById = ""; inputValue = ""; Suggestion = new List(); - Products = new List(); Messages.Add(Message); StateHasChanged(); cancelTokenSource = new CancellationTokenSource(); var cancellationToken = cancelTokenSource.Token; - const string animationChars = ".oOo"; - int animationIndex = 0; - var serverSentEvent = _apiClient.GetServerSentEventStreamed($"ProductsSearch/search/{chatId}", messageCreateDto, cancellationToken); bool first = true; - - Message = new Messages(); - Message.Role = "bot"; - Message.Id = ""; - Message.CreatedById = ""; - Message.Text = "Waiting for response"; - Messages.Add(Message); - - - - StateHasChanged(); - await Task.Delay(500); // Adjust delay as needed - animationIndex = (animationIndex + 1) % animationChars.Length; - await foreach (var sseEvent in serverSentEvent.WithCancellation(cancellationToken)) { - Console.WriteLine($"Received SSE Event: {sseEvent.Event}, Data: {sseEvent.Data}"); string input = sseEvent.Data; Regex regex = new Regex("\"(.*?)\""); Match match = regex.Match(input); string result = match.Groups[1].Value; + if(sseEvent.Event == SearchEventType.Message){ + Message = new Messages(); + Message.Text = result; + Message.Role = "bot"; + Message.Id = ""; + Message.CreatedById = ""; + if (first) { - var lengt = Messages.Count(); - Messages[lengt-1].Text = result; + Messages.Add(Message); first = false; } else { var lengt = Messages.Count(); - Messages[lengt-1].Text += result; - } + Messages[lengt-1].Text += Message.Text; + } StateHasChanged(); @@ -213,9 +162,7 @@ public partial class Chat : ComponentBase } } - - - if(Products.Count!=0) { + if(Products != null) { string n = name; _searchServise.SetProducts(Products); var url = $"/cards/{name}/{chatId}"; diff --git a/ShoppingAssistantWebClient.Web/Pages/Index.razor b/ShoppingAssistantWebClient.Web/Pages/Index.razor index 1d81c33..2dc266f 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Index.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Index.razor @@ -11,6 +11,8 @@ +@if(isLoading == false){ +
New chat
@@ -57,6 +59,15 @@
+}else{ + +
+ + Loading chat + +
+ +}
@@ -93,6 +104,13 @@ } + + + + + + + document.getElementById('choose_gift').addEventListener('click', switchGift); document.getElementById('choose_product').addEventListener('click', switchProduct); diff --git a/ShoppingAssistantWebClient.Web/Pages/Index.razor.cs b/ShoppingAssistantWebClient.Web/Pages/Index.razor.cs index f6a3c2c..2cc2915 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Index.razor.cs +++ b/ShoppingAssistantWebClient.Web/Pages/Index.razor.cs @@ -7,7 +7,6 @@ using Newtonsoft.Json; using ShoppingAssistantWebClient.Web.Network; using System; using Microsoft.JSInterop; -using ShoppingAssistantWebClient.Web.Services; namespace ShoppingAssistantWebClient.Web.Pages { @@ -21,10 +20,14 @@ namespace ShoppingAssistantWebClient.Web.Pages private NavigationManager Navigation { get; set; } [Inject] protected IJSRuntime JSRuntime { get; set; } - [Inject] - private SearchService _searchServise { get; set; } + private MessageCreateDto messageCreateDto; + + private CancellationTokenSource cancelTokenSource; + private string inputValue = ""; + public bool isLoading; + private async Task CreateNewChat() { @@ -35,6 +38,9 @@ namespace ShoppingAssistantWebClient.Web.Pages return; } + isLoading = true; + StateHasChanged(); + messageCreateDto = new MessageCreateDto { Text = inputValue }; var type = selectedChoice; var firstMessageText = $"What are you looking for?"; @@ -56,19 +62,58 @@ namespace ShoppingAssistantWebClient.Web.Pages var response = await _apiClient.QueryAsync(request); var responseData = response.Data; var chatId = responseData?.startPersonalWishlist?.id; + string wishlistId1 = chatId; + + var text = inputValue; + + cancelTokenSource = new CancellationTokenSource(); + var cancellationToken = cancelTokenSource.Token; + + var serverSentEvent = _apiClient.GetServerSentEventStreamed($"ProductsSearch/search/{chatId}", messageCreateDto, cancellationToken); + + await foreach (var sseEvent in serverSentEvent.WithCancellation(cancellationToken)) + { + // Handle each ServerSentEvent as needed + Console.WriteLine($"Received SSE Event: {sseEvent.Event}, Data: {sseEvent.Data}"); + } + string wishlistId = chatId; - _searchServise.SetFirstMassage(inputValue); - await UpdateSideMenu(wishlistId); + request = new GraphQLRequest + { + Query = @"mutation GenerateNameForPersonalWishlist($wishlistId: String!) { + generateNameForPersonalWishlist(wishlistId: $wishlistId) { + id + name + } + }", + Variables = new + { + wishlistId + + } + }; + + response = await _apiClient.QueryAsync(request); + + isLoading = false; + StateHasChanged(); + + await UpdateSideMenu(wishlistId1); var url = $"/chat/{chatId}"; Navigation.NavigateTo(url); - } - catch (Exception ex){ - - Console.WriteLine($"Error in CreateNewChat: {ex.Message}"); - } - + } + catch (Exception ex) + { + // Handle exceptions appropriately + Console.WriteLine($"Error in CreateNewChat: {ex.Message}"); + } + finally + { + isLoading = false; + cancelTokenSource?.Dispose(); + } } } diff --git a/ShoppingAssistantWebClient.Web/wwwroot/images/loading.svg b/ShoppingAssistantWebClient.Web/wwwroot/images/loading.svg new file mode 100644 index 0000000..4a32def --- /dev/null +++ b/ShoppingAssistantWebClient.Web/wwwroot/images/loading.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + +