From fcea7aad85cd14971f4f7ff3a21bf079cd36f927 Mon Sep 17 00:00:00 2001 From: Mykola Mozghovyi Date: Thu, 5 Dec 2024 20:40:52 +0200 Subject: [PATCH] Regen word data (#122) * + use gpt-4o * + RedoSampleWordCommand --- .../Components/Layout/GameComposite.razor | 2 +- My1kWordsEe/Components/Pages/WordPage.razor | 73 +++++++++++++++---- My1kWordsEe/Program.cs | 1 + My1kWordsEe/Services/Ai/OpenAiClient.cs | 3 +- .../Services/Cqs/RedoSampleWordCommand.cs | 56 ++++++++++++++ 5 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 My1kWordsEe/Services/Cqs/RedoSampleWordCommand.cs diff --git a/My1kWordsEe/Components/Layout/GameComposite.razor b/My1kWordsEe/Components/Layout/GameComposite.razor index 181b6ad..9fef6e3 100644 --- a/My1kWordsEe/Components/Layout/GameComposite.razor +++ b/My1kWordsEe/Components/Layout/GameComposite.razor @@ -105,7 +105,7 @@ else {
- + @* *@
} diff --git a/My1kWordsEe/Components/Pages/WordPage.razor b/My1kWordsEe/Components/Pages/WordPage.razor index a5026e3..a27e5b1 100644 --- a/My1kWordsEe/Components/Pages/WordPage.razor +++ b/My1kWordsEe/Components/Pages/WordPage.razor @@ -22,13 +22,14 @@ @inject AddToFavoritesCommand AddToFavoritesCommand @inject RemoveFromFavoritesCommand RemoveFromFavoritesCommand @inject DeleteSampleSentenceCommand DeleteSampleSentenceCommand +@inject RedoSampleWordCommand RedoSampleWordCommand @code { [Inject] protected PreloadService PreloadService { get; set; } = default!; [Inject] protected ToastService ToastService { get; set; } = default!; - private ConfirmDialog DeleteSampleDialog = default!; + private ConfirmDialog ConfirmDialog = default!; [Parameter] public required string EeWord { get; init; } @@ -46,8 +47,8 @@ } WordMetadata = EeWord.ValidateWord() - ? await ResolveSample() - : Result.Failure("Invalid word"); + ? await ResolveSample() + : Result.Failure("Invalid word"); await base.OnParametersSetAsync(); } @@ -90,7 +91,7 @@ StateHasChanged(); } - private async Task DeleteSample(SampleSentence sampleSentence) + private async Task DeleteSampleSentence(SampleSentence sampleSentence) { var options = new ConfirmDialogOptions { @@ -100,10 +101,10 @@ AutoFocusYesButton = false, }; - var confirmation = await DeleteSampleDialog.ShowAsync( - title: "Are you sure you want to delete this?", - message1: "This will delete the record. Once deleted can not be rolled back.", - message2: "Do you want to proceed?", options); + var confirmation = await ConfirmDialog.ShowAsync( + title: "Are you sure you want to delete this?", + message1: "This will delete the record. Once deleted can not be rolled back.", + message2: "Do you want to proceed?", options); if (confirmation) { @@ -126,6 +127,42 @@ } } + private async Task RedoSample() + { + var options = new ConfirmDialogOptions + { + IsScrollable = true, + Dismissable = true, + IsVerticallyCentered = true, + AutoFocusYesButton = false, + }; + + var confirmation = await ConfirmDialog.ShowAsync( + title: "Is this word described inadequately?", + message1: "This will regenerate the word explanation and all the samples will removed.", + message2: "Do you want to proceed?", options); + + if (confirmation) + { + PreloadService.Show(SpinnerColor.Light, "Saving data..."); + var redoResult = await this.RedoSampleWordCommand.Invoke(Value.EeWord); + if (redoResult.IsSuccess) + { + WordMetadata = redoResult; + StateHasChanged(); + } + else + { + ToastService.Notify(new ToastMessage(ToastType.Warning, redoResult.Error)); + } + PreloadService.Hide(); + } + else + { + ToastService.Notify(new ToastMessage(ToastType.Secondary, $"Redo action canceled.")); + } + } + private bool IsSampleGenerationInProgress = false; private async Task GenerateSample(MouseEventArgs e) @@ -153,9 +190,11 @@ private bool IsLoggedIn => User?.Identity?.IsAuthenticated ?? false; - private bool IsFavorite() => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess && Favorites.Value.Value.Words.ContainsKey(EeWord.ToLower()); + private bool IsFavorite() => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess && + Favorites.Value.Value.Words.ContainsKey(EeWord.ToLower()); - private bool IsFavorite(SampleSentence sentence) => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess && Favorites.Value.Value.Sentences.ContainsKey(sentence.EeSentence.ToLower()); + private bool IsFavorite(SampleSentence sentence) => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && + Favorites.Value.IsSuccess && Favorites.Value.Value.Sentences.ContainsKey(sentence.EeSentence.ToLower()); }