diff --git a/ShoppingAssistantWebClient.Web/Pages/Chat.razor b/ShoppingAssistantWebClient.Web/Pages/Chat.razor index 09d53f3..bdef32e 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor @@ -83,9 +83,9 @@
- - Send message + + Send message
@@ -100,7 +100,13 @@ } }; + window.clearInput = () => { + document.getElementById('chatInput').value = ''; + }; + function myJavaScriptFunction(wishlistId) { + UpdateMenu(wishlistId); + } document.getElementById('button_open').addEventListener('click', changetyle); @@ -108,16 +114,24 @@ [Parameter] public string chatId { get; set; } + public string inputValue = ""; + protected override async Task OnParametersSetAsync() { await LoadMessages(); } + private void InputChanged(ChangeEventArgs e) + { + inputValue = e.Value.ToString(); + + } + public void Enter(KeyboardEventArgs e) { if (e.Code == "Enter" || e.Code == "NumpadEnter") { - AddNewMessage(); + AddNewMessage(inputValue); } } @@ -128,7 +142,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 282567e..476cdd1 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs @@ -24,20 +24,53 @@ public partial class Chat : ComponentBase public List Messages { get; set; } + public List Products { get; set; } = new List(); public List Suggestion { get; set; } = new List(); public Messages Message { get; set; } + public Messages MessageBot { get; set; } private CancellationTokenSource cancelTokenSource; - + private bool isWaitingForResponse = false; private MessageCreateDto messageCreateDto; public bool isLoading = true; - private string inputValue = ""; 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(); + } } @@ -102,17 +135,24 @@ public partial class Chat : ComponentBase Console.WriteLine($"Error : {ex.Message}"); } } - private async Task AddNewMessage() + private async Task AddNewMessage(string inputMessage) { + + if (!isWaitingForResponse && !string.IsNullOrWhiteSpace(inputMessage)) + { + JSRuntime.InvokeVoidAsync("clearInput"); + isWaitingForResponse = true; + try{ - messageCreateDto = new MessageCreateDto { Text = inputValue };; + messageCreateDto = new MessageCreateDto { Text = inputMessage };; Message = new Messages(); - Message.Text = inputValue; + Message.Text = inputMessage; Message.Role = "User"; Message.Id = ""; Message.CreatedById = ""; - inputValue = ""; + Suggestion = new List(); + Products = new List(); Messages.Add(Message); StateHasChanged(); @@ -122,6 +162,15 @@ public partial class Chat : ComponentBase var serverSentEvent = _apiClient.GetServerSentEventStreamed($"ProductsSearch/search/{chatId}", messageCreateDto, cancellationToken); bool first = true; + MessageBot = new Messages(); + MessageBot.Role = "bot"; + MessageBot.Id = ""; + MessageBot.CreatedById = ""; + MessageBot.Text = "Waiting for response"; + Messages.Add(MessageBot); + var lengt = Messages.Count(); + StateHasChanged(); + await foreach (var sseEvent in serverSentEvent.WithCancellation(cancellationToken)) { Console.WriteLine($"Received SSE Event: {sseEvent.Event}, Data: {sseEvent.Data}"); @@ -133,21 +182,15 @@ public partial class Chat : ComponentBase if(sseEvent.Event == SearchEventType.Message){ - Message = new Messages(); - Message.Text = result; - Message.Role = "bot"; - Message.Id = ""; - Message.CreatedById = ""; - + if (first) { - Messages.Add(Message); + Messages[lengt-1].Text = result; first = false; } else { - var lengt = Messages.Count(); - Messages[lengt-1].Text += Message.Text; + Messages[lengt-1].Text += result; } StateHasChanged(); @@ -158,20 +201,20 @@ public partial class Chat : ComponentBase } else if(sseEvent.Event == SearchEventType.Suggestion){ - Suggestion.Add(sseEvent.Data); + Suggestion.Add(result); } } - if(Products != null) { + if(Products.Count!=0) { string n = name; _searchServise.SetProducts(Products); var url = $"/cards/{name}/{chatId}"; Navigation.NavigateTo(url); } - + isWaitingForResponse = false; } catch(Exception ex){ Console.WriteLine($"Error : {ex.Message}"); } } - + } } diff --git a/ShoppingAssistantWebClient.Web/Pages/Index.razor b/ShoppingAssistantWebClient.Web/Pages/Index.razor index 2dc266f..2ac3083 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Index.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Index.razor @@ -11,8 +11,6 @@ -@if(isLoading == false){ -
New chat
@@ -58,16 +56,6 @@
- -}else{ - -
- - Loading chat - -
- -} diff --git a/ShoppingAssistantWebClient.Web/Pages/Index.razor.cs b/ShoppingAssistantWebClient.Web/Pages/Index.razor.cs index 2cc2915..fdf4235 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Index.razor.cs +++ b/ShoppingAssistantWebClient.Web/Pages/Index.razor.cs @@ -7,6 +7,7 @@ using Newtonsoft.Json; using ShoppingAssistantWebClient.Web.Network; using System; using Microsoft.JSInterop; +using ShoppingAssistantWebClient.Web.Services; namespace ShoppingAssistantWebClient.Web.Pages { @@ -20,13 +21,10 @@ 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() { @@ -38,7 +36,6 @@ namespace ShoppingAssistantWebClient.Web.Pages return; } - isLoading = true; StateHasChanged(); messageCreateDto = new MessageCreateDto { Text = inputValue }; var type = selectedChoice; @@ -62,58 +59,22 @@ 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; - request = new GraphQLRequest - { - Query = @"mutation GenerateNameForPersonalWishlist($wishlistId: String!) { - generateNameForPersonalWishlist(wishlistId: $wishlistId) { - id - name - } - }", - Variables = new - { - wishlistId - - } - }; - response = await _apiClient.QueryAsync(request); + _searchServise.SetFirstMassage(inputValue); + await UpdateSideMenu(wishlistId); - isLoading = false; - StateHasChanged(); - - await UpdateSideMenu(wishlistId1); var url = $"/chat/{chatId}"; Navigation.NavigateTo(url); - } - catch (Exception ex) - { - // Handle exceptions appropriately - Console.WriteLine($"Error in CreateNewChat: {ex.Message}"); - } - finally - { - isLoading = false; - cancelTokenSource?.Dispose(); - } + } + catch (Exception ex) + { + // Handle exceptions appropriately + Console.WriteLine($"Error in CreateNewChat: {ex.Message}"); + } + } }