import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable, tap } from 'rxjs'; import { Recipe, Recipe01, RecipeDetail, RecipeDetailMat, RecipeOverview, RecipeOverviewList, RecipesDashboard, } from '../models/recipe.model'; import { environment } from 'src/environments/environment'; import { RecipeMetaData } from 'src/app/shared/types/recipe'; type RecipeOverviewParams = { filename: string; country: string; materialIds: number[]; offset: number; take: number; search: string; }; type RecipeDashboardParams = { filename: string; country: string; }; interface RecipeFiles { [key: string]: string[]; } @Injectable({ providedIn: 'root' }) export class RecipeService { private countries: string[] = []; private recipeFiles: RecipeFiles = {}; private tmp_files: string[] = []; private get tmpfiles(): string[] { return this.tmp_files; } constructor(private _httpClient: HttpClient) {} getRecipesDashboard( params: RecipeDashboardParams = { country: this.getCurrentCountry(), filename: this.getCurrentFile(), } ): Observable { return this._httpClient.get( environment.api + '/recipes/dashboard', { params: { country: params.country, filename: params.filename, }, withCredentials: true, responseType: 'json', } ); } getRecipeOverview( params: RecipeOverviewParams = { country: this.getCurrentCountry(), filename: this.getCurrentFile(), materialIds: [], offset: 0, take: 20, search: '', } ): Observable { return this._httpClient.get( environment.api + '/recipes/overview', { params: { country: params.country, filename: params.filename, materialIds: params.materialIds.join(','), offset: params.offset.toString(), take: params.take.toString(), search: params.search, }, withCredentials: true, responseType: 'json', } ); } getRecipeDetail(productCode: string): Observable { return this._httpClient.get( environment.api + '/recipes/' + productCode, { params: { filename: this.getCurrentFile(), country: this.getCurrentCountry(), }, withCredentials: true, responseType: 'json', } ); } getRecipeDetailMat( productCode: string ): Observable<{ result: RecipeDetailMat[] }> { return this._httpClient.get<{ result: RecipeDetailMat[] }>( environment.api + '/recipes/' + productCode + '/mat', { params: { filename: this.getCurrentFile(), country: this.getCurrentCountry(), }, withCredentials: true, responseType: 'json', } ); } getCurrentFile(): string { const currentRecipeFile = localStorage.getItem('currentRecipeFile'); if (currentRecipeFile) { return currentRecipeFile; } return 'coffeethai02_580.json'; } getCurrentCountry(): string { const currentRecipeCountry = localStorage.getItem('currentRecipeCountry'); if (currentRecipeCountry) { return currentRecipeCountry; } return 'Thailand'; } getRecipesById(id: string): Observable<{ recipe: Recipe01; recipeMetaData: RecipeMetaData; }> { return this._httpClient.get<{ recipe: Recipe01; recipeMetaData: RecipeMetaData; }>(environment.api + '/recipes/' + id, { withCredentials: true, responseType: 'json', }); } getRecipeCountries(): Observable { return this._httpClient .get(environment.api + '/recipes/versions', { withCredentials: true, responseType: 'json', }) .pipe(tap((countries) => (this.countries = countries))); } getRecipeFiles(country: string): Observable { return this._httpClient .get(environment.api + '/recipes/versions/' + country, { withCredentials: true, responseType: 'json', }) .pipe(tap((files) => (this.recipeFiles[country] = files))); } getRecipeFileCountries(): string[] { return this.countries; } getRecipeFileNames(country: string): string[] | null { return this.recipeFiles[country] ?? null; } editChanges(country: string, filename: string, change: any) { console.log('target version = ', filename); console.log('change in edit: ', change); return this._httpClient .post<{ status: string; }>( environment.api + ('/recipes/edit/' + country + '/' + filename), JSON.stringify(change), { withCredentials: true, responseType: 'json', } ) .subscribe({ next(value) { console.log( value); }, }); } getSavedTmp(country: string, filename:string) { console.log("loading saved .tmp* file", country, filename) // do split filename filename = filename.split('_')[1]; // this._user.getCurrentUser().subscribe((user) => { // this.user = user.user; // }) return this._httpClient .get( environment.api + ("/recipes/saved/"+ country + "/" + filename), { withCredentials: true, } ); // .subscribe({ // next(value) { // console.log( value); // }, // }); } }