Retain search prompt between navigations.

This commit is contained in:
Jesse Brault 2025-12-29 12:52:11 -06:00
parent 625b38701f
commit df3253ade1

View File

@ -3,6 +3,7 @@ import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angula
import { injectQuery } from '@tanstack/angular-query-experimental';
import { RecipeService } from '../service/recipe.service';
import { RecipeCardGrid } from '../recipe-card-grid/recipe-card-grid';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-recipes-search-page',
@ -12,6 +13,17 @@ import { RecipeCardGrid } from '../recipe-card-grid/recipe-card-grid';
})
export class RecipesSearchPage {
private readonly recipeService = inject(RecipeService);
private readonly router = inject(Router);
private readonly activatedRoute = inject(ActivatedRoute);
public constructor() {
this.activatedRoute.queryParams.subscribe((queryParams) => {
if (queryParams['prompt']) {
this.givenPrompt.set(queryParams['prompt']);
this.searchRecipesForm.controls.prompt.setValue(queryParams['prompt']);
}
});
}
protected readonly searchRecipesForm = new FormGroup({
prompt: new FormControl('', [Validators.required]),
@ -25,8 +37,11 @@ export class RecipesSearchPage {
enabled: () => !!this.givenPrompt(),
}));
protected onPromptSubmit() {
protected async onPromptSubmit() {
if (this.searchRecipesForm.value.prompt) {
await this.router.navigate(['/recipes-search'], {
queryParams: { prompt: this.searchRecipesForm.value.prompt },
});
this.givenPrompt.set(this.searchRecipesForm.value.prompt);
}
}