diff --git a/ShoppingAssistantWebClient.Web/Pages/Chat.razor b/ShoppingAssistantWebClient.Web/Pages/Chat.razor index a104726..6554c42 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor @@ -85,7 +85,7 @@
- Send message + Send message
@@ -97,14 +97,16 @@ window.scrollToBottom = function (element) { if (element) { element.scrollTop = 9999; - } }; window.clearInput = () => { - // Отримати елемент вводу за його ідентифікатором і обнулити його значення document.getElementById('chatInput').value = ''; }; + function myJavaScriptFunction(wishlistId) { + UpdateMenu(wishlistId); + } + document.getElementById('button_open').addEventListener('click', changetyle); @@ -123,12 +125,10 @@ } public void Enter(KeyboardEventArgs e) - { + { if (e.Code == "Enter" || e.Code == "NumpadEnter") { - AddNewMessage(); - - + AddNewMessage(inputValue); } } @@ -139,7 +139,12 @@ await JSRuntime.InvokeVoidAsync("scrollToBottom", chatMessageRef); } + private async Task UpdateSideMenu(string wishlistId) + { + await JSRuntime.InvokeVoidAsync("myJavaScriptFunction", wishlistId); + + } private void ClickOption(string item) { inputValue = item; diff --git a/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs b/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs index b66ffc7..39efa86 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs @@ -7,6 +7,7 @@ using ShoppingAssistantWebClient.Web.Models.Input; using ShoppingAssistantWebClient.Web.Models.Enums; using System.Text.RegularExpressions; using Microsoft.JSInterop; +using ShoppingAssistantWebClient.Web.Services; namespace ShoppingAssistantWebClient.Web.Pages; @@ -18,9 +19,11 @@ public partial class Chat : ComponentBase private ApiClient _apiClient { get; set; } [Inject] private NavigationManager Navigation { get; set; } - + [Inject] + private SearchService _searchServise { get; set; } public List Messages { get; set; } public List Suggestion { get; set; } = new List(); + public List Products { get; set; } = new List(); public Messages Message { get; set; } @@ -33,7 +36,39 @@ 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(); + } } @@ -98,7 +133,7 @@ public partial class Chat : ComponentBase Console.WriteLine($"Error : {ex.Message}"); } } - private async Task AddNewMessage() + private async Task AddNewMessage(string inputMessage) { @@ -117,15 +152,31 @@ public partial class Chat : ComponentBase Message.CreatedById = ""; inputValue = ""; Suggestion = new List(); + Products = new List(); Messages.Add(Message); StateHasChanged(); cancelTokenSource = new CancellationTokenSource(); var cancellationToken = cancelTokenSource.Token; + 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 foreach (var sseEvent in serverSentEvent.WithCancellation(cancellationToken)) { Console.WriteLine($"Received SSE Event: {sseEvent.Event}, Data: {sseEvent.Data}"); @@ -139,23 +190,17 @@ public partial class Chat : ComponentBase string result = match.Groups[1].Value; - - Message = new Messages(); - Message.Text = result; - Message.Role = "bot"; - Message.Id = ""; - Message.CreatedById = ""; - - if (first) - { - Messages.Add(Message); - first = false; - } - else - { - var lengt = Messages.Count(); - Messages[lengt-1].Text += Message.Text; - } + if (first) + { + var lengt = Messages.Count(); + Messages[lengt-1].Text = result; + first = false; + } + else + { + var lengt = Messages.Count(); + Messages[lengt-1].Text += result; + } StateHasChanged(); @@ -170,6 +215,13 @@ public partial class Chat : ComponentBase } } + + if(Products.Count!=0) { + string n = name; + _searchServise.SetProducts(Products); + var url = $"/cards/{name}/{chatId}"; + Navigation.NavigateTo(url); + } isWaitingForResponse = false; }catch(Exception ex){ diff --git a/ShoppingAssistantWebClient.Web/Pages/Index.razor b/ShoppingAssistantWebClient.Web/Pages/Index.razor index 2dc266f..b38bfa9 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Index.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Index.razor @@ -11,7 +11,6 @@ -@if(isLoading == false){
@@ -59,17 +58,6 @@
-}else{ - -
- - Loading chat - -
- -} - -