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

View File

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

View File

@ -6,6 +6,7 @@ using ShoppingAssistantWebClient.Web.Network;
using ShoppingAssistantWebClient.Web.Models.Input;
using ShoppingAssistantWebClient.Web.Models.Enums;
using System.Text.RegularExpressions;
using ShoppingAssistantWebClient.Web.Services;
namespace ShoppingAssistantWebClient.Web.Pages;
public partial class Chat : ComponentBase
@ -15,8 +16,12 @@ public partial class Chat : ComponentBase
private ApiClient _apiClient { get; set; }
[Inject]
private NavigationManager Navigation { get; set; }
[Inject]
private SearchService _searchServise { 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 Messages Message { get; set; }
@ -118,52 +123,52 @@ public partial class Chat : ComponentBase
{
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){
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;
}
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();
StateHasChanged();
}else if(sseEvent.Event == SearchEventType.Product){
} else if(sseEvent.Event == SearchEventType.Product){
var url = $"/chat/{chatId}/product";
Navigation.NavigateTo(url);
Products.Add(result);
}else if(sseEvent.Event == SearchEventType.Suggestion){
} else if(sseEvent.Event == SearchEventType.Suggestion){
Suggestion.Add(sseEvent.Data);
Suggestion.Add(sseEvent.Data);
}
}
}catch(Exception ex){
Console.WriteLine($"Error : {ex.Message}");
if(Products != null) {
string n = name;
_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 ShoppingAssistantWebClient.Web.Configurations;
using ShoppingAssistantWebClient.Web.Data;
using ShoppingAssistantWebClient.Web.Network;
using ShoppingAssistantWebClient.Web.Network;
using ShoppingAssistantWebClient.Web.Services;
var builder = WebApplication.CreateBuilder(args);
@ -11,6 +12,7 @@ builder.Services.AddServerSideBlazor();
builder.Services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddApiClient(builder.Configuration);
builder.Services.AddSingleton<SearchService>();
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" : "")">
<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_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>
}
}
@ -149,8 +149,8 @@
var url = $"/";
Navigation.NavigateTo(url);
}
private void RedirectToCard(string itemId) {
var url = $"/chat/{itemId}/cart";
private void RedirectToCart(string itemId) {
var url = $"/cart/{itemId}";
Navigation.NavigateTo(url);
}
private async void DeleteWishlist(string itemId) {

View File

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

View File

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