hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { Choice } from "@/api/client"; |
| 2 | |
| 3 | /** One editable choice row: a stable key, its text, and a not-yet-uploaded image. */ |
| 4 | export type ChoiceRow = Choice & { image: File | null }; |
| 5 | |
| 6 | /** The whole choices region of the question form. `correct` names a row by id. */ |
| 7 | export type ChoiceSet = { |
| 8 | rows: ChoiceRow[]; |
| 9 | correct: string | null; |
| 10 | }; |
| 11 | |
| 12 | let minted = 0; |
| 13 | |
| 14 | /** |
| 15 | * Key for a row the server has never seen. The server keeps any submitted key it |
| 16 | * already stores and treats every other key as a new option, so this only has to |
| 17 | * be unique within one payload — and `correct` can name a brand-new row with it. |
| 18 | * `:` is not in Crockford base32, so it can never collide with a stored ULID. |
| 19 | */ |
| 20 | export function newChoiceId(): string { |
| 21 | minted += 1; |
| 22 | return `new:${minted}`; |
| 23 | } |
| 24 | |
| 25 | export function blankChoiceSet(count = 4): ChoiceSet { |
| 26 | return { rows: Array.from({ length: count }, () => ({ id: newChoiceId(), text: "", image: null })), correct: null }; |
| 27 | } |
| 28 | |
| 29 | /** Edit an existing question: stored ids come along, so images stay attached. */ |
| 30 | export function storedChoiceSet(choices: Choice[] | null | undefined, correct: string | null | undefined): ChoiceSet { |
| 31 | if (!choices) return blankChoiceSet(); |
| 32 | return { |
| 33 | rows: choices.map((choice) => ({ id: choice.id, text: choice.text, image: null })), |
| 34 | correct: correct ?? null, |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | export function addChoice(set: ChoiceSet, max: number): ChoiceSet { |
| 39 | if (set.rows.length >= max) return set; |
| 40 | return { ...set, rows: [...set.rows, { id: newChoiceId(), text: "", image: null }] }; |
| 41 | } |
| 42 | |
| 43 | /** Removing the marked row unmarks it — there is no neighbouring index to fall back to. */ |
| 44 | export function removeChoice(set: ChoiceSet, id: string): ChoiceSet { |
| 45 | return { |
| 46 | rows: set.rows.filter((row) => row.id !== id), |
| 47 | correct: set.correct === id ? null : set.correct, |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | export function setChoiceText(set: ChoiceSet, id: string, text: string): ChoiceSet { |
| 52 | return { ...set, rows: set.rows.map((row) => (row.id === id ? { ...row, text } : row)) }; |
| 53 | } |
| 54 | |
| 55 | export function setChoiceImage(set: ChoiceSet, id: string, image: File | null): ChoiceSet { |
| 56 | return { ...set, rows: set.rows.map((row) => (row.id === id ? { ...row, image } : row)) }; |
| 57 | } |
| 58 | |
| 59 | export function setCorrect(set: ChoiceSet, id: string): ChoiceSet { |
| 60 | return set.rows.some((row) => row.id === id) ? { ...set, correct: id } : set; |
| 61 | } |
| 62 | |
| 63 | /** Move a row to another position; ids travel with their text, image and mark. */ |
| 64 | export function moveChoice(set: ChoiceSet, id: string, to: number): ChoiceSet { |
| 65 | const from = set.rows.findIndex((row) => row.id === id); |
| 66 | if (from < 0 || to < 0 || to >= set.rows.length || to === from) return set; |
| 67 | const rows = [...set.rows]; |
| 68 | const [row] = rows.splice(from, 1); |
| 69 | rows.splice(to, 0, row); |
| 70 | return { ...set, rows }; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * What actually goes on the wire: blank rows dropped, ids kept. Every row carries |
| 75 | * a key, so the server re-attaches each surviving option's picture itself — no |
| 76 | * index bookkeeping, no download-and-reupload. `correct` is null when the marked |
| 77 | * row was left blank or never picked; the caller reports that to the user. |
| 78 | */ |
| 79 | export function submitChoices(set: ChoiceSet): { choices: Choice[]; images: (File | null)[]; correct: string | null } { |
| 80 | const kept = set.rows.map((row) => ({ ...row, text: row.text.trim() })).filter((row) => row.text); |
| 81 | return { |
| 82 | choices: kept.map((row) => ({ id: row.id, text: row.text })), |
| 83 | images: kept.map((row) => row.image), |
| 84 | correct: kept.some((row) => row.id === set.correct) ? set.correct : null, |
| 85 | }; |
| 86 | } |