添加收藏
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { ThemedText } from "@/components/themed-text";
|
||||
import { IconSymbol } from "@/components/ui/icon-symbol";
|
||||
import { addFavorite, checkFavorite, removeFavorite } from "@/lib/api";
|
||||
import { MatchDetailData } from "@/types/api";
|
||||
import { LinearGradient } from "expo-linear-gradient";
|
||||
import { useRouter } from "expo-router";
|
||||
import React from "react";
|
||||
import React, { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Image, StyleSheet, TouchableOpacity, View } from "react-native";
|
||||
|
||||
@@ -18,6 +19,50 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) {
|
||||
const { t } = useTranslation();
|
||||
const { match } = data;
|
||||
|
||||
const [isFav, setIsFav] = useState(false);
|
||||
const [favLoading, setFavLoading] = useState(false);
|
||||
|
||||
// 检查收藏状态
|
||||
React.useEffect(() => {
|
||||
const loadFavStatus = async () => {
|
||||
try {
|
||||
const res = await checkFavorite("match", match.eventKey.toString());
|
||||
setIsFav(res.isFavorite);
|
||||
} catch (error) {
|
||||
console.error("Check favorite status error:", error);
|
||||
}
|
||||
};
|
||||
if (match.eventKey) {
|
||||
loadFavStatus();
|
||||
}
|
||||
}, [match.eventKey]);
|
||||
|
||||
const toggleFavorite = async () => {
|
||||
if (favLoading) return;
|
||||
setFavLoading(true);
|
||||
const newFavState = !isFav;
|
||||
try {
|
||||
if (newFavState) {
|
||||
await addFavorite({
|
||||
matchId: Number(match.eventKey),
|
||||
type: "match",
|
||||
typeId: match.eventKey.toString(),
|
||||
notify: true,
|
||||
});
|
||||
} else {
|
||||
await removeFavorite({
|
||||
type: "match",
|
||||
typeId: match.eventKey.toString(),
|
||||
});
|
||||
}
|
||||
setIsFav(newFavState);
|
||||
} catch (error) {
|
||||
console.error("Toggle favorite error:", error);
|
||||
} finally {
|
||||
setFavLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<LinearGradient
|
||||
colors={["#521e10", "#0e0e10"]}
|
||||
@@ -46,8 +91,16 @@ export function ScoreHeader({ data, isDark, topInset }: ScoreHeaderProps) {
|
||||
<TouchableOpacity style={styles.iconBtn}>
|
||||
<IconSymbol name="notifications-outline" size={24} color="#FFF" />
|
||||
</TouchableOpacity>
|
||||
<TouchableOpacity style={styles.iconBtn}>
|
||||
<IconSymbol name="star-outline" size={24} color="#FFF" />
|
||||
<TouchableOpacity
|
||||
style={styles.iconBtn}
|
||||
onPress={toggleFavorite}
|
||||
disabled={favLoading}
|
||||
>
|
||||
<IconSymbol
|
||||
name={isFav ? "star" : "star-outline"}
|
||||
size={24}
|
||||
color={isFav ? "#FFD700" : "#FFF"}
|
||||
/>
|
||||
</TouchableOpacity>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
Reference in New Issue
Block a user