fix: resolve collection fetch race condition and improve tasting deletion UI
This commit is contained in:
@@ -12,17 +12,22 @@ export default function Home() {
|
|||||||
const supabase = createClientComponentClient();
|
const supabase = createClientComponentClient();
|
||||||
const [bottles, setBottles] = useState<any[]>([]);
|
const [bottles, setBottles] = useState<any[]>([]);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [user, setUser] = useState<any>(null); // Added user state
|
const [user, setUser] = useState<any>(null);
|
||||||
|
const [fetchError, setFetchError] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Check session
|
// Check session
|
||||||
const checkUser = async () => {
|
const checkUser = async () => {
|
||||||
try {
|
try {
|
||||||
const { data: { session }, error } = await supabase.auth.getSession();
|
const { data: { session }, error } = await supabase.auth.getSession();
|
||||||
|
console.log('Initial session check:', session ? 'User logged in' : 'No session');
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error('Session retrieval error:', error);
|
console.error('Session retrieval error:', error);
|
||||||
}
|
}
|
||||||
setUser(session?.user ?? null);
|
setUser(session?.user ?? null);
|
||||||
|
if (session?.user) {
|
||||||
|
fetchCollection();
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Fatal error checking user:', err);
|
console.error('Fatal error checking user:', err);
|
||||||
setUser(null);
|
setUser(null);
|
||||||
@@ -34,7 +39,8 @@ export default function Home() {
|
|||||||
checkUser();
|
checkUser();
|
||||||
|
|
||||||
// Listen for auth changes
|
// Listen for auth changes
|
||||||
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
|
const { data: { subscription } } = supabase.auth.onAuthStateChange((event, session) => {
|
||||||
|
console.log('Auth state change:', event, session?.user ? 'User logged in' : 'No user');
|
||||||
setUser(session?.user ?? null);
|
setUser(session?.user ?? null);
|
||||||
if (session?.user) {
|
if (session?.user) {
|
||||||
fetchCollection();
|
fetchCollection();
|
||||||
@@ -60,7 +66,12 @@ export default function Home() {
|
|||||||
`)
|
`)
|
||||||
.order('created_at', { ascending: false });
|
.order('created_at', { ascending: false });
|
||||||
|
|
||||||
if (error) throw error;
|
if (error) {
|
||||||
|
console.error('Supabase fetch error:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Fetched ${data?.length || 0} bottles from Supabase`);
|
||||||
|
|
||||||
// Process data to get the absolute latest tasting date for each bottle
|
// Process data to get the absolute latest tasting date for each bottle
|
||||||
const processedBottles = (data || []).map(bottle => {
|
const processedBottles = (data || []).map(bottle => {
|
||||||
@@ -79,7 +90,8 @@ export default function Home() {
|
|||||||
|
|
||||||
setBottles(processedBottles);
|
setBottles(processedBottles);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching collection:', err);
|
console.error('Detailed fetch error:', err);
|
||||||
|
setFetchError('Die Sammlung konnte nicht geladen werden. Bitte versuche es später erneut.');
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
@@ -140,6 +152,17 @@ export default function Home() {
|
|||||||
<div className="flex justify-center py-12">
|
<div className="flex justify-center py-12">
|
||||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-amber-600"></div>
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-amber-600"></div>
|
||||||
</div>
|
</div>
|
||||||
|
) : fetchError ? (
|
||||||
|
<div className="p-8 bg-red-50 dark:bg-red-900/10 border border-red-200 dark:border-red-900/30 rounded-3xl text-center">
|
||||||
|
<p className="text-red-600 dark:text-red-400 font-bold mb-2">Hoppla!</p>
|
||||||
|
<p className="text-red-500/80 text-sm italic">{fetchError}</p>
|
||||||
|
<button
|
||||||
|
onClick={fetchCollection}
|
||||||
|
className="mt-4 px-6 py-2 bg-zinc-900 dark:bg-zinc-100 text-white dark:text-zinc-900 rounded-xl text-xs font-bold uppercase tracking-widest"
|
||||||
|
>
|
||||||
|
Erneut versuchen
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<BottleGrid bottles={bottles} />
|
<BottleGrid bottles={bottles} />
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -120,13 +120,16 @@ export default function TastingList({ initialTastings }: TastingListProps) {
|
|||||||
<button
|
<button
|
||||||
onClick={() => handleDelete(note.id, note.bottle_id)}
|
onClick={() => handleDelete(note.id, note.bottle_id)}
|
||||||
disabled={!!isDeleting}
|
disabled={!!isDeleting}
|
||||||
className="p-2 text-zinc-400 hover:text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-xl transition-all disabled:opacity-50"
|
className="px-3 py-1.5 text-zinc-400 hover:text-red-600 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-xl transition-all disabled:opacity-50 flex items-center gap-2 border border-transparent hover:border-red-200 dark:hover:border-red-900/30 font-black text-[9px] uppercase tracking-widest"
|
||||||
title="Tasting löschen"
|
title="Tasting löschen"
|
||||||
>
|
>
|
||||||
{isDeleting === note.id ? (
|
{isDeleting === note.id ? (
|
||||||
<Loader2 size={14} className="animate-spin" />
|
<Loader2 size={14} className="animate-spin" />
|
||||||
) : (
|
) : (
|
||||||
|
<>
|
||||||
<Trash2 size={14} />
|
<Trash2 size={14} />
|
||||||
|
<span className="opacity-0 group-hover:opacity-100 transition-opacity">Löschen</span>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user