为多个组件和API添加用户登录状态检查,确保只有在用户登录后才能查询和修改收藏状态
This commit is contained in:
@@ -6,6 +6,7 @@ import { Colors } from "@/constants/theme";
|
||||
import { useAppState } from "@/context/AppStateContext";
|
||||
import { useTheme } from "@/context/ThemeContext";
|
||||
import { checkFavorite, fetchLiveScore } from "@/lib/api";
|
||||
import { storage } from "@/lib/storage";
|
||||
import { LiveScoreMatch, Match } from "@/types/api";
|
||||
import { useRouter } from "expo-router";
|
||||
import React, { useEffect, useState } from "react";
|
||||
@@ -66,18 +67,23 @@ export default function LiveScreen() {
|
||||
isLive: true,
|
||||
}));
|
||||
|
||||
// 直接传递 match.id 查询是否收藏,并更新列表状态
|
||||
const listWithFavStatus = await Promise.all(
|
||||
converted.map(async (m) => {
|
||||
try {
|
||||
const favRes = await checkFavorite("match", m.id);
|
||||
return { ...m, fav: favRes.isFavorite };
|
||||
} catch (error) {
|
||||
console.error(`Check favorite failed for match ${m.id}:`, error);
|
||||
return m;
|
||||
}
|
||||
})
|
||||
);
|
||||
const token = await storage.getAccessToken();
|
||||
let listWithFavStatus = converted;
|
||||
|
||||
if (token) {
|
||||
// 直接传递 match.id 查询是否收藏,并更新列表状态
|
||||
listWithFavStatus = await Promise.all(
|
||||
converted.map(async (m) => {
|
||||
try {
|
||||
const favRes = await checkFavorite("match", m.id);
|
||||
return { ...m, fav: favRes.isFavorite };
|
||||
} catch (error) {
|
||||
console.error(`Check favorite failed for match ${m.id}:`, error);
|
||||
return m;
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// 将收藏的比赛置顶
|
||||
const sortedList = [...listWithFavStatus].sort((a, b) => {
|
||||
|
||||
Reference in New Issue
Block a user