diff options
Diffstat (limited to 'lib/anilist/useAnilist.js')
| -rw-r--r-- | lib/anilist/useAnilist.js | 250 |
1 files changed, 163 insertions, 87 deletions
diff --git a/lib/anilist/useAnilist.js b/lib/anilist/useAnilist.js index 72e11ca..17ab11b 100644 --- a/lib/anilist/useAnilist.js +++ b/lib/anilist/useAnilist.js @@ -1,63 +1,107 @@ -import { useState, useEffect } from "react"; import { toast } from "react-toastify"; -export const useAniList = (session, stats) => { - const [media, setMedia] = useState([]); +export const useAniList = (session) => { 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, + ...(accessToken && { Authorization: `Bearer ${accessToken}` }), }, 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 - } - } - } - } - } + const quickSearch = async ({ search, type, isAdult = false }) => { + if (!search || search === " ") return; + const searchQuery = ` + query ($type: MediaType, $search: String, $isAdult: Boolean) { + Page(perPage: 8) { + pageInfo { + total + hasNextPage + } + results: media(type: $type, isAdult: $isAdult, search: $search) { + id + title { + userPreferred + } + coverImage { + medium } + type + format + bannerImage + isLicensed + genres + startDate { + year + } + } + } +} `; - fetchGraphQL(queryMedia, { username, status: status?.stats }).then((data) => - setMedia(data.data.MediaListCollection.lists) - ); - }, [username, accessToken, status?.stats]); + const data = await fetchGraphQL(searchQuery, { search, type, isAdult }); + return data; + }; + + const multiSearch = async (search) => { + if (!search || search === " ") return; + const searchQuery = ` + query ($search: String, $isAdult: Boolean) { + anime: Page(perPage: 8) { + pageInfo { + total + hasNextPage + } + results: media(type: ANIME, isAdult: $isAdult, search: $search) { + id + title { + userPreferred + } + coverImage { + medium + } + type + format + bannerImage + isLicensed + genres + startDate { + year + } + } + } + manga: Page(perPage: 8) { + pageInfo { + total + hasNextPage + } + results: media(type: MANGA, isAdult: $isAdult, search: $search) { + id + title { + userPreferred + } + coverImage { + medium + } + type + format + bannerImage + isLicensed + startDate { + year + } + } + } +} +`; + const data = await fetchGraphQL(searchQuery, { search }); + return data; + }; const markComplete = async (mediaId) => { if (!accessToken) return; @@ -94,7 +138,10 @@ export const useAniList = (session, stats) => { query ($id: Int) { Media(id: $id) { mediaListEntry { + progress + status customLists + repeat } id type @@ -103,6 +150,11 @@ export const useAniList = (session, stats) => { english native } + format + episodes + nextAiringEpisode { + episode + } } } `; @@ -110,23 +162,11 @@ export const useAniList = (session, stats) => { 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) { + mutation($mediaId: Int, $progress: Int, $status: MediaListStatus, $progressVolumes: Int, $lists: [String], $repeat: Int) { + SaveMediaListEntry(mediaId: $mediaId, progress: $progress, status: $status, progressVolumes: $progressVolumes, customLists: $lists, repeat: $repeat) { id mediaId progress @@ -137,46 +177,82 @@ export const useAniList = (session, stats) => { 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); + if (media && media.type !== "MANGA") { + let customList; + + if (session.user.name) { + const res = await fetch( + `/api/user/profile?name=${session.user.name}` + ).then((res) => res.json()); + customList = res?.setting === null ? true : res?.setting?.CustomLists; } - let lists = media?.mediaListEntry?.customLists - ? Object.entries(media?.mediaListEntry?.customLists) + 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")) { + + if (customList === true && !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", - }); + + const singleEpisode = + (!media.episodes || + (media.format === "MOVIE" && media.episodes === 1)) && + 1; + const videoEpisode = Number(progress) || singleEpisode; + const mediaEpisode = + media.nextAiringEpisode?.episode || media.episodes || singleEpisode; + const status = + media.mediaListEntry?.status === "REPEATING" ? "REPEATING" : "CURRENT"; + + let variables = { + mediaId, + progress, + status, + progressVolumes: volumeProgress, + lists, + }; + + if (videoEpisode === mediaEpisode) { + variables.status = "COMPLETED"; + if (media.mediaListEntry?.status === "REPEATING") + variables.repeat = media.mediaListEntry.repeat + 1; } + + // if (lists.length > 0) { + await fetchGraphQL(progressWatched, variables); + console.log(`Progress Updated: ${progress}`, status); + // } + } else if (media && media.type === "MANGA") { + let variables = { + mediaId, + progress, + status: stats, + progressVolumes: volumeProgress, + }; + + await fetchGraphQL(progressWatched, variables); + console.log(`Progress Updated: ${progress}`, status); + toast.success(`Progress Updated: ${progress}`, { + position: "bottom-right", + autoClose: 5000, + hideProgressBar: false, + closeOnClick: true, + draggable: true, + theme: "dark", + }); } }; - return { media, markComplete, markProgress, markPlanning, getUserLists }; + return { + markComplete, + markProgress, + markPlanning, + getUserLists, + multiSearch, + quickSearch, + }; }; |