From 24e059dcb46b884c2dce878ffaa298a27f4ee110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C2=96=C2=96AsTroLog?= Date: Mon, 20 Nov 2023 09:03:32 +0000 Subject: [PATCH] SA-194 add message entering --- .../Pages/Chat.razor | 17 ++- .../Pages/Chat.razor.cs | 119 ++++++++++-------- .../Pages/Index.razor | 20 ++- 3 files changed, 97 insertions(+), 59 deletions(-) diff --git a/ShoppingAssistantWebClient.Web/Pages/Chat.razor b/ShoppingAssistantWebClient.Web/Pages/Chat.razor index 88b9c82..a104726 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor @@ -76,7 +76,6 @@ } - } @@ -84,11 +83,10 @@
- + Send message
- @@ -102,6 +100,10 @@ } }; + window.clearInput = () => { + // Отримати елемент вводу за його ідентифікатором і обнулити його значення + document.getElementById('chatInput').value = ''; + }; document.getElementById('button_open').addEventListener('click', changetyle); @@ -114,12 +116,19 @@ { await LoadMessages(); } + private void InputChanged(ChangeEventArgs e) + { + inputValue = e.Value.ToString(); + + } public void Enter(KeyboardEventArgs e) { if (e.Code == "Enter" || e.Code == "NumpadEnter") { AddNewMessage(); + + } } diff --git a/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs b/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs index d361622..b66ffc7 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs +++ b/ShoppingAssistantWebClient.Web/Pages/Chat.razor.cs @@ -6,8 +6,11 @@ using ShoppingAssistantWebClient.Web.Network; using ShoppingAssistantWebClient.Web.Models.Input; using ShoppingAssistantWebClient.Web.Models.Enums; using System.Text.RegularExpressions; +using Microsoft.JSInterop; + namespace ShoppingAssistantWebClient.Web.Pages; + public partial class Chat : ComponentBase { @@ -24,6 +27,7 @@ public partial class Chat : ComponentBase private CancellationTokenSource cancelTokenSource; private MessageCreateDto messageCreateDto; + private bool isWaitingForResponse = false; public bool isLoading = true; private string inputValue = ""; private string name = ""; @@ -96,74 +100,83 @@ public partial class Chat : ComponentBase } private async Task AddNewMessage() { - try{ - messageCreateDto = new MessageCreateDto { Text = inputValue };; - Message = new Messages(); - Message.Text = inputValue; - Message.Role = "User"; - Message.Id = ""; - Message.CreatedById = ""; - inputValue = ""; - Suggestion = 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; - - await foreach (var sseEvent in serverSentEvent.WithCancellation(cancellationToken)) + if (!isWaitingForResponse && !string.IsNullOrWhiteSpace(inputValue)) { - Console.WriteLine($"Received SSE Event: {sseEvent.Event}, Data: {sseEvent.Data}"); - - - if(sseEvent.Event == SearchEventType.Message){ - - string input = sseEvent.Data; - Regex regex = new Regex("\"(.*?)\""); - Match match = regex.Match(input); - string result = match.Groups[1].Value; - - + JSRuntime.InvokeVoidAsync("clearInput"); + isWaitingForResponse = true; + try{ + + messageCreateDto = new MessageCreateDto { Text = inputValue };; Message = new Messages(); - Message.Text = result; - Message.Role = "bot"; + Message.Text = inputValue; + Message.Role = "User"; Message.Id = ""; Message.CreatedById = ""; - - if (first) - { - Messages.Add(Message); - first = false; - } - else - { - var lengt = Messages.Count(); - Messages[lengt-1].Text += Message.Text; - } - + inputValue = ""; + Suggestion = new List(); + Messages.Add(Message); StateHasChanged(); - - }else if(sseEvent.Event == SearchEventType.Product){ - var url = $"/chat/{chatId}/product"; - Navigation.NavigateTo(url); + cancelTokenSource = new CancellationTokenSource(); + var cancellationToken = cancelTokenSource.Token; - }else if(sseEvent.Event == SearchEventType.Suggestion){ + var serverSentEvent = _apiClient.GetServerSentEventStreamed($"ProductsSearch/search/{chatId}", messageCreateDto, cancellationToken); + bool first = true; + + await foreach (var sseEvent in serverSentEvent.WithCancellation(cancellationToken)) + { + Console.WriteLine($"Received SSE Event: {sseEvent.Event}, Data: {sseEvent.Data}"); + + + if(sseEvent.Event == SearchEventType.Message){ + + string input = sseEvent.Data; + Regex regex = new Regex("\"(.*?)\""); + Match match = regex.Match(input); + 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; + } + + StateHasChanged(); + + }else if(sseEvent.Event == SearchEventType.Product){ + + var url = $"/chat/{chatId}/product"; + Navigation.NavigateTo(url); + + }else if(sseEvent.Event == SearchEventType.Suggestion){ + + Suggestion.Add(sseEvent.Data); + } - Suggestion.Add(sseEvent.Data); } + isWaitingForResponse = false; + }catch(Exception ex){ + Console.WriteLine($"Error : {ex.Message}"); + } } - }catch(Exception ex){ - Console.WriteLine($"Error : {ex.Message}"); - } - - } } diff --git a/ShoppingAssistantWebClient.Web/Pages/Index.razor b/ShoppingAssistantWebClient.Web/Pages/Index.razor index a3db7bb..2dc266f 100644 --- a/ShoppingAssistantWebClient.Web/Pages/Index.razor +++ b/ShoppingAssistantWebClient.Web/Pages/Index.razor @@ -52,8 +52,8 @@
- + Send message
@@ -133,4 +133,20 @@ } + private void InputChanged(ChangeEventArgs e) + { + // Оновіть значення поля введення при кожному введенні тексту + inputValue = e.Value.ToString(); + } + public void Enter(KeyboardEventArgs e) + { + if (e.Code == "Enter" || e.Code == "NumpadEnter") + { + + if (!string.IsNullOrWhiteSpace(inputValue)) + { + CreateNewChat(); + } + } + } }