aboutsummaryrefslogtreecommitdiff
path: root/lib/anilist/getMedia.js
diff options
context:
space:
mode:
authorFactiven <[email protected]>2023-09-13 00:45:53 +0700
committerGitHub <[email protected]>2023-09-13 00:45:53 +0700
commit7327a69b55a20b99b14ee0803d6cf5f8b88c45ef (patch)
treecbcca777593a8cc4b0282e7d85a6fc51ba517e25 /lib/anilist/getMedia.js
parentUpdate issue templates (diff)
downloadmoopa-7327a69b55a20b99b14ee0803d6cf5f8b88c45ef.tar.xz
moopa-7327a69b55a20b99b14ee0803d6cf5f8b88c45ef.zip
Update v4 - Merge pre-push to main (#71)
* Create build-test.yml * initial v4 commit * update: github workflow * update: push on branch * Update .github/ISSUE_TEMPLATE/bug_report.md * configuring next.config.js file
Diffstat (limited to 'lib/anilist/getMedia.js')
-rw-r--r--lib/anilist/getMedia.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/lib/anilist/getMedia.js b/lib/anilist/getMedia.js
new file mode 100644
index 0000000..c4628ab
--- /dev/null
+++ b/lib/anilist/getMedia.js
@@ -0,0 +1,90 @@
+import { useEffect, useState } from "react";
+
+export default function GetMedia(session, stats) {
+ const [media, setMedia] = useState([]);
+ const [recommendations, setRecommendations] = 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, $sort: [RecommendationSort]) {
+ Page(page: 1, perPage: 10) {
+ recommendations(sort: $sort, onList: true) {
+ mediaRecommendation {
+ id
+ title {
+ userPreferred
+ }
+ description
+ format
+ type
+ status(version: 2)
+ bannerImage
+ isAdult
+ coverImage {
+ extraLarge
+ }
+ }
+ }
+ }
+ MediaListCollection(userName: $username, type: ANIME, status: $status, sort: UPDATED_TIME_DESC) {
+ 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,
+ sort: "ID_DESC",
+ }).then((data) => {
+ setMedia(data.data.MediaListCollection.lists);
+ setRecommendations(
+ data.data.Page.recommendations.map((i) => i.mediaRecommendation)
+ );
+ });
+ }, [username, accessToken, status?.stats]);
+
+ return { media, recommendations };
+}