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

233 lines
7.4 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-19 15:45:24 +07:00
import { BehaviorSubject, finalize, map } 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 { animate, style, transition, trigger } from '@angular/animations';
2023-10-06 15:33:10 +07:00
import { MaterialService } from 'src/app/core/services/material.service';
import { RecipeMetaData, RecipeDetail } from 'src/app/shared/types/recipe';
import { RecipeListComponent } from './recipe-list/recipe-list.component';
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,
RecipeListComponent,
2023-10-06 15:33:10 +07:00
],
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-19 15:45:24 +07:00
originalRecipeDetail: BehaviorSubject<RecipeDetail | null> =
new BehaviorSubject<RecipeDetail | null>(null);
matForRecipeList = this.originalRecipeDetail.pipe(
map((x) => x?.recipe.recipes)
);
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,
});
2023-10-19 15:45:24 +07:00
this.originalRecipeDetail.next({
recipe: {
lastModified: recipe.LastChange,
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-19 15:45:24 +07:00
});
2023-10-05 16:04:08 +07:00
this.recipeMetaData = recipeMetaData;
this.isLoaded = true;
2023-10-19 15:45:24 +07:00
});
2023-10-06 15:33:10 +07:00
2023-10-19 15:45:24 +07:00
this.originalRecipeDetail.subscribe((originalRecipeDetail) => {
if (originalRecipeDetail == null) return;
2023-10-19 15:45:24 +07:00
const ids = originalRecipeDetail.recipes?.map(
(recipe) => recipe.materialPathId
);
2023-10-20 17:05:24 +07:00
if (originalRecipeDetail.recipe.recipes != null) {
return;
}
2023-10-19 15:45:24 +07:00
this._materialService.getMaterialCodes(ids).subscribe((data) => {
this.originalRecipeDetail.next({
...originalRecipeDetail,
recipe: {
...originalRecipeDetail.recipe,
recipes: originalRecipeDetail
.recipes!.map((item) => {
for (let i = 0; i < data.length; i++) {
if (item.materialPathId === 0) {
return {
id: 0,
name: '',
enable: item.isUse,
mixOrder: item.MixOrder,
stirTime: item.stirTime,
powderGram: item.powderGram,
powderTime: item.powderTime,
syrupGram: item.syrupGram,
syrupTime: item.syrupTime,
waterCold: item.waterCold,
waterHot: item.waterYield,
};
}
if (item.materialPathId === data[i].materialID) {
return {
id: data[i].materialID,
name: data[i].PackageDescription,
enable: item.isUse,
mixOrder: item.MixOrder,
stirTime: item.stirTime,
powderGram: item.powderGram,
powderTime: item.powderTime,
syrupGram: item.syrupGram,
syrupTime: item.syrupTime,
waterCold: item.waterCold,
waterHot: item.waterYield,
};
}
}
2023-10-19 15:45:24 +07:00
return {
id: item.materialPathId,
name: '',
enable: item.isUse,
mixOrder: item.MixOrder,
stirTime: item.stirTime,
powderGram: item.powderGram,
powderTime: item.powderTime,
syrupGram: item.syrupGram,
syrupTime: item.syrupTime,
waterCold: item.waterCold,
waterHot: item.waterYield,
};
})
.sort((a, b) => {
return a.id === 0 ? 1 : a.id > b.id ? 1 : -1;
}),
},
2023-10-06 15:33:10 +07:00
});
2023-10-19 15:45:24 +07:00
this.isMatLoaded = true;
2023-10-05 16:04:08 +07:00
});
2023-10-19 15:45:24 +07:00
});
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-20 14:32:16 +07:00
// TODO: update value in targeted recipe
this._recipeService.editChanges(
this._recipeService.getCurrentVersion(),
{
...this.recipeDetail
}
);
console.log("Sending changes")
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() {
2023-10-19 15:45:24 +07:00
return !isEqual(
this.recipeDetail.value,
this.originalRecipeDetail.getValue()?.recipe
);
2023-10-06 09:11:01 +07:00
}
2023-10-05 16:04:08 +07:00
}