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

159 lines
4.4 KiB
TypeScript
Raw Normal View History

import { CommonModule, DatePipe } from '@angular/common';
2023-10-05 16:04:08 +07:00
import { Component, EventEmitter, OnInit } from '@angular/core';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
2023-10-05 16:04:08 +07:00
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { Observable, first } 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';
import { RecipeListComponent } from './recipe-list/recipe-list.component';
import {
RecipeDetail,
RecipeDetailMat,
} from 'src/app/core/models/recipe.model';
import { Action, ActionRecord } from 'src/app/shared/actionRecord/actionRecord';
import { isEqual } from 'lodash';
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: [
CommonModule,
2023-10-06 15:33:10 +07:00
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';
recipeDetail$!: Observable<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
actionRecord: ActionRecord<RecipeDetail | RecipeDetailMat> =
new ActionRecord();
recipeOriginalDetail!: typeof this.recipeDetailForm.value;
2023-10-05 16:04:08 +07:00
constructor(
private _formBuilder: FormBuilder,
2023-10-05 16:04:08 +07:00
private _route: ActivatedRoute,
private _router: Router,
private _recipeService: RecipeService
2023-10-05 16:04:08 +07:00
) {}
productCode!: string;
recipeDetailForm = this._formBuilder.group({
productCode: '',
name: '',
otherName: '',
description: '',
otherDescription: '',
lastModified: new Date(),
price: 0,
isUse: false,
isShow: false,
disable: false,
2023-10-05 16:04:08 +07:00
});
ngOnInit() {
this.productCode = this._route.snapshot.params['productCode'];
this.recipeDetail$ = this._recipeService
.getRecipeDetail(this.productCode)
.pipe(first());
this.recipeDetail$.subscribe((detail) => {
this.recipeDetailForm.patchValue(detail);
this.isLoaded = true;
this.recipeOriginalDetail = { ...this.recipeDetailForm.getRawValue() };
});
2023-11-27 19:58:07 +07:00
this.recipeDetailForm.valueChanges.subscribe(this.onRecipeDetailFormChange);
// snap recipe detail form value
this.actionRecord.registerOnAddAction((currAction, allAction) => {
if (currAction.type === 'recipeListData') {
switch (currAction.action) {
case 'add':
break;
case 'delete':
break;
}
}
console.log('Action Record', allAction);
});
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.getCurrentCountry(),
// this._recipeService.getCurrentFile(),
// {
// ...this.recipeDetail,
// }
// );
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
2023-11-27 19:58:07 +07:00
isValueChanged: boolean = false;
onRecipeDetailFormChange(recipeDetail: typeof this.recipeDetailForm.value) {
console.log('Recipe Detail Form Changed', recipeDetail);
}
onRecipeListFormChange(isValueChanged: boolean) {
console.log('Recipe List Form Changed', isValueChanged);
this.isValueChanged ||= isValueChanged;
2023-10-06 09:11:01 +07:00
}
2023-10-05 16:04:08 +07:00
}