mirror of
https://github.com/Shchoholiev/shopping-assistant-web-client.git
synced 2025-04-04 16:49:36 +00:00
Merge remote-tracking branch 'origin/develop' into SA-106-wishlists-cart-pages
This commit is contained in:
commit
bfb63ecffd
@ -76,7 +76,6 @@
|
||||
}
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
}
|
||||
|
||||
@ -84,11 +83,10 @@
|
||||
|
||||
|
||||
<div class="chat_input">
|
||||
<input @bind="inputValue" @onkeydown="Enter" class="input_messages" type="text" id="chatInput"
|
||||
placeholder="Describe what you are looking for....">
|
||||
<img @onclick="AddNewMessage" class="button_sende" src="/images/send.svg" alt="Send message">
|
||||
<input @onkeydown="Enter" @oninput="InputChanged" class="input_messages" type="text" id="chatInput"
|
||||
placeholder="Describe what you are looking for...." autocomplete="off">
|
||||
<img @onclick="() => AddNewMessage(inputValue)" class="button_sende" src="/images/send.svg" alt="Send message">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
@ -102,7 +100,13 @@
|
||||
|
||||
}
|
||||
};
|
||||
window.clearInput = () => {
|
||||
document.getElementById('chatInput').value = '';
|
||||
};
|
||||
|
||||
function myJavaScriptFunction(wishlistId) {
|
||||
UpdateMenu(wishlistId);
|
||||
}
|
||||
document.getElementById('button_open').addEventListener('click', changetyle);
|
||||
|
||||
</script>
|
||||
@ -110,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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,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;
|
||||
|
@ -7,8 +7,11 @@ using ShoppingAssistantWebClient.Web.Models.Input;
|
||||
using ShoppingAssistantWebClient.Web.Models.Enums;
|
||||
using System.Text.RegularExpressions;
|
||||
using ShoppingAssistantWebClient.Web.Services;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace ShoppingAssistantWebClient.Web.Pages;
|
||||
|
||||
|
||||
public partial class Chat : ComponentBase
|
||||
{
|
||||
|
||||
@ -21,21 +24,59 @@ public partial class Chat : ComponentBase
|
||||
|
||||
public List<Messages> Messages { get; set; }
|
||||
|
||||
|
||||
public List<String> Products { get; set; } = new List<string>();
|
||||
|
||||
public List<String> Suggestion { get; set; } = new List<String>();
|
||||
|
||||
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()
|
||||
{
|
||||
try{
|
||||
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();
|
||||
}
|
||||
}catch(Exception ex){
|
||||
Console.WriteLine($"Error OnInitializedAsync: {ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -100,17 +141,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<String>();
|
||||
Products = new List<String>();
|
||||
Messages.Add(Message);
|
||||
StateHasChanged();
|
||||
|
||||
@ -120,6 +168,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}");
|
||||
@ -131,21 +188,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();
|
||||
@ -160,21 +211,21 @@ public partial class Chat : ComponentBase
|
||||
|
||||
} else if(sseEvent.Event == SearchEventType.Suggestion){
|
||||
|
||||
Suggestion.Add(sseEvent.Data);
|
||||
Suggestion.Add(result);
|
||||
}
|
||||
|
||||
}
|
||||
if(Products.Any()) {
|
||||
if(Products.Count!=0) {
|
||||
string n = name;
|
||||
_searchServise.SetProducts(Products);
|
||||
Products = null;
|
||||
var url = $"/cards/{name}/{chatId}";
|
||||
Navigation.NavigateTo(url);
|
||||
}
|
||||
|
||||
isWaitingForResponse = false;
|
||||
} catch(Exception ex){
|
||||
Console.WriteLine($"Error : {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,6 @@
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@if(isLoading == false){
|
||||
|
||||
<div class="new_chat">
|
||||
<div class="chat_message">
|
||||
<div class="title_one_frame">New chat</div>
|
||||
@ -52,23 +50,13 @@
|
||||
|
||||
</div>
|
||||
<div class="chat_input">
|
||||
<input @bind="inputValue" class="input_messages" type="text" id="chatInput"
|
||||
placeholder="Describe what you are looking for....">
|
||||
<input @bind="inputValue" @onkeydown="Enter" @oninput="InputChanged" class="input_messages" type="text" id="chatInput"
|
||||
placeholder="Describe what you are looking for...." autocomplete="off">
|
||||
<img @onclick="CreateNewChat" class="button_sende" src="/images/send.svg" alt="Send message">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
}else{
|
||||
|
||||
<div class="new_chat">
|
||||
|
||||
<img class="loading" src="/images/loading.svg" alt="Loading chat">
|
||||
|
||||
</div>
|
||||
|
||||
}
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
@ -133,4 +121,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,44 +59,12 @@ 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
|
||||
|
||||
}
|
||||
};
|
||||
_searchServise.SetFirstMassage(inputValue);
|
||||
await UpdateSideMenu(wishlistId);
|
||||
|
||||
response = await _apiClient.QueryAsync(request);
|
||||
|
||||
isLoading = false;
|
||||
StateHasChanged();
|
||||
|
||||
await UpdateSideMenu(wishlistId1);
|
||||
var url = $"/chat/{chatId}";
|
||||
Navigation.NavigateTo(url);
|
||||
|
||||
@ -109,11 +74,7 @@ namespace ShoppingAssistantWebClient.Web.Pages
|
||||
// Handle exceptions appropriately
|
||||
Console.WriteLine($"Error in CreateNewChat: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
isLoading = false;
|
||||
cancelTokenSource?.Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user