Retain search prompt between navigations.
This commit is contained in:
parent
625b38701f
commit
df3253ade1
@ -3,6 +3,7 @@ import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angula
|
|||||||
import { injectQuery } from '@tanstack/angular-query-experimental';
|
import { injectQuery } from '@tanstack/angular-query-experimental';
|
||||||
import { RecipeService } from '../service/recipe.service';
|
import { RecipeService } from '../service/recipe.service';
|
||||||
import { RecipeCardGrid } from '../recipe-card-grid/recipe-card-grid';
|
import { RecipeCardGrid } from '../recipe-card-grid/recipe-card-grid';
|
||||||
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-recipes-search-page',
|
selector: 'app-recipes-search-page',
|
||||||
@ -12,6 +13,17 @@ import { RecipeCardGrid } from '../recipe-card-grid/recipe-card-grid';
|
|||||||
})
|
})
|
||||||
export class RecipesSearchPage {
|
export class RecipesSearchPage {
|
||||||
private readonly recipeService = inject(RecipeService);
|
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({
|
protected readonly searchRecipesForm = new FormGroup({
|
||||||
prompt: new FormControl('', [Validators.required]),
|
prompt: new FormControl('', [Validators.required]),
|
||||||
@ -25,8 +37,11 @@ export class RecipesSearchPage {
|
|||||||
enabled: () => !!this.givenPrompt(),
|
enabled: () => !!this.givenPrompt(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
protected onPromptSubmit() {
|
protected async onPromptSubmit() {
|
||||||
if (this.searchRecipesForm.value.prompt) {
|
if (this.searchRecipesForm.value.prompt) {
|
||||||
|
await this.router.navigate(['/recipes-search'], {
|
||||||
|
queryParams: { prompt: this.searchRecipesForm.value.prompt },
|
||||||
|
});
|
||||||
this.givenPrompt.set(this.searchRecipesForm.value.prompt);
|
this.givenPrompt.set(this.searchRecipesForm.value.prompt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user