From ac45ca47d5a18496eaefc4a61537efa12ab1def6 Mon Sep 17 00:00:00 2001 From: Kenta420-Poom Date: Wed, 27 Sep 2023 13:50:53 +0700 Subject: [PATCH] update recipe modal --- .../src/app/core/services/recipe.service.ts | 9 ++- .../shared/modal/recipe-modal.component.html | 56 ++++++++++++++++--- .../shared/modal/recipe-modal.component.ts | 38 ++++++++++++- server/routers/recipe.go | 19 +++++++ 4 files changed, 110 insertions(+), 12 deletions(-) diff --git a/client/src/app/core/services/recipe.service.ts b/client/src/app/core/services/recipe.service.ts index e75d3d4..e01aa73 100644 --- a/client/src/app/core/services/recipe.service.ts +++ b/client/src/app/core/services/recipe.service.ts @@ -1,7 +1,7 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable, distinctUntilChanged } from 'rxjs'; -import { Recipe } from '../models/recipe.model'; +import { Recipe, Recipe01 } from '../models/recipe.model'; import { environment } from 'src/environments/environment'; interface RecipeParams { @@ -33,4 +33,11 @@ export class RecipeService { responseType: 'json', }); } + + getRecipesById(id: string): Observable { + return this._httpClient.get(environment.api + '/recipes/' + id, { + withCredentials: true, + responseType: 'json', + }); + } } diff --git a/client/src/app/shared/modal/recipe-modal.component.html b/client/src/app/shared/modal/recipe-modal.component.html index 5d0b95f..3fa523c 100644 --- a/client/src/app/shared/modal/recipe-modal.component.html +++ b/client/src/app/shared/modal/recipe-modal.component.html @@ -18,13 +18,53 @@ diff --git a/client/src/app/shared/modal/recipe-modal.component.ts b/client/src/app/shared/modal/recipe-modal.component.ts index e79e748..1adfc77 100644 --- a/client/src/app/shared/modal/recipe-modal.component.ts +++ b/client/src/app/shared/modal/recipe-modal.component.ts @@ -1,13 +1,32 @@ import { Component, ElementRef, Input, ViewChild } from '@angular/core'; +import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; +import { RecipeService } from 'src/app/core/services/recipe.service'; + +interface RecipeDetail { + productCode: string; + name: string; + otherName: string; + description: string; +} @Component({ selector: 'recipe-modal', templateUrl: './recipe-modal.component.html', + imports: [ReactiveFormsModule], standalone: true, }) export class RecipeModalComponent { @Input({ required: true }) id!: string; + title: string = 'Recipe Detail'; + + recipeDetail = new FormGroup({ + productCode: new FormControl(''), + name: new FormControl(''), + otherName: new FormControl(''), + description: new FormControl(''), + }); + private detailModal: ElementRef | null = null; @ViewChild('detailModal', { static: false }) set setDetailModal( @@ -16,13 +35,26 @@ export class RecipeModalComponent { this.detailModal = modal; } + constructor(private recipeService: RecipeService) {} + openModal() { this.detailModal?.nativeElement.showModal(); if (this.detailModal?.nativeElement.open) { - console.log('open'); - } else { - console.log('close'); + this.recipeService.getRecipesById(this.id).subscribe((recipe) => { + this.title = recipe.name + ' | ' + recipe.productCode; + this.recipeDetail.setValue({ + productCode: recipe.productCode, + name: recipe.name, + otherName: recipe.otherName, + description: recipe.Description, + }); + }); } } + + save() { + console.log(this.recipeDetail.value); + this.detailModal?.nativeElement.close(); + } } diff --git a/server/routers/recipe.go b/server/routers/recipe.go index 936af58..6dec754 100644 --- a/server/routers/recipe.go +++ b/server/routers/recipe.go @@ -67,6 +67,25 @@ func (rr *RecipeRouter) Route(r chi.Router) { }) }) + r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + id, err := strconv.ParseUint(chi.URLParam(r, "id"), 10, 64) + if err != nil { + http.Error(w, "Invalid ID", http.StatusBadRequest) + return + } + + recipe := rr.data.GetRecipe01() + for _, v := range recipe { + if uint64(v.ID) == id { + json.NewEncoder(w).Encode(v) + return + } + } + + http.Error(w, "Recipe not found", http.StatusNotFound) + }) + r.Get("/json", func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(rr.data.GetRecipe())