1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 | import { useState, useEffect } from 'react';
import styled from 'styled-components';
import { useAuth, useUserAnimeList, MediaListStatus } from '../../index';
import { CardGrid } from '../../index';
const Container = styled.div`
margin-top: 1rem;
margin-bottom: 1rem;
`;
const NoEntriesMessage = styled.div`
margin: 1.5rem;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5rem;
font-weight: bold;
`;
const NotLoggedIn = styled.div`
margin: 5rem;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5rem;
font-weight: bold;
`;
const StatusDropdown = styled.select`
margin-left: 1rem;
margin-bottom: 1.5rem;
padding: 0.75rem;
border-radius: var(--global-border-radius);
background-color: var(--global-secondary-bg);
color: var(--global-text);
border: none;
`;
const statusLabels = {
CURRENT: 'Watching',
PLANNING: 'Plan to Watch',
COMPLETED: 'Completed',
REPEATING: 'Re-watching',
PAUSED: 'Paused',
DROPPED: 'Dropped',
};
const apiStatusToUserFriendly = {
FINISHED: 'Completed',
RELEASING: 'Ongoing',
NOT_YET_RELEASED: 'Not yet aired',
CANCELLED: 'Cancelled',
HIATUS: 'Paused',
};
export const WatchingAnilist = () => {
const { isLoggedIn, userData } = useAuth();
const [selectedStatus, setSelectedStatus] = useState<string>(
localStorage.getItem('selectedStatus') || 'CURRENT',
);
useEffect(() => {
if (isLoggedIn && userData) {
console.log('User is logged in, username:', userData.name);
} else {
console.log('User is not logged in or userData is not available');
}
}, [isLoggedIn, userData]);
const { animeList, loading, error } = useUserAnimeList(
userData?.name,
selectedStatus as MediaListStatus,
);
if (!isLoggedIn)
return <NotLoggedIn>Please Log in to view your AniList.</NotLoggedIn>;
if (loading) return <NoEntriesMessage>Loading...</NoEntriesMessage>;
if (error)
return (
<NoEntriesMessage>
Error loading anime list: {error.message}
</NoEntriesMessage>
);
const animeData = animeList.lists.flatMap((list) =>
list.entries.map((entry) => ({
id: entry.media.id,
image: entry.media.coverImage.large,
title: {
romaji: entry.media.title.romaji,
english: entry.media.title.english || entry.media.title.romaji,
},
status: apiStatusToUserFriendly[entry.media.status] || 'Unknown',
rating: entry.media.averageScore,
releaseDate: entry.media.startDate.year,
totalEpisodes: entry.media.episodes,
color: entry.media.coverImage.color,
type: entry.media.format,
})),
);
const handleStatusChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const newStatus = e.target.value;
setSelectedStatus(newStatus);
localStorage.setItem('selectedStatus', newStatus);
};
return (
<Container>
<h3>
AniList
<StatusDropdown value={selectedStatus} onChange={handleStatusChange}>
{Object.values(MediaListStatus).map((status) => (
<option key={status} value={status}>
{statusLabels[status] || status}
</option>
))}
</StatusDropdown>
</h3>
{animeData.length > 0 ? (
<CardGrid
animeData={animeData}
hasNextPage={false}
onLoadMore={() => {}}
/>
) : (
<NoEntriesMessage>No Results</NoEntriesMessage>
)}
</Container>
);
};
|