Refactor favorite button: wrap in form for better HTMX and AntiForgeryToken support
This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
@model Francesco.Recipes.World.Models.BackendModels.Recipe.Recipe
|
||||
|
||||
<button class="btn btn-link p-0"
|
||||
style="width: 40px; height: 40px;"
|
||||
hx-post="@Url.Action(Model.IsFavorite ? "RemoveFavorite" : "AddFavorite", "Recipe")"
|
||||
hx-vals='{"recipeId": "@Model.Id"}'
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML">
|
||||
@if (Model.IsFavorite)
|
||||
{
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#FFD700">
|
||||
<path d="M12 17.27L18.18 21 16.54 13.97
|
||||
22 9.24l-7.19-.62L12 2 9.19 8.62
|
||||
2 9.24l5.46 4.73L5.82 21z" />
|
||||
</svg>
|
||||
}
|
||||
else
|
||||
{
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#999" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="M12 17.27L18.18 21 16.54 13.97
|
||||
22 9.24l-7.19-.62L12 2 9.19 8.62
|
||||
2 9.24l5.46 4.73L5.82 21z" />
|
||||
</svg>
|
||||
}
|
||||
</button>
|
||||
<form hx-post="@Url.Action(Model.IsFavorite ? "RemoveFavorite" : "AddFavorite", "Recipe")"
|
||||
hx-target="this"
|
||||
hx-swap="outerHTML">
|
||||
@Html.AntiForgeryToken()
|
||||
<input type="hidden" name="recipeId" value="@Model.Id" />
|
||||
<button type="submit" class="btn btn-link p-0" style="width: 40px; height: 40px;">
|
||||
@if (Model.IsFavorite)
|
||||
{
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#FFD700">
|
||||
<path d="M12 17.27L18.18 21 16.54 13.97
|
||||
22 9.24l-7.19-.62L12 2 9.19 8.62
|
||||
2 9.24l5.46 4.73L5.82 21z" />
|
||||
</svg>
|
||||
}
|
||||
else
|
||||
{
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#999" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="M12 17.27L18.18 21 16.54 13.97
|
||||
22 9.24l-7.19-.62L12 2 9.19 8.62
|
||||
2 9.24l5.46 4.73L5.82 21z" />
|
||||
</svg>
|
||||
}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user