124 lines
3.4 KiB
TypeScript
124 lines
3.4 KiB
TypeScript
import * as Yup from "yup";
|
|
import { Question } from "../../../../types/Item";
|
|
import { getLocalStorage } from "../../../../utils/LocalStorage";
|
|
import { QUESTION_OBJECT_KEY } from "../../../../config/AppKey";
|
|
|
|
export const getInitialValues = (objectToEdit: Question): any => {
|
|
const tags = objectToEdit?.tags?.map((item: any, index: number) => {
|
|
return { ...item };
|
|
});
|
|
|
|
return {
|
|
id: objectToEdit?.id ?? null,
|
|
content: objectToEdit?.content ?? "",
|
|
content_image: objectToEdit?.content_image ?? "",
|
|
subject_id: objectToEdit?.subject_id ?? "",
|
|
canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0,
|
|
hint: objectToEdit?.hint ?? "",
|
|
isBase: 0,
|
|
parent_id: objectToEdit?.parent_id ?? "",
|
|
answers: objectToEdit?.answers ?? [],
|
|
tags: tags ?? [],
|
|
};
|
|
};
|
|
|
|
export const getValidationSchema = () => {
|
|
// validate input
|
|
return Yup.object().shape({
|
|
content_image: Yup.string().nullable(),
|
|
content: Yup.string().required("validation.required"),
|
|
answers: Yup.array()
|
|
.of(
|
|
Yup.object().shape({
|
|
content: Yup.string().required("validation.required"),
|
|
content_image: Yup.string().nullable(),
|
|
isCorrect: Yup.boolean(),
|
|
}),
|
|
)
|
|
.nullable("Params are required"),
|
|
});
|
|
};
|
|
|
|
export const getInitialValuesBase = (objectToEdit: Question): any => {
|
|
const newQuestions = objectToEdit?.Questions?.map((item: any) => {
|
|
const tags = item?.tags?.map((tag: any) => ({
|
|
id: tag?.id,
|
|
name: tag?.name,
|
|
}));
|
|
console.log(tags);
|
|
|
|
return {
|
|
...item,
|
|
canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0,
|
|
hint: objectToEdit?.hint ?? "",
|
|
isBase:0,
|
|
tags,
|
|
};
|
|
});
|
|
|
|
const questions = newQuestions ?? [];
|
|
|
|
return {
|
|
id: objectToEdit?.id ?? null,
|
|
content: objectToEdit?.content ?? "",
|
|
content_image: objectToEdit?.content_image ?? "",
|
|
subject_id: objectToEdit?.subject_id ?? "",
|
|
isBase: 1,
|
|
parent_id: objectToEdit?.parent_id ?? "",
|
|
canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0,
|
|
hint: objectToEdit?.hint ?? "",
|
|
Questions: questions,
|
|
};
|
|
};
|
|
|
|
export const getValidationSchemaBase = () => {
|
|
// validate input
|
|
return Yup.object().shape({
|
|
content_image: Yup.string().nullable(),
|
|
content: Yup.string().required("validation.required"),
|
|
Questions: Yup.array().of(
|
|
Yup.object().shape({
|
|
image: Yup.string().nullable(),
|
|
content: Yup.string().required("validation.required"),
|
|
answers: Yup.array()
|
|
.of(
|
|
Yup.object().shape({
|
|
content: Yup.string().required("validation.required"),
|
|
answer_image: Yup.string().nullable(),
|
|
isCorrect: Yup.boolean(),
|
|
}),
|
|
)
|
|
.nullable("required"),
|
|
}),
|
|
),
|
|
});
|
|
};
|
|
|
|
export function processTags(DataToSend: any) {
|
|
console.log(DataToSend?.tags);
|
|
|
|
const oldTags = DataToSend?.tags
|
|
?.map((item: any, index: number) => {
|
|
if (typeof item === "number") {
|
|
return item;
|
|
}
|
|
})
|
|
.filter((item: any) => item !== undefined);
|
|
|
|
const newTags = DataToSend?.tags
|
|
?.map((item: any, index: number) => {
|
|
console.log(item);
|
|
|
|
if (typeof item === "string" && item !== "") {
|
|
console.log(item);
|
|
|
|
return { name: item };
|
|
}
|
|
})
|
|
.filter((item: any) => item !== undefined);
|
|
console.log(newTags);
|
|
console.log(oldTags);
|
|
|
|
return { new: newTags, old: oldTags };
|
|
}
|