aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Media/Anime/cache.ts
blob: e988f255e57e3b19f4c5bbb0ee609d9a1d12a3b4 (plain) (blame)
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
import { get } from "svelte/store";
import anime from "$stores/anime";
import { mediaListCollection, Type } from "../../Data/AniList/media";
import lastPruneTimes from "$stores/lastPruneTimes";
import type {
	AniListAuthorisation,
	UserIdentity,
} from "../../Data/AniList/identity";

export const cleanCache = (
	user: AniListAuthorisation,
	identity: UserIdentity,
) =>
	mediaListCollection(
		user,
		identity,
		Type.Anime,
		get(anime),
		get(lastPruneTimes).anime,
		{
			forcePrune: true,
		},
	);

export const incrementMediaProgress = (
	id: number,
	progress: number | undefined,
	user: AniListAuthorisation,
	callback: () => void,
) => {
	fetch("https://graphql.anilist.co", {
		method: "POST",
		headers: {
			Authorization: `${user.tokenType} ${user.accessToken}`,
			"Content-Type": "application/json",
			Accept: "application/json",
		},
		body: JSON.stringify({
			query: `mutation { SaveMediaListEntry(mediaId: ${id}, progress: ${
				(progress || 0) + 1
			}) { id } }`,
		}),
	}).then(callback);
};