fix delay material fetching

This commit is contained in:
pakintada@gmail.com 2024-01-18 16:59:06 +07:00
parent db131d10c0
commit 4ece2cf30c
13 changed files with 220 additions and 143 deletions

View file

@ -5,6 +5,7 @@ import { environment } from 'src/environments/environment';
import { Observable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { getCountryMapSwitcher } from 'src/app/shared/helpers/recipe';
import { AsyncStorage } from 'src/app/shared/helpers/asyncStorage';
@Injectable({ providedIn: 'root' })
export class MaterialService {
@ -13,16 +14,20 @@ export class MaterialService {
constructor(private _httpClient: HttpClient, private _route: ActivatedRoute) {}
getMaterialCodes(
async getMaterialCodes(
matIds?: number[],
country?: string,
filename?: string
): Observable<MaterialCode[]> {
): Promise<Observable<MaterialCode[]>> {
// async country
let asyncCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
return this._httpClient.get<MaterialCode[]>(
`${environment.api}/materials/code`,
{
params: {
country: country || this.getCurrentCountry(this.department!),
country: country || asyncCountry,
filename: filename || this.getCurrentFile(),
mat_ids: matIds?.join(',') || '',
},
@ -31,17 +36,19 @@ export class MaterialService {
);
}
getFullMaterialDetail(
async getFullMaterialDetail(
country?: string,
filename?: string
): Observable<{
): Promise<Observable<{
"materialId": number,
"name": string,
"type": string
}[] | null>{
console.log("getFullMaterialDetail", country, filename);
}[] | null>>{
console.log("getFullMaterialDetail", country, filename, country || this.getCurrentCountry(this.department!));
country = country || this.getCurrentCountry(this.department!);
let asyncCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
country = country || asyncCountry;
filename = filename || this.getCurrentFile();
// finalize fetch from what?
@ -56,16 +63,19 @@ export class MaterialService {
});
}
getMaterialSettingById(
async getMaterialSettingById(
id: number,
country?: string,
filename?: string
): Observable<MaterialSetting> {
): Promise<Observable<MaterialSetting>> {
let asyncCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
return this._httpClient.get<MaterialSetting>(
`${environment.api}/materials/setting/${id}`,
{
params: {
country: country || this.getCurrentCountry(this.department!),
country: country || asyncCountry,
filename: filename || this.getCurrentFile(),
},
withCredentials: true,
@ -82,7 +92,7 @@ export class MaterialService {
return 'default';
}
getCurrentCountry(department? : string): string {
async getCurrentCountry(department? : string): Promise<string> {
// fetch by using department
if(department){
@ -97,7 +107,10 @@ export class MaterialService {
}
const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
// const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
const currentRecipeCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
if (currentRecipeCountry) {
return currentRecipeCountry;
}

View file

@ -14,6 +14,7 @@ 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';
import { AsyncStorage } from 'src/app/shared/helpers/asyncStorage';
type RecipeOverviewParams = {
filename: string;
@ -49,7 +50,7 @@ export class RecipeService {
constructor(private _httpClient: HttpClient, private _route: ActivatedRoute) {}
getRecipesDashboard(
params: RecipeDashboardParams = {
params: any = {
country: this.getCurrentCountry(this.department!),
filename: this.getCurrentFile(),
}
@ -67,8 +68,8 @@ export class RecipeService {
);
}
getRecipeOverview(
params: RecipeOverviewParams = {
async getRecipeOverview(
params: any = {
country: this.getCurrentCountry(this.department!),
filename: this.getCurrentFile(),
materialIds: [],
@ -76,7 +77,7 @@ export class RecipeService {
take: 20,
search: '',
}
): Observable<RecipeOverviewList> {
): Promise<Observable<RecipeOverviewList>> {
return this._httpClient.get<RecipeOverviewList>(
environment.api + '/recipes/overview',
{
@ -94,13 +95,13 @@ export class RecipeService {
);
}
getRecipeDetail(productCode: string): Observable<RecipeDetail> {
async getRecipeDetail(productCode: string): Promise<Observable<RecipeDetail>> {
return this._httpClient.get<RecipeDetail>(
environment.api + '/recipes/' + productCode,
{
params: {
filename: this.getCurrentFile(),
country: this.getCurrentCountry(this.department!),
country: await this.getCurrentCountry(this.department!),
},
withCredentials: true,
responseType: 'json',
@ -108,15 +109,15 @@ export class RecipeService {
);
}
getRecipeDetailMat(
async getRecipeDetailMat(
productCode: string
): Observable<{ result: RecipeDetailMat[] }> {
): Promise<Observable<{ result: RecipeDetailMat[]; }>> {
return this._httpClient.get<{ result: RecipeDetailMat[] }>(
environment.api + '/recipes/' + productCode + '/mat',
{
params: {
filename: this.getCurrentFile(),
country: this.getCurrentCountry(this.department!),
country: await this.getCurrentCountry(this.department!),
},
withCredentials: true,
responseType: 'json',
@ -141,7 +142,7 @@ export class RecipeService {
localStorage.setItem('currentRecipeFile', filename);
}
getCurrentCountry(department?: string): string {
async getCurrentCountry(department?: string): Promise<string> {
if(department){
@ -151,10 +152,15 @@ export class RecipeService {
console.log('fullname: ', fullname);
// localStorage.setItem('currentRecipeCountry', fullname);
await AsyncStorage.setItem('currentRecipeCountry', fullname);
return fullname;
}
const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
// const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
const currentRecipeCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
if (currentRecipeCountry) {
return currentRecipeCountry;
}