hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { blobClient, client, formClient } from "../client"; |
| 2 | import type { Page, PageParams } from "../client"; |
| 3 | import type { PersonRef } from "../client"; |
| 4 | |
| 5 | export interface PoolImageMeta { |
| 6 | content_type: string; |
| 7 | size: number; |
| 8 | } |
| 9 | |
| 10 | export 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 | |
| 24 | export interface AskQuestion { |
| 25 | title: string; |
| 26 | body: string; |
| 27 | } |
| 28 | |
| 29 | export 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 | |
| 43 | export async function getQuestionById(id: string): Promise<PoolQuestionResponse> { |
| 44 | return client(`/questions/${id}`); |
| 45 | } |
| 46 | |
| 47 | export 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. */ |
| 59 | export 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 | |
| 65 | export async function deleteQuestionImage(id: string): Promise<void> { |
| 66 | return client(`/questions/${id}/image`, { |
| 67 | method: "DELETE", |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | export async function postQuestionApprove(id: string): Promise<PoolQuestionResponse> { |
| 72 | return client(`/questions/${id}/approve`, { |
| 73 | method: "POST", |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | export async function deleteQuestionById(id: string): Promise<void> { |
| 78 | return client(`/questions/${id}`, { |
| 79 | method: "DELETE", |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | export 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. */ |
| 88 | export function getQuestionImageBlob(id: string, signal?: AbortSignal): Promise<Blob> { |
| 89 | return blobClient(`/questions/${id}/image`, signal); |
| 90 | } |