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

220 lines
7 KiB
TypeScript
Raw Normal View History

import { CommonModule, DatePipe } from '@angular/common';
import { Component, EventEmitter, OnInit } from '@angular/core';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
2023-12-08 14:46:07 +07:00
import {Observable, first, map} from 'rxjs';
import { RecipeService } from 'src/app/core/services/recipe.service';
import { ConfirmModal } from 'src/app/shared/modal/confirm/confirm-modal.component';
import { animate, style, transition, trigger } from '@angular/animations';
import { RecipeListComponent } from './recipe-list/recipe-list.component';
import {
RecipeDetail,
RecipeDetailMat,
2023-12-29 16:10:57 +07:00
Topping,
} from 'src/app/core/models/recipe.model';
2023-12-08 14:46:07 +07:00
import { ActionRecord } from 'src/app/shared/actionRecord/actionRecord';
import { UserService } from 'src/app/core/services/user.service';
2023-12-18 09:20:42 +07:00
import { UserPermissions } from 'src/app/core/auth/userPermissions';
2023-12-29 16:10:57 +07:00
import { ToppingService } from 'src/app/core/services/topping.service';
@Component({
selector: 'app-recipe-details',
templateUrl: './recipe-details.component.html',
standalone: true,
imports: [
CommonModule,
RouterLink,
ReactiveFormsModule,
ConfirmModal,
DatePipe,
RecipeListComponent,
],
animations: [
trigger('inOutAnimation', [
transition(':enter', [
style({ opacity: 0 }),
animate('1s ease-out', style({ opacity: 1 })),
]),
]),
],
})
export class RecipeDetailsComponent implements OnInit {
title: string = 'Recipe Detail';
recipeDetail$!: Observable<RecipeDetail>;
isLoaded: boolean = false;
isMatLoaded: boolean = false;
actionRecord: ActionRecord<RecipeDetail | RecipeDetailMat> =
new ActionRecord();
recipeOriginalDetail!: typeof this.recipeDetailForm.value;
commit_msg :string = "";
2023-12-15 09:32:39 +07:00
//
department: string = this._route.parent!.snapshot.params['department'];
constructor(
private _formBuilder: FormBuilder,
private _route: ActivatedRoute,
private _router: Router,
private _recipeService: RecipeService,
2023-12-29 16:10:57 +07:00
private _toppingService: ToppingService,
private _userService: UserService
) {}
productCode!: string;
recipeDetailForm = this._formBuilder.group({
productCode: '',
name: '',
otherName: '',
2023-12-29 16:10:57 +07:00
description: '',
otherDescription: '',
lastModified: new Date(),
price: 0,
isUse: false,
isShow: false,
disable: false,
});
repl = []
2023-12-29 16:10:57 +07:00
topping: Topping | null = null;
ngOnInit() {
this.productCode = this._route.snapshot.params['productCode'];
this.recipeDetail$ = this._recipeService
.getRecipeDetail(this.productCode)
.pipe(first());
this.recipeDetail$.subscribe((detail) => {
console.log('Recipe Detail', detail);
this.recipeDetailForm.patchValue(detail);
this.isLoaded = true;
this.recipeOriginalDetail = { ...this.recipeDetailForm.getRawValue() };
});
this.recipeDetailForm.valueChanges.subscribe(this.onRecipeDetailFormChange);
2023-12-29 16:10:57 +07:00
this._toppingService.getToppings(this.department, this._recipeService.getCurrentFile()).subscribe((data) => {
this.topping = data;
})
// 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);
});
}
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');
// get username
let username:string = ""
2023-12-27 08:38:14 +07:00
username = this._userService.getCurrentUser()!.name;
let to_send = {
edit_by: username,
commit_msg: this.commit_msg,
productCode: this.productCode,
name: this.recipeDetailForm.getRawValue().name != this.recipeOriginalDetail.name ? this.recipeDetailForm.getRawValue().name : this.recipeOriginalDetail.name,
otherName: this.recipeDetailForm.getRawValue().otherName != this.recipeOriginalDetail.otherName ? this.recipeDetailForm.getRawValue().otherName : this.recipeOriginalDetail.otherName,
2023-12-29 16:10:57 +07:00
description: this.recipeDetailForm.getRawValue().description != this.recipeOriginalDetail.description ? this.recipeDetailForm.getRawValue().description : this.recipeOriginalDetail.description,
2023-12-27 08:38:14 +07:00
otherDescription: this.recipeDetailForm.getRawValue().otherDescription != this.recipeOriginalDetail.otherDescription ? this.recipeDetailForm.getRawValue().otherDescription : this.recipeOriginalDetail.otherDescription,
LastChange: this.recipeDetailForm.getRawValue().lastModified != this.recipeOriginalDetail.lastModified ? this.recipeDetailForm.getRawValue().lastModified : this.recipeOriginalDetail.lastModified,
price: this.recipeDetailForm.getRawValue().price != this.recipeOriginalDetail.price ? this.recipeDetailForm.getRawValue().price : this.recipeOriginalDetail.price,
// isUse: this,
// isShow: null,
// disable: null,
recipes: [
...(this.repl.length <= 0 ? [] : this.repl)
],
}
2023-12-27 08:38:14 +07:00
// TODO: update value in targeted recipe
console.log('to_send', to_send);
this._recipeService.editChanges(
this._recipeService.getCurrentCountry(),
this._recipeService.getCurrentFile(),
{
...to_send,
}
);
console.log('Sending changes');
void this._router.navigate(['/'+this.department+'/recipes']);
},
};
onKeyUpCommitMsg(e: any){
this.commit_msg = e.target.value;
}
confirmClose = {
title: 'The changes will be lost!',
message: 'Do you want to close without saving?',
confirmCallBack: () => {
console.log('confirm close');
2023-12-15 09:32:39 +07:00
this._router.navigate(['/'+this.department+'/recipes']);
},
};
onPressConfirmSave() {
if (this.isValueChanged) {
this.showConfirmSaveModal.emit(true);
} else {
2023-12-15 09:32:39 +07:00
this._router.navigate(['/'+this.department+'/recipes']);
}
}
onPressConfirmClose() {
if (this.isValueChanged) {
this.showConfirmCloseModal.emit(true);
} else {
2023-12-15 09:32:39 +07:00
this._router.navigate(['/'+this.department+'/recipes']);
}
}
isValueChanged: boolean = false;
onRecipeDetailFormChange(recipeDetail: typeof this.recipeDetailForm.value) {
2023-12-27 08:38:14 +07:00
// console.log('Recipe Detail Form Changed', recipeDetail);
}
onRecipeListFormChange(repl: unknown[]) {
2023-12-27 08:38:14 +07:00
// console.log('Recipe List Form Changed', repl);
this.repl = repl as never[];
2023-12-08 14:46:07 +07:00
this.isValueChanged ||= repl != undefined;
}
2023-12-18 09:20:42 +07:00
isEditable(){
return this._userService.getCurrentUser()!.permissions.includes(UserPermissions.EDITOR);
}
}