frontPanel/src/model/question/question.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-11-14 20:15:52 +00:00
import { AnyQuizQuestion } from "@model/questionTypes/shared";
import { defaultQuestionContentByType } from "../../constants/default";
export type QuestionType =
| "variant"
| "images"
| "varimg"
| "emoji"
| "text"
| "select"
| "date"
| "number"
| "file"
| "page"
| "rating";
/** 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
export function rawQuestionToQuestion(rawQuestion: RawQuestion): AnyQuizQuestion {
let content = defaultQuestionContentByType[rawQuestion.type];
try {
content = JSON.parse(rawQuestion.content);
} catch (error) {
console.warn("Cannot parse question content from string, using default content", error);
}
return {
id: rawQuestion.id,
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,
} as AnyQuizQuestion;
}