forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB0069847Burak Boduroğlu2 months ago552 commits

questions.ts90 lines, 2.6 KiBHistoryRawDownload
1import { blobClient, client, formClient } from "../client";
2import type { Page, PageParams } from "../client";
3import type { PersonRef } from "../client";
4
5export interface PoolImageMeta {
6 content_type: string;
7 size: number;
8}
9
10export interface PoolQuestionResponse {
11 id: string;
12 /** The student who asked — field is `asker` on the backend */
13 asker: PersonRef;
14 title: string;
15 body: string;
16 status: "pending" | "approved";
17 asked_at: number;
18 approved_by: PersonRef | null;
19 /** null when there is no image */
20 image: PoolImageMeta | null;
21 solution_count: number;
22}
23
24export interface AskQuestion {
25 title: string;
26 body: string;
27}
28
29export async function getQuestions(
30 status?: "pending" | "approved",
31 params?: PageParams
32): Promise<Page<PoolQuestionResponse>> {
33 const searchParams = new URLSearchParams();
34 if (status) searchParams.set("status", status);
35 if (params?.limit !== undefined) searchParams.set("limit", params.limit.toString());
36 if (params?.offset !== undefined) searchParams.set("offset", params.offset.toString());
37
38 const query = searchParams.toString();
39 const url = query ? `/questions?${query}` : "/questions";
40 return client(url);
41}
42
43export async function getQuestionById(id: string): Promise<PoolQuestionResponse> {
44 return client(`/questions/${id}`);
45}
46
47export async function postQuestion(body: AskQuestion, image?: File): Promise<PoolQuestionResponse> {
48 const res = await client<PoolQuestionResponse>("/questions", {
49 method: "POST",
50 body,
51 });
52
53 if (image) await postQuestionImage(res.id, image);
54
55 return res;
56}
57
58/** Attaches or replaces the question's single illustration. */
59export async function postQuestionImage(id: string, image: File): Promise<void> {
60 const fd = new FormData();
61 fd.append("file", image);
62 await formClient(`/questions/${id}/image`, fd);
63}
64
65export async function deleteQuestionImage(id: string): Promise<void> {
66 return client(`/questions/${id}/image`, {
67 method: "DELETE",
68 });
69}
70
71export async function postQuestionApprove(id: string): Promise<PoolQuestionResponse> {
72 return client(`/questions/${id}/approve`, {
73 method: "POST",
74 });
75}
76
77export async function deleteQuestionById(id: string): Promise<void> {
78 return client(`/questions/${id}`, {
79 method: "DELETE",
80 });
81}
82
83export function getQuestionImageUrl(id: string): string {
84 return `/api/questions/${id}/image`;
85}
86
87/** Raw bytes of the same image — needed to recover an embedded drawing scene. */
88export function getQuestionImageBlob(id: string, signal?: AbortSignal): Promise<Blob> {
89 return blobClient(`/questions/${id}/image`, signal);
90}