recipe now can memorize recipe version have been selected

This commit is contained in:
Kenta420 2023-10-04 14:23:44 +07:00
parent f2005dcb58
commit 8eab23e9ba
2 changed files with 38 additions and 22 deletions

View file

@ -1,6 +1,6 @@
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, distinctUntilChanged } from 'rxjs'; import { BehaviorSubject, Observable, distinctUntilChanged, tap } from 'rxjs';
import { Recipe, Recipe01 } from '../models/recipe.model'; import { Recipe, Recipe01 } from '../models/recipe.model';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
@ -20,27 +20,44 @@ export class RecipeService {
take: 10, take: 10,
offset: 0, offset: 0,
search: '', search: '',
version: 'coffeethai02_580.json', version: this.getCurrentVersion(),
} }
): Observable<{ ): Observable<{
fileName: string; fileName: string;
recipes: Recipe; recipes: Recipe;
hasMore: boolean; hasMore: boolean;
}> { }> {
return this._httpClient.get<{ return this._httpClient
fileName: string; .get<{
recipes: Recipe; fileName: string;
hasMore: boolean; recipes: Recipe;
}>(environment.api + '/recipes', { hasMore: boolean;
params: { }>(environment.api + '/recipes', {
offset: params.offset, params: {
take: params.take, offset: params.offset,
search: params.search, take: params.take,
version: params.version, search: params.search,
}, version: params.version,
withCredentials: true, },
responseType: 'json', withCredentials: true,
}); responseType: 'json',
})
.pipe(
tap((data) => {
if (data.fileName !== this.getCurrentVersion()) {
localStorage.setItem('currentRecipeVersion', data.fileName);
}
})
);
}
getCurrentVersion(): string {
const currentRecipeVersion = localStorage.getItem('currentRecipeVersion');
if (currentRecipeVersion) {
return currentRecipeVersion;
}
return 'coffeethai02_580.json';
} }
getRecipesById(id: string): Observable<Recipe01> { getRecipesById(id: string): Observable<Recipe01> {

View file

@ -32,7 +32,6 @@ export class DashboardComponent implements OnInit {
private offset = 0; private offset = 0;
private take = 20; private take = 20;
private recipeVersionData: string[] = []; private recipeVersionData: string[] = [];
private currentRecipeVersion: string = 'coffeethai02_580.json';
recipeVersion: BehaviorSubject<string> = new BehaviorSubject<string>(''); recipeVersion: BehaviorSubject<string> = new BehaviorSubject<string>('');
recipeVersion$ = this.recipeVersion.asObservable(); recipeVersion$ = this.recipeVersion.asObservable();
@ -61,7 +60,7 @@ export class DashboardComponent implements OnInit {
offset: this.offset, offset: this.offset,
take: this.take, take: this.take,
search: this.oldSearchStr, search: this.oldSearchStr,
version: this.currentRecipeVersion, version: this._recipeService.getCurrentVersion(),
}) })
.subscribe(({ recipes, hasMore, fileName }) => { .subscribe(({ recipes, hasMore, fileName }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes; const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
@ -100,7 +99,7 @@ export class DashboardComponent implements OnInit {
offset: this.offset, offset: this.offset,
take: this.take, take: this.take,
search: this.oldSearchStr, search: this.oldSearchStr,
version: this.currentRecipeVersion, version: this._recipeService.getCurrentVersion(),
}) })
.subscribe(({ recipes, hasMore, fileName }) => { .subscribe(({ recipes, hasMore, fileName }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes; const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
@ -139,7 +138,7 @@ export class DashboardComponent implements OnInit {
offset: this.offset, offset: this.offset,
take: this.take, take: this.take,
search: this.searchStr, search: this.searchStr,
version: this.currentRecipeVersion, version: this._recipeService.getCurrentVersion(),
}) })
.subscribe(({ recipes, hasMore, fileName }) => { .subscribe(({ recipes, hasMore, fileName }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes; const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
@ -165,7 +164,6 @@ export class DashboardComponent implements OnInit {
this.isHasMore = true; this.isHasMore = true;
this.isLoadMore = false; this.isLoadMore = false;
this.oldSearchStr = ''; this.oldSearchStr = '';
this.currentRecipeVersion = recipeVersion;
this._recipeService this._recipeService
.getRecipes({ .getRecipes({
@ -206,7 +204,8 @@ export class DashboardComponent implements OnInit {
openJsonTab() { openJsonTab() {
window.open( window.open(
environment.api + `/recipes/${this.currentRecipeVersion}/json`, environment.api +
`/recipes/${this._recipeService.getCurrentVersion()}/json`,
'_blank' '_blank'
); );
} }