add search service and fix some bugs

This commit is contained in:
AndriiSyrotenko 2023-11-20 15:48:48 +00:00
parent 4f984d4136
commit d74e394280
9 changed files with 70 additions and 74 deletions

View File

@ -4,6 +4,7 @@ using ShoppingAssistantWebClient.Web.Network;
using GraphQL; using GraphQL;
using Newtonsoft.Json; using Newtonsoft.Json;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using ShoppingAssistantWebClient.Web.Services;
namespace ShoppingAssistantWebClient.Web.Pages; namespace ShoppingAssistantWebClient.Web.Pages;
@ -12,6 +13,9 @@ public partial class Cards
[Inject] [Inject]
private ApiClient _apiClient { get; set; } private ApiClient _apiClient { get; set; }
[Inject]
SearchService _searchService { get; set; }
private int currentIndex = 0; private int currentIndex = 0;
private int currentProduct = 0; private int currentProduct = 0;
@ -37,6 +41,7 @@ public partial class Cards
//}; //};
public List<Product> Products {get; set;} public List<Product> Products {get; set;}
public List<String> productsNames {get; set;}
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
@ -44,6 +49,7 @@ public partial class Cards
currentImage = Products[currentProduct].ImagesUrls[currentIndex]; currentImage = Products[currentProduct].ImagesUrls[currentIndex];
} }
else { else {
productsNames = _searchService.Products;
currentImage = ""; currentImage = "";
isProductsNull = true; isProductsNull = true;
} }

View File

@ -1,4 +1,4 @@
@page "/cart/{chatId}/{currentWishlistId}" @page "/cart/{currentWishlistId}"
@inject NavigationManager navigationManager; @inject NavigationManager navigationManager;

View File

@ -6,6 +6,7 @@ using ShoppingAssistantWebClient.Web.Network;
using ShoppingAssistantWebClient.Web.Models.Input; using ShoppingAssistantWebClient.Web.Models.Input;
using ShoppingAssistantWebClient.Web.Models.Enums; using ShoppingAssistantWebClient.Web.Models.Enums;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using ShoppingAssistantWebClient.Web.Services;
namespace ShoppingAssistantWebClient.Web.Pages; namespace ShoppingAssistantWebClient.Web.Pages;
public partial class Chat : ComponentBase public partial class Chat : ComponentBase
@ -15,8 +16,12 @@ public partial class Chat : ComponentBase
private ApiClient _apiClient { get; set; } private ApiClient _apiClient { get; set; }
[Inject] [Inject]
private NavigationManager Navigation { get; set; } private NavigationManager Navigation { get; set; }
[Inject]
private SearchService _searchServise { get; set; }
public List<Messages> Messages { get; set; } 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 List<String> Suggestion { get; set; } = new List<String>();
public Messages Message { get; set; } public Messages Message { get; set; }
@ -118,52 +123,52 @@ public partial class Chat : ComponentBase
{ {
Console.WriteLine($"Received SSE Event: {sseEvent.Event}, Data: {sseEvent.Data}"); 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){ if(sseEvent.Event == SearchEventType.Message){
string input = sseEvent.Data; Message = new Messages();
Regex regex = new Regex("\"(.*?)\""); Message.Text = result;
Match match = regex.Match(input); Message.Role = "bot";
string result = match.Groups[1].Value; Message.Id = "";
Message.CreatedById = "";
if (first)
{
Messages.Add(Message);
first = false;
}
else
{
var lengt = Messages.Count();
Messages[lengt-1].Text += Message.Text;
}
Message = new Messages(); StateHasChanged();
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){ } else if(sseEvent.Event == SearchEventType.Product){
var url = $"/chat/{chatId}/product"; Products.Add(result);
Navigation.NavigateTo(url);
}else if(sseEvent.Event == SearchEventType.Suggestion){ } else if(sseEvent.Event == SearchEventType.Suggestion){
Suggestion.Add(sseEvent.Data); Suggestion.Add(sseEvent.Data);
} }
} }
if(Products != null) {
}catch(Exception ex){ string n = name;
Console.WriteLine($"Error : {ex.Message}"); _searchServise.SetProducts(Products);
var url = $"/cards/{name}/{chatId}";
Navigation.NavigateTo(url);
} }
} catch(Exception ex){
Console.WriteLine($"Error : {ex.Message}");
}
} }
} }

View File

@ -1,7 +1,8 @@
using GraphQL.Client.Http; using GraphQL.Client.Http;
using ShoppingAssistantWebClient.Web.Configurations; using ShoppingAssistantWebClient.Web.Configurations;
using ShoppingAssistantWebClient.Web.Data; using ShoppingAssistantWebClient.Web.Data;
using ShoppingAssistantWebClient.Web.Network; using ShoppingAssistantWebClient.Web.Network;
using ShoppingAssistantWebClient.Web.Services;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -11,6 +12,7 @@ builder.Services.AddServerSideBlazor();
builder.Services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; }); builder.Services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
builder.Services.AddSingleton<WeatherForecastService>(); builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddApiClient(builder.Configuration); builder.Services.AddApiClient(builder.Configuration);
builder.Services.AddSingleton<SearchService>();
var app = builder.Build(); var app = builder.Build();

View File

@ -0,0 +1,17 @@
namespace ShoppingAssistantWebClient.Web.Services;
public class SearchService
{
public List<String> Products { get; set; }
public string firstMassage { get; set; }
public void SetProducts(List<String> products) {
Products = products;
}
public void SetFirstMassage(string massage) {
firstMassage = massage;
}
}

View File

@ -1,24 +0,0 @@
.page {
position: relative;
width: 100%;
height: 100vh;
padding: 1.25em;
}
.sidebar-menu {
position: absolute;
width: 20em;
top: 1.25em;
bottom: 1.25em;
margin-right: 1.5em;
transition: 1s;
}
.right-frame {
position: absolute;
right: 1.25em;
left: 23.25em;
top: 1.25em;
bottom: 1.25em;
transition: 1s;
}

View File

@ -40,7 +40,7 @@
<section class="cont_wishlist @(selectedWishlistId == item.Id ? "selected_wishlist" : "")"> <section class="cont_wishlist @(selectedWishlistId == item.Id ? "selected_wishlist" : "")">
<div class="wishlist_name" @onclick="() => RedirectToPage(item.Id)">@item.Name</div> <div class="wishlist_name" @onclick="() => RedirectToPage(item.Id)">@item.Name</div>
<img class="button_delete_chat" @onclick="() => DeleteWishlist(item.Id)" src="/images/icon_delete.svg" alt="Delete wishlist"> <img class="button_delete_chat" @onclick="() => DeleteWishlist(item.Id)" src="/images/icon_delete.svg" alt="Delete wishlist">
<img class="button_open_card" @onclick="() => RedirectToCard(item.Id)" src="/images/icon_open_card.svg" alt="Card open"> <img class="button_open_card" @onclick="() => RedirectToCart(item.Id)" src="/images/icon_open_card.svg" alt="Card open">
</section> </section>
} }
} }
@ -149,8 +149,8 @@
var url = $"/"; var url = $"/";
Navigation.NavigateTo(url); Navigation.NavigateTo(url);
} }
private void RedirectToCard(string itemId) { private void RedirectToCart(string itemId) {
var url = $"/chat/{itemId}/cart"; var url = $"/cart/{itemId}";
Navigation.NavigateTo(url); Navigation.NavigateTo(url);
} }
private async void DeleteWishlist(string itemId) { private async void DeleteWishlist(string itemId) {

View File

@ -17,17 +17,7 @@ namespace ShoppingAssistantWebClient.Web.Shared
public int pageSize { get; set; } public int pageSize { get; set; }
public int currentPage { get; set; } public int currentPage { get; set; }
private readonly ApiClient _apiClient; protected override async Task OnInitializedAsync()
public NavMenu()
{
}
public NavMenu(ApiClient apiClient)
{
_apiClient = apiClient;
}
public async Task OnGetAsync()
{ {
pageSize = 200; pageSize = 200;
currentPage = 1; currentPage = 1;
@ -35,6 +25,7 @@ namespace ShoppingAssistantWebClient.Web.Shared
await LoadMenus(currentPage, pageSize); await LoadMenus(currentPage, pageSize);
} }
public async Task LoadMenus(int pageNumber, int pageSize ) public async Task LoadMenus(int pageNumber, int pageSize )
{ {
try{ try{

View File

@ -3,7 +3,6 @@
width: 100%; width: 100%;
height: 100vh; height: 100vh;
border: 1.5% solid; border: 1.5% solid;
border-color: #edf106;
padding: 1.25em; padding: 1.25em;
} }
@ -23,4 +22,4 @@
top: 1.25em; top: 1.25em;
bottom: 1.25em; bottom: 1.25em;
transition: 1s; transition: 1s;
} }