frontPanel/src/model/question/question.ts

75 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-11-29 13:49:52 +00:00
import { AnyTypedQuizQuestion } from "@model/questionTypes/shared";
2023-11-15 18:38:02 +00:00
import { defaultQuestionByType } from "../../constants/default";
2023-11-27 23:07:24 +00:00
import { nanoid } from "nanoid";
2023-11-14 20:15:52 +00:00
export type QuestionType =
| "variant"
| "images"
| "varimg"
| "emoji"
| "text"
| "select"
| "date"
| "number"
| "file"
| "page"
2023-12-07 21:30:26 +00:00
| "rating"
| "result";
2023-11-14 20:15:52 +00:00
/** Type that comes from server */
export interface RawQuestion {
/** Id of created question */
2023-11-02 16:45:28 +00:00
id: number;
2023-11-14 20:15:52 +00:00
/** relation to quiz */
2023-11-02 16:45:28 +00:00
quiz_id: number;
2023-11-14 20:15:52 +00:00
/** title of question. max 512 length */
2023-11-02 16:45:28 +00:00
title: string;
2023-11-14 20:15:52 +00:00
/** description of question */
2023-11-02 16:45:28 +00:00
description: string;
2023-11-14 20:15:52 +00:00
/** status of question. allow only text, select, file, variant, images, varimg, emoji, date, number, page, rating */
type: QuestionType;
/** user must pass this question */
2023-11-02 16:45:28 +00:00
required: boolean;
2023-11-14 20:15:52 +00:00
/** true if question is deleted */
2023-11-02 16:45:28 +00:00
deleted: boolean;
2023-11-14 20:15:52 +00:00
/** page if question */
2023-11-02 16:45:28 +00:00
page: number;
2023-11-14 20:15:52 +00:00
/** serialized json of created question */
2023-11-02 16:45:28 +00:00
content: string;
2023-11-14 20:15:52 +00:00
/** version of quiz */
2023-11-02 16:45:28 +00:00
version: number;
2023-11-14 20:15:52 +00:00
/** array of previous versions of quiz */
2023-11-02 16:45:28 +00:00
parent_ids: number[];
created_at: string;
updated_at: string;
}
2023-11-14 20:15:52 +00:00
2023-11-29 13:49:52 +00:00
export function rawQuestionToQuestion(rawQuestion: RawQuestion): AnyTypedQuizQuestion {
2023-11-15 18:38:02 +00:00
let content = defaultQuestionByType[rawQuestion.type].content;
const frontId = nanoid()
2023-11-14 20:15:52 +00:00
try {
content = JSON.parse(rawQuestion.content);
if (content.id.length === 0 || content.id.length === undefined) content.id = frontId
2023-11-14 20:15:52 +00:00
} catch (error) {
console.warn("Cannot parse question content from string, using default content", error);
}
return {
2023-11-27 23:07:24 +00:00
backendId: rawQuestion.id,
id: frontId,
2023-11-14 20:15:52 +00:00
description: rawQuestion.description,
page: rawQuestion.page,
quizId: rawQuestion.quiz_id,
required: rawQuestion.required,
title: rawQuestion.title,
type: rawQuestion.type,
expanded: true,
openedModalSettings: false,
deleted: false,
deleteTimeoutId: 0,
content,
2023-11-29 13:49:52 +00:00
} as AnyTypedQuizQuestion;
2023-11-14 20:15:52 +00:00
}