fix delay material fetching
This commit is contained in:
parent
db131d10c0
commit
4ece2cf30c
13 changed files with 220 additions and 143 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
14
client/src/app/shared/helpers/asyncStorage.ts
Normal file
14
client/src/app/shared/helpers/asyncStorage.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
export class AsyncStorage {
|
||||
static async getItem<T>(key: string) {
|
||||
return new Promise<T>((resolve) => {
|
||||
resolve(localStorage.getItem(key) as T);
|
||||
});
|
||||
}
|
||||
|
||||
static async setItem(key: string, value: string) {
|
||||
return new Promise<void>((resolve) => {
|
||||
localStorage.setItem(key, value);
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ import (
|
|||
)
|
||||
|
||||
type RecipeWithTimeStamps struct {
|
||||
Recipe models.Recipe
|
||||
Recipe map[string]*models.Recipe
|
||||
TimeStamps int64
|
||||
}
|
||||
|
||||
|
|
@ -27,7 +27,7 @@ type Data struct {
|
|||
CurrentCountryID map[string]string
|
||||
DefaultCountryMap []DefaultByCountry
|
||||
AllRecipeFiles map[string][]helpers.RecipePath
|
||||
currentRecipe *models.Recipe
|
||||
currentRecipe map[string]*models.Recipe
|
||||
recipeMap map[string]RecipeWithTimeStamps
|
||||
Countries []helpers.CountryName
|
||||
taoLogger *logger.TaoLogger
|
||||
|
|
@ -58,7 +58,6 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
allRecipeFiles := helpers.ScanRecipeFiles(countries)
|
||||
|
||||
defaultFile := "coffeethai02_600.json"
|
||||
defaultCountry := "tha"
|
||||
|
||||
// TODO: read 'version' file by country
|
||||
|
||||
|
|
@ -102,6 +101,7 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
|
||||
currentFileMap := make(map[string]string)
|
||||
CurrentCountryIDMap := make(map[string]string)
|
||||
currentDefaultFileForEachCountry := make(map[string]*models.Recipe)
|
||||
|
||||
// all default versions as string
|
||||
versionsString := ""
|
||||
|
|
@ -125,6 +125,13 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
|
||||
versionsString = versionsString + v.CountryShortName + ":" + strconv.Itoa(current_version_iter) + ","
|
||||
|
||||
// do read default
|
||||
defaultRecipe, err := helpers.ReadRecipeFile(v.CountryShortName, v2.Name)
|
||||
if err != nil {
|
||||
log.Panic("Error when read default recipe file for each country:", v.CountryShortName, err)
|
||||
}
|
||||
|
||||
currentDefaultFileForEachCountry[v.CountryShortName] = defaultRecipe
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
@ -148,22 +155,24 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
// }
|
||||
// }
|
||||
|
||||
taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", versionsString))
|
||||
// FIXME: default file bug. do assign each default recipe model to each country
|
||||
|
||||
defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
||||
// taoLogger.Log.Debug("defaultFile", zap.Any("defaultFile", defaultFile), zap.Any("latest_version", versionsString))
|
||||
|
||||
if err != nil {
|
||||
log.Panic("Error when read default recipe file:", err)
|
||||
}
|
||||
// defaultRecipe, err := helpers.ReadRecipeFile(defaultCountry, defaultFile)
|
||||
|
||||
// if err != nil {
|
||||
// log.Panic("Error when read default recipe file:", err)
|
||||
// }
|
||||
|
||||
return &Data{
|
||||
CurrentFile: currentFileMap,
|
||||
CurrentCountryID: CurrentCountryIDMap,
|
||||
AllRecipeFiles: allRecipeFiles,
|
||||
currentRecipe: defaultRecipe,
|
||||
currentRecipe: currentDefaultFileForEachCountry,
|
||||
recipeMap: map[string]RecipeWithTimeStamps{
|
||||
defaultFile: {
|
||||
Recipe: *defaultRecipe,
|
||||
Recipe: currentDefaultFileForEachCountry,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
},
|
||||
},
|
||||
|
|
@ -176,17 +185,17 @@ func NewData(taoLogger *logger.TaoLogger) *Data {
|
|||
func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
||||
|
||||
if countryID == "" {
|
||||
return d.currentRecipe
|
||||
return d.currentRecipe["tha"]
|
||||
}
|
||||
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
return d.currentRecipe
|
||||
return d.currentRecipe[countryID]
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
return &recipe.Recipe
|
||||
return recipe.Recipe[countryID]
|
||||
}
|
||||
|
||||
// change current version and read new recipe
|
||||
|
|
@ -202,10 +211,10 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
|||
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe
|
||||
return d.currentRecipe[countryID]
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
|
|
@ -222,34 +231,34 @@ func (d *Data) GetRecipe(countryID, filename string) *models.Recipe {
|
|||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
Recipe: d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
return d.currentRecipe
|
||||
return d.currentRecipe[countryID]
|
||||
}
|
||||
|
||||
func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||
return d.currentRecipe.Recipe01
|
||||
}
|
||||
// func (d *Data) GetRecipe01() []models.Recipe01 {
|
||||
// return d.currentRecipe.Recipe01
|
||||
// }
|
||||
|
||||
func (d *Data) GetCurrentRecipe() *models.Recipe {
|
||||
return d.currentRecipe
|
||||
}
|
||||
// func (d *Data) GetCurrentRecipe() *models.Recipe {
|
||||
// return d.currentRecipe
|
||||
// }
|
||||
|
||||
func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string) (models.Recipe01, error) {
|
||||
|
||||
if !strings.Contains(filename, "tmp") {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadCurrent", filename, d.CurrentFile)
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
for _, v := range d.currentRecipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
fmt.Println("GetRecipe01ByProductCode.ReadMap", filename, d.CurrentFile)
|
||||
for _, v := range recipe.Recipe.Recipe01 {
|
||||
for _, v := range recipe.Recipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
|
|
@ -278,7 +287,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
for _, v := range d.currentRecipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
return v, nil
|
||||
}
|
||||
|
|
@ -287,7 +296,7 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
|
||||
d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("version", recipe.MachineSetting.ConfigNumber))
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
|
|
@ -304,11 +313,11 @@ func (d *Data) GetRecipe01ByProductCode(filename, countryID, productCode string)
|
|||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
Recipe: d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
for _, v := range d.currentRecipe.Recipe01 {
|
||||
for _, v := range d.currentRecipe[countryID].Recipe01 {
|
||||
if v.ProductCode == productCode {
|
||||
// d.taoLogger.Log.Debug("GetRecipe01ByProductCode", zap.Any("productCode", productCode), zap.Any("result", v))
|
||||
return v, nil
|
||||
|
|
@ -358,22 +367,22 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
result := make([]models.MaterialSetting, 0)
|
||||
|
||||
if countryID == "" {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
copy(result, d.currentRecipe[countryID].MaterialSetting)
|
||||
return result
|
||||
}
|
||||
|
||||
if !strings.Contains(filename, "tmp") {
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
copy(result, d.currentRecipe[countryID].MaterialSetting)
|
||||
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("result", result))
|
||||
return d.currentRecipe.MaterialSetting
|
||||
return d.currentRecipe[countryID].MaterialSetting
|
||||
}
|
||||
|
||||
if recipe, ok := d.recipeMap[filename]; ok {
|
||||
copy(result, recipe.Recipe.MaterialSetting)
|
||||
copy(result, recipe.Recipe[countryID].MaterialSetting)
|
||||
d.CurrentFile[countryID] = filename
|
||||
d.CurrentCountryID[countryID] = countryID
|
||||
return d.currentRecipe.MaterialSetting
|
||||
return d.currentRecipe[countryID].MaterialSetting
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -389,13 +398,13 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
copy(result, d.currentRecipe.MaterialSetting)
|
||||
return d.currentRecipe.MaterialSetting
|
||||
copy(result, d.currentRecipe[countryID].MaterialSetting)
|
||||
return d.currentRecipe[countryID].MaterialSetting
|
||||
}
|
||||
|
||||
d.taoLogger.Log.Debug("GetMaterialSetting", zap.Any("recipe", recipe.MaterialSetting))
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
|
|
@ -412,7 +421,7 @@ func (d *Data) GetMaterialSetting(countryID, filename string) []models.MaterialS
|
|||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
Recipe: d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
|
|
@ -424,10 +433,10 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
|||
var result []models.MaterialCode
|
||||
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
result = d.currentRecipe.MaterialCode
|
||||
result = d.currentRecipe[countryID].MaterialCode
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile[countryID] = filename
|
||||
return recipe.Recipe.MaterialCode
|
||||
return recipe.Recipe[countryID].MaterialCode
|
||||
} else {
|
||||
|
||||
if filename == "default" {
|
||||
|
|
@ -440,10 +449,10 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
|||
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe.MaterialCode
|
||||
return d.currentRecipe[countryID].MaterialCode
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
// save to map
|
||||
if len(d.recipeMap) > 5 { // limit keep in memory 5 version
|
||||
|
|
@ -460,11 +469,11 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
|||
}
|
||||
|
||||
d.recipeMap[filename] = RecipeWithTimeStamps{
|
||||
Recipe: *d.currentRecipe,
|
||||
Recipe: d.currentRecipe,
|
||||
TimeStamps: time.Now().Unix(),
|
||||
}
|
||||
|
||||
result = d.currentRecipe.MaterialCode
|
||||
result = d.currentRecipe[countryID].MaterialCode
|
||||
}
|
||||
|
||||
if len(ids) == 0 {
|
||||
|
|
@ -491,10 +500,10 @@ func (d *Data) GetMaterialCode(ids []uint64, countryID, filename string) []model
|
|||
func (d *Data) GetToppings(countryID, filename string) models.Topping {
|
||||
|
||||
if filename == "" || filename == d.CurrentFile[countryID] {
|
||||
return d.currentRecipe.Topping
|
||||
return d.currentRecipe[countryID].Topping
|
||||
} else if recipe, ok := d.recipeMap[filename]; ok {
|
||||
d.CurrentFile[countryID] = filename
|
||||
return recipe.Recipe.Topping
|
||||
return recipe.Recipe[countryID].Topping
|
||||
}
|
||||
|
||||
if filename == "default" {
|
||||
|
|
@ -507,10 +516,10 @@ func (d *Data) GetToppings(countryID, filename string) models.Topping {
|
|||
|
||||
if err != nil {
|
||||
d.taoLogger.Log.Error("Error when read recipe file, Return default recipe", zap.Error(err))
|
||||
return d.currentRecipe.Topping
|
||||
return d.currentRecipe[countryID].Topping
|
||||
}
|
||||
|
||||
d.currentRecipe = recipe
|
||||
d.currentRecipe[countryID] = recipe
|
||||
|
||||
return recipe.Topping
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ func (mr *MaterialRouter) GetFullMaterialDetail(w http.ResponseWriter, r *http.R
|
|||
})
|
||||
}
|
||||
|
||||
// mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("materialDetails", materialDetails))
|
||||
mr.taoLogger.Log.Debug("GetFullMaterialDetail", zap.Any("materialDetails", materialDetails[0]))
|
||||
|
||||
// send result
|
||||
if err := json.NewEncoder(w).Encode(materialDetails); err != nil {
|
||||
|
|
|
|||
|
|
@ -173,6 +173,8 @@ func (rr *RecipeRouter) dashBoard(w http.ResponseWriter, r *http.Request) {
|
|||
country := r.URL.Query().Get("country")
|
||||
filename := r.URL.Query().Get("filename")
|
||||
|
||||
rr.taoLogger.Log.Debug("RecipeRouter.Dashboard", zap.Any("country", country), zap.Any("filename", filename))
|
||||
|
||||
result, err := rr.recipeService.GetRecipeDashboard(&contracts.RecipeDashboardRequest{
|
||||
Country: country,
|
||||
Filename: filename,
|
||||
|
|
@ -205,6 +207,8 @@ func (rr *RecipeRouter) overview(w http.ResponseWriter, r *http.Request) {
|
|||
filename := r.URL.Query().Get("filename")
|
||||
materialIds := r.URL.Query().Get("materialIds")
|
||||
|
||||
rr.taoLogger.Log.Debug("RecipeRouter.Overview", zap.Any("take", take), zap.Any("offset", offset), zap.Any("country", country), zap.Any("filename", filename), zap.Any("materialIds", materialIds))
|
||||
|
||||
var materialIdsUint []int
|
||||
for _, v := range strings.Split(materialIds, ",") {
|
||||
materialIdUint, err := strconv.ParseUint(v, 10, 64)
|
||||
|
|
|
|||
|
|
@ -186,6 +186,16 @@ func (rs *recipeService) GetRecipeDashboard(request *contracts.RecipeDashboardRe
|
|||
|
||||
recipe := rs.db.GetRecipe(countryID, request.Filename)
|
||||
|
||||
// recheck if filename is `default`
|
||||
|
||||
if request.Filename == "default" {
|
||||
for _, v := range rs.db.DefaultCountryMap {
|
||||
if v.CountryShortName == request.Country || v.CountryLongName == request.Country {
|
||||
request.Filename = rs.db.CurrentFile[v.CountryShortName]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result := contracts.RecipeDashboardResponse{
|
||||
ConfigNumber: recipe.MachineSetting.ConfigNumber,
|
||||
LastUpdated: recipe.Timestamp,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue