Taobin-Recipe-Manager/client/src/app/features/recipes/recipe-details/recipe-details.component.ts

186 lines
5.3 KiB
TypeScript
Raw Normal View History

2023-10-06 15:33:10 +07:00
import { DatePipe, NgFor, NgIf } from '@angular/common';
2023-10-05 16:04:08 +07:00
import { Component, EventEmitter, OnInit } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { isEqual } from 'lodash';
2023-10-06 15:33:10 +07:00
import { delay, finalize } from 'rxjs';
2023-10-05 16:04:08 +07:00
import { RecipeService } from 'src/app/core/services/recipe.service';
import { ConfirmModal } from 'src/app/shared/modal/confirm/confirm-modal.component';
2023-10-06 09:11:01 +07:00
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { animate, style, transition, trigger } from '@angular/animations';
2023-10-06 15:33:10 +07:00
import { MatRecipe } from 'src/app/core/models/recipe.model';
import { MaterialService } from 'src/app/core/services/material.service';
2023-10-05 16:04:08 +07:00
interface RecipeDetail {
2023-10-06 15:33:10 +07:00
lastModified: Date;
2023-10-05 16:04:08 +07:00
productCode: string;
name: string;
otherName: string;
description: string;
otherDescription: string;
price: number;
isUse: boolean;
isShow: boolean;
disable: boolean;
2023-10-06 15:33:10 +07:00
recipes: MatRecipe[];
matData?: MaterialData[];
}
interface MaterialData {
id: number;
name: string;
enable: boolean;
2023-10-05 16:04:08 +07:00
}
interface RecipeMetaData {
productCode: string;
name: string;
otherName: string;
description: string;
otherDescription: string;
picture: string;
}
2023-10-05 13:07:56 +07:00
@Component({
selector: 'app-recipe-details',
templateUrl: './recipe-details.component.html',
standalone: true,
2023-10-06 15:33:10 +07:00
imports: [
NgIf,
NgFor,
RouterLink,
ReactiveFormsModule,
ConfirmModal,
DatePipe,
],
2023-10-06 09:11:01 +07:00
animations: [
trigger('inOutAnimation', [
transition(':enter', [
style({ opacity: 0 }),
animate('1s ease-out', style({ opacity: 1 })),
]),
]),
],
2023-10-05 13:07:56 +07:00
})
2023-10-05 16:04:08 +07:00
export class RecipeDetailsComponent implements OnInit {
title: string = 'Recipe Detail';
recipeMetaData: RecipeMetaData | null = null;
2023-10-06 15:33:10 +07:00
originalRecipeDetail!: RecipeDetail;
2023-10-05 16:04:08 +07:00
isLoaded: boolean = false;
2023-10-06 15:33:10 +07:00
isMatLoaded: boolean = false;
2023-10-05 16:04:08 +07:00
constructor(
private _route: ActivatedRoute,
private _router: Router,
2023-10-06 15:33:10 +07:00
private _recipeService: RecipeService,
private _materialService: MaterialService
2023-10-05 16:04:08 +07:00
) {}
recipeDetail = new FormGroup({
productCode: new FormControl<string>(''),
name: new FormControl<string>(''),
otherName: new FormControl<string>(''),
description: new FormControl<string>(''),
otherDescription: new FormControl<string>(''),
price: new FormControl<number>(0),
isUse: new FormControl<boolean>(false),
isShow: new FormControl<boolean>(false),
disable: new FormControl<boolean>(false),
});
ngOnInit() {
this._recipeService
.getRecipesById(this._route.snapshot.params['productCode'])
2023-10-06 15:33:10 +07:00
.pipe(finalize(() => {}))
2023-10-05 16:04:08 +07:00
.subscribe(({ recipe, recipeMetaData }) => {
this.title = recipe.name + ' | ' + recipe.productCode;
this.recipeDetail.patchValue({
productCode: recipe.productCode,
name: recipe.name,
otherName: recipe.otherName,
description: recipe.Description,
otherDescription: recipe.otherDescription,
price: recipe.cashPrice,
isUse: recipe.isUse,
isShow: recipe.isShow,
disable: recipe.disable,
});
this.originalRecipeDetail = {
2023-10-06 15:33:10 +07:00
lastModified: recipe.LastChange,
2023-10-05 16:04:08 +07:00
productCode: recipe.productCode,
name: recipe.name,
otherName: recipe.otherName,
description: recipe.Description,
otherDescription: recipe.otherDescription,
price: recipe.cashPrice,
isUse: recipe.isUse,
isShow: recipe.isShow,
disable: recipe.disable,
2023-10-06 15:33:10 +07:00
recipes: recipe.recipes,
2023-10-05 16:04:08 +07:00
};
this.recipeMetaData = recipeMetaData;
this.isLoaded = true;
2023-10-06 15:33:10 +07:00
const ids = this.originalRecipeDetail.recipes.map(
(recipe) => recipe.materialPathId
);
this._materialService.getMaterialCodes(ids).subscribe((data) => {
this.originalRecipeDetail.matData = data
.map((item) => {
return {
id: item.materialID,
name: item.PackageDescription,
enable: false,
};
})
.sort((a, b) => (a.id > b.id ? -1 : -1));
this.isMatLoaded = true;
});
2023-10-05 16:04:08 +07:00
});
}
showConfirmSaveModal: EventEmitter<boolean> = new EventEmitter<boolean>();
showConfirmCloseModal: EventEmitter<boolean> = new EventEmitter<boolean>();
confirmSave = {
title: 'The changes detected!',
message: 'Do you want to save changes?',
confirmCallBack: () => {
console.log('confirm save');
2023-10-06 09:11:01 +07:00
this._router.navigate(['/recipes']);
2023-10-05 16:04:08 +07:00
},
};
confirmClose = {
title: 'The changes will be lost!',
message: 'Do you want to close without saving?',
confirmCallBack: () => {
console.log('confirm close');
2023-10-06 09:11:01 +07:00
this._router.navigate(['/recipes']);
2023-10-05 16:04:08 +07:00
},
};
onPressConfirmSave() {
2023-10-06 09:11:01 +07:00
if (this.isValueChanged) {
2023-10-05 16:04:08 +07:00
this.showConfirmSaveModal.emit(true);
} else {
2023-10-06 09:11:01 +07:00
this._router.navigate(['/recipes']);
2023-10-05 16:04:08 +07:00
}
}
onPressConfirmClose() {
2023-10-06 09:11:01 +07:00
if (this.isValueChanged) {
2023-10-05 16:04:08 +07:00
this.showConfirmCloseModal.emit(true);
} else {
2023-10-06 09:11:01 +07:00
this._router.navigate(['/recipes']);
2023-10-05 16:04:08 +07:00
}
}
2023-10-06 09:11:01 +07:00
get isValueChanged() {
return !isEqual(this.recipeDetail.value, this.originalRecipeDetail);
}
2023-10-05 16:04:08 +07:00
}