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 { 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 { environment } from 'src/environments/environment';
@ -20,27 +20,44 @@ export class RecipeService {
take: 10,
offset: 0,
search: '',
version: 'coffeethai02_580.json',
version: this.getCurrentVersion(),
}
): Observable<{
fileName: string;
recipes: Recipe;
hasMore: boolean;
}> {
return this._httpClient.get<{
fileName: string;
recipes: Recipe;
hasMore: boolean;
}>(environment.api + '/recipes', {
params: {
offset: params.offset,
take: params.take,
search: params.search,
version: params.version,
},
withCredentials: true,
responseType: 'json',
});
return this._httpClient
.get<{
fileName: string;
recipes: Recipe;
hasMore: boolean;
}>(environment.api + '/recipes', {
params: {
offset: params.offset,
take: params.take,
search: params.search,
version: params.version,
},
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> {