aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/apis/hooks/series.ts
blob: 1e395a6e55f3af91a3fbfcfb6de08df6db6aa04c (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
import {
  QueryClient,
  useMutation,
  useQuery,
  useQueryClient,
} from "react-query";
import { usePaginationQuery } from "../queries/hooks";
import { QueryKeys } from "../queries/keys";
import api from "../raw";

function cacheSeries(client: QueryClient, series: Item.Series[]) {
  series.forEach((item) => {
    client.setQueryData([QueryKeys.Series, item.sonarrSeriesId], item);
  });
}

export function useSeriesByIds(ids: number[]) {
  const client = useQueryClient();
  return useQuery([QueryKeys.Series, ...ids], () => api.series.series(ids), {
    onSuccess: (data) => {
      cacheSeries(client, data);
    },
  });
}

export function useSeriesById(id: number) {
  return useQuery([QueryKeys.Series, id], async () => {
    const response = await api.series.series([id]);
    return response.length > 0 ? response[0] : undefined;
  });
}

export function useSeries() {
  const client = useQueryClient();
  return useQuery(
    [QueryKeys.Series, QueryKeys.All],
    () => api.series.series(),
    {
      onSuccess: (data) => {
        cacheSeries(client, data);
      },
    },
  );
}

export function useSeriesPagination() {
  return usePaginationQuery([QueryKeys.Series], (param) =>
    api.series.seriesBy(param),
  );
}

export function useSeriesModification() {
  const client = useQueryClient();
  return useMutation(
    [QueryKeys.Series],
    (form: FormType.ModifyItem) => api.series.modify(form),
    {
      onSuccess: (_, form) => {
        form.id.forEach((v) => {
          client.invalidateQueries([QueryKeys.Series, v]);
        });
        client.invalidateQueries([QueryKeys.Series]);
      },
    },
  );
}

export function useSeriesAction() {
  const client = useQueryClient();
  return useMutation(
    [QueryKeys.Actions, QueryKeys.Series],
    (form: FormType.SeriesAction) => api.series.action(form),
    {
      onSuccess: () => {
        client.invalidateQueries([QueryKeys.Series]);
      },
    },
  );
}