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

@ -94,11 +94,11 @@ export class RecipeDetailsComponent implements OnInit {
toppingSet: ToppingSet[] | null = null;
submenus: Recipe01[] | null = null;
ngOnInit() {
async ngOnInit() {
this.productCode = this._route.snapshot.params['productCode'];
this.recipeDetail$ = this._recipeService
.getRecipeDetail(this.productCode)
this.recipeDetail$ = (await this._recipeService
.getRecipeDetail(this.productCode))
.pipe(first());
this.recipeDetail$.subscribe((detail) => {
@ -109,7 +109,7 @@ export class RecipeDetailsComponent implements OnInit {
this.recipeOriginalDetail = { ...this.recipeDetailForm.getRawValue() };
});
this._recipeService.getSubMenus(this._recipeService.getCurrentCountry(this.department), this._recipeService.getCurrentFile(), this.productCode).subscribe((data) => {
this._recipeService.getSubMenus(await this._recipeService.getCurrentCountry(this.department), this._recipeService.getCurrentFile(), this.productCode).subscribe((data) => {
console.log('Submenus', data);
this.submenus = data;
});
@ -143,7 +143,7 @@ export class RecipeDetailsComponent implements OnInit {
confirmSave = {
title: 'The changes detected!',
message: 'Do you want to save changes?',
confirmCallBack: () => {
confirmCallBack: async () => {
console.log('confirm save');
// get username
@ -177,7 +177,7 @@ export class RecipeDetailsComponent implements OnInit {
// TODO: update value in targeted recipe
console.log('to_send', to_send);
this._recipeService.editChanges(
this._recipeService.getCurrentCountry(this.department),
await this._recipeService.getCurrentCountry(this.department),
this._recipeService.getCurrentFile(),
{
...to_send,

View file

@ -96,9 +96,9 @@ export class RecipeListComponent implements OnInit {
private _recipeListOriginalArray!: RecipeDetailMat[];
ngOnInit(): void {
this._recipeService
.getRecipeDetailMat(this.productCode)
async ngOnInit(): Promise<void> {
(await this._recipeService
.getRecipeDetailMat(this.productCode))
.pipe(first())
.subscribe(({ result }) => {
this._recipeListOriginalArray = result;
@ -287,11 +287,13 @@ export class RecipeListComponent implements OnInit {
});
// TODO: embed this to recipelist
this._materialService.getMaterialCodes().subscribe((materials) => {
(await
// TODO: embed this to recipelist
this._materialService.getMaterialCodes()).subscribe((materials) => {
this.materialList = materials;
});
this._materialService.getFullMaterialDetail().subscribe((materials) => {
(await this._materialService.getFullMaterialDetail()).subscribe((materials) => {
this.fullMaterialList = materials;
this.categoriedMaterial = this.ListCategory();
console.log(this.categoriedMaterial);

View file

@ -32,10 +32,10 @@ export class RecipeToppingComponent implements OnInit {
private _recipeService: RecipeService,
private _toppingService: ToppingService
) {}
ngOnInit(): void {
async ngOnInit(): Promise<void> {
this._toppingService
.getToppings(
this._recipeService.getCurrentCountry(),
await this._recipeService.getCurrentCountry(),
this._recipeService.getCurrentFile()
)
.subscribe((data) => {

View file

@ -64,10 +64,10 @@ export class RecipeToppingsetComponent implements OnInit {
return this.toppingForm.get('toppingList') as FormArray;
}
ngOnInit(): void {
async ngOnInit(): Promise<void> {
this._toppingService
.getToppingsOfRecipe(
this._recipeService.getCurrentCountry(),
await this._recipeService.getCurrentCountry(),
this._recipeService.getCurrentFile(),
this.productCode
)
@ -94,7 +94,7 @@ export class RecipeToppingsetComponent implements OnInit {
// fetch all toppings : group and list
this._toppingService
.getToppings(
this._recipeService.getCurrentCountry(),
await this._recipeService.getCurrentCountry(),
this._recipeService.getCurrentFile()
)
.subscribe((data) => {

View file

@ -11,7 +11,7 @@
<div class="flex flex-row py-3 justify-between items-center">
<div class="flex flex-col">
<span
>Recipe Version {{ currentVersion }} |
>Recipe Version {{ recipesDashboard.configNumber }} |
{{ recipesDashboard.filename }}</span
>
</div>

View file

@ -34,6 +34,7 @@ import { UserPermissions } from 'src/app/core/auth/userPermissions';
import { ToppingService } from 'src/app/core/services/topping.service';
import { copy, transformToTSV } from 'src/app/shared/helpers/copy';
import { getCountryMapSwitcher } from 'src/app/shared/helpers/recipe';
import { AsyncStorage } from 'src/app/shared/helpers/asyncStorage';
@Component({
selector: 'app-recipes',
@ -96,7 +97,7 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
@ViewChild('table', { static: false }) set content(table: ElementRef) {
table.nativeElement.addEventListener(
'scroll',
() => {
async () => {
if (this.isHasMore === false) {
return;
}
@ -105,15 +106,15 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
const isBottom = scrollTop + clientHeight >= scrollHeight - 10;
if (isBottom && !this.isLoadMore) {
this.isLoadMore = true;
this._recipeService
(await this._recipeService
.getRecipeOverview({
offset: this.offset,
take: this.take,
search: this.oldSearchStr,
filename: this._recipeService.getCurrentFile(),
country: this._recipeService.getCurrentCountry(this.department),
country: await this._recipeService.getCurrentCountry(this.department),
materialIds: this.selectMaterialFilter || [],
})
}))
.subscribe(({ result, hasMore, totalCount }) => {
if (this.recipeOverviewList) {
this.recipeOverviewList =
@ -140,24 +141,24 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
private _router: Router
) {}
ngOnInit(): void {
async ngOnInit(): Promise<void> {
console.log('Trigger onInit where department = ', this.department);
this.recipesDashboard$ = this._recipeService
.getRecipesDashboard({
filename: this._recipeService.getCurrentFile(),
country: this._recipeService.getCurrentCountry(this.department!),
country: await this._recipeService.getCurrentCountry(this.department!),
})
.pipe(
finalize(() => {
this._recipeService
finalize(async () => {
(await this._recipeService
.getRecipeOverview({
offset: this.offset,
take: this.take,
search: this.oldSearchStr,
filename: this._recipeService.getCurrentFile(),
country: this._recipeService.getCurrentCountry(this.department!),
country: await this._recipeService.getCurrentCountry(this.department!),
materialIds: this.selectMaterialFilter || [],
})
}))
.subscribe(({ result, hasMore, totalCount }) => {
this.recipeOverviewList = result;
this.offset += 10;
@ -177,14 +178,14 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
console.log('ngAfterViewInit::department', this.department);
console.log('::CurrentFile', this._recipeService.getCurrentFile());
this._materialService
(await this._materialService
.getFullMaterialDetail(
this.department,
this._recipeService.getCurrentFile()
)
))
.subscribe((mat) => {
this.materialDetail = mat;
console.log(this.materialDetail?.length);
console.log(this.materialDetail?.length, "[0]=", mat?.at(0), "current country", this._recipeService.getCurrentCountry(), "currentFile", this._recipeService.getCurrentFile());
// check material detail
console.log('first material', this.materialDetail![0]);
@ -194,7 +195,7 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
this._recipeService
.getSavedTmp(
this._recipeService.getCurrentCountry(this.department),
await this._recipeService.getCurrentCountry(this.department),
this._recipeService.getCurrentFile()
)
.subscribe({
@ -224,8 +225,10 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
});
// TODO: get all materials; MaterialSetting + MaterialCode
this._materialService
.getMaterialCodes()
(await
// TODO: get all materials; MaterialSetting + MaterialCode
this._materialService
.getMaterialCodes())
.pipe(
map((mat) =>
mat.map((m) => ({
@ -260,19 +263,26 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
this.searchStr = (event.target as HTMLInputElement).value;
}
search(event: Event) {
async search(event: Event) {
// country
let country = await this._recipeService.getCurrentCountry(this.department).then(
(country) => country
);
this.offset = 0;
this.isLoadMore = true;
this.oldSearchStr = this.searchStr;
this._recipeService
(await this._recipeService
.getRecipeOverview({
offset: this.offset,
take: this.take,
search: this.oldSearchStr,
filename: this._recipeService.getCurrentFile(),
country: this._recipeService.getCurrentCountry(this.department),
country: country,
materialIds: this.selectMaterialFilter || [],
})
}))
.subscribe(({ result, hasMore, totalCount }) => {
this.recipeOverviewList = result;
this.offset += 10;
@ -420,20 +430,19 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
);
}
countrySelected(country: string) {
async countrySelected(country: string) {
this.selectedCountry = country;
this.isCountrySelected = true;
// localStorage.setItem('currentRecipeCountry', country);
await AsyncStorage.setItem('currentRecipeCountry', country);
// force reload, will fix this later
void this._router
.navigate([`/${getCountryMapSwitcher(country)}/recipes`])
.then(() => {
window.location.reload();
});
.navigate([`/${getCountryMapSwitcher(country)}/recipes`]);
}
loadRecipe(recipeFileName: string) {
async loadRecipe(recipeFileName: string) {
// clear all recipes
this.offset = 0;
this.isHasMore = true;
@ -441,33 +450,43 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
this.oldSearchStr = '';
// localStorage.setItem('currentRecipeFile', recipeFileName);
console.log('loadRecipe', recipeFileName, "currentCountry", this.department);
await AsyncStorage.setItem('currentRecipeFile', recipeFileName);
this.recipesDashboard$ = this._recipeService.getRecipesDashboard({
filename: recipeFileName,
country: this.department!,
});
this.recipesDashboard$.subscribe((data) => {
this.currentVersion = data.configNumber;
console.log('current version', this.currentVersion);
});
this._recipeService
.getRecipeOverview({
offset: this.offset,
take: this.take,
search: this.oldSearchStr,
filename: recipeFileName,
country: this.selectedCountry!,
materialIds: this.selectMaterialFilter || [],
})
.subscribe(({ result, hasMore, totalCount }) => {
this.recipeOverviewList = result;
this.offset += 10;
this.isHasMore = hasMore;
this.isLoadMore = false;
});
console.log('loadRecipe', recipeFileName, "currentCountry", this.department, "selectedCountry", this.selectedCountry);
// clear all menus
this.recipeOverviewList = [];
// this.recipesDashboard$ = this._recipeService.getRecipesDashboard({
// filename: recipeFileName,
// country: this.selectedCountry!,
// });
// this.recipesDashboard$.subscribe((data) => {
// this.currentVersion = data.configNumber;
// console.log('current version', this.currentVersion);
// });
// this._recipeService
// .getRecipeOverview({
// offset: this.offset,
// take: this.take,
// search: this.oldSearchStr,
// filename: recipeFileName,
// country: this.selectedCountry!,
// materialIds: this.selectMaterialFilter || [],
// })
// .subscribe(({ result, hasMore, totalCount }) => {
// this.recipeOverviewList = result;
// this.offset += 10;
// this.isHasMore = hasMore;
// this.isLoadMore = false;
// });
window.location.reload();
}
// end of Recipe Version selection
@ -487,7 +506,7 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
this.saveTab = true;
}
loadSavedFile(file_commit: any) {
async loadSavedFile(file_commit: any) {
this.showSaveNoti = false;
this.saveTab = false;
console.log('loadSavedFile', file_commit, this.department);
@ -511,7 +530,7 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
console.log(file_commit.Change_file.split('/')[2]);
this._recipeService
(await this._recipeService
.getRecipeOverview({
offset: this.offset,
take: this.take,
@ -519,7 +538,7 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
filename: file_commit.Change_file.split('/')[2],
country: country,
materialIds: this.selectMaterialFilter || [],
})
}))
.subscribe(({ result, hasMore, totalCount }) => {
console.log('loadSavedFile', result);
this._recipeService.setCurrentFile(