fix default file reader bug

This commit is contained in:
pakintada@gmail.com 2024-01-17 17:38:23 +07:00
parent 21109e4bf9
commit db131d10c0
15 changed files with 636 additions and 254 deletions

View file

@ -3,10 +3,15 @@ import { Injectable } from '@angular/core';
import { MaterialCode, MaterialSetting } from '../models/recipe.model';
import { environment } from 'src/environments/environment';
import { Observable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { getCountryMapSwitcher } from 'src/app/shared/helpers/recipe';
@Injectable({ providedIn: 'root' })
export class MaterialService {
constructor(private _httpClient: HttpClient) {}
private department = this._route.snapshot.paramMap.get('department')
constructor(private _httpClient: HttpClient, private _route: ActivatedRoute) {}
getMaterialCodes(
matIds?: number[],
@ -17,7 +22,7 @@ export class MaterialService {
`${environment.api}/materials/code`,
{
params: {
country: country || this.getCurrentCountry(),
country: country || this.getCurrentCountry(this.department!),
filename: filename || this.getCurrentFile(),
mat_ids: matIds?.join(',') || '',
},
@ -36,9 +41,12 @@ export class MaterialService {
}[] | null>{
console.log("getFullMaterialDetail", country, filename);
country = country || this.getCurrentCountry();
country = country || this.getCurrentCountry(this.department!);
filename = filename || this.getCurrentFile();
// finalize fetch from what?
console.log("country, filename", country, filename);
return this._httpClient.get<{
"materialId": number,
"name": string,
@ -57,7 +65,7 @@ export class MaterialService {
`${environment.api}/materials/setting/${id}`,
{
params: {
country: country || this.getCurrentCountry(),
country: country || this.getCurrentCountry(this.department!),
filename: filename || this.getCurrentFile(),
},
withCredentials: true,
@ -71,10 +79,24 @@ export class MaterialService {
return currentRecipeFile;
}
return 'coffeethai02_580.json';
return 'default';
}
getCurrentCountry(): string {
getCurrentCountry(department? : string): string {
// fetch by using department
if(department){
// translate back to full name
let fullname = getCountryMapSwitcher(department);
console.log('Material.service::fullname: ', fullname);
// localStorage.setItem('currentRecipeCountry', fullname);
return fullname;
}
const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
if (currentRecipeCountry) {
return currentRecipeCountry;

View file

@ -12,6 +12,8 @@ import {
} from '../models/recipe.model';
import { environment } from 'src/environments/environment';
import { RecipeMetaData } from 'src/app/shared/types/recipe';
import { getCountryMapSwitcher } from 'src/app/shared/helpers/recipe';
import { ActivatedRoute } from '@angular/router';
type RecipeOverviewParams = {
filename: string;
@ -38,15 +40,17 @@ export class RecipeService {
private tmp_files: string[] = [];
private department = this._route.snapshot.paramMap.get('department');
private get tmpfiles(): string[] {
return this.tmp_files;
}
constructor(private _httpClient: HttpClient) {}
constructor(private _httpClient: HttpClient, private _route: ActivatedRoute) {}
getRecipesDashboard(
params: RecipeDashboardParams = {
country: this.getCurrentCountry(),
country: this.getCurrentCountry(this.department!),
filename: this.getCurrentFile(),
}
): Observable<RecipesDashboard> {
@ -65,7 +69,7 @@ export class RecipeService {
getRecipeOverview(
params: RecipeOverviewParams = {
country: this.getCurrentCountry(),
country: this.getCurrentCountry(this.department!),
filename: this.getCurrentFile(),
materialIds: [],
offset: 0,
@ -96,7 +100,7 @@ export class RecipeService {
{
params: {
filename: this.getCurrentFile(),
country: this.getCurrentCountry(),
country: this.getCurrentCountry(this.department!),
},
withCredentials: true,
responseType: 'json',
@ -112,7 +116,7 @@ export class RecipeService {
{
params: {
filename: this.getCurrentFile(),
country: this.getCurrentCountry(),
country: this.getCurrentCountry(this.department!),
},
withCredentials: true,
responseType: 'json',
@ -121,19 +125,35 @@ export class RecipeService {
}
getCurrentFile(): string {
// TODO: get default from server
const currentRecipeFile = localStorage.getItem('currentRecipeFile');
if (currentRecipeFile) {
return currentRecipeFile;
}
return 'coffeethai02_580.json';
return 'default';
}
setCurrentFile(filename: string) {
localStorage.setItem('currentRecipeFile', filename);
}
getCurrentCountry(): string {
getCurrentCountry(department?: string): string {
if(department){
// translate back to full name
let fullname = getCountryMapSwitcher(department);
console.log('fullname: ', fullname);
// localStorage.setItem('currentRecipeCountry', fullname);
return fullname;
}
const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
if (currentRecipeCountry) {
return currentRecipeCountry;