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

184 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 {
FormArray,
FormControl,
FormGroup,
ReactiveFormsModule,
} from '@angular/forms';
2023-10-05 16:04:08 +07:00
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { isEqual } from 'lodash';
import { BehaviorSubject, Subject, 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,
RecipeListDataFormGroup,
} from './recipe-list/recipe-list.component';
import { MatRecipe } from 'src/app/core/models/recipe.model';
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,
private _recipeService: RecipeService
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>(''),
lastModified: new FormControl<Date>(new Date()),
2023-10-05 16:04:08 +07:00
price: new FormControl<number>(0),
isUse: new FormControl<boolean>(false),
isShow: new FormControl<boolean>(false),
disable: new FormControl<boolean>(false),
});
materialListIds$: Subject<{
ids: number[];
matRecipeList: MatRecipe[];
}> = new Subject<{
ids: number[];
matRecipeList: MatRecipe[];
}>();
2023-10-05 16:04:08 +07:00
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,
lastModified: recipe.LastChange,
2023-10-05 16:04:08 +07:00
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-06 15:33:10 +07:00
const ids = recipe.recipes?.map((recipe) => recipe.materialPathId);
this.materialListIds$.next({
ids: ids || [],
matRecipeList: recipe.recipes || [],
2023-10-06 15:33:10 +07:00
});
this.recipeMetaData = recipeMetaData;
this.isLoaded = 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-20 14:32:16 +07:00
// TODO: update value in targeted recipe
this._recipeService.editChanges(
2023-10-24 18:01:52 +07:00
this._recipeService.getCurrentCountry(),
this._recipeService.getCurrentFile(),
2023-10-20 14:32:16 +07:00
{
2023-10-24 18:01:52 +07:00
...this.recipeDetail,
2023-10-20 14:32:16 +07:00
}
);
2023-10-24 18:01:52 +07:00
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
}