@@ -4,32 +4,20 @@ import LocalPrintshopOutlinedIcon from "@mui/icons-material/LocalPrintshopOutlin
44import HeaderBar from "./HeaderBar" ;
55import Sidebar from "./Sidebar" ;
66import { API_BASE_URL } from "../config" ;
7- import AuthService from "../services/AuthService" ;
87import { useAuth } from "../context/AuthContext" ;
98import { authFetch } from "../utils/authFetch" ;
109import { getLastActiveEvent } from "../services/eventStorage" ;
1110
1211function StickerDetail ( ) {
1312 const { id } = useParams ( ) ;
1413 const navigate = useNavigate ( ) ;
15- const { user, isLoading : authLoading } = useAuth ( ) ;
14+ const { user } = useAuth ( ) ;
1615 const [ sticker , setSticker ] = useState ( null ) ;
1716 const [ loading , setLoading ] = useState ( true ) ;
1817 const [ error , setError ] = useState ( null ) ;
1918 const [ isOwned , setIsOwned ] = useState ( false ) ;
2019
2120 useEffect ( ( ) => {
22- const userId = user ?. sub || user ?. email ;
23-
24- // Validate userId before fetching
25- if ( ! userId ) {
26- if ( ! authLoading ) {
27- setError ( 'Unable to identify user. Please log in again.' ) ;
28- setLoading ( false ) ;
29- }
30- return ;
31- }
32-
3321 const controller = new AbortController ( ) ;
3422
3523 const fetchSticker = async ( ) => {
@@ -47,20 +35,14 @@ function StickerDetail() {
4735 }
4836
4937 const data = await response . json ( ) ;
50- setSticker ( data ) ;
51-
52- // Check if user owns this sticker
53- const awardsResponse = await authFetch (
54- `${ API_BASE_URL } /api/awards/v1/assignments/${ encodeURIComponent ( userId ) } `
55- ) ;
56- if ( awardsResponse . ok ) {
57- const awardsData = await awardsResponse . json ( ) ;
58- const assignments = awardsData . stickers || [ ] ;
59- setIsOwned ( assignments . some ( ( a ) => a . stickerId === id ) ) ;
38+ if ( ! controller . signal . aborted ) {
39+ setSticker ( data ) ;
6040 }
6141 } catch ( err ) {
6242 console . error ( "Error fetching sticker:" , err ) ;
63- setError ( err . message ) ;
43+ if ( ! controller . signal . aborted ) {
44+ setError ( err . message ) ;
45+ }
6446 } finally {
6547 if ( ! controller . signal . aborted ) {
6648 setLoading ( false ) ;
@@ -69,8 +51,35 @@ function StickerDetail() {
6951 } ;
7052
7153 fetchSticker ( ) ;
54+ return ( ) => controller . abort ( ) ;
7255 } , [ id ] ) ;
7356
57+ // Check ownership when user context is available
58+ useEffect ( ( ) => {
59+ const userId = user ?. sub || user ?. email ;
60+ if ( ! userId || ! sticker ) return ;
61+
62+ const controller = new AbortController ( ) ;
63+
64+ const checkOwnership = async ( ) => {
65+ try {
66+ const awardsResponse = await authFetch (
67+ `${ API_BASE_URL } /api/awards/v1/assignments/${ encodeURIComponent ( userId ) } `
68+ ) ;
69+ if ( awardsResponse . ok && ! controller . signal . aborted ) {
70+ const awardsData = await awardsResponse . json ( ) ;
71+ const assignments = awardsData . stickers || [ ] ;
72+ setIsOwned ( assignments . some ( ( a ) => a . stickerId === id ) ) ;
73+ }
74+ } catch ( err ) {
75+ console . error ( "Error checking ownership:" , err ) ;
76+ }
77+ } ;
78+
79+ checkOwnership ( ) ;
80+ return ( ) => controller . abort ( ) ;
81+ } , [ id , user , sticker ] ) ;
82+
7483 const formatDate = ( dateString ) => {
7584 if ( ! dateString ) return "Unknown" ;
7685 return new Date ( dateString ) . toLocaleDateString ( "en-US" , {
0 commit comments