mirror of
https://github.com/Shchoholiev/shopping-assistant-web-client.git
synced 2025-04-12 01:48:49 +00:00
add chat query
This commit is contained in:
parent
e5496ccbfa
commit
f1a06726ef
@ -1,8 +1,10 @@
|
|||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using ShoppingAssistantWebClient.Web.Models;
|
using ShoppingAssistantWebClient.Web.Models.ProductSearch;
|
||||||
|
using ShoppingAssistantWebClient.Web.Models.Input;
|
||||||
using GraphQL;
|
using GraphQL;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using ShoppingAssistantWebClient.Web.Network;
|
using ShoppingAssistantWebClient.Web.Network;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
|
||||||
namespace ShoppingAssistantWebClient.Web.Pages
|
namespace ShoppingAssistantWebClient.Web.Pages
|
||||||
@ -14,6 +16,9 @@ namespace ShoppingAssistantWebClient.Web.Pages
|
|||||||
private ApiClient _apiClient { get; set; }
|
private ApiClient _apiClient { get; set; }
|
||||||
[Inject]
|
[Inject]
|
||||||
private NavigationManager Navigation { get; set; }
|
private NavigationManager Navigation { get; set; }
|
||||||
|
private MessageCreateDto messageCreateDto;
|
||||||
|
|
||||||
|
private CancellationTokenSource cancelTokenSource;
|
||||||
|
|
||||||
private string inputValue = "";
|
private string inputValue = "";
|
||||||
public bool isLoading = true;
|
public bool isLoading = true;
|
||||||
@ -21,36 +26,61 @@ namespace ShoppingAssistantWebClient.Web.Pages
|
|||||||
|
|
||||||
private async Task CreateNewChat() {
|
private async Task CreateNewChat() {
|
||||||
|
|
||||||
if(inputValue!=""){
|
try
|
||||||
|
|
||||||
var type = selectedChoice;
|
|
||||||
|
|
||||||
var firstMessageText= inputValue;
|
|
||||||
var request = new GraphQLRequest
|
|
||||||
{
|
{
|
||||||
Query = @"mutation StartPersonalWishlist($type: String!, $firstMessageText: String!) {
|
if (string.IsNullOrWhiteSpace(inputValue))
|
||||||
startPersonalWishlist(dto: { type: $type, firstMessageText: $firstMessageText }) {
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
",
|
|
||||||
|
|
||||||
Variables = new
|
|
||||||
{
|
{
|
||||||
type,
|
return;
|
||||||
firstMessageText
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
var response = await _apiClient.QueryAsync(request);
|
isLoading = true;
|
||||||
var responseData = response.Data;
|
messageCreateDto = new MessageCreateDto { Text = inputValue };
|
||||||
var chat_id = responseData.startPersonalWishlist.id;
|
var type = selectedChoice;
|
||||||
var url = $"/chat/{chat_id}";
|
var firstMessageText = $"[Question] What are you looking for? [Suggestions] " + inputValue;
|
||||||
Navigation.NavigateTo(url);
|
|
||||||
|
|
||||||
}
|
var request = new GraphQLRequest
|
||||||
|
{
|
||||||
|
Query = @"
|
||||||
|
mutation StartPersonalWishlist($type: String!, $firstMessageText: String!) {
|
||||||
|
startPersonalWishlist(dto: { type: $type, firstMessageText: $firstMessageText }) {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}",
|
||||||
|
Variables = new
|
||||||
|
{
|
||||||
|
type,
|
||||||
|
firstMessageText
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var response = await _apiClient.QueryAsync(request);
|
||||||
|
var responseData = response.Data;
|
||||||
|
var chatId = responseData?.startPersonalWishlist?.id;
|
||||||
|
isLoading = false;
|
||||||
|
var url = $"/chat/{chatId}";
|
||||||
|
|
||||||
|
cancelTokenSource = new CancellationTokenSource();
|
||||||
|
var cancellationToken = cancelTokenSource.Token;
|
||||||
|
|
||||||
|
var serverSentEvent = _apiClient.GetServerSentEventStreamed($"ProductsSearch/search/{chatId}", messageCreateDto, cancellationToken);
|
||||||
|
Navigation.NavigateTo(url);
|
||||||
|
await foreach (var sseEvent in serverSentEvent.WithCancellation(cancellationToken))
|
||||||
|
{
|
||||||
|
// Handle each ServerSentEvent as needed
|
||||||
|
Console.WriteLine($"Received SSE Event: {sseEvent.Event}, Data: {sseEvent.Data}");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Handle exceptions appropriately
|
||||||
|
Console.WriteLine($"Error in CreateNewChat: {ex.Message}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
isLoading = false;
|
||||||
|
cancelTokenSource?.Dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ namespace ShoppingAssistantWebClient.Web.Shared
|
|||||||
private ApiClient _apiClient { get; set; }
|
private ApiClient _apiClient { get; set; }
|
||||||
public List<Wishlist> Wishlists { get; set; }
|
public List<Wishlist> Wishlists { get; set; }
|
||||||
public bool isLoading = true;
|
public bool isLoading = true;
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
await LoadMenus();
|
await LoadMenus();
|
||||||
@ -35,7 +36,7 @@ namespace ShoppingAssistantWebClient.Web.Shared
|
|||||||
Variables = new
|
Variables = new
|
||||||
{
|
{
|
||||||
pageNumber,
|
pageNumber,
|
||||||
pageSize = 10,
|
pageSize = 40,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user