aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/apis/hooks/episodes.ts
blob: 956fd103f61351453602429a98b53bcbdb1e5a5e (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
119
120
121
122
123
124
125
import { useEffect } from "react";
import {
  QueryClient,
  useMutation,
  useQuery,
  useQueryClient,
} from "@tanstack/react-query";
import { usePaginationQuery } from "@/apis/queries/hooks";
import { QueryKeys } from "@/apis/queries/keys";
import api from "@/apis/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 useEpisodesBySeriesId(id: number) {
  const client = useQueryClient();

  const query = useQuery({
    queryKey: [QueryKeys.Series, id, QueryKeys.Episodes, QueryKeys.All],
    queryFn: () => api.episodes.bySeriesId([id]),
  });

  useEffect(() => {
    if (query.isSuccess && query.data) {
      cacheEpisodes(client, query.data);
    }
  }, [query.isSuccess, query.data, client]);

  return query;
}

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

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

export function useEpisodeAddBlacklist() {
  const client = useQueryClient();
  return useMutation({
    mutationKey: [QueryKeys.Series, QueryKeys.Episodes, QueryKeys.Blacklist],

    mutationFn: (param: {
      seriesId: number;
      episodeId: number;
      form: FormType.AddBlacklist;
    }) => {
      const { seriesId, episodeId, form } = param;
      return api.episodes.addBlacklist(seriesId, episodeId, form);
    },

    onSuccess: (_, { seriesId }) => {
      void client.invalidateQueries({
        queryKey: [QueryKeys.Series, QueryKeys.Episodes, QueryKeys.Blacklist],
      });

      void client.invalidateQueries({
        queryKey: [QueryKeys.Series, seriesId],
      });
    },
  });
}

export function useEpisodeDeleteBlacklist() {
  const client = useQueryClient();
  return useMutation({
    mutationKey: [QueryKeys.Series, QueryKeys.Episodes, QueryKeys.Blacklist],

    mutationFn: (param: { all?: boolean; form?: FormType.DeleteBlacklist }) =>
      api.episodes.deleteBlacklist(param.all, param.form),

    onSuccess: () => {
      void client.invalidateQueries({
        queryKey: [QueryKeys.Series, QueryKeys.Episodes, 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({
    queryKey: [
      QueryKeys.Series,
      QueryKeys.Episodes,
      QueryKeys.History,
      episodeId,
    ],

    queryFn: () => {
      if (episodeId) {
        return api.episodes.historyBy(episodeId);
      }

      return [];
    },
  });
}