fix switch department not change country

This commit is contained in:
pakintada@gmail.com 2024-02-08 16:40:06 +07:00
parent d9a7961806
commit adefe31090
6 changed files with 152 additions and 92 deletions

View file

@ -3,6 +3,9 @@ import { CommonModule, NgOptimizedImage } from '@angular/common';
import { Router } from '@angular/router';
import { UserService } from '../services/user.service';
import { UserPermissions } from '../auth/userPermissions';
import { NotFoundHandler } from 'src/app/shared/helpers/notFoundHandler';
import { AsyncStorage } from 'src/app/shared/helpers/asyncStorage';
import { getCountryMapSwitcher } from 'src/app/shared/helpers/recipe';
@Component({
standalone: true,
@ -80,6 +83,9 @@ export class DepartmentComponent {
},
];
//
notfoundHandler = new NotFoundHandler();
constructor(
private router: Router,
private _userService: UserService
@ -108,6 +114,19 @@ export class DepartmentComponent {
}
onClick(id: string) {
// TODO: add handler for redirect
this.notfoundHandler.handleSwitchCountry(id, async () => {
// set country
await AsyncStorage.setItem('currentRecipeCountry', getCountryMapSwitcher(id));
// set filename, don't know which file was a target so use default
await AsyncStorage.setItem('currentRecipeFile', 'default');
}, async () => {
// set country to `tha`
await AsyncStorage.setItem('currentRecipeCountry', 'Thailand');
// set filename, don't know which file was a target so use default
await AsyncStorage.setItem('currentRecipeFile', 'default');
// safely return to recipes
});
void this.router.navigate([`/${id}/recipes`]);
}
}

View file

@ -59,7 +59,7 @@ export class MaterialSettingsComponent implements OnInit {
) {}
async ngOnInit(): Promise<void> {
this.notfoundHandler.handleInvalidDepartment(this.department!, () => {
this.notfoundHandler.handleInvalidDepartment(this.department!, async () => {
this._router.navigate(['departments']).then(() => {
window.location.reload();
});

View file

@ -110,27 +110,28 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
const isBottom = scrollTop + clientHeight >= scrollHeight - 10;
if (isBottom && !this.isLoadMore) {
this.isLoadMore = true;
(await this._recipeService
.getRecipeOverview({
(
await this._recipeService.getRecipeOverview({
offset: this.offset,
take: this.take,
search: this.oldSearchStr,
filename: this._recipeService.getCurrentFile(),
country: await this._recipeService.getCurrentCountry(this.department),
country: await this._recipeService.getCurrentCountry(
this.department
),
materialIds: this.selectMaterialFilter || [],
}))
.subscribe(({ result, hasMore, totalCount }) => {
// console.log("result in scroll", result);
if (this.recipeOverviewList) {
this.recipeOverviewList =
this.recipeOverviewList.concat(result);
} else {
this.recipeOverviewList = result;
}
this.offset += 10;
this.isHasMore = hasMore;
this.isLoadMore = false;
});
})
).subscribe(({ result, hasMore, totalCount }) => {
// console.log("result in scroll", result);
if (this.recipeOverviewList) {
this.recipeOverviewList = this.recipeOverviewList.concat(result);
} else {
this.recipeOverviewList = result;
}
this.offset += 10;
this.isHasMore = hasMore;
this.isLoadMore = false;
});
}
},
{ passive: true }
@ -151,12 +152,23 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
// TODO: check if department is legit
this.notfoundHandler.handleInvalidDepartment(this.department!, () => {
this._router.navigate(['departments']).then( () => {
this.notfoundHandler.handleInvalidDepartment(
this.department!,
async () => {
await this._router.navigate(['departments']);
window.location.reload();
} );
});
},
async () => {
console.log('error callback', this.department!);
// await AsyncStorage.setItem(
// 'currentRecipeCountry',
// getCountryMapSwitcher(this.department!)
// );
// // get default file that should be opened
}
);
this.recipesDashboard$ = this._recipeService
.getRecipesDashboard({
@ -165,21 +177,23 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
})
.pipe(
finalize(async () => {
(await this._recipeService
.getRecipeOverview({
(
await this._recipeService.getRecipeOverview({
offset: this.offset,
take: this.take,
search: this.oldSearchStr,
filename: this._recipeService.getCurrentFile(),
country: await 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;
this.isHasMore = hasMore;
this.isLoadMore = false;
});
})
).subscribe(({ result, hasMore, totalCount }) => {
this.recipeOverviewList = result;
this.offset += 10;
this.isHasMore = hasMore;
this.isLoadMore = false;
});
})
);
@ -188,12 +202,15 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
this.recipesDashboard$.subscribe(async (data) => {
this.currentVersion = data.configNumber;
console.log('current version', this.currentVersion);
console.log("data : ", data);
console.log('data : ', data);
// set default country and filename if was "default"
let currentFile = this._recipeService.getCurrentFile();
if(currentFile == "default"){
await AsyncStorage.setItem('currentRecipeCountry', await this._recipeService.getCurrentCountry(this.department!));
if (currentFile == 'default') {
await AsyncStorage.setItem(
'currentRecipeCountry',
await this._recipeService.getCurrentCountry(this.department!)
);
await AsyncStorage.setItem('currentRecipeFile', data.filename);
}
});
@ -201,18 +218,26 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
console.log('ngAfterViewInit::department', this.department);
console.log('::CurrentFile', this._recipeService.getCurrentFile());
(await this._materialService
.getFullMaterialDetail(
(
await this._materialService.getFullMaterialDetail(
this.department,
this._recipeService.getCurrentFile()
))
.subscribe((mat) => {
this.materialDetail = mat;
console.log(this.materialDetail?.length, "[0]=", mat?.at(0), "current country", this._recipeService.getCurrentCountry(), "currentFile", this._recipeService.getCurrentFile());
)
).subscribe((mat) => {
this.materialDetail = mat;
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]);
});
// check material detail
console.log('first material', this.materialDetail![0]);
});
// end of FIXME
@ -248,9 +273,7 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
},
});
(await
this._materialService
.getMaterialCodes())
(await this._materialService.getMaterialCodes())
.pipe(
map((mat) =>
mat.map((m) => ({
@ -263,15 +286,18 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
this.materialList = materials;
});
(await this._toppingService
.getToppings(this.department, this._recipeService.getCurrentFile()))
.subscribe((tp) => {
this.toppings = {
toppingGroup: tp.ToppingGroup,
toppingList: tp.ToppingList,
};
// console.log(this.toppings);
});
(
await this._toppingService.getToppings(
this.department,
this._recipeService.getCurrentFile()
)
).subscribe((tp) => {
this.toppings = {
toppingGroup: tp.ToppingGroup,
toppingList: tp.ToppingList,
};
// console.log(this.toppings);
});
this.initRecipeSelection();
}
@ -286,31 +312,29 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
}
async search(event: Event) {
// country
let country = await this._recipeService.getCurrentCountry(this.department).then(
(country) => country
);
let country = await this._recipeService
.getCurrentCountry(this.department)
.then((country) => country);
this.offset = 0;
this.isLoadMore = true;
this.oldSearchStr = this.searchStr;
(await this._recipeService
.getRecipeOverview({
(
await this._recipeService.getRecipeOverview({
offset: this.offset,
take: this.take,
search: this.oldSearchStr,
filename: this._recipeService.getCurrentFile(),
country: country,
materialIds: this.selectMaterialFilter || [],
}))
.subscribe(({ result, hasMore, totalCount }) => {
this.recipeOverviewList = result;
this.offset += 10;
this.isHasMore = hasMore;
this.isLoadMore = false;
});
})
).subscribe(({ result, hasMore, totalCount }) => {
this.recipeOverviewList = result;
this.offset += 10;
this.isHasMore = hasMore;
this.isLoadMore = false;
});
}
// Recipe Version selection
@ -460,8 +484,7 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
await AsyncStorage.setItem('currentRecipeCountry', country);
// force reload, will fix this later
void this._router
.navigate([`/${getCountryMapSwitcher(country)}/recipes`]);
void this._router.navigate([`/${getCountryMapSwitcher(country)}/recipes`]);
}
async loadRecipe(recipeFileName: string) {
@ -474,10 +497,14 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
await AsyncStorage.setItem('currentRecipeFile', recipeFileName);
console.log('loadRecipe', recipeFileName, "currentCountry", this.department, "selectedCountry", this.selectedCountry);
console.log(
'loadRecipe',
recipeFileName,
'currentCountry',
this.department,
'selectedCountry',
this.selectedCountry
);
// clear all menus
this.recipeOverviewList = [];
@ -552,22 +579,20 @@ export class RecipesComponent implements OnInit, OnDestroy, AfterViewInit {
console.log(file_commit.Change_file.split('/')[2]);
(await this._recipeService
.getRecipeOverview({
(
await this._recipeService.getRecipeOverview({
offset: this.offset,
take: this.take,
search: this.oldSearchStr,
filename: file_commit.Change_file.split('/')[2],
country: country,
materialIds: this.selectMaterialFilter || [],
}))
.subscribe(({ result, hasMore, totalCount }) => {
console.log('loadSavedFile', result);
this._recipeService.setCurrentFile(
file_commit.Change_file.split('/')[2]
);
window.location.reload();
});
})
).subscribe(({ result, hasMore, totalCount }) => {
console.log('loadSavedFile', result);
this._recipeService.setCurrentFile(file_commit.Change_file.split('/')[2]);
window.location.reload();
});
}
ngOnDestroy(): void {

View file

@ -89,7 +89,7 @@ export class ToppingsComponent implements OnInit {
async ngOnInit(): Promise<void> {
this.notfoundHandler.handleInvalidDepartment(this.department!, () => {
this.notfoundHandler.handleInvalidDepartment(this.department!, async () => {
this._router.navigate(['departments']).then( () => {
window.location.reload();
} );

View file

@ -15,10 +15,23 @@ export class NotFoundHandler {
}
// if invalid, value is true then do callback
handleInvalidDepartment(department: string, callback: () => void) {
handleInvalidDepartment(department: string, callback: () => Promise<any>, error?: () => Promise<any>) {
let checkCondition = !departmentList().includes(department);
if(checkCondition) {
callback();
} else {
if(error)
error();
}
}
handleSwitchCountry(country: string, callback: () => Promise<any>, error?: () => Promise<any>) {
let checkCondition = departmentList().includes(country);
if(checkCondition) {
callback();
} else {
if(error)
error();
}
}