diff options
| author | Factiven <[email protected]> | 2023-04-14 00:14:12 +0700 |
|---|---|---|
| committer | Factiven <[email protected]> | 2023-04-14 00:14:12 +0700 |
| commit | 70fda74d27d9b55c9030932794daa25c1e6cf50d (patch) | |
| tree | 0c1f639be4e8ec1b960ba642b8e99c1485e83b1a /pages/lib/useAnilist.js | |
| parent | Update 5th (diff) | |
| download | moopa-70fda74d27d9b55c9030932794daa25c1e6cf50d.tar.xz moopa-70fda74d27d9b55c9030932794daa25c1e6cf50d.zip | |
Update 6th
Diffstat (limited to 'pages/lib/useAnilist.js')
| -rw-r--r-- | pages/lib/useAnilist.js | 208 |
1 files changed, 0 insertions, 208 deletions
diff --git a/pages/lib/useAnilist.js b/pages/lib/useAnilist.js deleted file mode 100644 index 12317f8..0000000 --- a/pages/lib/useAnilist.js +++ /dev/null @@ -1,208 +0,0 @@ -import { useState, useEffect } from "react"; - -export function useAniList(session) { - const [media, setMedia] = useState([]); - // const [aniAdvanceSearch, setAniAdvanceSearch] = useState([]); - - // Queries - - const queryMedia = ` - query ($username: String) { - MediaListCollection(userName: $username, type: ANIME) { - lists { - status - name - entries { - id - mediaId - status - progress - score - media { - id - title { - english - romaji - } - episodes - coverImage { - large - } - } - } - } - } - } - `; - - const advance = ` - query ($search: String, $type: MediaType, $status: MediaStatus, $season: MediaSeason, $year: Int, $genres: [String], $tags: [String], $sort: [MediaSort], $page: Int, $perPage: Int) { - Page (page: $page, perPage: $perPage) { - pageInfo { - total - currentPage - lastPage - hasNextPage - } - media (search: $search, type: $type, status: $status, season: $season, seasonYear: $year, genre_in: $genres, tag_in: $tags, sort: $sort) { - id - title { - userPreferred - } - type - episodes - status - format - coverImage { - extraLarge - color - } - averageScore - isAdult - } - } - } - `; - - // Mutations - - const completeQuery = ` - mutation($mediaId: Int ) { - SaveMediaListEntry(mediaId: $mediaId, status: COMPLETED) { - id - mediaId - status - } - } - `; - - const progressWatched = ` - mutation($mediaId: Int, $progress: Int) { - SaveMediaListEntry(mediaId: $mediaId, progress: $progress) { - id - mediaId - progress - status - } - } - `; - - const username = session?.user?.name; - const accessToken = session?.user?.token; - - useEffect(() => { - async function fetchData() { - if (!username || !accessToken) return; - - const response = await fetch("https://graphql.anilist.co/", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - query: queryMedia, - variables: { - username: username, - }, - }), - }); - - const data = await response.json(); - setMedia(data.data.MediaListCollection.lists); - } - - fetchData(); - }, [queryMedia, username, accessToken]); - - async function markComplete(mediaId) { - if (!accessToken) return; - const response = await fetch("https://graphql.anilist.co/", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${accessToken}`, - }, - body: JSON.stringify({ - query: completeQuery, - variables: { - mediaId: mediaId, - }, - }), - }); - if (response.ok) { - const data = await response.json(); - console.log({ Complete: data }); - } else if (response.status === 401) { - console.log("Unauthorized"); - } else if (response.status === 400) { - console.log("validation error"); - } - } - - async function markProgress(mediaId, progress) { - if (!accessToken) return; - const response = await fetch("https://graphql.anilist.co/", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${accessToken}`, - }, - body: JSON.stringify({ - query: progressWatched, - variables: { - mediaId: mediaId, - progress: progress, - }, - }), - }); - if (response.ok) { - console.log("Progress Updated"); - } else if (response.status === 401) { - console.log("Unauthorized"); - } else if (response.status === 400) { - console.log("validation error"); - } - } - - async function aniAdvanceSearch( - search, - type, - seasonYear, - season, - genres, - perPage, - sort - ) { - const response = await fetch("https://graphql.anilist.co/", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - query: advance, - variables: { - search: search, - type: type, - seasonYear: seasonYear, - season: season, - genres: genres, - perPage: perPage, - sort: sort, - page: 1, - }, - }), - }); - - const datas = await response.json(); - // console.log(search); - const data = datas.data.Page; - return data; - } - - return { - media, - markComplete, - aniAdvanceSearch, - markProgress, - }; -} |