添加推荐问题修改接口
This commit is contained in:
1 parent
62292500ba
commit
2f2ba44c7c
13 files changed
+481
-126
No files matched your search
@@ -1,58 +1,53 @@
|
||||
import { useCallback, useSyncExternalStore } from "react";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import {
|
||||
DEFAULT_QUESTIONS,
|
||||
getRecommendedQuestions,
|
||||
saveRecommendedQuestions,
|
||||
type RecommendedQuestion,
|
||||
} from "./index";
|
||||
createRecommendedQuestion,
|
||||
deleteRecommendedQuestion,
|
||||
listRecommendedQuestions,
|
||||
reorderRecommendedQuestions,
|
||||
updateRecommendedQuestion,
|
||||
} from "./api";
|
||||
import type { CreateQuestionRequest, UpdateQuestionRequest } from "./index";
|
||||
|
||||
type Listener = () => void;
|
||||
const QK = ["recommended-questions"] as const;
|
||||
|
||||
const listeners = new Set<Listener>();
|
||||
let cached: RecommendedQuestion[] | null = null;
|
||||
|
||||
function getSnapshot(): RecommendedQuestion[] {
|
||||
if (cached === null) {
|
||||
cached = getRecommendedQuestions();
|
||||
}
|
||||
return cached;
|
||||
export function useRecommendedQuestions() {
|
||||
const { data, isLoading, error } = useQuery({
|
||||
queryKey: QK,
|
||||
queryFn: listRecommendedQuestions,
|
||||
});
|
||||
return { questions: data ?? [], isLoading, error };
|
||||
}
|
||||
|
||||
function getServerSnapshot(): RecommendedQuestion[] {
|
||||
return DEFAULT_QUESTIONS;
|
||||
export function useCreateRecommendedQuestion() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (body: CreateQuestionRequest) => createRecommendedQuestion(body),
|
||||
onSuccess: () => void qc.invalidateQueries({ queryKey: QK }),
|
||||
});
|
||||
}
|
||||
|
||||
function subscribe(listener: Listener): () => void {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
export function useUpdateRecommendedQuestion() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, body }: { id: string; body: UpdateQuestionRequest }) =>
|
||||
updateRecommendedQuestion(id, body),
|
||||
onSuccess: () => void qc.invalidateQueries({ queryKey: QK }),
|
||||
});
|
||||
}
|
||||
|
||||
function emitChange() {
|
||||
for (const l of listeners) l();
|
||||
export function useDeleteRecommendedQuestion() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => deleteRecommendedQuestion(id),
|
||||
onSuccess: () => void qc.invalidateQueries({ queryKey: QK }),
|
||||
});
|
||||
}
|
||||
|
||||
export function updateRecommendedQuestions(
|
||||
questions: RecommendedQuestion[],
|
||||
): void {
|
||||
cached = questions;
|
||||
saveRecommendedQuestions(questions);
|
||||
emitChange();
|
||||
}
|
||||
|
||||
export function useRecommendedQuestions(): [
|
||||
RecommendedQuestion[],
|
||||
(questions: RecommendedQuestion[]) => void,
|
||||
] {
|
||||
const questions = useSyncExternalStore(
|
||||
subscribe,
|
||||
getSnapshot,
|
||||
getServerSnapshot,
|
||||
);
|
||||
|
||||
const save = useCallback((qs: RecommendedQuestion[]) => {
|
||||
updateRecommendedQuestions(qs);
|
||||
}, []);
|
||||
|
||||
return [questions, save];
|
||||
export function useReorderRecommendedQuestions() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (ids: string[]) => reorderRecommendedQuestions(ids),
|
||||
onSuccess: () => void qc.invalidateQueries({ queryKey: QK }),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user