summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/apis/hooks/episodes.ts
blob: 0dd473c4fe9c33268f16e7e9cdc590bf5d6f52c9 (plain)
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
import {
  QueryClient,
  useMutation,
  useQuery,
  useQueryClient,
} from "react-query";
import { usePaginationQuery } from "../queries/hooks";
import { QueryKeys } from "../queries/keys";
import api from "../raw";

const cacheEpisodes = (client: QueryClient, episodes: Item.Episode[]) => {
  episodes.forEach((item) => {
    client.setQueryData([QueryKeys.Episodes, item.sonarrEpisodeId], item);

    client.setQueryData(
      [
        QueryKeys.Series,
        item.sonarrSeriesId,
        QueryKeys.Episodes,
        item.sonarrEpisodeId,
      ],
      item
    );
  });
};

export function useEpisodesByIds(ids: number[]) {
  const client = useQueryClient();
  return useQuery(
    [QueryKeys.Series, QueryKeys.Episodes, ids],
    () => api.episodes.byEpisodeId(ids),
    {
      onSuccess: (data) => {
        cacheEpisodes(client, data);
      },
    }
  );
}

export function useEpisodesBySeriesId(id: number) {
  const client = useQueryClient();
  return useQuery(
    [QueryKeys.Series, id, QueryKeys.Episodes, QueryKeys.All],
    () => api.episodes.bySeriesId([id]),
    {
      onSuccess: (data) => {
        cacheEpisodes(client, data);
      },
    }
  );
}

export function useEpisodeWantedPagination() {
  return usePaginationQuery([QueryKeys.Series, QueryKeys.Wanted], (param) =>
    api.episodes.wanted(param)
  );
}

export function useEpisodeBlacklist() {
  return useQuery(
    [QueryKeys.Series, QueryKeys.Episodes, QueryKeys.Blacklist],
    () => api.episodes.blacklist()
  );
}

export function useEpisodeAddBlacklist() {
  const client = useQueryClient();
  return useMutation(
    [QueryKeys.Series, QueryKeys.Episodes, QueryKeys.Blacklist],
    (param: {
      seriesId: number;
      episodeId: number;
      form: FormType.AddBlacklist;
    }) => {
      const { seriesId, episodeId, form } = param;
      return api.episodes.addBlacklist(seriesId, episodeId, form);
    },
    {
      onSuccess: (_, { seriesId, episodeId }) => {
        client.invalidateQueries([QueryKeys.Series, QueryKeys.Blacklist]);
        client.invalidateQueries([QueryKeys.Series, seriesId]);
      },
    }
  );
}

export function useEpisodeDeleteBlacklist() {
  const client = useQueryClient();
  return useMutation(
    [QueryKeys.Series, QueryKeys.Episodes, QueryKeys.Blacklist],
    (param: { all?: boolean; form?: FormType.DeleteBlacklist }) =>
      api.episodes.deleteBlacklist(param.all, param.form),
    {
      onSuccess: (_, param) => {
        client.invalidateQueries([QueryKeys.Series, QueryKeys.Blacklist]);
      },
    }
  );
}

export function useEpisodeHistoryPagination() {
  return usePaginationQuery(
    [QueryKeys.Series, QueryKeys.Episodes, QueryKeys.History],
    (param) => api.episodes.history(param),
    false
  );
}

export function useEpisodeHistory(episodeId?: number) {
  return useQuery(
    [QueryKeys.Series, QueryKeys.Episodes, QueryKeys.History, episodeId],
    () => {
      if (episodeId) {
        return api.episodes.historyBy(episodeId);
      }
    }
  );
}