Implement user favorites feature for recipes

- Add UserFavorites MongoDB model with ObjectId references
- Create authenticated API endpoints for favorites management
- Add Heart icon and FavoriteButton components with toggle functionality
- Display favorite button below recipe tags for logged-in users
- Add Favoriten navigation link (visible only when authenticated)
- Create favorites page with grid layout and search functionality
- Store favorites by MongoDB ObjectId for data integrity

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-01 20:18:57 +02:00
parent 1d78b5439e
commit fe46ab194e
11 changed files with 460 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
<script>
</script>
<style>
@keyframes shake{
0%{
transform: rotate(0)
scale(1,1);
}
25%{
box-shadow: 0em 0em 1em 0.2em rgba(0, 0, 0, 0.6);
transform: rotate(30deg)
scale(1.2,1.2)
;
}
50%{
box-shadow: 0em 0em 1em 0.2em rgba(0, 0, 0, 0.6);
transform: rotate(-30deg)
scale(1.2,1.2);
}
74%{
box-shadow: 0em 0em 1em 0.2em rgba(0, 0, 0, 0.6);
transform: rotate(30deg)
scale(1.2, 1.2);
}
100%{
transform: rotate(0)
scale(1,1);
}
}
</style>
<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512" {...$$restProps}><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="m47.6 300.4 180.7 168.7c7.5 7 17.4 10.9 27.7 10.9s20.2-3.9 27.7-10.9L464.4 300.4c30.4-28.3 47.6-68 47.6-109.5v-5.8c0-69.9-50.5-129.5-119.4-141C347 36.5 300.6 51.4 268 84L256 96 244 84c-32.6-32.6-79-47.5-124.6-39.9C50.5 55.6 0 115.2 0 185.1v5.8c0 41.5 17.2 81.2 47.6 109.5z"/></svg>

View File

@@ -0,0 +1,102 @@
<script lang="ts">
import Heart from '$lib/assets/icons/Heart.svelte';
export let recipeId: string;
export let isFavorite: boolean = false;
export let isLoggedIn: boolean = false;
let isLoading = false;
async function toggleFavorite() {
if (!isLoggedIn || isLoading) return;
isLoading = true;
try {
const method = isFavorite ? 'DELETE' : 'POST';
const response = await fetch('/api/rezepte/favorites', {
method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ recipeId }),
});
if (response.ok) {
isFavorite = !isFavorite;
}
} catch (error) {
console.error('Failed to toggle favorite:', error);
} finally {
isLoading = false;
}
}
</script>
<style>
.favorite-button {
all: unset;
color: var(--nord0);
font-size: 1.1rem;
background-color: var(--nord5);
border-radius: 10000px;
padding: 0.5em 1em;
transition: 100ms;
box-shadow: 0em 0em 0.5em 0.05em rgba(0,0,0,0.3);
display: flex;
align-items: center;
gap: 0.5em;
cursor: pointer;
}
.favorite-button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.favorite-button.favorited {
background-color: var(--nord11);
color: white;
}
.favorite-button:not(.favorited):hover,
.favorite-button:not(.favorited):focus-visible {
transform: scale(1.1);
background-color: var(--nord11);
color: white;
box-shadow: 0.1em 0.1em 0.5em 0.1em rgba(0,0,0,0.3);
}
.favorite-button.favorited:hover,
.favorite-button.favorited:focus-visible {
transform: scale(1.1);
background-color: var(--nord5);
color: var(--nord0);
box-shadow: 0.1em 0.1em 0.5em 0.1em rgba(0,0,0,0.3);
}
@media (prefers-color-scheme: dark) {
.favorite-button {
background-color: var(--nord0);
color: white;
}
.favorite-button.favorited:hover,
.favorite-button.favorited:focus-visible {
background-color: var(--nord6);
color: var(--nord0);
}
}
</style>
{#if isLoggedIn}
<button
class="favorite-button"
class:favorited={isFavorite}
disabled={isLoading}
on:click={toggleFavorite}
>
<Heart />
{isFavorite ? 'Favorit entfernen' : 'Als Favorit speichern'}
</button>
{/if}