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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
import { useState, useEffect } from "react";
import { toast } from "react-toastify";
export const useAniList = (session, stats) => {
const [media, setMedia] = useState([]);
const accessToken = session?.user?.token;
const username = session?.user?.name;
const status = stats || null;
const fetchGraphQL = async (query, variables) => {
const response = await fetch("https://graphql.anilist.co/", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: accessToken ? `Bearer ${accessToken}` : undefined,
},
body: JSON.stringify({ query, variables }),
});
return response.json();
};
useEffect(() => {
if (!username || !accessToken) return;
const queryMedia = `
query ($username: String, $status: MediaListStatus) {
MediaListCollection(userName: $username, type: ANIME, status: $status) {
lists {
status
name
entries {
id
mediaId
status
progress
score
media {
id
status
nextAiringEpisode {
timeUntilAiring
episode
}
title {
english
romaji
}
episodes
coverImage {
large
}
}
}
}
}
}
`;
fetchGraphQL(queryMedia, { username, status: status?.stats }).then((data) =>
setMedia(data.data.MediaListCollection.lists)
);
}, [username, accessToken, status?.stats]);
const markComplete = async (mediaId) => {
if (!accessToken) return;
const completeQuery = `
mutation($mediaId: Int) {
SaveMediaListEntry(mediaId: $mediaId, status: COMPLETED) {
id
mediaId
status
}
}
`;
const data = await fetchGraphQL(completeQuery, { mediaId });
console.log({ Complete: data });
};
const markPlanning = async (mediaId) => {
if (!accessToken) return;
const planningQuery = `
mutation($mediaId: Int ) {
SaveMediaListEntry(mediaId: $mediaId, status: PLANNING) {
id
mediaId
status
}
}
`;
const data = await fetchGraphQL(planningQuery, { mediaId });
console.log({ added_to_list: data });
};
const getUserLists = async (id) => {
const getLists = `
query ($id: Int) {
Media(id: $id) {
mediaListEntry {
customLists
}
id
type
title {
romaji
english
native
}
}
}
`;
const data = await fetchGraphQL(getLists, { id });
return data;
};
const customLists = async (lists) => {
const setList = `
mutation($lists: [String]){
UpdateUser(animeListOptions: { customLists: $lists }){
id
}
}
`;
const data = await fetchGraphQL(setList, { lists });
return data;
};
const markProgress = async (mediaId, progress, stats, volumeProgress) => {
if (!accessToken) return;
const progressWatched = `
mutation($mediaId: Int, $progress: Int, $status: MediaListStatus, $progressVolumes: Int, $lists: [String]) {
SaveMediaListEntry(mediaId: $mediaId, progress: $progress, status: $status, progressVolumes: $progressVolumes, customLists: $lists) {
id
mediaId
progress
status
}
}
`;
const user = await getUserLists(mediaId);
const media = user?.data?.Media;
if (media) {
let checkList = media?.mediaListEntry?.customLists
? Object.entries(media?.mediaListEntry?.customLists).map(
([key, value]) => key
) || []
: [];
if (!checkList?.includes("Watched using Moopa")) {
checkList.push("Watched using Moopa");
await customLists(checkList);
}
let lists = media?.mediaListEntry?.customLists
? Object.entries(media?.mediaListEntry?.customLists)
.filter(([key, value]) => value === true)
.map(([key, value]) => key) || []
: [];
if (!lists?.includes("Watched using Moopa")) {
lists.push("Watched using Moopa");
}
if (lists.length > 0) {
await fetchGraphQL(progressWatched, {
mediaId,
progress,
status: stats,
progressVolumes: volumeProgress,
lists,
});
console.log(`Progress Updated: ${progress}`);
toast.success(`Progress Updated: ${progress}`, {
position: "bottom-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
draggable: true,
theme: "dark",
});
}
}
};
return { media, markComplete, markProgress, markPlanning, getUserLists };
};
|