hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB7122d71Burak Boduroğlu4 months ago419 commits
| 1 | import { blobClient, client, formClient } from "../client"; |
| 2 | import type { Page, PageParams } from "../client"; |
| 3 | import type { PersonRef } from "../client"; |
| 4 | |
| 5 | import { type PoolImageMeta } from "./questions"; |
| 6 | |
| 7 | export interface SolutionResponse { |
| 8 | id: string; |
| 9 | question: string; |
| 10 | author: PersonRef; |
| 11 | body: string; |
| 12 | offered_at: number; |
| 13 | /** null when there is no image */ |
| 14 | image: PoolImageMeta | null; |
| 15 | } |
| 16 | |
| 17 | export interface OfferSolution { |
| 18 | body: string; |
| 19 | } |
| 20 | |
| 21 | export async function getSolutions( |
| 22 | id: string, |
| 23 | params?: PageParams |
| 24 | ): Promise<Page<SolutionResponse>> { |
| 25 | const searchParams = new URLSearchParams(); |
| 26 | if (params?.limit !== undefined) searchParams.set("limit", params.limit.toString()); |
| 27 | if (params?.offset !== undefined) searchParams.set("offset", params.offset.toString()); |
| 28 | |
| 29 | const query = searchParams.toString(); |
| 30 | const url = query ? `/questions/${id}/solutions?${query}` : `/questions/${id}/solutions`; |
| 31 | return client(url); |
| 32 | } |
| 33 | |
| 34 | export async function postSolution( |
| 35 | id: string, |
| 36 | body: OfferSolution, |
| 37 | image?: File |
| 38 | ): Promise<SolutionResponse> { |
| 39 | const res = await client<SolutionResponse>(`/questions/${id}/solutions`, { |
| 40 | method: "POST", |
| 41 | body, |
| 42 | }); |
| 43 | |
| 44 | if (image) { |
| 45 | const fd = new FormData(); |
| 46 | fd.append("file", image); |
| 47 | await formClient(`/questions/${id}/solutions/${res.id}/image`, fd); |
| 48 | } |
| 49 | |
| 50 | return res; |
| 51 | } |
| 52 | |
| 53 | export async function patchSolutionById( |
| 54 | id: string, |
| 55 | sid: string, |
| 56 | body: OfferSolution |
| 57 | ): Promise<SolutionResponse> { |
| 58 | return client(`/questions/${id}/solutions/${sid}`, { |
| 59 | method: "PATCH", |
| 60 | body, |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | export async function deleteSolutionById(id: string, sid: string): Promise<void> { |
| 65 | return client(`/questions/${id}/solutions/${sid}`, { |
| 66 | method: "DELETE", |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | export function getSolutionImageUrl(id: string, sid: string): string { |
| 71 | return `/api/questions/${id}/solutions/${sid}/image`; |
| 72 | } |
| 73 | |
| 74 | /** Raw bytes of the same image — needed to recover an embedded drawing scene. */ |
| 75 | export function getSolutionImageBlob(id: string, sid: string, signal?: AbortSignal): Promise<Blob> { |
| 76 | return blobClient(`/questions/${id}/solutions/${sid}/image`, signal); |
| 77 | } |