54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import {
|
|
createRecommendedQuestion,
|
|
deleteRecommendedQuestion,
|
|
listRecommendedQuestions,
|
|
reorderRecommendedQuestions,
|
|
updateRecommendedQuestion,
|
|
} from "./api";
|
|
import type { CreateQuestionRequest, UpdateQuestionRequest } from "./index";
|
|
|
|
const QK = ["recommended-questions"] as const;
|
|
|
|
export function useRecommendedQuestions() {
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: QK,
|
|
queryFn: listRecommendedQuestions,
|
|
});
|
|
return { questions: data ?? [], isLoading, error };
|
|
}
|
|
|
|
export function useCreateRecommendedQuestion() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (body: CreateQuestionRequest) => createRecommendedQuestion(body),
|
|
onSuccess: () => void qc.invalidateQueries({ queryKey: QK }),
|
|
});
|
|
}
|
|
|
|
export function useUpdateRecommendedQuestion() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, body }: { id: string; body: UpdateQuestionRequest }) =>
|
|
updateRecommendedQuestion(id, body),
|
|
onSuccess: () => void qc.invalidateQueries({ queryKey: QK }),
|
|
});
|
|
}
|
|
|
|
export function useDeleteRecommendedQuestion() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => deleteRecommendedQuestion(id),
|
|
onSuccess: () => void qc.invalidateQueries({ queryKey: QK }),
|
|
});
|
|
}
|
|
|
|
export function useReorderRecommendedQuestions() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (ids: string[]) => reorderRecommendedQuestions(ids),
|
|
onSuccess: () => void qc.invalidateQueries({ queryKey: QK }),
|
|
});
|
|
}
|