fix: use dynamic recipeLang in API calls instead of hardcoded /api/rezepte
Client-side navigation to /recipes hung because getUserFavorites and other endpoints were hardcoded to /api/rezepte, causing fetch mismatches during SvelteKit's client-side routing.
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
<script lang="ts">
|
||||
import { browser } from '$app/environment';
|
||||
import { enhance } from '$app/forms';
|
||||
import { page } from '$app/stores';
|
||||
|
||||
let { recipeId, isFavorite = $bindable(false), isLoggedIn = false } = $props<{ recipeId: string, isFavorite?: boolean, isLoggedIn?: boolean }>();
|
||||
|
||||
const recipeLang = $derived($page.url.pathname.split('/')[1] || 'rezepte');
|
||||
|
||||
let isLoading = $state(false);
|
||||
|
||||
async function toggleFavorite(event: Event) {
|
||||
@@ -17,7 +20,7 @@
|
||||
|
||||
try {
|
||||
const method = isFavorite ? 'DELETE' : 'POST';
|
||||
const response = await fetch('/api/rezepte/favorites', {
|
||||
const response = await fetch(`/api/${recipeLang}/favorites`, {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
||||
@@ -284,7 +284,7 @@
|
||||
const apiBase = `/api/${isEnglish ? 'recipes' : 'rezepte'}`;
|
||||
const [tagsRes, iconsRes] = await Promise.all([
|
||||
fetch(`${apiBase}/items/tag`),
|
||||
fetch('/api/rezepte/items/icon')
|
||||
fetch(`${apiBase}/items/icon`)
|
||||
]);
|
||||
availableTags = await tagsRes.json();
|
||||
availableIcons = await iconsRes.json();
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { Session } from '@auth/sveltekit';
|
||||
|
||||
type BriefRecipeWithFavorite = BriefRecipeType & { isFavorite: boolean };
|
||||
|
||||
export async function getUserFavorites(fetch: typeof globalThis.fetch, locals: App.Locals): Promise<string[]> {
|
||||
export async function getUserFavorites(fetch: typeof globalThis.fetch, locals: App.Locals, recipeLang = 'rezepte'): Promise<string[]> {
|
||||
const session = await locals.auth();
|
||||
|
||||
if (!session?.user?.nickname) {
|
||||
@@ -15,7 +15,7 @@ export async function getUserFavorites(fetch: typeof globalThis.fetch, locals: A
|
||||
}
|
||||
|
||||
try {
|
||||
const favRes = await fetch('/api/rezepte/favorites');
|
||||
const favRes = await fetch(`/api/${recipeLang}/favorites`);
|
||||
if (favRes.ok) {
|
||||
const favData = await favRes.json();
|
||||
return favData.favorites || [];
|
||||
@@ -44,11 +44,12 @@ export function addFavoriteStatusToRecipes(recipes: BriefRecipeType[], userFavor
|
||||
export async function loadRecipesWithFavorites(
|
||||
fetch: typeof globalThis.fetch,
|
||||
locals: App.Locals,
|
||||
recipeLoader: () => Promise<BriefRecipeType[]>
|
||||
recipeLoader: () => Promise<BriefRecipeType[]>,
|
||||
recipeLang = 'rezepte'
|
||||
): Promise<{ recipes: BriefRecipeWithFavorite[], session: Session | null }> {
|
||||
const [recipes, userFavorites, session] = await Promise.all([
|
||||
recipeLoader(),
|
||||
getUserFavorites(fetch, locals),
|
||||
getUserFavorites(fetch, locals, recipeLang),
|
||||
locals.auth()
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user