update recipe modal
This commit is contained in:
parent
2b0590709c
commit
ac45ca47d5
4 changed files with 110 additions and 12 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { BehaviorSubject, Observable, distinctUntilChanged } from 'rxjs';
|
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';
|
import { environment } from 'src/environments/environment';
|
||||||
|
|
||||||
interface RecipeParams {
|
interface RecipeParams {
|
||||||
|
|
@ -33,4 +33,11 @@ export class RecipeService {
|
||||||
responseType: 'json',
|
responseType: 'json',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getRecipesById(id: string): Observable<Recipe01> {
|
||||||
|
return this._httpClient.get<Recipe01>(environment.api + '/recipes/' + id, {
|
||||||
|
withCredentials: true,
|
||||||
|
responseType: 'json',
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,13 +18,53 @@
|
||||||
|
|
||||||
<dialog class="modal" #detailModal>
|
<dialog class="modal" #detailModal>
|
||||||
<div class="modal-box w-11/12 max-w-5xl">
|
<div class="modal-box w-11/12 max-w-5xl">
|
||||||
<h3 class="font-bold text-lg">Hello! {{ id }}</h3>
|
<h3 class="font-bold text-lg mb-3">{{ title }}</h3>
|
||||||
<p class="py-4">Click the button below to close</p>
|
<form
|
||||||
<div class="modal-action">
|
class="flex flex-col gap-2"
|
||||||
<form method="dialog">
|
[formGroup]="recipeDetail"
|
||||||
<!-- if there is a button, it will close the modal -->
|
(ngSubmit)="save()"
|
||||||
<button class="btn">Close</button>
|
>
|
||||||
</form>
|
<div class="flex flex-col gap-1">
|
||||||
</div>
|
<label>
|
||||||
|
<span class="text-base label-text">Product Code: </span>
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="w-full input input-sm input-bordered input-ghost"
|
||||||
|
formControlName="productCode"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col gap-1">
|
||||||
|
<label><span class="text-base label-text">Name: </span> </label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="w-full input input-sm input-bordered input-ghost"
|
||||||
|
formControlName="name"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col gap-1">
|
||||||
|
<label><span class="text-base label-text"> Other Name:</span> </label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="w-full input input-sm input-bordered input-ghost"
|
||||||
|
formControlName="otherName"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="flex flex-col gap-1">
|
||||||
|
<label><span class="text-base label-text"> Description:</span> </label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
class="w-full input input-sm input-bordered input-ghost"
|
||||||
|
formControlName="description"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="modal-action">
|
||||||
|
<button type="submit" class="btn">Save</button>
|
||||||
|
<form method="dialog">
|
||||||
|
<!-- if there is a button, it will close the modal -->
|
||||||
|
<button class="btn">Close</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</dialog>
|
</dialog>
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,32 @@
|
||||||
import { Component, ElementRef, Input, ViewChild } from '@angular/core';
|
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({
|
@Component({
|
||||||
selector: 'recipe-modal',
|
selector: 'recipe-modal',
|
||||||
templateUrl: './recipe-modal.component.html',
|
templateUrl: './recipe-modal.component.html',
|
||||||
|
imports: [ReactiveFormsModule],
|
||||||
standalone: true,
|
standalone: true,
|
||||||
})
|
})
|
||||||
export class RecipeModalComponent {
|
export class RecipeModalComponent {
|
||||||
@Input({ required: true }) id!: string;
|
@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<HTMLDialogElement> | null = null;
|
private detailModal: ElementRef<HTMLDialogElement> | null = null;
|
||||||
|
|
||||||
@ViewChild('detailModal', { static: false }) set setDetailModal(
|
@ViewChild('detailModal', { static: false }) set setDetailModal(
|
||||||
|
|
@ -16,13 +35,26 @@ export class RecipeModalComponent {
|
||||||
this.detailModal = modal;
|
this.detailModal = modal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constructor(private recipeService: RecipeService) {}
|
||||||
|
|
||||||
openModal() {
|
openModal() {
|
||||||
this.detailModal?.nativeElement.showModal();
|
this.detailModal?.nativeElement.showModal();
|
||||||
|
|
||||||
if (this.detailModal?.nativeElement.open) {
|
if (this.detailModal?.nativeElement.open) {
|
||||||
console.log('open');
|
this.recipeService.getRecipesById(this.id).subscribe((recipe) => {
|
||||||
} else {
|
this.title = recipe.name + ' | ' + recipe.productCode;
|
||||||
console.log('close');
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
r.Get("/json", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(rr.data.GetRecipe())
|
json.NewEncoder(w).Encode(rr.data.GetRecipe())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue