From 8aa585f2510b52fc1fc0f030d5bffcf7a3022c34 Mon Sep 17 00:00:00 2001 From: Egamorf Date: Thu, 17 Apr 2025 21:18:40 +0200 Subject: [PATCH] Add js for end text --- .../Pages/Games/Game.razor | 2 +- .../Pages/Games/Game.razor.cs | 31 ++++++------------- .../Components/Search/SearchInput.razor | 6 ++-- .../Components/Search/SearchInput.razor.cs | 11 +++++-- .../Components/Search/SearchInput.razor.js | 19 ++++++++++++ .../GameIdeas.BlazorApp/wwwroot/index.html | 2 +- 6 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor.js diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Game.razor b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Game.razor index d5011b5..369b8bf 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Game.razor +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Game.razor @@ -40,5 +40,5 @@ - + diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Game.razor.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Game.razor.cs index 198eeba..4299b9f 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Game.razor.cs +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Pages/Games/Game.razor.cs @@ -18,12 +18,13 @@ public partial class Game private IEnumerable GamesDto = []; private int CurrentPage; - protected override async Task OnInitializedAsync() + protected override async Task OnAfterRenderAsync(bool firstRender) { CurrentPage = 1; - await HandleFetchDatas(); - await base.OnInitializedAsync(); + await HandleFetchDatas(firstRender); + await base.OnAfterRenderAsync(firstRender); } + private void HandleAddClicked(AddType addType) { switch (addType) @@ -41,13 +42,15 @@ public partial class Game { ManualAddPopup?.Close(); } - private async Task HandleFetchDatas() + private async Task HandleFetchDatas(bool loadCategories = false) { try { IsLoading = true; - Categories = await GameGateway.FetchCategories(); + if (loadCategories) + Categories = await GameGateway.FetchCategories(); + GamesDto = await GameGateway.FetchGames(GameFilter, CurrentPage); } catch (Exception) @@ -62,22 +65,6 @@ public partial class Game private async Task HandleFilterChanged(GameFilterParams args) { GameFilter = args; - - - - try - { - IsLoading = true; - - GamesDto = await GameGateway.FetchGames(GameFilter, CurrentPage); - } - catch (Exception) - { - throw; - } - finally - { - IsLoading = false; - } + await HandleFetchDatas(false); } } \ No newline at end of file diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor index 886e722..1fe279c 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor @@ -3,14 +3,13 @@
@@ -25,5 +24,4 @@ @GetSearchIcon()
- - + \ No newline at end of file diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor.cs b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor.cs index fd0f42e..97a2e29 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor.cs +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor.cs @@ -1,11 +1,13 @@ using GameIdeas.BlazorApp.Shared.Constants; using GameIdeas.Shared.Constants; using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; namespace GameIdeas.BlazorApp.Shared.Components.Search; public partial class SearchInput { + [Inject] private IJSRuntime Js { get; set; } = default!; [Parameter] public string? Text { get; set; } [Parameter] public string? Placeholder { get; set; } [Parameter] public bool IsDisable { get; set; } @@ -17,17 +19,20 @@ public partial class SearchInput private ElementReference InputText; - protected override void OnInitialized() + protected override async Task OnAfterRenderAsync(bool firstRender) { Text = string.Empty; + await Js.InvokeVoidAsync("addStopWriteListener", DotNetObjectReference.Create(this)); + await base.OnAfterRenderAsync(firstRender); } public void SetText(string str) { Text = str; } - - private async Task HandleTextChanged() + + [JSInvokable] + public async Task HandleTextEnd() { await TextChanged.InvokeAsync(Text); } diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor.js b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor.js new file mode 100644 index 0000000..d9ba0d9 --- /dev/null +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/Shared/Components/Search/SearchInput.razor.js @@ -0,0 +1,19 @@ +function debounce(func, delay, dotNetReference) { + let timeoutId; + return function () { + clearTimeout(timeoutId); + timeoutId = setTimeout(() => { + func.apply(this, dotNetReference); + }, delay); + }; +} + +function handleInputStop(dotNetReference) { + console.log("L'utilisateur a arrêté de taper."); + //dotNetReference.invokeMethodAsync("HandleTextEnd"); +} + +function addStopWriteListener(dotNetReference) { + const input = document.getElementById('searchInput'); + input.addEventListener('input', debounce(handleInputStop, 1000, dotNetReference)); // 500ms après la dernière frappe +} \ No newline at end of file diff --git a/src/GameIdeas/Client/GameIdeas.BlazorApp/wwwroot/index.html b/src/GameIdeas/Client/GameIdeas.BlazorApp/wwwroot/index.html index bc8684e..2b4ad65 100644 --- a/src/GameIdeas/Client/GameIdeas.BlazorApp/wwwroot/index.html +++ b/src/GameIdeas/Client/GameIdeas.BlazorApp/wwwroot/index.html @@ -27,7 +27,7 @@ - +