Taobin-Recipe-Manager/client/src/app/features/dashboard/dashboard.component.ts

150 lines
4 KiB
TypeScript
Raw Normal View History

2023-09-25 15:29:42 +07:00
import {
AfterContentChecked,
AfterRenderRef,
AfterViewChecked,
AfterViewInit,
Component,
ElementRef,
OnInit,
ViewChild,
} from '@angular/core';
2023-09-18 08:50:13 +07:00
import { UserService } from 'src/app/core/services/user.service';
import { User } from 'src/app/core/models/user.model';
2023-09-21 17:28:37 +07:00
import { DatePipe, NgFor, NgIf } from '@angular/common';
2023-09-25 15:29:42 +07:00
import { Recipe, Recipe01 } from 'src/app/core/models/recipe.model';
2023-09-21 17:28:37 +07:00
import { RecipeService } from 'src/app/core/services/recipe.service';
2023-09-25 15:29:42 +07:00
import { BehaviorSubject } from 'rxjs';
2023-09-25 16:59:10 +07:00
import { environment } from 'src/environments/environment';
2023-09-18 08:50:13 +07:00
@Component({
selector: 'app-dashboard',
standalone: true,
2023-09-21 17:28:37 +07:00
imports: [NgIf, NgFor, DatePipe],
2023-09-18 08:50:13 +07:00
templateUrl: './dashboard.component.html',
})
export class DashboardComponent implements OnInit {
userInfo: User | null = null;
2023-09-21 17:28:37 +07:00
recipes: Recipe | null = null;
2023-09-25 15:29:42 +07:00
recipes01: Recipe01[] | null = null;
tableHeads: string[] = [
'Product Code',
'Name',
'Other Name',
'Description',
'Last Updated',
];
private offset = 0;
private take = 10;
2023-09-21 15:57:35 +07:00
isLoaded: boolean = false;
2023-09-25 15:29:42 +07:00
isLoadMore: boolean = false;
isHasMore: boolean = true;
2023-09-25 16:59:10 +07:00
private searchStr = '';
private oldSearchStr = '';
2023-09-25 15:29:42 +07:00
@ViewChild('table', { static: false }) set content(table: ElementRef) {
table.nativeElement.addEventListener(
'scroll',
() => {
if (this.isHasMore === false) {
return;
}
const { scrollTop, scrollHeight, clientHeight } = table.nativeElement;
const isBottom = scrollTop + clientHeight >= scrollHeight - 10;
if (isBottom && !this.isLoadMore) {
this.isLoadMore = true;
this._recipeService
.getRecipes({
offset: this.offset,
take: this.take,
2023-09-25 16:59:10 +07:00
search: this.oldSearchStr,
2023-09-25 15:29:42 +07:00
})
.subscribe(({ recipes, hasMore }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
if (this.recipes01 && this.isHasMore) {
this.recipes01 = [...this.recipes01, ...Recipe01];
} else {
this.recipes01 = Recipe01;
}
this.recipes = {
...recipesWithoutRecipe01,
Recipe01: [],
};
this.offset += 10;
this.isLoadMore = false;
this.isHasMore = hasMore;
});
}
},
{ passive: true }
);
}
2023-09-21 15:57:35 +07:00
constructor(
private _userService: UserService,
2023-09-21 17:28:37 +07:00
private _recipeService: RecipeService
2023-09-21 15:57:35 +07:00
) {}
2023-09-18 08:50:13 +07:00
ngOnInit(): void {
this._userService.currentUser.subscribe((user) => {
this.userInfo = user;
});
2023-09-25 15:29:42 +07:00
this._recipeService
.getRecipes({
offset: this.offset,
take: this.take,
2023-09-25 16:59:10 +07:00
search: this.oldSearchStr,
2023-09-25 15:29:42 +07:00
})
.subscribe(({ recipes, hasMore }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
if (this.recipes01 && this.isHasMore) {
this.recipes01 = [...this.recipes01, ...Recipe01];
} else {
this.recipes01 = Recipe01;
}
this.recipes = {
...recipesWithoutRecipe01,
Recipe01: [],
};
this.offset += 10;
this.isLoaded = true;
this.isHasMore = hasMore;
});
2023-09-18 08:50:13 +07:00
}
2023-09-25 16:59:10 +07:00
setSearch(event: Event) {
this.searchStr = (event.target as HTMLInputElement).value;
}
search(event: Event) {
this.offset = 0;
this.oldSearchStr = this.searchStr;
this._recipeService
.getRecipes({
offset: this.offset,
take: this.take,
search: this.searchStr,
})
.subscribe(({ recipes, hasMore }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
this.recipes01 = Recipe01;
this.recipes = {
...recipesWithoutRecipe01,
Recipe01: [],
};
this.offset += 10;
this.isLoaded = true;
this.isHasMore = hasMore;
});
}
openJsonTab() {
window.open(environment.api + '/recipes/json', '_blank');
}
2023-09-18 08:50:13 +07:00
}