Skip to content

Commit

Permalink
feat(full-app-gourmet): add ingredient on recipe creation flow to red…
Browse files Browse the repository at this point in the history
…irect to recipe edit
  • Loading branch information
dylanhitt committed Oct 29, 2024
1 parent 20c0f75 commit 15b1845
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 73 deletions.
143 changes: 74 additions & 69 deletions examples/full-app-gourmet/templa/admin/recipe.form.templ
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type RecipeFormProps struct {

templ RecipeForm(props RecipeFormProps) {
<form
id="recipe"
action={ templ.URL(props.FormAction) }
if props.IsCreating {
method="POST"
Expand Down Expand Up @@ -100,75 +101,79 @@ templ RecipeForm(props RecipeFormProps) {
</form>
<section id="ingredients-section">
<h2 class="mb-2 text-2xl font-bold">Ingredients</h2>
<ul class="list-disc pl-4">
for _, dosing := range props.Dosings {
<li class="mb-1">
<span>
{ dosing.Ingredient.Name }
</span>
if dosing.Unit != "unit" {
: <span class="font-semibold">{ strconv.Itoa(int(dosing.Quantity)) }</span>
<span>{ string(dosing.Unit) }</span>
}
</li>
}
</ul>
<form
method="POST"
class="form"
action="/admin/dosings-new"
hx-boost="true"
hx-target="#ingredients-section"
hx-select="#ingredients-section"
>
<input type="hidden" name="RecipeID" value={ props.Recipe.ID }/>
<div class="flex w-full items-end gap-2 md:gap-4">
<label class="label">
Name
<select
name="IngredientID"
class="input"
hx-get="/ingredients/preselect-unit"
hx-trigger="change"
hx-target="#unit"
hx-select="#unit"
hx-swap="outerHTML"
>
<option value="" selected>-</option>
for _, ingredient := range props.AllIngredients {
<option value={ ingredient.ID }>
{ ingredient.Name }
</option>
}
</select>
</label>
<label class="label">
Quantity
<input class="input" type="number" name="quantity"/>
</label>
<label class="label">
Unit
<select
id="unit"
name="unit"
class="input"
style="min-width: 5rem;"
>
for _, unit := range props.Units {
<option value={ unit }>
{ unit }
</option>
if props.IsCreating {
<button class="btn btn-primary" form="recipe" type="submit">Add Ingredients</button>
} else {
<ul class="list-disc pl-4">
for _, dosing := range props.Dosings {
<li class="mb-1">
<span>
{ dosing.Ingredient.Name }
</span>
if dosing.Unit != "unit" {
: <span class="font-semibold">{ strconv.Itoa(int(dosing.Quantity)) }</span>
<span>{ string(dosing.Unit) }</span>
}
</select>
</label>
<label class="label">
Optional
<div class="flex items-center justify-center">
<input type="checkbox" name=""/>
</div>
</label>
<button class="btn btn-primary aspect-square">+</button>
</div>
</form>
</li>
}
</ul>
<form
method="POST"
class="form"
action="/admin/dosings/new"
hx-boost="true"
hx-target="#ingredients-section"
hx-select="#ingredients-section"
>
<input type="hidden" name="RecipeID" value={ props.Recipe.ID }/>
<div class="flex w-full items-end gap-2 md:gap-4">
<label class="label">
Name
<select
name="IngredientID"
class="input"
hx-get="/ingredients/preselect-unit"
hx-trigger="change"
hx-target="#unit"
hx-select="#unit"
hx-swap="outerHTML"
>
<option value="" selected>-</option>
for _, ingredient := range props.AllIngredients {
<option value={ ingredient.ID }>
{ ingredient.Name }
</option>
}
</select>
</label>
<label class="label">
Quantity
<input class="input" type="number" name="quantity"/>
</label>
<label class="label">
Unit
<select
id="unit"
name="unit"
class="input"
style="min-width: 5rem;"
>
for _, unit := range props.Units {
<option value={ unit }>
{ unit }
</option>
}
</select>
</label>
<label class="label">
Optional
<div class="flex items-center justify-center">
<input type="checkbox" name=""/>
</div>
</label>
<button class="btn btn-primary aspect-square">+</button>
</div>
</form>
}
</section>
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ <h2 class="mb-2 text-2xl font-bold">Ingredients</h2>
<form
method="POST"
class="form"
action="/admin/dosings-new"
action="/admin/dosings/new"
hx-boost="true"
hx-target="#ingredients-section"
hx-select="#ingredients-section"
Expand Down
4 changes: 2 additions & 2 deletions examples/full-app-gourmet/views/admin.recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ func (rs Resource) adminAddRecipes(c *fuego.ContextWithBody[store.CreateRecipePa
return "", err
}

_, err = rs.RecipesQueries.CreateRecipe(c.Context(), body)
r, err := rs.RecipesQueries.CreateRecipe(c.Context(), body)
if err != nil {
return "", err
}

return c.Redirect(http.StatusMovedPermanently, "/admin/recipes")
return c.Redirect(http.StatusSeeOther, "/admin/recipes/"+r.ID)
}

func (rs Resource) adminCreateRecipePage(c *fuego.ContextNoBody) (fuego.Templ, error) {
Expand Down
2 changes: 1 addition & 1 deletion examples/full-app-gourmet/views/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (rs Resource) Routes(s *fuego.Server) {
fuego.Get(adminRoutes, "/recipes/create", rs.adminCreateRecipePage)
fuego.Put(adminRoutes, "/recipes/edit", rs.editRecipe)
fuego.Post(adminRoutes, "/recipes/new", rs.adminAddRecipes)
fuego.Post(adminRoutes, "/dosings-new", rs.adminAddDosing)
fuego.Post(adminRoutes, "/dosings/new", rs.adminAddDosing)
fuego.Get(adminRoutes, "/ingredients", rs.adminIngredients,
optionPagination,
)
Expand Down

0 comments on commit 15b1845

Please sign in to comment.