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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
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 cacheMovies = (client: QueryClient, movies: Item.Movie[]) => {
movies.forEach((item) => {
client.setQueryData([QueryKeys.Movies, item.radarrId], item);
});
};
export function useMovieById(id: number) {
return useQuery({
queryKey: [QueryKeys.Movies, id],
queryFn: async () => {
const response = await api.movies.movies([id]);
return response.length > 0 ? response[0] : undefined;
},
});
}
export function useMovies() {
const client = useQueryClient();
const query = useQuery({
queryKey: [QueryKeys.Movies, QueryKeys.All],
queryFn: () => api.movies.movies(),
});
useEffect(() => {
if (query.isSuccess && query.data) {
cacheMovies(client, query.data);
}
}, [query.isSuccess, query.data, client]);
return query;
}
export function useMoviesPagination() {
return usePaginationQuery([QueryKeys.Movies], (param) =>
api.movies.moviesBy(param),
);
}
export function useMovieModification() {
const client = useQueryClient();
return useMutation({
mutationKey: [QueryKeys.Movies],
mutationFn: (form: FormType.ModifyItem) => api.movies.modify(form),
onSuccess: (_, form) => {
form.id.forEach((v) => {
void client.invalidateQueries({
queryKey: [QueryKeys.Movies, v],
});
});
// TODO: query less
void client.invalidateQueries({
queryKey: [QueryKeys.Movies],
});
},
});
}
export function useMovieAction() {
const client = useQueryClient();
return useMutation({
mutationKey: [QueryKeys.Actions, QueryKeys.Movies],
mutationFn: (form: FormType.MoviesAction) => api.movies.action(form),
onSuccess: () => {
void client.invalidateQueries({
queryKey: [QueryKeys.Movies],
});
},
});
}
export function useMovieWantedPagination() {
return usePaginationQuery([QueryKeys.Movies, QueryKeys.Wanted], (param) =>
api.movies.wanted(param),
);
}
export function useMovieBlacklist() {
return useQuery({
queryKey: [QueryKeys.Movies, QueryKeys.Blacklist],
queryFn: () => api.movies.blacklist(),
});
}
export function useMovieAddBlacklist() {
const client = useQueryClient();
return useMutation({
mutationKey: [QueryKeys.Movies, QueryKeys.Blacklist],
mutationFn: (param: { id: number; form: FormType.AddBlacklist }) => {
const { id, form } = param;
return api.movies.addBlacklist(id, form);
},
onSuccess: (_, { id }) => {
void client.invalidateQueries({
queryKey: [QueryKeys.Movies, QueryKeys.Blacklist],
});
void client.invalidateQueries({
queryKey: [QueryKeys.Movies, id],
});
},
});
}
export function useMovieDeleteBlacklist() {
const client = useQueryClient();
return useMutation({
mutationKey: [QueryKeys.Movies, QueryKeys.Blacklist],
mutationFn: (param: { all?: boolean; form?: FormType.DeleteBlacklist }) =>
api.movies.deleteBlacklist(param.all, param.form),
onSuccess: () => {
void client.invalidateQueries({
queryKey: [QueryKeys.Movies, QueryKeys.Blacklist],
});
},
});
}
export function useMovieHistoryPagination() {
return usePaginationQuery(
[QueryKeys.Movies, QueryKeys.History],
(param) => api.movies.history(param),
false,
);
}
export function useMovieHistory(radarrId?: number) {
return useQuery({
queryKey: [QueryKeys.Movies, QueryKeys.History, radarrId],
queryFn: () => {
if (radarrId) {
return api.movies.historyBy(radarrId);
}
return [];
},
});
}
|