diff --git a/ShoppingAssistantWebClient.Web/Pages/Chat.razor b/ShoppingAssistantWebClient.Web/Pages/Chat.razor index 88b9c82..dbc9d45 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor @@ -86,7 +86,7 @@
- Send message + Send message
@@ -103,6 +103,12 @@ } }; + function myJavaScriptFunction(wishlistId) { + + UpdateMenu(wishlistId); + + } + document.getElementById('button_open').addEventListener('click', changetyle); @@ -119,7 +125,7 @@ { if (e.Code == "Enter" || e.Code == "NumpadEnter") { - AddNewMessage(); + AddNewMessage(inputValue); } } @@ -136,4 +142,11 @@ 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 0590f6f..865eb1d 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs @@ -34,12 +34,47 @@ 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; @@ -62,29 +97,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 - } + 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 - { - wishlistId, - pageNumber, - pageSize = 200 - } - }; + Variables = new + { + wishlistId, + pageNumber, + pageSize = 200 + } + }; @@ -99,53 +134,69 @@ public partial class Chat : ComponentBase Console.WriteLine($"Error : {ex.Message}"); } } - private async Task AddNewMessage() + private async Task AddNewMessage(string inputMessage) { + + 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(); 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) { - Messages.Add(Message); + var lengt = Messages.Count(); + Messages[lengt-1].Text = result; first = false; } else { var lengt = Messages.Count(); - Messages[lengt-1].Text += Message.Text; - } + Messages[lengt-1].Text += result; + } StateHasChanged(); @@ -159,7 +210,9 @@ public partial class Chat : ComponentBase } } - if(Products != null) { + + + if(Products.Count!=0) { 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 a3db7bb..1fe2e9a 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Index.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Index.razor @@ -11,8 +11,6 @@ -@if(isLoading == false){ -
New chat
@@ -59,15 +57,6 @@
-}else{ - -
- - Loading chat - -
- -}
@@ -104,13 +93,6 @@ } - - - - - - - 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 2cc2915..f6a3c2c 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,14 +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,9 +35,6 @@ namespace ShoppingAssistantWebClient.Web.Pages return; } - isLoading = true; - StateHasChanged(); - messageCreateDto = new MessageCreateDto { Text = inputValue }; var type = selectedChoice; var firstMessageText = $"What are you looking for?"; @@ -62,58 +56,19 @@ 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); - - isLoading = false; - StateHasChanged(); - - await UpdateSideMenu(wishlistId1); + _searchServise.SetFirstMassage(inputValue); + await UpdateSideMenu(wishlistId); 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){ + + Console.WriteLine($"Error in CreateNewChat: {ex.Message}"); + } + } } diff --git a/ShoppingAssistantWebClient.Web/wwwroot/images/loading.svg b/ShoppingAssistantWebClient.Web/wwwroot/images/loading.svg deleted file mode 100644 index 4a32def..0000000 --- a/ShoppingAssistantWebClient.Web/wwwroot/images/loading.svg +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - -