refactor(client): requireProfile() + asyncFetch wrapper

requireProfile():
- src/lib/client/profile.svelte.ts: neuer Helper, returnt das aktive
  Profile oder null nach standardisiertem alertAction
- 5x in recipes/[id]/+page.svelte: setRating, toggleFavorite, logCooked,
  addComment, toggleWishlist verlieren je 7 Zeilen Guard-Klausel
- profile-Variable im Closure macht den ! am profileStore.active obsolet

asyncFetch():
- src/lib/client/api-fetch-wrapper.ts: returnt Response auf 2xx, null
  nach alertAction auf Fehler
- 4 Call-Sites umgestellt: saveRecipe + saveTitle (recipes/[id]),
  saveEdit (admin/domains), rename (admin/profiles)
- admin/domains add() bewusst nicht migriert — inline-Error-UX statt Modal

Findings aus REVIEW-2026-04-18.md (Quick-Win 5) und redundancy.md
This commit is contained in:
hsiegeln
2026-04-18 22:22:19 +02:00
parent ff293e9db8
commit 30a447a3ea
5 changed files with 114 additions and 110 deletions

View File

@@ -1,4 +1,5 @@
import type { Profile } from '$lib/types';
import { alertAction } from '$lib/client/confirm.svelte';
const STORAGE_KEY = 'kochwas.activeProfileId';
@@ -60,3 +61,17 @@ class ProfileStore {
}
export const profileStore = new ProfileStore();
/**
* Returns the active profile, or null after showing the standard
* "kein Profil gewählt" dialog. Use as the first line of any per-profile
* action so we don't repeat the guard at every call-site.
*/
export async function requireProfile(): Promise<Profile | null> {
if (profileStore.active) return profileStore.active;
await alertAction({
title: 'Kein Profil gewählt',
message: 'Tippe oben rechts auf „Profil wählen", dann klappt die Aktion.'
});
return null;
}