67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
import { apiFetch } from "@/core/api";
|
|
import { getBackendBaseURL } from "@/core/config";
|
|
|
|
import type {
|
|
CreateQuestionRequest,
|
|
RecommendedQuestion,
|
|
UpdateQuestionRequest,
|
|
} from "./index";
|
|
|
|
const BASE = () => `${getBackendBaseURL()}/api/recommended-questions`;
|
|
|
|
export async function listRecommendedQuestions(): Promise<RecommendedQuestion[]> {
|
|
const res = await apiFetch(BASE());
|
|
if (!res.ok) throw new Error(`Failed to load questions: ${res.statusText}`);
|
|
const data = (await res.json()) as { questions: RecommendedQuestion[] };
|
|
return data.questions;
|
|
}
|
|
|
|
export async function createRecommendedQuestion(
|
|
body: CreateQuestionRequest,
|
|
): Promise<RecommendedQuestion> {
|
|
const res = await apiFetch(BASE(), {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
if (!res.ok) {
|
|
const err = (await res.json().catch(() => ({}))) as { detail?: string };
|
|
throw new Error(err.detail ?? `Failed to create question: ${res.statusText}`);
|
|
}
|
|
return res.json() as Promise<RecommendedQuestion>;
|
|
}
|
|
|
|
export async function updateRecommendedQuestion(
|
|
id: string,
|
|
body: UpdateQuestionRequest,
|
|
): Promise<RecommendedQuestion> {
|
|
const res = await apiFetch(`${BASE()}/${id}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
if (!res.ok) {
|
|
const err = (await res.json().catch(() => ({}))) as { detail?: string };
|
|
throw new Error(err.detail ?? `Failed to update question: ${res.statusText}`);
|
|
}
|
|
return res.json() as Promise<RecommendedQuestion>;
|
|
}
|
|
|
|
export async function deleteRecommendedQuestion(id: string): Promise<void> {
|
|
const res = await apiFetch(`${BASE()}/${id}`, { method: "DELETE" });
|
|
if (!res.ok) throw new Error(`Failed to delete question: ${res.statusText}`);
|
|
}
|
|
|
|
export async function reorderRecommendedQuestions(
|
|
ids: string[],
|
|
): Promise<RecommendedQuestion[]> {
|
|
const res = await apiFetch(`${getBackendBaseURL()}/api/recommended-questions-reorder`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ ids }),
|
|
});
|
|
if (!res.ok) throw new Error(`Failed to reorder questions: ${res.statusText}`);
|
|
const data = (await res.json()) as { questions: RecommendedQuestion[] };
|
|
return data.questions;
|
|
}
|